From 0643b0d920a121ebc831143c5e6d7ade6b25c11b Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 7 Oct 2018 16:44:29 +0300 Subject: [PATCH] [DEV-189] invert arguments order of daghash.Less (#83) * [DEV-189] invert arguments order or daghash.Less * [DEV-189] invert arguments order of daghash.Less in blockset.highest * [DEV-189] change to equivalent condition in Hash.Less to make it prettier --- blockdag/blockset.go | 6 +++--- blockdag/phantom.go | 2 +- blockdag/validate.go | 2 +- dagconfig/daghash/hash.go | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/blockdag/blockset.go b/blockdag/blockset.go index b6602e023..0d9042435 100644 --- a/blockdag/blockset.go +++ b/blockdag/blockset.go @@ -38,7 +38,7 @@ func (bs blockSet) maxHeight() int32 { func (bs blockSet) highest() *blockNode { var highest *blockNode for _, node := range bs { - if highest.height < node.height || daghash.Less(&highest.hash, &node.hash) { + if highest.height < node.height || daghash.Less(&node.hash, &highest.hash) { highest = node } } @@ -130,7 +130,7 @@ func (bs blockSet) hashes() []daghash.Hash { hashes = append(hashes, hash) } sort.Slice(hashes, func(i, j int) bool { - return daghash.Less(&hashes[i], &hashes[j]) + return daghash.Less(&hashes[j], &hashes[i]) }) return hashes } @@ -160,7 +160,7 @@ func (bs blockSet) bluest() *blockNode { for _, node := range bs { if bluestNode == nil || node.blueScore > maxScore || - (node.blueScore == maxScore && daghash.Less(&bluestNode.hash, &node.hash)) { + (node.blueScore == maxScore && daghash.Less(&node.hash, &bluestNode.hash)) { bluestNode = node maxScore = node.blueScore } diff --git a/blockdag/phantom.go b/blockdag/phantom.go index ba7af226d..fe22cddbc 100644 --- a/blockdag/phantom.go +++ b/blockdag/phantom.go @@ -27,7 +27,7 @@ func phantom(block *blockNode, k uint32) (blues []*blockNode, selectedParent *bl blues := traverseCandidates(block, candidates, parent) score := uint64(len(blues)) + parent.blueScore - if score > bestScore || (score == bestScore && (bestHash == nil || daghash.Less(bestHash, &parent.hash))) { + if score > bestScore || (score == bestScore && (bestHash == nil || daghash.Less(&parent.hash, bestHash))) { bestScore = score bestBlues = blues bestParent = parent diff --git a/blockdag/validate.go b/blockdag/validate.go index 20a68123a..795f3205d 100644 --- a/blockdag/validate.go +++ b/blockdag/validate.go @@ -435,7 +435,7 @@ func checkBlockParentsOrder(header *wire.BlockHeader) error { sortedHashes = append(sortedHashes, hash) } sort.Slice(sortedHashes, func(i, j int) bool { - return daghash.Less(&sortedHashes[i], &sortedHashes[j]) + return daghash.Less(&sortedHashes[j], &sortedHashes[i]) }) if !daghash.AreEqual(header.PrevBlocks, sortedHashes) { return ruleError(ErrWrongParentsOrder, "block parents are not ordered by hash") diff --git a/dagconfig/daghash/hash.go b/dagconfig/daghash/hash.go index 9afac1721..86fcc5b6d 100644 --- a/dagconfig/daghash/hash.go +++ b/dagconfig/daghash/hash.go @@ -178,9 +178,9 @@ func (hash *Hash) Cmp(target *Hash) int { return HashToBig(hash).Cmp(HashToBig(target)) } -//Less returns true iff hash b is less than hash a +//Less returns true iff hash a is less than hash b func Less(a *Hash, b *Hash) bool { - return a.Cmp(b) > 0 + return a.Cmp(b) < 0 } //JoinHashesStrings joins all the stringified hashes separated by a separator