diff --git a/blockdag/blockset.go b/blockdag/blockset.go index 690039b3c..20648d4c8 100644 --- a/blockdag/blockset.go +++ b/blockdag/blockset.go @@ -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[j], &hashes[i]) + return daghash.Less(&hashes[i], &hashes[j]) }) return hashes } diff --git a/blockdag/blockset_test.go b/blockdag/blockset_test.go new file mode 100644 index 000000000..90a84df3e --- /dev/null +++ b/blockdag/blockset_test.go @@ -0,0 +1,35 @@ +package blockdag + +import ( + "testing" + + "github.com/daglabs/btcd/dagconfig/daghash" +) + +func TestHashes(t *testing.T) { + bs := setFromSlice( + &blockNode{ + hash: daghash.Hash{3}, + }, + &blockNode{ + hash: daghash.Hash{1}, + }, + &blockNode{ + hash: daghash.Hash{0}, + }, + &blockNode{ + hash: daghash.Hash{2}, + }, + ) + + expected := []daghash.Hash{ + daghash.Hash{0}, + daghash.Hash{1}, + daghash.Hash{2}, + daghash.Hash{3}, + } + + if !daghash.AreEqual(bs.hashes(), expected) { + t.Errorf("TestHashes: hashes are not ordered as expected") + } +}