From 6279db2bf1f9606c8593ee6615ff07ee75f83846 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Tue, 5 Jan 2021 19:10:59 +0200 Subject: [PATCH] Reverse ghostdag tie break direction (#1359) * Remove hash reversal in ghostdag * Update tests after chaning ghostdag hash direction Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com> --- domain/consensus/model/externalapi/hash.go | 16 +---- .../dagtraversalmanager/window_test.go | 64 +++++++++---------- .../processes/ghostdag2/ghostdagimpl.go | 2 +- .../ghostdagmanager/ghostdag_test.go | 2 +- .../processes/pruningmanager/pruning_test.go | 17 ++--- domain/consensus/testdata/dags/dag1.json | 28 ++++---- domain/consensus/testdata/dags/dag2.json | 4 +- 7 files changed, 61 insertions(+), 72 deletions(-) diff --git a/domain/consensus/model/externalapi/hash.go b/domain/consensus/model/externalapi/hash.go index b6baa24e7..21ab2fca6 100644 --- a/domain/consensus/model/externalapi/hash.go +++ b/domain/consensus/model/externalapi/hash.go @@ -1,6 +1,7 @@ package externalapi import ( + "bytes" "encoding/hex" "github.com/pkg/errors" @@ -106,20 +107,7 @@ func HashesEqual(a, b []*DomainHash) bool { return true } -func cmp(a, b *DomainHash) int { - // We compare the hashes backwards because Hash is stored as a little endian byte array. - for i := DomainHashSize - 1; i >= 0; i-- { - switch { - case a.hashArray[i] < b.hashArray[i]: - return -1 - case a.hashArray[i] > b.hashArray[i]: - return 1 - } - } - return 0 -} - // Less returns true iff hash a is less than hash b func Less(a, b *DomainHash) bool { - return cmp(a, b) < 0 + return bytes.Compare(a.hashArray[:], b.hashArray[:]) < 0 } diff --git a/domain/consensus/processes/dagtraversalmanager/window_test.go b/domain/consensus/processes/dagtraversalmanager/window_test.go index 802757822..d1faec258 100644 --- a/domain/consensus/processes/dagtraversalmanager/window_test.go +++ b/domain/consensus/processes/dagtraversalmanager/window_test.go @@ -36,12 +36,12 @@ func TestBlueBlockWindow(t *testing.T) { expectedWindowWithGenesisPadding: []string{"B", "A", "A", "A", "A", "A", "A", "A", "A", "A"}, }, { - parents: []string{"D", "C"}, + parents: []string{"C", "D"}, id: "E", expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"}, }, { - parents: []string{"D", "C"}, + parents: []string{"C", "D"}, id: "F", expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"}, }, @@ -58,37 +58,37 @@ func TestBlueBlockWindow(t *testing.T) { { parents: []string{"H", "F"}, id: "I", - expectedWindowWithGenesisPadding: []string{"F", "H", "C", "D", "G", "B", "A", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"F", "C", "D", "H", "G", "B", "A", "A", "A", "A"}, }, { parents: []string{"I"}, id: "J", - expectedWindowWithGenesisPadding: []string{"I", "F", "H", "C", "D", "G", "B", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"I", "F", "C", "D", "H", "G", "B", "A", "A", "A"}, }, { parents: []string{"J"}, id: "K", - expectedWindowWithGenesisPadding: []string{"J", "I", "F", "H", "C", "D", "G", "B", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"J", "I", "F", "C", "D", "H", "G", "B", "A", "A"}, }, { parents: []string{"K"}, id: "L", - expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "H", "C", "D", "G", "B", "A"}, + expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "C", "D", "H", "G", "B", "A"}, }, { parents: []string{"L"}, id: "M", - expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "H", "C", "D", "G", "B"}, + expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "C", "D", "H", "G", "B"}, }, { parents: []string{"M"}, id: "N", - expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "H", "C", "D", "G"}, + expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "C", "D", "H", "G"}, }, { parents: []string{"N"}, id: "O", - expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "H", "C", "D"}, + expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "C", "D", "H"}, }, }, "kaspa-testnet": { @@ -108,14 +108,14 @@ func TestBlueBlockWindow(t *testing.T) { expectedWindowWithGenesisPadding: []string{"B", "A", "A", "A", "A", "A", "A", "A", "A", "A"}, }, { - parents: []string{"D", "C"}, + parents: []string{"C", "D"}, id: "E", - expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"}, }, { - parents: []string{"D", "C"}, + parents: []string{"C", "D"}, id: "F", - expectedWindowWithGenesisPadding: []string{"D", "C", "B", "A", "A", "A", "A", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"C", "D", "B", "A", "A", "A", "A", "A", "A", "A"}, }, { parents: []string{"A"}, @@ -130,37 +130,37 @@ func TestBlueBlockWindow(t *testing.T) { { parents: []string{"H", "F"}, id: "I", - expectedWindowWithGenesisPadding: []string{"F", "H", "D", "C", "G", "B", "A", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"F", "C", "H", "D", "G", "B", "A", "A", "A", "A"}, }, { parents: []string{"I"}, id: "J", - expectedWindowWithGenesisPadding: []string{"I", "F", "H", "D", "C", "G", "B", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"I", "F", "C", "H", "D", "G", "B", "A", "A", "A"}, }, { parents: []string{"J"}, id: "K", - expectedWindowWithGenesisPadding: []string{"J", "I", "F", "H", "D", "C", "G", "B", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"J", "I", "F", "C", "H", "D", "G", "B", "A", "A"}, }, { parents: []string{"K"}, id: "L", - expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "H", "D", "C", "G", "B", "A"}, + expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "C", "H", "D", "G", "B", "A"}, }, { parents: []string{"L"}, id: "M", - expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "H", "D", "C", "G", "B"}, + expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "C", "H", "D", "G", "B"}, }, { parents: []string{"M"}, id: "N", - expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "H", "D", "C", "G"}, + expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "C", "H", "D", "G"}, }, { parents: []string{"N"}, id: "O", - expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "H", "D", "C"}, + expectedWindowWithGenesisPadding: []string{"N", "M", "L", "K", "J", "I", "F", "C", "H", "D"}, }, }, "kaspa-devnet": { @@ -202,32 +202,32 @@ func TestBlueBlockWindow(t *testing.T) { { parents: []string{"H", "F"}, id: "I", - expectedWindowWithGenesisPadding: []string{"F", "D", "H", "C", "G", "B", "A", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"F", "D", "H", "C", "B", "G", "A", "A", "A", "A"}, }, { parents: []string{"I"}, id: "J", - expectedWindowWithGenesisPadding: []string{"I", "F", "D", "H", "C", "G", "B", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"I", "F", "D", "H", "C", "B", "G", "A", "A", "A"}, }, { parents: []string{"J"}, id: "K", - expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "H", "C", "G", "B", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "H", "C", "B", "G", "A", "A"}, }, { parents: []string{"K"}, id: "L", - expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "H", "C", "G", "B", "A"}, + expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "H", "C", "B", "G", "A"}, }, { parents: []string{"L"}, id: "M", - expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "H", "C", "G", "B"}, + expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "H", "C", "B", "G"}, }, { parents: []string{"M"}, id: "N", - expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "G"}, + expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "B"}, }, { parents: []string{"N"}, @@ -274,32 +274,32 @@ func TestBlueBlockWindow(t *testing.T) { { parents: []string{"H", "F"}, id: "I", - expectedWindowWithGenesisPadding: []string{"F", "D", "H", "C", "G", "B", "A", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"F", "D", "H", "C", "B", "G", "A", "A", "A", "A"}, }, { parents: []string{"I"}, id: "J", - expectedWindowWithGenesisPadding: []string{"I", "F", "D", "H", "C", "G", "B", "A", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"I", "F", "D", "H", "C", "B", "G", "A", "A", "A"}, }, { parents: []string{"J"}, id: "K", - expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "H", "C", "G", "B", "A", "A"}, + expectedWindowWithGenesisPadding: []string{"J", "I", "F", "D", "H", "C", "B", "G", "A", "A"}, }, { parents: []string{"K"}, id: "L", - expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "H", "C", "G", "B", "A"}, + expectedWindowWithGenesisPadding: []string{"K", "J", "I", "F", "D", "H", "C", "B", "G", "A"}, }, { parents: []string{"L"}, id: "M", - expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "H", "C", "G", "B"}, + expectedWindowWithGenesisPadding: []string{"L", "K", "J", "I", "F", "D", "H", "C", "B", "G"}, }, { parents: []string{"M"}, id: "N", - expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "G"}, + expectedWindowWithGenesisPadding: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "B"}, }, { parents: []string{"N"}, diff --git a/domain/consensus/processes/ghostdag2/ghostdagimpl.go b/domain/consensus/processes/ghostdag2/ghostdagimpl.go index 0b6fee8eb..2afec5464 100644 --- a/domain/consensus/processes/ghostdag2/ghostdagimpl.go +++ b/domain/consensus/processes/ghostdag2/ghostdagimpl.go @@ -124,7 +124,7 @@ func ismoreHash(parent *externalapi.DomainHash, selectedParent *externalapi.Doma parentByteArray := parent.ByteArray() selectedParentByteArray := selectedParent.ByteArray() //Check if parentHash is more then selectedParentHash - for i := len(parentByteArray) - 1; i >= 0; i-- { + for i := 0; i < len(parentByteArray); i++ { switch { case parentByteArray[i] < selectedParentByteArray[i]: return false diff --git a/domain/consensus/processes/ghostdagmanager/ghostdag_test.go b/domain/consensus/processes/ghostdagmanager/ghostdag_test.go index eaa2c5104..71f87120b 100644 --- a/domain/consensus/processes/ghostdagmanager/ghostdag_test.go +++ b/domain/consensus/processes/ghostdagmanager/ghostdag_test.go @@ -181,7 +181,7 @@ func TestGHOSTDAG(t *testing.T) { func hashesToStrings(arr []*externalapi.DomainHash) []string { var strArr = make([]string, len(arr)) for i, hash := range arr { - strArr[i] = hash.String() + strArr[i] = string(hash.ByteSlice()) } return strArr } diff --git a/domain/consensus/processes/pruningmanager/pruning_test.go b/domain/consensus/processes/pruningmanager/pruning_test.go index 3b3f10808..fd25d69c0 100644 --- a/domain/consensus/processes/pruningmanager/pruning_test.go +++ b/domain/consensus/processes/pruningmanager/pruning_test.go @@ -2,14 +2,15 @@ package pruningmanager_test import ( "encoding/json" - "github.com/kaspanet/kaspad/domain/consensus" - "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" - "github.com/kaspanet/kaspad/domain/consensus/utils/testutils" - "github.com/kaspanet/kaspad/domain/dagconfig" "os" "path/filepath" "testing" "time" + + "github.com/kaspanet/kaspad/domain/consensus" + "github.com/kaspanet/kaspad/domain/consensus/model/externalapi" + "github.com/kaspanet/kaspad/domain/consensus/utils/testutils" + "github.com/kaspanet/kaspad/domain/dagconfig" ) type jsonBlock struct { @@ -32,10 +33,10 @@ func TestPruning(t *testing.T) { "kaspa-testnet": "1582", }, "dag-for-test-pruning.json": { - "kaspa-mainnet": "503", - "kaspa-simnet": "503", - "kaspa-devnet": "502", - "kaspa-testnet": "503", + "kaspa-mainnet": "502", + "kaspa-simnet": "502", + "kaspa-devnet": "503", + "kaspa-testnet": "502", }, } diff --git a/domain/consensus/testdata/dags/dag1.json b/domain/consensus/testdata/dags/dag1.json index d751b3c00..58db801fb 100644 --- a/domain/consensus/testdata/dags/dag1.json +++ b/domain/consensus/testdata/dags/dag1.json @@ -3,7 +3,7 @@ "GenesisID": "0", "Blocks": [ { - "ID": "1111", + "ID": "a1", "ExpectedScore": 1, "ExpectedSelectedParent": "0", "ExpectedReds": [], @@ -41,13 +41,13 @@ { "ID": "4", "ExpectedScore": 2, - "ExpectedSelectedParent": "1111", + "ExpectedSelectedParent": "a1", "ExpectedReds": [], "ExpectedBlues": [ - "1111" + "a1" ], "Parents": [ - "1111" + "a1" ] }, { @@ -91,14 +91,14 @@ { "ID": "8", "ExpectedScore": 3, - "ExpectedSelectedParent": "1111", + "ExpectedSelectedParent": "a1", "ExpectedReds": [], "ExpectedBlues": [ - "1111", + "a1", "2" ], "Parents": [ - "1111", + "a1", "2" ] }, @@ -117,7 +117,7 @@ ] }, { - "ID": "10", + "ID": "a10", "ExpectedScore": 5, "ExpectedSelectedParent": "8", "ExpectedReds": [], @@ -147,18 +147,18 @@ { "ID": "12", "ExpectedScore": 8, - "ExpectedSelectedParent": "10", + "ExpectedSelectedParent": "a10", "ExpectedReds": [ "3", "6" ], "ExpectedBlues": [ - "10", + "a10", "5", "9" ], "Parents": [ - "10", + "a10", "9" ] }, @@ -186,11 +186,11 @@ ], "ExpectedBlues": [ "13", - "10" + "a10" ], "Parents": [ "13", - "10" + "a10" ] }, { @@ -198,7 +198,7 @@ "ExpectedScore": 9, "ExpectedSelectedParent": "11", "ExpectedReds": [ - "1111", + "a1", "8" ], "ExpectedBlues": [ diff --git a/domain/consensus/testdata/dags/dag2.json b/domain/consensus/testdata/dags/dag2.json index 1be478e7f..f1c5c6242 100644 --- a/domain/consensus/testdata/dags/dag2.json +++ b/domain/consensus/testdata/dags/dag2.json @@ -84,8 +84,8 @@ "ExpectedReds": [], "ExpectedBlues": [ "f154", - "d1c", - "21d" + "21d", + "d1c" ], "Parents": [ "d1c",