From b25e0b47e04f91d5b2bc99c934f7eb777980cabe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ecl=C3=A9sio=20Junior?= Date: Wed, 7 Dec 2022 12:37:53 -0400 Subject: [PATCH] feat(dot/sync): Remove the `EndBlockHash` from `BlockRequestMessage` (#2977) --- dot/network/helpers_test.go | 4 +- dot/network/message.go | 31 +------ dot/network/message_test.go | 16 +--- dot/network/proto/api.v1.pb.go | 75 +++++++--------- dot/network/proto/api.v1.proto | 2 - dot/sync/chain_sync.go | 8 -- dot/sync/chain_sync_test.go | 16 ---- dot/sync/errors.go | 7 +- dot/sync/message.go | 120 ++++++++----------------- dot/sync/message_integration_test.go | 127 --------------------------- dot/sync/message_test.go | 68 +++----------- go.mod | 2 +- lib/blocktree/blocktree.go | 4 +- 13 files changed, 97 insertions(+), 383 deletions(-) diff --git a/dot/network/helpers_test.go b/dot/network/helpers_test.go index 6132e86510..2c5e9a331c 100644 --- a/dot/network/helpers_test.go +++ b/dot/network/helpers_test.go @@ -15,10 +15,9 @@ import ( "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/common/variadic" gomock "github.com/golang/mock/gomock" - "github.com/stretchr/testify/require" - libp2pnetwork "github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/peer" + "github.com/stretchr/testify/require" ) const ( @@ -131,7 +130,6 @@ func newTestBlockRequestMessage(t *testing.T) *BlockRequestMessage { return &BlockRequestMessage{ RequestedData: RequestedDataHeader + RequestedDataBody + RequestedDataJustification, StartingBlock: *starting, - EndBlockHash: &common.Hash{}, Direction: 1, Max: &one, } diff --git a/dot/network/message.go b/dot/network/message.go index 38e44f04c4..c948c2de16 100644 --- a/dot/network/message.go +++ b/dot/network/message.go @@ -75,48 +75,32 @@ func (sd SyncDirection) String() string { type BlockRequestMessage struct { RequestedData byte StartingBlock variadic.Uint32OrHash // first byte 0 = block hash (32 byte), first byte 1 = block number (uint32) - EndBlockHash *common.Hash - Direction SyncDirection // 0 = ascending, 1 = descending + Direction SyncDirection // 0 = ascending, 1 = descending Max *uint32 } // String formats a BlockRequestMessage as a string func (bm *BlockRequestMessage) String() string { - hash := common.Hash{} max := uint32(0) - if bm.EndBlockHash != nil { - hash = *bm.EndBlockHash - } if bm.Max != nil { max = *bm.Max } - return fmt.Sprintf("BlockRequestMessage RequestedData=%d StartingBlock=%v EndBlockHash=%s Direction=%d Max=%d", + return fmt.Sprintf("BlockRequestMessage RequestedData=%d StartingBlock=%v Direction=%d Max=%d", bm.RequestedData, bm.StartingBlock, - hash.String(), bm.Direction, max) } // Encode returns the protobuf encoded BlockRequestMessage func (bm *BlockRequestMessage) Encode() ([]byte, error) { - var ( - toBlock []byte - max uint32 - ) - - if bm.EndBlockHash != nil { - hash := bm.EndBlockHash - toBlock = hash[:] - } - + var max uint32 if bm.Max != nil { max = *bm.Max } msg := &pb.BlockRequest{ Fields: uint32(bm.RequestedData) << 24, // put byte in most significant byte of uint32 - ToBlock: toBlock, Direction: pb.Direction(bm.Direction), MaxBlocks: max, } @@ -149,7 +133,6 @@ func (bm *BlockRequestMessage) Decode(in []byte) error { var ( startingBlock *variadic.Uint32OrHash - endBlockHash *common.Hash max *uint32 ) @@ -171,13 +154,6 @@ func (bm *BlockRequestMessage) Decode(in []byte) error { return err } - if len(msg.ToBlock) != 0 { - hash := common.NewHash(msg.ToBlock) - endBlockHash = &hash - } else { - endBlockHash = nil - } - if msg.MaxBlocks != 0 { max = &msg.MaxBlocks } else { @@ -186,7 +162,6 @@ func (bm *BlockRequestMessage) Decode(in []byte) error { bm.RequestedData = byte(msg.Fields >> 24) bm.StartingBlock = *startingBlock - bm.EndBlockHash = endBlockHash bm.Direction = SyncDirection(byte(msg.Direction)) bm.Max = max diff --git a/dot/network/message_test.go b/dot/network/message_test.go index 5299968492..44aed673a9 100644 --- a/dot/network/message_test.go +++ b/dot/network/message_test.go @@ -18,15 +18,14 @@ import ( func TestEncodeBlockRequestMessage(t *testing.T) { t.Parallel() - expected := common.MustHexToBytes("0x08808080082220fd19d9ebac759c993fd2e05a1cff9e757d8741c2704c8682c15b5503496b6aa1280130011220dcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b") //nolint:lll + expected := common.MustHexToBytes("0x0880808008280130011220dcd1346701ca8396496e52" + + "aa2785b1748deb6db09551b72159dcb3e08991025b") genesisHash := common.MustHexToBytes("0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b") - endBlock := common.MustHexToHash("0xfd19d9ebac759c993fd2e05a1cff9e757d8741c2704c8682c15b5503496b6aa1") var one uint32 = 1 bm := &BlockRequestMessage{ RequestedData: 1, StartingBlock: *variadic.NewUint32OrHashFromBytes(append([]byte{0}, genesisHash...)), - EndBlockHash: &endBlock, Direction: 1, Max: &one, } @@ -34,7 +33,7 @@ func TestEncodeBlockRequestMessage(t *testing.T) { encMsg, err := bm.Encode() require.NoError(t, err) - require.Equal(t, expected, encMsg) // Pass! + require.Equal(t, expected, encMsg) res := new(BlockRequestMessage) err = res.Decode(encMsg) @@ -46,13 +45,11 @@ func TestEncodeBlockRequestMessage_BlockHash(t *testing.T) { t.Parallel() genesisHash := common.MustHexToBytes("0xdcd1346701ca8396496e52aa2785b1748deb6db09551b72159dcb3e08991025b") - endBlock := common.MustHexToHash("0xfd19d9ebac759c993fd2e05a1cff9e757d8741c2704c8682c15b5503496b6aa1") var one uint32 = 1 bm := &BlockRequestMessage{ RequestedData: 1, StartingBlock: *variadic.NewUint32OrHashFromBytes(append([]byte{0}, genesisHash...)), - EndBlockHash: &endBlock, Direction: 1, Max: &one, } @@ -69,13 +66,10 @@ func TestEncodeBlockRequestMessage_BlockHash(t *testing.T) { func TestEncodeBlockRequestMessage_BlockNumber(t *testing.T) { t.Parallel() - endBlock := common.MustHexToHash("0xfd19d9ebac759c993fd2e05a1cff9e757d8741c2704c8682c15b5503496b6aa1") - var one uint32 = 1 bm := &BlockRequestMessage{ RequestedData: 1, StartingBlock: *variadic.NewUint32OrHashFromBytes([]byte{1, 1}), - EndBlockHash: &endBlock, Direction: 1, Max: &one, } @@ -97,13 +91,12 @@ func TestBlockRequestString(t *testing.T) { bm := &BlockRequestMessage{ RequestedData: 1, StartingBlock: *variadic.NewUint32OrHashFromBytes(append([]byte{0}, genesisHash...)), - EndBlockHash: nil, Direction: 1, Max: nil, } var blockRequestStringRegex = regexp.MustCompile( - `^\ABlockRequestMessage RequestedData=[0-9]* StartingBlock={[\[0-9(\s?)]+\]} EndBlockHash=0x[0-9]+ Direction=[0-9]* Max=[0-9]*\z$`) //nolint:lll + `^\ABlockRequestMessage RequestedData=[0-9]* StartingBlock={[\[0-9(\s?)]+\]} Direction=[0-9]* Max=[0-9]*\z$`) //nolint:lll match := blockRequestStringRegex.MatchString(bm.String()) require.True(t, match) @@ -117,7 +110,6 @@ func TestEncodeBlockRequestMessage_NoOptionals(t *testing.T) { bm := &BlockRequestMessage{ RequestedData: 1, StartingBlock: *variadic.NewUint32OrHashFromBytes(append([]byte{0}, genesisHash...)), - EndBlockHash: nil, Direction: 1, Max: nil, } diff --git a/dot/network/proto/api.v1.pb.go b/dot/network/proto/api.v1.pb.go index 10ad646144..aa3259f98d 100644 --- a/dot/network/proto/api.v1.pb.go +++ b/dot/network/proto/api.v1.pb.go @@ -88,8 +88,6 @@ type BlockRequest struct { // *BlockRequest_Hash // *BlockRequest_Number FromBlock isBlockRequest_FromBlock `protobuf_oneof:"from_block"` - // End at this block. An implementation defined maximum is used when unspecified. - ToBlock []byte `protobuf:"bytes,4,opt,name=to_block,json=toBlock,proto3" json:"to_block,omitempty"` // optional // Sequence direction. Direction Direction `protobuf:"varint,5,opt,name=direction,proto3,enum=api.v1.Direction" json:"direction,omitempty"` // Maximum number of blocks to return. An implementation defined maximum is used when unspecified. @@ -156,13 +154,6 @@ func (x *BlockRequest) GetNumber() []byte { return nil } -func (x *BlockRequest) GetToBlock() []byte { - if x != nil { - return x.ToBlock - } - return nil -} - func (x *BlockRequest) GetDirection() Direction { if x != nil { return x.Direction @@ -354,45 +345,43 @@ var File_api_v1_proto protoreflect.FileDescriptor var file_api_v1_proto_rawDesc = []byte{ 0x0a, 0x0c, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, - 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x22, 0xcf, 0x01, 0x0a, 0x0c, 0x42, 0x6c, 0x6f, 0x63, 0x6b, + 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x22, 0xb4, 0x01, 0x0a, 0x0c, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x66, 0x69, 0x65, 0x6c, 0x64, 0x73, 0x12, 0x14, 0x0a, 0x04, 0x68, 0x61, 0x73, 0x68, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x04, 0x68, 0x61, 0x73, 0x68, 0x12, 0x18, 0x0a, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x06, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x12, - 0x19, 0x0a, 0x08, 0x74, 0x6f, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x18, 0x04, 0x20, 0x01, 0x28, - 0x0c, 0x52, 0x07, 0x74, 0x6f, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x12, 0x2f, 0x0a, 0x09, 0x64, 0x69, - 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x11, 0x2e, - 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, - 0x52, 0x09, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x1d, 0x0a, 0x0a, 0x6d, - 0x61, 0x78, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x73, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, - 0x09, 0x6d, 0x61, 0x78, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x73, 0x42, 0x0c, 0x0a, 0x0a, 0x66, 0x72, - 0x6f, 0x6d, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x0a, 0x0d, 0x42, 0x6c, 0x6f, 0x63, - 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x29, 0x0a, 0x06, 0x62, 0x6c, 0x6f, - 0x63, 0x6b, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x61, 0x70, 0x69, 0x2e, - 0x76, 0x31, 0x2e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x52, 0x06, 0x62, 0x6c, - 0x6f, 0x63, 0x6b, 0x73, 0x22, 0xe6, 0x01, 0x0a, 0x09, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x44, 0x61, - 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x61, 0x73, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, - 0x52, 0x04, 0x68, 0x61, 0x73, 0x68, 0x12, 0x16, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x12, - 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x04, 0x62, 0x6f, - 0x64, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x18, 0x04, 0x20, - 0x01, 0x28, 0x0c, 0x52, 0x07, 0x72, 0x65, 0x63, 0x65, 0x69, 0x70, 0x74, 0x12, 0x23, 0x0a, 0x0d, - 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x71, 0x75, 0x65, 0x75, 0x65, 0x18, 0x05, 0x20, - 0x01, 0x28, 0x0c, 0x52, 0x0c, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x51, 0x75, 0x65, 0x75, - 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x6a, 0x75, 0x73, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, - 0x6f, 0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0d, 0x6a, 0x75, 0x73, 0x74, 0x69, 0x66, - 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x34, 0x0a, 0x16, 0x69, 0x73, 0x5f, 0x65, 0x6d, - 0x70, 0x74, 0x79, 0x5f, 0x6a, 0x75, 0x73, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, - 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x14, 0x69, 0x73, 0x45, 0x6d, 0x70, 0x74, 0x79, - 0x4a, 0x75, 0x73, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2a, 0x2a, 0x0a, - 0x09, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x0d, 0x0a, 0x09, 0x41, 0x73, - 0x63, 0x65, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x10, 0x00, 0x12, 0x0e, 0x0a, 0x0a, 0x44, 0x65, 0x73, - 0x63, 0x65, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x10, 0x01, 0x42, 0x31, 0x5a, 0x2f, 0x67, 0x69, 0x74, - 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x68, 0x61, 0x69, 0x6e, 0x53, 0x61, 0x66, - 0x65, 0x2f, 0x67, 0x6f, 0x73, 0x73, 0x61, 0x6d, 0x65, 0x72, 0x2f, 0x64, 0x6f, 0x74, 0x2f, 0x6e, - 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x2f, 0x0a, 0x09, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x0e, 0x32, 0x11, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x44, 0x69, 0x72, 0x65, + 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x09, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, + 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x61, 0x78, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x73, 0x18, 0x06, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x09, 0x6d, 0x61, 0x78, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x73, 0x42, + 0x0c, 0x0a, 0x0a, 0x66, 0x72, 0x6f, 0x6d, 0x5f, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x0a, + 0x0d, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x29, + 0x0a, 0x06, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x11, + 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x44, 0x61, 0x74, + 0x61, 0x52, 0x06, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x73, 0x22, 0xe6, 0x01, 0x0a, 0x09, 0x42, 0x6c, + 0x6f, 0x63, 0x6b, 0x44, 0x61, 0x74, 0x61, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x61, 0x73, 0x68, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x68, 0x61, 0x73, 0x68, 0x12, 0x16, 0x0a, 0x06, 0x68, + 0x65, 0x61, 0x64, 0x65, 0x72, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x06, 0x68, 0x65, 0x61, + 0x64, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x18, 0x03, 0x20, 0x03, 0x28, + 0x0c, 0x52, 0x04, 0x62, 0x6f, 0x64, 0x79, 0x12, 0x18, 0x0a, 0x07, 0x72, 0x65, 0x63, 0x65, 0x69, + 0x70, 0x74, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x72, 0x65, 0x63, 0x65, 0x69, 0x70, + 0x74, 0x12, 0x23, 0x0a, 0x0d, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x5f, 0x71, 0x75, 0x65, + 0x75, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0c, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x51, 0x75, 0x65, 0x75, 0x65, 0x12, 0x24, 0x0a, 0x0d, 0x6a, 0x75, 0x73, 0x74, 0x69, 0x66, + 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0d, 0x6a, + 0x75, 0x73, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x34, 0x0a, 0x16, + 0x69, 0x73, 0x5f, 0x65, 0x6d, 0x70, 0x74, 0x79, 0x5f, 0x6a, 0x75, 0x73, 0x74, 0x69, 0x66, 0x69, + 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x07, 0x20, 0x01, 0x28, 0x08, 0x52, 0x14, 0x69, 0x73, + 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4a, 0x75, 0x73, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, + 0x6f, 0x6e, 0x2a, 0x2a, 0x0a, 0x09, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, + 0x0d, 0x0a, 0x09, 0x41, 0x73, 0x63, 0x65, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x10, 0x00, 0x12, 0x0e, + 0x0a, 0x0a, 0x44, 0x65, 0x73, 0x63, 0x65, 0x6e, 0x64, 0x69, 0x6e, 0x67, 0x10, 0x01, 0x42, 0x31, + 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43, 0x68, 0x61, + 0x69, 0x6e, 0x53, 0x61, 0x66, 0x65, 0x2f, 0x67, 0x6f, 0x73, 0x73, 0x61, 0x6d, 0x65, 0x72, 0x2f, + 0x64, 0x6f, 0x74, 0x2f, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x2f, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/dot/network/proto/api.v1.proto b/dot/network/proto/api.v1.proto index 2c68c7619e..df2ed91715 100644 --- a/dot/network/proto/api.v1.proto +++ b/dot/network/proto/api.v1.proto @@ -29,8 +29,6 @@ message BlockRequest { // Start with given block number. bytes number = 3; } - // End at this block. An implementation defined maximum is used when unspecified. - bytes to_block = 4; // optional // Sequence direction. Direction direction = 5; // Maximum number of blocks to return. An implementation defined maximum is used when unspecified. diff --git a/dot/sync/chain_sync.go b/dot/sync/chain_sync.go index 7eb6ced84f..f3f8beb5e2 100644 --- a/dot/sync/chain_sync.go +++ b/dot/sync/chain_sync.go @@ -1038,17 +1038,9 @@ func workerToRequests(w *worker) ([]*network.BlockRequestMessage, error) { } } - var end *common.Hash - if !w.targetHash.IsEmpty() && i == numRequests-1 { - // if we're on our last request (which should contain the target hash), - // then add it - end = &w.targetHash - } - reqs[i] = &network.BlockRequestMessage{ RequestedData: w.requestData, StartingBlock: *start, - EndBlockHash: end, Direction: w.direction, Max: &max, } diff --git a/dot/sync/chain_sync_test.go b/dot/sync/chain_sync_test.go index 28869a0e99..09061f7357 100644 --- a/dot/sync/chain_sync_test.go +++ b/dot/sync/chain_sync_test.go @@ -401,7 +401,6 @@ func TestChainSync_sync_bootstrap_withWorkerError(t *testing.T) { mockNetwork.EXPECT().DoBlockRequest(peer.ID("noot"), &network.BlockRequestMessage{ RequestedData: 19, StartingBlock: *startingBlock, - EndBlockHash: nil, Direction: 0, Max: &max, }) @@ -538,7 +537,6 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1), - EndBlockHash: nil, Direction: network.Ascending, Max: &max128, }, @@ -555,14 +553,12 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1), - EndBlockHash: nil, Direction: network.Ascending, Max: &max128, }, { RequestedData: network.RequestedDataHeader + network.RequestedDataBody + network.RequestedDataJustification, StartingBlock: *variadic.MustNewUint32OrHash(1 + maxResponseSize), - EndBlockHash: nil, Direction: network.Ascending, Max: &max128, }, @@ -579,7 +575,6 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1), - EndBlockHash: nil, Direction: network.Ascending, Max: &max128, }, @@ -596,7 +591,6 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(10), - EndBlockHash: nil, Direction: network.Descending, Max: &max9, }, @@ -613,14 +607,12 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1), - EndBlockHash: nil, Direction: network.Ascending, Max: &max128, }, { RequestedData: network.RequestedDataHeader + network.RequestedDataBody + network.RequestedDataJustification, StartingBlock: *variadic.MustNewUint32OrHash(1 + maxResponseSize), - EndBlockHash: nil, Direction: network.Ascending, Max: &max128, }, @@ -638,7 +630,6 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1), - EndBlockHash: &(common.Hash{0xa}), Direction: network.Ascending, Max: &max128, }, @@ -657,7 +648,6 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(common.Hash{0xb}), - EndBlockHash: &(common.Hash{0xc}), Direction: network.Ascending, Max: &max128, }, @@ -690,14 +680,12 @@ func TestWorkerToRequests(t *testing.T) { { RequestedData: network.RequestedDataHeader + network.RequestedDataBody + network.RequestedDataJustification, StartingBlock: *variadic.MustNewUint32OrHash(1 + (maxResponseSize / 2)), - EndBlockHash: nil, Direction: network.Descending, Max: &max64, }, { RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1 + maxResponseSize + (maxResponseSize / 2)), - EndBlockHash: nil, Direction: network.Descending, Max: &max128, }, @@ -894,7 +882,6 @@ func TestChainSync_doSync(t *testing.T) { req := &network.BlockRequestMessage{ RequestedData: bootstrapRequestData, StartingBlock: *variadic.MustNewUint32OrHash(1), - EndBlockHash: nil, Direction: network.Ascending, Max: &max, } @@ -917,7 +904,6 @@ func TestChainSync_doSync(t *testing.T) { mockNetwork.EXPECT().DoBlockRequest(peer.ID("noot"), &network.BlockRequestMessage{ RequestedData: 19, StartingBlock: *startingBlock, - EndBlockHash: nil, Direction: 0, Max: &max1, }) @@ -943,7 +929,6 @@ func TestChainSync_doSync(t *testing.T) { mockNetwork.EXPECT().DoBlockRequest(peer.ID("noot"), &network.BlockRequestMessage{ RequestedData: 19, StartingBlock: *startingBlock, - EndBlockHash: nil, Direction: 0, Max: &max1, }).Return(resp, nil) @@ -985,7 +970,6 @@ func TestChainSync_doSync(t *testing.T) { mockNetwork.EXPECT().DoBlockRequest(peer.ID("noot"), &network.BlockRequestMessage{ RequestedData: 19, StartingBlock: *startingBlock, - EndBlockHash: nil, Direction: 1, Max: &max1, }).Return(resp, nil) diff --git a/dot/sync/errors.go b/dot/sync/errors.go index ff0f1fa1e3..51334ab66f 100644 --- a/dot/sync/errors.go +++ b/dot/sync/errors.go @@ -15,10 +15,9 @@ var ( ErrInvalidBlock = errors.New("could not verify block") // ErrInvalidBlockRequest is returned when an invalid block request is received - ErrInvalidBlockRequest = errors.New("invalid block request") - errInvalidRequestDirection = errors.New("invalid request direction") - errRequestStartTooHigh = errors.New("request start number is higher than our best block") - errFailedToGetEndHashAncestor = errors.New("failed to get ancestor of end block") + ErrInvalidBlockRequest = errors.New("invalid block request") + errInvalidRequestDirection = errors.New("invalid request direction") + errRequestStartTooHigh = errors.New("request start number is higher than our best block") // chainSync errors errEmptyBlockData = errors.New("empty block data") diff --git a/dot/sync/message.go b/dot/sync/message.go index b896ace9ce..2c44629ed1 100644 --- a/dot/sync/message.go +++ b/dot/sync/message.go @@ -30,54 +30,33 @@ func (s *Service) CreateBlockResponse(req *network.BlockRequestMessage) (*networ func (s *Service) handleAscendingRequest(req *network.BlockRequestMessage) (*network.BlockResponseMessage, error) { var ( - startHash *common.Hash - endHash = req.EndBlockHash - startNumber, endNumber, max uint + max uint = maxResponseSize + startHash *common.Hash + startNumber uint ) - max = maxResponseSize // determine maximum response size if req.Max != nil && *req.Max < maxResponseSize { max = uint(*req.Max) } + bestBlockNumber, err := s.blockState.BestBlockNumber() + if err != nil { + return nil, fmt.Errorf("getting best block for request: %w", err) + } + switch startBlock := req.StartingBlock.Value().(type) { case uint32: if startBlock == 0 { startBlock = 1 } - bestBlockNumber, err := s.blockState.BestBlockNumber() - if err != nil { - return nil, fmt.Errorf("failed to get best block %d for request: %w", bestBlockNumber, err) - } - // if request start is higher than our best block, return error if bestBlockNumber < uint(startBlock) { return nil, errRequestStartTooHigh } startNumber = uint(startBlock) - - if endHash != nil { - // TODO: end hash is provided but start hash isn't, so we need to determine a start block - // that is an ancestor of the end block - sh, err := s.blockState.GetHashByNumber(startNumber) - if err != nil { - return nil, fmt.Errorf("failed to get start block %d for request: %w", startNumber, err) - } - - is, err := s.blockState.IsDescendantOf(sh, *endHash) - if err != nil { - return nil, err - } - - if !is { - return nil, fmt.Errorf("%w: hash=%s", errFailedToGetEndHashAncestor, *endHash) - } - - startHash = &sh - } case common.Hash: startHash = &startBlock @@ -92,28 +71,14 @@ func (s *Service) handleAscendingRequest(req *network.BlockRequestMessage) (*net return nil, ErrInvalidBlockRequest } - if endHash == nil { - endNumber = startNumber + max - 1 - bestBlockNumber, err := s.blockState.BestBlockNumber() - if err != nil { - return nil, fmt.Errorf("failed to get best block %d for request: %w", bestBlockNumber, err) - } - - if endNumber > bestBlockNumber { - endNumber = bestBlockNumber - } - } else { - header, err := s.blockState.GetHeader(*endHash) - if err != nil { - return nil, fmt.Errorf("failed to get end block %s: %w", *endHash, err) - } - - endNumber = header.Number + endNumber := startNumber + max - 1 + if endNumber > bestBlockNumber { + endNumber = bestBlockNumber } - // start hash provided, need to determine end hash that is descendant of start hash + var endHash *common.Hash if startHash != nil { - eh, err := s.checkOrGetDescendantHash(*startHash, endHash, endNumber) + eh, err := s.checkOrGetDescendantHash(*startHash, nil, endNumber) if err != nil { return nil, err } @@ -121,24 +86,28 @@ func (s *Service) handleAscendingRequest(req *network.BlockRequestMessage) (*net endHash = &eh } - if startHash == nil || endHash == nil { - logger.Debugf("handling BlockRequestMessage with direction %s "+ - "from start block with number %d to end block with number %d", + if startHash == nil { + logger.Debugf("handling block request: direction %s, "+ + "start block number: %d, "+ + "end block number: %d", req.Direction, startNumber, endNumber) + return s.handleAscendingByNumber(startNumber, endNumber, req.RequestedData) } - logger.Debugf("handling BlockRequestMessage with direction %s from start block with hash %s to end block with hash %s", + logger.Debugf("handling block request: direction %s, "+ + "start block hash: %s, "+ + "end block hash: %s", req.Direction, *startHash, *endHash) + return s.handleChainByHash(*startHash, *endHash, max, req.RequestedData, req.Direction) } func (s *Service) handleDescendingRequest(req *network.BlockRequestMessage) (*network.BlockResponseMessage, error) { var ( - startHash *common.Hash - endHash = req.EndBlockHash - startNumber, endNumber uint - max uint = maxResponseSize + startHash *common.Hash + startNumber uint + max uint = maxResponseSize ) // determine maximum response size @@ -173,33 +142,21 @@ func (s *Service) handleDescendingRequest(req *network.BlockRequestMessage) (*ne return nil, ErrInvalidBlockRequest } - // end hash provided, need to determine start hash that is descendant of end hash - if endHash != nil { - sh, err := s.checkOrGetDescendantHash(*endHash, startHash, startNumber) - startHash = &sh - if err != nil { - return nil, err - } + endNumber := uint(1) + if startNumber > max+1 { + endNumber = startNumber - max + 1 } - // end hash is not provided, calculate end by number - if endHash == nil { - if startNumber <= max+1 { - endNumber = 1 - } else { - endNumber = startNumber - max + 1 + var endHash *common.Hash + if startHash != nil { + // need to get blocks by subchain if start hash is provided, get end hash + endHeader, err := s.blockState.GetHeaderByNumber(endNumber) + if err != nil { + return nil, fmt.Errorf("getting end block %d for request: %w", endNumber, err) } - if startHash != nil { - // need to get blocks by subchain if start hash is provided, get end hash - endHeader, err := s.blockState.GetHeaderByNumber(endNumber) - if err != nil { - return nil, fmt.Errorf("failed to get end block %d for request: %w", endNumber, err) - } - - hash := endHeader.Hash() - endHash = &hash - } + hash := endHeader.Hash() + endHash = &hash } if startHash == nil || endHash == nil { @@ -209,7 +166,8 @@ func (s *Service) handleDescendingRequest(req *network.BlockRequestMessage) (*ne return s.handleDescendingByNumber(startNumber, endNumber, req.RequestedData) } - logger.Debugf("handling BlockRequestMessage with direction %s from start block with hash %s to end block with hash %s", + logger.Debugf("handling block request message with direction %s "+ + "from start block with hash %s to end block with hash %s", req.Direction, *startHash, *endHash) return s.handleChainByHash(*endHash, *startHash, max, req.RequestedData, req.Direction) } @@ -335,7 +293,7 @@ func (s *Service) handleChainByHash(ancestor, descendant common.Hash, *network.BlockResponseMessage, error) { subchain, err := s.blockState.SubChain(ancestor, descendant) if err != nil { - return nil, err + return nil, fmt.Errorf("retrieving subchain: %w", err) } // If the direction is descending, prune from the start. diff --git a/dot/sync/message_integration_test.go b/dot/sync/message_integration_test.go index 9a86e7628d..6a159b78fa 100644 --- a/dot/sync/message_integration_test.go +++ b/dot/sync/message_integration_test.go @@ -57,7 +57,6 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) { req := &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Ascending, Max: nil, } @@ -72,7 +71,6 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Ascending, Max: &max, } @@ -87,7 +85,6 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Ascending, Max: &max, } @@ -105,7 +102,6 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Descending, Max: nil, } @@ -123,7 +119,6 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Descending, Max: &max, } @@ -138,7 +133,6 @@ func TestService_CreateBlockResponse_MaxSize(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Descending, Max: &max, } @@ -164,7 +158,6 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { req := &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Ascending, Max: nil, } @@ -175,24 +168,6 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { require.Equal(t, uint(1), resp.BlockData[0].Number()) require.Equal(t, uint(128), resp.BlockData[127].Number()) - endHash, err := s.blockState.GetHashByNumber(16) - require.NoError(t, err) - - // test ascending with non-nil endBlockHash - req = &network.BlockRequestMessage{ - RequestedData: 3, - StartingBlock: *start, - EndBlockHash: &endHash, - Direction: network.Ascending, - Max: nil, - } - - resp, err = s.CreateBlockResponse(req) - require.NoError(t, err) - require.Equal(t, int(16), len(resp.BlockData)) - require.Equal(t, uint(1), resp.BlockData[0].Number()) - require.Equal(t, uint(16), resp.BlockData[15].Number()) - // test descending with nil endBlockHash startHash, err = s.blockState.GetHashByNumber(16) require.NoError(t, err) @@ -203,7 +178,6 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Descending, Max: nil, } @@ -214,14 +188,9 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { require.Equal(t, uint(16), resp.BlockData[0].Number()) require.Equal(t, uint(1), resp.BlockData[15].Number()) - // test descending with non-nil endBlockHash - endHash, err = s.blockState.GetHashByNumber(1) - require.NoError(t, err) - req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: &endHash, Direction: network.Descending, Max: nil, } @@ -242,7 +211,6 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Descending, Max: nil, } @@ -262,7 +230,6 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { req = &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: nil, Direction: network.Descending, Max: nil, } @@ -274,95 +241,6 @@ func TestService_CreateBlockResponse_StartHash(t *testing.T) { require.Equal(t, uint(1), resp.BlockData[127].Number()) } -func TestService_CreateBlockResponse_Ascending_EndHash(t *testing.T) { - t.Parallel() - s := newTestSyncer(t) - addTestBlocksToState(t, uint(maxResponseSize+1), s.blockState) - - // should error if end < start - start, err := variadic.NewUint32OrHash(uint32(128)) - require.NoError(t, err) - - end, err := s.blockState.GetHashByNumber(1) - require.NoError(t, err) - - req := &network.BlockRequestMessage{ - RequestedData: 3, - StartingBlock: *start, - EndBlockHash: &end, - Direction: network.Ascending, - Max: nil, - } - - _, err = s.CreateBlockResponse(req) - require.Error(t, err) - - // base case - start, err = variadic.NewUint32OrHash(uint32(1)) - require.NoError(t, err) - - end, err = s.blockState.GetHashByNumber(128) - require.NoError(t, err) - - req = &network.BlockRequestMessage{ - RequestedData: 3, - StartingBlock: *start, - EndBlockHash: &end, - Direction: network.Ascending, - Max: nil, - } - - resp, err := s.CreateBlockResponse(req) - require.NoError(t, err) - require.Equal(t, int(maxResponseSize), len(resp.BlockData)) - require.Equal(t, uint(1), resp.BlockData[0].Number()) - require.Equal(t, uint(128), resp.BlockData[127].Number()) -} - -func TestService_CreateBlockResponse_Descending_EndHash(t *testing.T) { - s := newTestSyncer(t) - addTestBlocksToState(t, uint(maxResponseSize+1), s.blockState) - - // should error if start < end - start, err := variadic.NewUint32OrHash(uint32(1)) - require.NoError(t, err) - - end, err := s.blockState.GetHashByNumber(128) - require.NoError(t, err) - - req := &network.BlockRequestMessage{ - RequestedData: 3, - StartingBlock: *start, - EndBlockHash: &end, - Direction: network.Descending, - Max: nil, - } - - _, err = s.CreateBlockResponse(req) - require.Error(t, err) - - // base case - start, err = variadic.NewUint32OrHash(uint32(128)) - require.NoError(t, err) - - end, err = s.blockState.GetHashByNumber(1) - require.NoError(t, err) - - req = &network.BlockRequestMessage{ - RequestedData: 3, - StartingBlock: *start, - EndBlockHash: &end, - Direction: network.Descending, - Max: nil, - } - - resp, err := s.CreateBlockResponse(req) - require.NoError(t, err) - require.Equal(t, int(maxResponseSize), len(resp.BlockData)) - require.Equal(t, uint(128), resp.BlockData[0].Number()) - require.Equal(t, uint(1), resp.BlockData[127].Number()) -} - func TestService_checkOrGetDescendantHash_integration(t *testing.T) { t.Parallel() s := newTestSyncer(t) @@ -477,7 +355,6 @@ func TestService_CreateBlockResponse_Fields(t *testing.T) { Justification: &c, } - endHash := s.blockState.BestBlockHash() start, err := variadic.NewUint32OrHash(uint32(1)) require.NoError(t, err) @@ -494,7 +371,6 @@ func TestService_CreateBlockResponse_Fields(t *testing.T) { value: &network.BlockRequestMessage{ RequestedData: 3, StartingBlock: *start, - EndBlockHash: &endHash, Direction: network.Ascending, Max: nil, }, @@ -513,7 +389,6 @@ func TestService_CreateBlockResponse_Fields(t *testing.T) { value: &network.BlockRequestMessage{ RequestedData: 1, StartingBlock: *start, - EndBlockHash: &endHash, Direction: network.Ascending, Max: nil, }, @@ -532,7 +407,6 @@ func TestService_CreateBlockResponse_Fields(t *testing.T) { value: &network.BlockRequestMessage{ RequestedData: 4, StartingBlock: *start, - EndBlockHash: &endHash, Direction: network.Ascending, Max: nil, }, @@ -552,7 +426,6 @@ func TestService_CreateBlockResponse_Fields(t *testing.T) { value: &network.BlockRequestMessage{ RequestedData: 8, StartingBlock: *start, - EndBlockHash: &endHash, Direction: network.Ascending, Max: nil, }, diff --git a/dot/sync/message_test.go b/dot/sync/message_test.go index 8abfd1923f..71fc20f81b 100644 --- a/dot/sync/message_test.go +++ b/dot/sync/message_test.go @@ -29,15 +29,17 @@ func TestService_CreateBlockResponse(t *testing.T) { }{ "invalid block request": { blockStateBuilder: func(ctrl *gomock.Controller) BlockState { - return nil + mockBlockState := NewMockBlockState(ctrl) + mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil) + return mockBlockState }, args: args{req: &network.BlockRequestMessage{}}, err: ErrInvalidBlockRequest, }, - "ascending request nil startHash nil endHash": { + "ascending request nil startHash": { blockStateBuilder: func(ctrl *gomock.Controller) BlockState { mockBlockState := NewMockBlockState(ctrl) - mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil).Times(2) + mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil) mockBlockState.EXPECT().GetHashByNumber(uint(1)).Return(common.Hash{1, 2}, nil) return mockBlockState }, @@ -63,33 +65,7 @@ func TestService_CreateBlockResponse(t *testing.T) { err: errRequestStartTooHigh, want: nil, }, - "ascending request endHash not nil": { - blockStateBuilder: func(ctrl *gomock.Controller) BlockState { - mockBlockState := NewMockBlockState(ctrl) - mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil) - mockBlockState.EXPECT().GetHashByNumber(uint(1)).Return(common.Hash{1, 2}, nil) - mockBlockState.EXPECT().IsDescendantOf(common.Hash{1, 2}, common.Hash{1, 2, 3}).Return(true, - nil).Times(2) - mockBlockState.EXPECT().GetHeader(common.Hash{1, 2}).Return(&types.Header{ - Number: 1, - }, nil) - mockBlockState.EXPECT().GetHeader(common.Hash{1, 2, 3}).Return(&types.Header{ - Number: 2, - }, nil) - mockBlockState.EXPECT().SubChain(common.Hash{1, 2}, common.Hash{1, 2, 3}).Return([]common.Hash{{1, 2}}, - nil) - return mockBlockState - }, - args: args{req: &network.BlockRequestMessage{ - StartingBlock: *variadic.MustNewUint32OrHash(0), - EndBlockHash: &common.Hash{1, 2, 3}, - Direction: network.Ascending, - }}, - want: &network.BlockResponseMessage{BlockData: []*types.BlockData{{ - Hash: common.Hash{1, 2}, - }}}, - }, - "descending request nil startHash nil endHash": { + "descending request nil startHash": { blockStateBuilder: func(ctrl *gomock.Controller) BlockState { mockBlockState := NewMockBlockState(ctrl) mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil) @@ -117,28 +93,7 @@ func TestService_CreateBlockResponse(t *testing.T) { Hash: common.Hash{1, 2}, }}}, }, - "descending request endHash not nil": { - blockStateBuilder: func(ctrl *gomock.Controller) BlockState { - mockBlockState := NewMockBlockState(ctrl) - mockBlockState.EXPECT().BestBlockNumber().Return(uint(1), nil) - mockBlockState.EXPECT().GetHashByNumber(uint(0)).Return(common.Hash{1, 2}, nil) - mockBlockState.EXPECT().IsDescendantOf(common.Hash{1, 2, 3}, common.Hash{1, 2}).Return(true, - nil) - mockBlockState.EXPECT().SubChain(common.Hash{1, 2, 3}, common.Hash{1, 2}).Return([]common.Hash{{1, - 2}}, - nil) - return mockBlockState - }, - args: args{req: &network.BlockRequestMessage{ - StartingBlock: *variadic.MustNewUint32OrHash(0), - EndBlockHash: &common.Hash{1, 2, 3}, - Direction: network.Descending, - }}, - want: &network.BlockResponseMessage{BlockData: []*types.BlockData{{ - Hash: common.Hash{1, 2}, - }}}, - }, - "ascending request startHash nil endHash": { + "ascending request startHash": { blockStateBuilder: func(ctrl *gomock.Controller) BlockState { mockBlockState := NewMockBlockState(ctrl) mockBlockState.EXPECT().GetHeader(common.Hash{}).Return(&types.Header{ @@ -161,7 +116,7 @@ func TestService_CreateBlockResponse(t *testing.T) { Hash: common.Hash{1, 2}, }}}, }, - "descending request startHash nil endHash": { + "descending request startHash": { blockStateBuilder: func(ctrl *gomock.Controller) BlockState { mockBlockState := NewMockBlockState(ctrl) mockBlockState.EXPECT().GetHeader(common.Hash{}).Return(&types.Header{ @@ -187,9 +142,10 @@ func TestService_CreateBlockResponse(t *testing.T) { blockStateBuilder: func(ctrl *gomock.Controller) BlockState { return nil }, - args: args{req: &network.BlockRequestMessage{ - Direction: network.SyncDirection(3), - }}, + args: args{ + req: &network.BlockRequestMessage{ + Direction: network.SyncDirection(3), + }}, err: errInvalidRequestDirection, }, } diff --git a/go.mod b/go.mod index 083c52109a..b41672efa7 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/fatih/color v1.13.0 github.com/go-playground/validator/v10 v10.11.1 github.com/golang/mock v1.6.0 - github.com/golang/protobuf v1.5.2 + github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.9 github.com/google/uuid v1.3.0 github.com/gorilla/mux v1.8.0 diff --git a/lib/blocktree/blocktree.go b/lib/blocktree/blocktree.go index 5396f1b00a..c0b683f0a5 100644 --- a/lib/blocktree/blocktree.go +++ b/lib/blocktree/blocktree.go @@ -214,11 +214,11 @@ func (bt *BlockTree) String() string { func (bt *BlockTree) subChain(start, end Hash) ([]*node, error) { sn := bt.getNode(start) if sn == nil { - return nil, ErrStartNodeNotFound + return nil, fmt.Errorf("%w: %s", ErrStartNodeNotFound, start) } en := bt.getNode(end) if en == nil { - return nil, ErrEndNodeNotFound + return nil, fmt.Errorf("%w: %s", ErrEndNodeNotFound, end) } return sn.subChain(en) }