From 41d1f45c99df8e6ad067eaeb5da9bd568e0f4e32 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Fri, 1 Apr 2022 20:46:17 +0530 Subject: [PATCH] implement Hash function for block announce handshake --- dot/network/block_announce.go | 12 +++++++++--- dot/network/gossip.go | 4 +++- dot/network/notifications_test.go | 4 +++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/dot/network/block_announce.go b/dot/network/block_announce.go index e7eafc5575..8c5771b67e 100644 --- a/dot/network/block_announce.go +++ b/dot/network/block_announce.go @@ -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 diff --git a/dot/network/gossip.go b/dot/network/gossip.go index ab0d862f50..d91f1c017e 100644 --- a/dot/network/gossip.go +++ b/dot/network/gossip.go @@ -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 } diff --git a/dot/network/notifications_test.go b/dot/network/notifications_test.go index 1d31a261d1..5670114262 100644 --- a/dot/network/notifications_test.go +++ b/dot/network/notifications_test.go @@ -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)