2019-09-24 14:56:50 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
libp2pcore "github.com/libp2p/go-libp2p-core"
|
2021-09-03 16:00:56 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-05-17 19:25:59 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2021-09-27 16:19:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/signing"
|
2019-09-24 14:56:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-09-25 17:00:04 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/encoder"
|
2021-09-03 16:00:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p/types"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-23 15:23:37 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2021-09-17 19:20:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/network/forks"
|
2021-07-28 21:23:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/block"
|
2021-09-16 09:46:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
2019-09-24 14:56:50 +00:00
|
|
|
)
|
|
|
|
|
2021-09-03 16:00:56 +00:00
|
|
|
// chunkBlockWriter writes the given message as a chunked response to the given network
|
2019-09-24 14:56:50 +00:00
|
|
|
// stream.
|
2021-05-17 19:25:59 +00:00
|
|
|
// response_chunk ::= <result> | <context-bytes> | <encoding-dependent-header> | <encoded-payload>
|
2021-09-03 16:00:56 +00:00
|
|
|
func (s *Service) chunkBlockWriter(stream libp2pcore.Stream, blk block.SignedBeaconBlock) error {
|
2020-06-25 22:36:18 +00:00
|
|
|
SetStreamWriteDeadline(stream, defaultWriteDuration)
|
2021-11-05 19:08:58 +00:00
|
|
|
return WriteBlockChunk(stream, s.cfg.chain, s.cfg.p2p.Encoding(), blk)
|
2019-09-25 17:00:04 +00:00
|
|
|
}
|
|
|
|
|
2021-09-24 17:42:16 +00:00
|
|
|
// WriteBlockChunk writes block chunk object to stream.
|
2021-05-17 19:25:59 +00:00
|
|
|
// response_chunk ::= <result> | <context-bytes> | <encoding-dependent-header> | <encoded-payload>
|
2021-09-03 16:00:56 +00:00
|
|
|
func WriteBlockChunk(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, encoding encoder.NetworkEncoding, blk block.SignedBeaconBlock) error {
|
2019-09-24 14:56:50 +00:00
|
|
|
if _, err := stream.Write([]byte{responseCodeSuccess}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-24 17:42:16 +00:00
|
|
|
var obtainedCtx []byte
|
2021-09-03 16:00:56 +00:00
|
|
|
switch blk.Version() {
|
|
|
|
case version.Phase0:
|
2022-02-14 13:34:38 +00:00
|
|
|
valRoot := chain.GenesisValidatorsRoot()
|
2021-09-17 19:20:50 +00:00
|
|
|
digest, err := forks.ForkDigestFromEpoch(params.BeaconConfig().GenesisEpoch, valRoot[:])
|
2021-09-03 16:00:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obtainedCtx = digest[:]
|
|
|
|
case version.Altair:
|
2022-02-14 13:34:38 +00:00
|
|
|
valRoot := chain.GenesisValidatorsRoot()
|
2021-09-17 19:20:50 +00:00
|
|
|
digest, err := forks.ForkDigestFromEpoch(params.BeaconConfig().AltairForkEpoch, valRoot[:])
|
2021-09-03 16:00:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obtainedCtx = digest[:]
|
2022-01-28 16:26:52 +00:00
|
|
|
case version.Bellatrix:
|
2022-02-14 13:34:38 +00:00
|
|
|
valRoot := chain.GenesisValidatorsRoot()
|
2022-01-28 16:26:52 +00:00
|
|
|
digest, err := forks.ForkDigestFromEpoch(params.BeaconConfig().BellatrixForkEpoch, valRoot[:])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
obtainedCtx = digest[:]
|
2021-09-03 16:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeContextToStream(obtainedCtx, stream, chain); err != nil {
|
2021-05-17 19:25:59 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-09-03 16:00:56 +00:00
|
|
|
_, err := encoding.EncodeWithMaxLength(stream, blk)
|
2019-09-24 14:56:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadChunkedBlock handles each response chunk that is sent by the
|
|
|
|
// peer and converts it into a beacon block.
|
2021-07-23 20:10:15 +00:00
|
|
|
func ReadChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P, isFirstChunk bool) (block.SignedBeaconBlock, error) {
|
2020-07-13 19:40:12 +00:00
|
|
|
// Handle deadlines differently for first chunk
|
|
|
|
if isFirstChunk {
|
2021-05-17 19:25:59 +00:00
|
|
|
return readFirstChunkedBlock(stream, chain, p2p)
|
2020-07-13 19:40:12 +00:00
|
|
|
}
|
2021-09-03 16:00:56 +00:00
|
|
|
|
|
|
|
return readResponseChunk(stream, chain, p2p)
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 19:40:12 +00:00
|
|
|
// readFirstChunkedBlock reads the first chunked block and applies the appropriate deadlines to
|
|
|
|
// it.
|
2021-07-23 20:10:15 +00:00
|
|
|
func readFirstChunkedBlock(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P) (block.SignedBeaconBlock, error) {
|
2020-07-13 19:40:12 +00:00
|
|
|
code, errMsg, err := ReadStatusCode(stream, p2p.Encoding())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if code != 0 {
|
|
|
|
return nil, errors.New(errMsg)
|
|
|
|
}
|
2021-09-03 16:00:56 +00:00
|
|
|
rpcCtx, err := readContextFromStream(stream, chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blk, err := extractBlockDataType(rpcCtx, chain)
|
2021-05-17 19:25:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-13 19:40:12 +00:00
|
|
|
err = p2p.Encoding().DecodeWithMaxLength(stream, blk)
|
2021-09-03 16:00:56 +00:00
|
|
|
return blk, err
|
2020-07-13 19:40:12 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 14:56:50 +00:00
|
|
|
// readResponseChunk reads the response from the stream and decodes it into the
|
|
|
|
// provided message type.
|
2021-09-03 16:00:56 +00:00
|
|
|
func readResponseChunk(stream libp2pcore.Stream, chain blockchain.ChainInfoFetcher, p2p p2p.P2P) (block.SignedBeaconBlock, error) {
|
2020-07-01 09:47:59 +00:00
|
|
|
SetStreamReadDeadline(stream, respTimeout)
|
2020-07-13 19:40:12 +00:00
|
|
|
code, errMsg, err := readStatusCodeNoDeadline(stream, p2p.Encoding())
|
2019-09-24 14:56:50 +00:00
|
|
|
if err != nil {
|
2021-09-03 16:00:56 +00:00
|
|
|
return nil, err
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
|
|
|
if code != 0 {
|
2021-09-03 16:00:56 +00:00
|
|
|
return nil, errors.New(errMsg)
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
2021-05-17 19:25:59 +00:00
|
|
|
// No-op for now with the rpc context.
|
2021-09-03 16:00:56 +00:00
|
|
|
rpcCtx, err := readContextFromStream(stream, chain)
|
2021-05-17 19:25:59 +00:00
|
|
|
if err != nil {
|
2021-09-03 16:00:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
blk, err := extractBlockDataType(rpcCtx, chain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = p2p.Encoding().DecodeWithMaxLength(stream, blk)
|
|
|
|
return blk, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractBlockDataType(digest []byte, chain blockchain.ChainInfoFetcher) (block.SignedBeaconBlock, error) {
|
|
|
|
if len(digest) == 0 {
|
|
|
|
bFunc, ok := types.BlockMap[bytesutil.ToBytes4(params.BeaconConfig().GenesisForkVersion)]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("no block type exists for the genesis fork version.")
|
|
|
|
}
|
|
|
|
return bFunc()
|
|
|
|
}
|
|
|
|
if len(digest) != forkDigestLength {
|
|
|
|
return nil, errors.Errorf("invalid digest returned, wanted a length of %d but received %d", forkDigestLength, len(digest))
|
|
|
|
}
|
2022-02-14 13:34:38 +00:00
|
|
|
vRoot := chain.GenesisValidatorsRoot()
|
2021-09-03 16:00:56 +00:00
|
|
|
for k, blkFunc := range types.BlockMap {
|
2021-09-27 16:19:20 +00:00
|
|
|
rDigest, err := signing.ComputeForkDigest(k[:], vRoot[:])
|
2021-09-03 16:00:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if rDigest == bytesutil.ToBytes4(digest) {
|
|
|
|
return blkFunc()
|
|
|
|
}
|
2021-05-17 19:25:59 +00:00
|
|
|
}
|
2021-09-03 16:00:56 +00:00
|
|
|
return nil, errors.New("no valid digest matched")
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|