Skip to content

Commit

Permalink
feat(dot/sync): Remove the EndBlockHash from BlockRequestMessage (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
EclesioMeloJunior authored Dec 7, 2022
1 parent 87d58e6 commit b25e0b4
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 383 deletions.
4 changes: 1 addition & 3 deletions dot/network/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
}
Expand Down
31 changes: 3 additions & 28 deletions dot/network/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -149,7 +133,6 @@ func (bm *BlockRequestMessage) Decode(in []byte) error {

var (
startingBlock *variadic.Uint32OrHash
endBlockHash *common.Hash
max *uint32
)

Expand All @@ -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 {
Expand All @@ -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

Expand Down
16 changes: 4 additions & 12 deletions dot/network/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ 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,
}

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)
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand All @@ -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)
Expand All @@ -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,
}
Expand Down
75 changes: 32 additions & 43 deletions dot/network/proto/api.v1.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions dot/network/proto/api.v1.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 0 additions & 8 deletions dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
Loading

0 comments on commit b25e0b4

Please sign in to comment.