From 8320a2de02e1122ff3dd75c1d03cbea7d52f5c8c Mon Sep 17 00:00:00 2001 From: Darshan Kathiriya <8559992+lakshya-sky@users.noreply.github.com> Date: Fri, 9 Dec 2022 05:09:54 -0500 Subject: [PATCH] chore(lib/common): `Equal` method of `Hash` replaced with `==` (#2985) --- dot/core/service.go | 2 +- dot/network/block_announce.go | 2 +- dot/state/block.go | 2 +- dot/state/block_finalisation.go | 10 +++++----- dot/state/grandpa_changes.go | 8 ++++---- dot/sync/chain_sync.go | 4 ++-- dot/sync/message_integration_test.go | 4 ++-- dot/sync/tip_syncer.go | 2 +- lib/babe/babe.go | 2 +- lib/babe/verify.go | 2 +- lib/common/hash.go | 8 +------- lib/common/hash_test.go | 6 ------ lib/grandpa/message_handler.go | 2 +- lib/grandpa/round_integration_test.go | 4 ++-- 14 files changed, 23 insertions(+), 35 deletions(-) diff --git a/dot/core/service.go b/dot/core/service.go index 2f19279a8d..ea4a7d708f 100644 --- a/dot/core/service.go +++ b/dot/core/service.go @@ -138,7 +138,7 @@ func (s *Service) HandleBlockImport(block *types.Block, state *rtstorage.TrieSta } bestBlockHash := s.blockState.BestBlockHash() - isBestBlock := bestBlockHash.Equal(block.Header.Hash()) + isBestBlock := bestBlockHash == block.Header.Hash() blockAnnounce, err := createBlockAnnounce(block, isBestBlock) if err != nil { diff --git a/dot/network/block_announce.go b/dot/network/block_announce.go index b4091aacde..83afeecbeb 100644 --- a/dot/network/block_announce.go +++ b/dot/network/block_announce.go @@ -164,7 +164,7 @@ func (s *Service) validateBlockAnnounceHandshake(from peer.ID, hs Handshake) err return fmt.Errorf("%w: %d", errInvalidRole, bhs.Roles) } - if !bhs.GenesisHash.Equal(s.blockState.GenesisHash()) { + if bhs.GenesisHash != s.blockState.GenesisHash() { s.host.cm.peerSetHandler.ReportPeer(peerset.ReputationChange{ Value: peerset.GenesisMismatch, Reason: peerset.GenesisMismatchReason, diff --git a/dot/state/block.go b/dot/state/block.go index d12e9b5011..4eeae7e75b 100644 --- a/dot/state/block.go +++ b/dot/state/block.go @@ -621,7 +621,7 @@ func (bs *BlockState) HandleRuntimeChanges(newState *rtstorage.TrieState, codeSubBlockHash := bs.baseState.LoadCodeSubstitutedBlockHash() - if !codeSubBlockHash.Equal(common.Hash{}) { + if codeSubBlockHash != (common.Hash{}) { newVersion, err := wasmer.GetRuntimeVersion(code) if err != nil { return err diff --git a/dot/state/block_finalisation.go b/dot/state/block_finalisation.go index cf867c9f45..2ebf138632 100644 --- a/dot/state/block_finalisation.go +++ b/dot/state/block_finalisation.go @@ -156,7 +156,7 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er // if nothing was previously finalised, set the first slot of the network to the // slot number of block 1, which is now being set as final - if bs.lastFinalised.Equal(bs.genesisHash) && !hash.Equal(bs.genesisHash) { + if bs.lastFinalised == bs.genesisHash && hash != bs.genesisHash { if err := bs.setFirstSlotOnFinalisation(); err != nil { return fmt.Errorf("failed to set first slot on finalisation: %w", err) } @@ -174,7 +174,7 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er ), ) - if !bs.lastFinalised.Equal(hash) { + if bs.lastFinalised != hash { defer func(lastFinalised common.Hash) { err := bs.deleteFromTries(lastFinalised) if err != nil { @@ -202,7 +202,7 @@ func (bs *BlockState) deleteFromTries(lastFinalised common.Hash) error { } func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error { - if curr.Equal(bs.lastFinalised) { + if curr == bs.lastFinalised { return nil } @@ -211,7 +211,7 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error { return fmt.Errorf("failed to get highest finalised hash: %w", err) } - if prev.Equal(curr) { + if prev == curr { return nil } @@ -224,7 +224,7 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error { // root of subchain is previously finalised block, which has already been stored in the db for _, hash := range subchain[1:] { - if hash.Equal(bs.genesisHash) { + if hash == bs.genesisHash { continue } diff --git a/dot/state/grandpa_changes.go b/dot/state/grandpa_changes.go index f28ef37339..e06f1fe0fa 100644 --- a/dot/state/grandpa_changes.go +++ b/dot/state/grandpa_changes.go @@ -42,7 +42,7 @@ func (oc orderedPendingChanges) findApplicable(importedHash common.Hash, importe announcingHash := forced.announcingHeader.Hash() effectiveNumber := forced.effectiveNumber() - if importedHash.Equal(announcingHash) && effectiveNumber == importedNumber { + if importedHash == announcingHash && effectiveNumber == importedNumber { return true, nil } @@ -81,7 +81,7 @@ func (oc *orderedPendingChanges) importChange(pendingChange pendingChange, isDes for _, change := range *oc { changeBlockHash := change.announcingHeader.Hash() - if changeBlockHash.Equal(announcingHeader) { + if changeBlockHash == announcingHeader { return fmt.Errorf("%w: %s", errDuplicateHashes, changeBlockHash) } @@ -144,7 +144,7 @@ func (c *pendingChangeNode) importNode(blockHash common.Hash, blockNumber uint, isDescendantOf isDescendantOfFunc) (imported bool, err error) { announcingHash := c.change.announcingHeader.Hash() - if blockHash.Equal(announcingHash) { + if blockHash == announcingHash { return false, fmt.Errorf("%w: %s", errDuplicateHashes, blockHash) } @@ -265,7 +265,7 @@ func (ct changeTree) findApplicableChange(hash common.Hash, number uint, } changeNodeHash := pcn.change.announcingHeader.Hash() - if !hash.Equal(changeNodeHash) { + if hash != changeNodeHash { isDescendant, err := isDescendantOf(changeNodeHash, hash) if err != nil { return false, fmt.Errorf("cannot verify ancestry: %w", err) diff --git a/dot/sync/chain_sync.go b/dot/sync/chain_sync.go index f3f8beb5e2..81164bd49e 100644 --- a/dot/sync/chain_sync.go +++ b/dot/sync/chain_sync.go @@ -292,7 +292,7 @@ func (cs *chainSync) setPeerHead(p peer.ID, hash common.Hash, number uint) error return fmt.Errorf("get block hash by number: %w", err) } - if ourHash.Equal(ps.hash) { + if ourHash == ps.hash { return nil } @@ -896,7 +896,7 @@ func (cs *chainSync) validateResponse(req *network.BlockRequestMessage, // otherwise, check that this response forms a chain // ie. curr's parent hash is hash of previous header, and curr's number is previous number + 1 - if !prev.Hash().Equal(curr.ParentHash) || curr.Number != prev.Number+1 { + if prev.Hash() != curr.ParentHash || curr.Number != prev.Number+1 { // the response is missing some blocks, place blocks from curr onwards into pending blocks set for _, bd := range resp.BlockData[i:] { if err := cs.pendingBlocks.addBlock(&types.Block{ diff --git a/dot/sync/message_integration_test.go b/dot/sync/message_integration_test.go index 6a159b78fa..13c8ac05fe 100644 --- a/dot/sync/message_integration_test.go +++ b/dot/sync/message_integration_test.go @@ -270,7 +270,7 @@ func TestService_checkOrGetDescendantHash_integration(t *testing.T) { require.NoError(t, err) for _, leaf := range leaves { - if !leaf.Equal(descendant) { + if leaf != descendant { descendant = leaf break } @@ -304,7 +304,7 @@ func TestService_checkOrGetDescendantHash_integration(t *testing.T) { // set ancestor to non-canonical block 9 for _, block := range block9s { - if !canonical.Equal(block) { + if canonical != block { ancestor = block break } diff --git a/dot/sync/tip_syncer.go b/dot/sync/tip_syncer.go index a7264d9ccb..00d2318cff 100644 --- a/dot/sync/tip_syncer.go +++ b/dot/sync/tip_syncer.go @@ -128,7 +128,7 @@ func (*tipSyncer) hasCurrentWorker(w *worker, workers map[uint64]*worker) bool { // worker (start, end) is within curr (start, end), if hashes are equal then the request is either // for the same data or some subset of data that is covered by curr - if w.startHash.Equal(curr.startHash) || w.targetHash.Equal(curr.targetHash) { + if w.startHash == curr.startHash || w.targetHash == curr.targetHash { return true } } diff --git a/lib/babe/babe.go b/lib/babe/babe.go index 63c3b0dc74..9fff6e2d30 100644 --- a/lib/babe/babe.go +++ b/lib/babe/babe.go @@ -464,7 +464,7 @@ func (b *Service) getParentForBlockAuthoring(slotNum uint64) (*types.Header, err return nil, errNilParentHeader } - atGenesisBlock := b.blockState.GenesisHash().Equal(parentHeader.Hash()) + atGenesisBlock := b.blockState.GenesisHash() == parentHeader.Hash() if !atGenesisBlock { bestBlockSlotNum, err := b.blockState.GetSlotForBlock(parentHeader.Hash()) if err != nil { diff --git a/lib/babe/verify.go b/lib/babe/verify.go index 9a91ffa238..a0f628a821 100644 --- a/lib/babe/verify.go +++ b/lib/babe/verify.go @@ -358,7 +358,7 @@ func (b *verifier) verifyBlockEquivocation(header *types.Header) (bool, error) { } for _, blockHashInSlot := range blockHashesInSlot { - if blockHashInSlot.Equal(currentHash) { + if blockHashInSlot == currentHash { continue } diff --git a/lib/common/hash.go b/lib/common/hash.go index 0c33b93699..005edb27d3 100644 --- a/lib/common/hash.go +++ b/lib/common/hash.go @@ -4,7 +4,6 @@ package common import ( - "bytes" "encoding/hex" "encoding/json" "errors" @@ -41,7 +40,7 @@ func HashValidator(field reflect.Value) interface{} { // Try to convert to hash type. if valuer, ok := field.Interface().(Hash); ok { // Check if the hash is empty. - if valuer.Equal(Hash{}) { + if valuer == (Hash{}) { return "" } return valuer.ToBytes() @@ -49,11 +48,6 @@ func HashValidator(field reflect.Value) interface{} { return "" } -// Equal compares two hashes -func (h Hash) Equal(g Hash) bool { - return bytes.Equal(h[:], g[:]) -} - // IsEmpty returns true if the hash is empty, false otherwise. func (h Hash) IsEmpty() bool { return h == Hash{} diff --git a/lib/common/hash_test.go b/lib/common/hash_test.go index 0de0f0390b..6e2627a6e4 100644 --- a/lib/common/hash_test.go +++ b/lib/common/hash_test.go @@ -104,10 +104,4 @@ func Benchmark_IsEmpty(b *testing.B) { _ = h == empty } }) - - b.Run("using bytes.Equal", func(b *testing.B) { - for i := 0; i < b.N; i++ { - _ = h.Equal(Hash{}) - } - }) } diff --git a/lib/grandpa/message_handler.go b/lib/grandpa/message_handler.go index 1914028886..335da1a5ae 100644 --- a/lib/grandpa/message_handler.go +++ b/lib/grandpa/message_handler.go @@ -412,7 +412,7 @@ func (s *Service) VerifyBlockJustification(hash common.Hash, justification []byt return nil, err } - if !hash.Equal(fj.Commit.Hash) { + if hash != fj.Commit.Hash { return nil, fmt.Errorf("%w: justification %s and block hash %s", ErrJustificationMismatch, fj.Commit.Hash.Short(), hash.Short()) } diff --git a/lib/grandpa/round_integration_test.go b/lib/grandpa/round_integration_test.go index 3577efa95c..5469ab6d5e 100644 --- a/lib/grandpa/round_integration_test.go +++ b/lib/grandpa/round_integration_test.go @@ -289,7 +289,7 @@ func TestPlayGrandpaRound(t *testing.T) { var latestHash common.Hash = grandpaServices[0].head.Hash() for _, grandpaService := range grandpaServices[1:] { serviceFinalizedHash := grandpaService.head.Hash() - eql := serviceFinalizedHash.Equal(latestHash) + eql := serviceFinalizedHash == latestHash if !eql { t.Errorf("miss match service finalized hash\n\texpecting %s\n\tgot%s\n", latestHash, serviceFinalizedHash) @@ -513,7 +513,7 @@ func assertSamefinalisationAndChainGrowth(t *testing.T, services []*Service, cur var latestFinalized common.Hash = finalizedHeaderCurrentRound[0].Hash() for _, finalizedHead := range finalizedHeaderCurrentRound[1:] { - eq := finalizedHead.Hash().Equal(latestFinalized) + eq := finalizedHead.Hash() == latestFinalized if !eq { t.Errorf("miss match finalized hash\n\texpected %s\n\tgot%s\n", latestFinalized, finalizedHead)