mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-05 13:46:42 +00:00

* Add DAAScore, BlueWork, and FinalityPoint to externalapi.BlockHeader. * Add DAAScore, BlueWork, and FinalityPoint to NewImmutableBlockHeader and fix compilation errors. * Add DAAScore, BlueWork, and FinalityPoint to protowire header types and fix failing tests. * Check for header DAA score in validateDifficulty. * Add DAA score to buildBlock. * Fix failing tests. * Add a blue work check in validateDifficultyDAAAndBlueWork. * Add blue work to buildBlock and fix failing tests. * Add finality point validation to ValidateHeaderInContext. * Fix genesis blocks' finality points. * Add finalityPoint to blockBuilder. * Fix tests that failed due to missing reachability data. * Make blockBuilder use VirtualFinalityPoint instead of directly calling FinalityPoint with the virtual hash. * Don't validate the finality point for blocks with trusted data. * Add debug logs. * Skip finality point validation for block whose finality points are the virtual genesis. * Revert "Add debug logs." This reverts commit 3c18f519ccbb382f86f63904dbb1c4cd6bc68b00. * Move checkDAAScore and checkBlueWork to validateBlockHeaderInContext. * Add checkCoinbaseBlueScore to validateBodyInContext. * Fix failing tests. * Add DAAScore, blueWork, and finalityPoint to blocks' hashes. * Generate new genesis blocks. * Fix failing tests. * In BuildUTXOInvalidBlock, get the bits from StageDAADataAndReturnRequiredDifficulty instead of calling RequiredDifficulty separately.
369 lines
9.6 KiB
Go
369 lines
9.6 KiB
Go
package dagtraversalmanager_test
|
|
|
|
import (
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus/model"
|
|
|
|
"github.com/kaspanet/kaspad/domain/consensus"
|
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/hashset"
|
|
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
|
|
"github.com/kaspanet/kaspad/domain/dagconfig"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
func TestBlockWindow(t *testing.T) {
|
|
tests := map[string][]*struct {
|
|
parents []string
|
|
id string //id is a virtual entity that is used only for tests so we can define relations between blocks without knowing their hash
|
|
expectedWindow []string
|
|
}{
|
|
dagconfig.MainnetParams.Name: {
|
|
{
|
|
parents: []string{"A"},
|
|
id: "B",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "C",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "D",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"C", "D"},
|
|
id: "E",
|
|
expectedWindow: []string{"C", "D", "B"},
|
|
},
|
|
{
|
|
parents: []string{"C", "D"},
|
|
id: "F",
|
|
expectedWindow: []string{"C", "D", "B"},
|
|
},
|
|
{
|
|
parents: []string{"A"},
|
|
id: "G",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"G"},
|
|
id: "H",
|
|
expectedWindow: []string{"G"},
|
|
},
|
|
{
|
|
parents: []string{"H", "F"},
|
|
id: "I",
|
|
expectedWindow: []string{"F", "H", "C", "D", "G", "B"},
|
|
},
|
|
{
|
|
parents: []string{"I"},
|
|
id: "J",
|
|
expectedWindow: []string{"I", "F", "H", "C", "D", "G", "B"},
|
|
},
|
|
{
|
|
parents: []string{"J"},
|
|
id: "K",
|
|
expectedWindow: []string{"J", "I", "F", "H", "C", "D", "G", "B"},
|
|
},
|
|
{
|
|
parents: []string{"K"},
|
|
id: "L",
|
|
expectedWindow: []string{"K", "J", "I", "F", "H", "C", "D", "G", "B"},
|
|
},
|
|
{
|
|
parents: []string{"L"},
|
|
id: "M",
|
|
expectedWindow: []string{"L", "K", "J", "I", "F", "H", "C", "D", "G", "B"},
|
|
},
|
|
{
|
|
parents: []string{"M"},
|
|
id: "N",
|
|
expectedWindow: []string{"M", "L", "K", "J", "I", "F", "H", "C", "D", "G"},
|
|
},
|
|
{
|
|
parents: []string{"N"},
|
|
id: "O",
|
|
expectedWindow: []string{"N", "M", "L", "K", "J", "I", "F", "H", "C", "D"},
|
|
},
|
|
},
|
|
dagconfig.TestnetParams.Name: {
|
|
{
|
|
parents: []string{"A"},
|
|
id: "B",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "C",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "D",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"C", "D"},
|
|
id: "E",
|
|
expectedWindow: []string{"C", "D", "B"},
|
|
},
|
|
{
|
|
parents: []string{"C", "D"},
|
|
id: "F",
|
|
expectedWindow: []string{"C", "D", "B"},
|
|
},
|
|
{
|
|
parents: []string{"A"},
|
|
id: "G",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"G"},
|
|
id: "H",
|
|
expectedWindow: []string{"G"},
|
|
},
|
|
{
|
|
parents: []string{"H", "F"},
|
|
id: "I",
|
|
expectedWindow: []string{"F", "C", "H", "D", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"I"},
|
|
id: "J",
|
|
expectedWindow: []string{"I", "F", "C", "H", "D", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"J"},
|
|
id: "K",
|
|
expectedWindow: []string{"J", "I", "F", "C", "H", "D", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"K"},
|
|
id: "L",
|
|
expectedWindow: []string{"K", "J", "I", "F", "C", "H", "D", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"L"},
|
|
id: "M",
|
|
expectedWindow: []string{"L", "K", "J", "I", "F", "C", "H", "D", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"M"},
|
|
id: "N",
|
|
expectedWindow: []string{"M", "L", "K", "J", "I", "F", "C", "H", "D", "B"},
|
|
},
|
|
{
|
|
parents: []string{"N"},
|
|
id: "O",
|
|
expectedWindow: []string{"N", "M", "L", "K", "J", "I", "F", "C", "H", "D"},
|
|
},
|
|
},
|
|
dagconfig.DevnetParams.Name: {
|
|
{
|
|
parents: []string{"A"},
|
|
id: "B",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "C",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "D",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"C", "D"},
|
|
id: "E",
|
|
expectedWindow: []string{"D", "C", "B"},
|
|
},
|
|
{
|
|
parents: []string{"C", "D"},
|
|
id: "F",
|
|
expectedWindow: []string{"D", "C", "B"},
|
|
},
|
|
{
|
|
parents: []string{"A"},
|
|
id: "G",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"G"},
|
|
id: "H",
|
|
expectedWindow: []string{"G"},
|
|
},
|
|
{
|
|
parents: []string{"H", "F"},
|
|
id: "I",
|
|
expectedWindow: []string{"F", "D", "H", "C", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"I"},
|
|
id: "J",
|
|
expectedWindow: []string{"I", "F", "D", "H", "C", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"J"},
|
|
id: "K",
|
|
expectedWindow: []string{"J", "I", "F", "D", "H", "C", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"K"},
|
|
id: "L",
|
|
expectedWindow: []string{"K", "J", "I", "F", "D", "H", "C", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"L"},
|
|
id: "M",
|
|
expectedWindow: []string{"L", "K", "J", "I", "F", "D", "H", "C", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"M"},
|
|
id: "N",
|
|
expectedWindow: []string{"M", "L", "K", "J", "I", "F", "D", "H", "C", "B"},
|
|
},
|
|
{
|
|
parents: []string{"N"},
|
|
id: "O",
|
|
expectedWindow: []string{"N", "M", "L", "K", "J", "I", "F", "D", "H", "C"},
|
|
},
|
|
},
|
|
dagconfig.SimnetParams.Name: {
|
|
{
|
|
parents: []string{"A"},
|
|
id: "B",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "C",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"B"},
|
|
id: "D",
|
|
expectedWindow: []string{"B"},
|
|
},
|
|
{
|
|
parents: []string{"D", "C"},
|
|
id: "E",
|
|
expectedWindow: []string{"D", "C", "B"},
|
|
},
|
|
{
|
|
parents: []string{"D", "C"},
|
|
id: "F",
|
|
expectedWindow: []string{"D", "C", "B"},
|
|
},
|
|
{
|
|
parents: []string{"A"},
|
|
id: "G",
|
|
expectedWindow: []string{},
|
|
},
|
|
{
|
|
parents: []string{"G"},
|
|
id: "H",
|
|
expectedWindow: []string{"G"},
|
|
},
|
|
{
|
|
parents: []string{"H", "F"},
|
|
id: "I",
|
|
expectedWindow: []string{"F", "D", "C", "H", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"I"},
|
|
id: "J",
|
|
expectedWindow: []string{"I", "F", "D", "C", "H", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"J"},
|
|
id: "K",
|
|
expectedWindow: []string{"J", "I", "F", "D", "C", "H", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"K"},
|
|
id: "L",
|
|
expectedWindow: []string{"K", "J", "I", "F", "D", "C", "H", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"L"},
|
|
id: "M",
|
|
expectedWindow: []string{"L", "K", "J", "I", "F", "D", "C", "H", "B", "G"},
|
|
},
|
|
{
|
|
parents: []string{"M"},
|
|
id: "N",
|
|
expectedWindow: []string{"M", "L", "K", "J", "I", "F", "D", "C", "H", "B"},
|
|
},
|
|
{
|
|
parents: []string{"N"},
|
|
id: "O",
|
|
expectedWindow: []string{"N", "M", "L", "K", "J", "I", "F", "D", "C", "H"},
|
|
},
|
|
},
|
|
}
|
|
testutils.ForAllNets(t, true, func(t *testing.T, consensusConfig *consensus.Config) {
|
|
consensusConfig.K = 1
|
|
factory := consensus.NewFactory()
|
|
tc, tearDown, err := factory.NewTestConsensus(consensusConfig, "TestBlockWindow")
|
|
if err != nil {
|
|
t.Fatalf("NewTestConsensus: %s", err)
|
|
}
|
|
defer tearDown(false)
|
|
|
|
windowSize := 10
|
|
blockByIDMap := make(map[string]*externalapi.DomainHash)
|
|
idByBlockMap := make(map[externalapi.DomainHash]string)
|
|
blockByIDMap["A"] = consensusConfig.GenesisHash
|
|
idByBlockMap[*consensusConfig.GenesisHash] = "A"
|
|
|
|
blocksData := tests[consensusConfig.Name]
|
|
|
|
for _, blockData := range blocksData {
|
|
parents := hashset.New()
|
|
for _, parentID := range blockData.parents {
|
|
parent := blockByIDMap[parentID]
|
|
parents.Add(parent)
|
|
}
|
|
|
|
block, _, err := tc.AddBlock(parents.ToSlice(), nil, nil)
|
|
if err != nil {
|
|
t.Fatalf("AddBlock: %+v", err)
|
|
}
|
|
|
|
blockByIDMap[blockData.id] = block
|
|
idByBlockMap[*block] = blockData.id
|
|
|
|
stagingArea := model.NewStagingArea()
|
|
|
|
window, err := tc.DAGTraversalManager().BlockWindow(stagingArea, block, windowSize)
|
|
if err != nil {
|
|
t.Fatalf("BlockWindow: %s", err)
|
|
}
|
|
sort.Sort(testutils.NewTestGhostDAGSorter(stagingArea, window, tc, t))
|
|
if err := checkWindowIDs(window, blockData.expectedWindow, idByBlockMap); err != nil {
|
|
t.Errorf("Unexpected values for window for block %s: %s", blockData.id, err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
func checkWindowIDs(window []*externalapi.DomainHash, expectedIDs []string, idByBlockMap map[externalapi.DomainHash]string) error {
|
|
ids := make([]string, len(window))
|
|
for i, node := range window {
|
|
ids[i] = idByBlockMap[*node]
|
|
}
|
|
if !reflect.DeepEqual(ids, expectedIDs) {
|
|
return errors.Errorf("window expected to have blocks %s but got %s", expectedIDs, ids)
|
|
}
|
|
return nil
|
|
}
|