mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-31 23:41:22 +00:00
0326be86b5
* starting on patch * finish determining all required patches * properly redefine the patch rules * new patch * rem double semicolon * fix patch file * Merge branch 'master' of github.com:prysmaticlabs/prysm into deprecate-eth-protos * building the deps * test target passes using ethereumapis * compile gateway * attempting to build everything * e2e use ethereumapis * more fixes for slasher * other item * getting closer to compiling slasher * build slasher package * Merge branch 'master' into deprecate-eth-protos * Merge branch 'master' into deprecate-eth-protos * fix benches * lint gazelle * Merge branch 'deprecate-eth-protos' of github.com:prysmaticlabs/prysm into deprecate-eth-protos * proper gateway * lint * Merge branch 'master' into deprecate-eth-protos * fix build * Merge branch 'deprecate-eth-protos' of github.com:prysmaticlabs/prysm into deprecate-eth-protos * use swag * resolve * ignore change * include new patch changes * fix test * builds * fix e2e * gaz
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package sync
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
|
eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
|
)
|
|
|
|
// chunkWriter writes the given message as a chunked response to the given network
|
|
// stream.
|
|
// response_chunk ::= | <result> | <encoding-dependent-header> | <encoded-payload>
|
|
func (r *RegularSync) chunkWriter(stream libp2pcore.Stream, msg interface{}) error {
|
|
setStreamWriteDeadline(stream, defaultWriteDuration)
|
|
return WriteChunk(stream, r.p2p.Encoding(), msg)
|
|
}
|
|
|
|
// WriteChunk object to stream.
|
|
// response_chunk ::= | <result> | <encoding-dependent-header> | <encoded-payload>
|
|
func WriteChunk(stream libp2pcore.Stream, encoding encoder.NetworkEncoding, msg interface{}) error {
|
|
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
|
|
return err
|
|
}
|
|
_, err := encoding.EncodeWithMaxLength(stream, msg, maxChunkSize)
|
|
return err
|
|
}
|
|
|
|
// ReadChunkedBlock handles each response chunk that is sent by the
|
|
// peer and converts it into a beacon block.
|
|
func ReadChunkedBlock(stream libp2pcore.Stream, p2p p2p.P2P) (*eth.BeaconBlock, error) {
|
|
blk := ð.BeaconBlock{}
|
|
if err := readResponseChunk(stream, p2p, blk); err != nil {
|
|
return nil, err
|
|
}
|
|
return blk, nil
|
|
}
|
|
|
|
// readResponseChunk reads the response from the stream and decodes it into the
|
|
// provided message type.
|
|
func readResponseChunk(stream libp2pcore.Stream, p2p p2p.P2P, to interface{}) error {
|
|
setStreamReadDeadline(stream, 10*time.Second)
|
|
code, errMsg, err := ReadStatusCode(stream, p2p.Encoding())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if code != 0 {
|
|
return errors.New(errMsg)
|
|
}
|
|
return p2p.Encoding().DecodeWithMaxLength(stream, to, maxChunkSize)
|
|
}
|