Skip to content

Commit

Permalink
implement Hash function for block announce handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
kishansagathiya committed Apr 1, 2022
1 parent 1fcfb7f commit 41d1f45
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 9 additions & 3 deletions dot/network/block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,15 @@ func (*BlockAnnounceHandshake) Type() byte {
return 0
}

// Hash ...
func (*BlockAnnounceHandshake) Hash() (common.Hash, error) {
return common.Hash{}, nil
// Hash returns blake2b hash of block announce handshake.
func (hs *BlockAnnounceHandshake) Hash() (common.Hash, error) {
// scale encode each extrinsic
encMsg, err := hs.Encode()
if err != nil {
return common.Hash{}, fmt.Errorf("cannot encode handshake: %w", err)
}

return common.Blake2bHash(encMsg)
}

// IsHandshake returns true
Expand Down
4 changes: 3 additions & 1 deletion dot/network/gossip.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ func (g *gossip) hasSeen(msg NotificationsMessage) (bool, error) {
_, ok := g.seenMap[msgHash]
if !ok {
// set message to has been seen
g.seenMap[msgHash] = struct{}{}
if !msg.IsHandshake() {
g.seenMap[msgHash] = struct{}{}
}
return false, nil
}

Expand Down
4 changes: 3 additions & 1 deletion dot/network/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ func TestCreateNotificationsMessageHandler_BlockAnnounceHandshake(t *testing.T)
Roles: 4,
BestBlockNumber: 77,
BestBlockHash: common.Hash{1},
GenesisHash: common.Hash{2},
// we are using a different genesis here, thus this
// handshake would be validated to be incorrect.
GenesisHash: common.Hash{2},
}

err = handler(stream, testHandshake)
Expand Down

0 comments on commit 41d1f45

Please sign in to comment.