prysm-pulse/beacon-chain/sync/rpc_chunked_response.go
Preston Van Loon 5345ddf686 Initial Sync: Round robin (#3538)
* first pass, step 1 works

* naive from finalized to head

* delete commented code

* checkpoint progress on tests

* passing test

* abstract code slightly

* failure cases

* chkpt

* mostly working, missing a single block and having timeout

* passing tests

* comments

* comments

* gaz

* clarify comments

* progress on a few new cases

* add back bootnode query tool

* bootstrap from DHT

* chunked responses in round robin

* fix tests and deadlines

* add basic counter, time estimation

* hello -> handshakes

* show peers in use during sync

* just one last test failure

* only request blocks starting in the finalized epoch for step 1

* revert that

* comment out test and add better commentary

* move requestBlocks out to pointer receiver

* mathutil

* Update beacon-chain/sync/initial-sync/round_robin.go

Co-Authored-By: Raul Jordan <raul@prysmaticlabs.com>

* PR feedback

* PR feedback
2019-09-25 12:00:04 -05:00

55 lines
1.8 KiB
Go

package sync
import (
"errors"
"time"
libp2pcore "github.com/libp2p/go-libp2p-core"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
eth "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
)
// 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 := &eth.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)
}