Skip to content

Commit

Permalink
chore(lib/common): Equal method of Hash replaced with == (#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
lakshya-sky authored Dec 9, 2022
1 parent 32a80aa commit 8320a2d
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion dot/network/block_announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions dot/state/block_finalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

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

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

Expand Down
8 changes: 4 additions & 4 deletions dot/state/grandpa_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions dot/sync/chain_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions dot/sync/message_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/tip_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/babe/babe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/babe/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 1 addition & 7 deletions lib/common/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package common

import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -41,19 +40,14 @@ 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()
}
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{}
Expand Down
6 changes: 0 additions & 6 deletions lib/common/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
})
}
2 changes: 1 addition & 1 deletion lib/grandpa/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions lib/grandpa/round_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8320a2d

Please sign in to comment.