mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-03-30 15:08:33 +00:00
Move testGHOSTDagSorter to testutils, and build a boilerplate for overriding specific managers (#1486)
* Move testGHOSTDagSorter to testutils * Allow overriding managers in consensus, starting with ghostdag * Add test prefix to SetDataDir and SetGHOSTDAGManager Co-authored-by: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com>
This commit is contained in:
parent
4a354cd538
commit
3d0a2a47b2
6
domain/consensus/constructors.go
Normal file
6
domain/consensus/constructors.go
Normal file
@ -0,0 +1,6 @@
|
||||
package consensus
|
||||
|
||||
import "github.com/kaspanet/kaspad/domain/consensus/model"
|
||||
|
||||
// GHOSTDAGManagerConstructor is the function signature for a constructor of a type implementing model.GHOSTDAGManager
|
||||
type GHOSTDAGManagerConstructor func(model.DBReader, model.DAGTopologyManager, model.GHOSTDAGDataStore, model.BlockHeaderStore, model.KType) model.GHOSTDAGManager
|
@ -51,15 +51,21 @@ type Factory interface {
|
||||
externalapi.Consensus, error)
|
||||
NewTestConsensus(dagParams *dagconfig.Params, isArchivalNode bool, testName string) (
|
||||
tc testapi.TestConsensus, teardown func(keepDataDir bool), err error)
|
||||
NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataDir string, isArchivalNode bool) (
|
||||
tc testapi.TestConsensus, teardown func(keepDataDir bool), err error)
|
||||
|
||||
SetTestDataDir(dataDir string)
|
||||
SetTestGHOSTDAGManager(ghostdagConstructor GHOSTDAGManagerConstructor)
|
||||
}
|
||||
|
||||
type factory struct{}
|
||||
type factory struct {
|
||||
dataDir string
|
||||
ghostdagConstructor GHOSTDAGManagerConstructor
|
||||
}
|
||||
|
||||
// NewFactory creates a new Consensus factory
|
||||
func NewFactory() Factory {
|
||||
return &factory{}
|
||||
return &factory{
|
||||
ghostdagConstructor: ghostdagmanager.New,
|
||||
}
|
||||
}
|
||||
|
||||
// NewConsensus instantiates a new Consensus
|
||||
@ -107,7 +113,7 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, db infrastructuredat
|
||||
reachabilityManager,
|
||||
blockRelationStore,
|
||||
ghostdagDataStore)
|
||||
ghostdagManager := ghostdagmanager.New(
|
||||
ghostdagManager := f.ghostdagConstructor(
|
||||
dbManager,
|
||||
dagTopologyManager,
|
||||
ghostdagDataStore,
|
||||
@ -382,19 +388,14 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, db infrastructuredat
|
||||
|
||||
func (f *factory) NewTestConsensus(dagParams *dagconfig.Params, isArchivalNode bool, testName string) (
|
||||
tc testapi.TestConsensus, teardown func(keepDataDir bool), err error) {
|
||||
|
||||
dataDir, err := ioutil.TempDir("", testName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
datadir := f.dataDir
|
||||
if datadir == "" {
|
||||
datadir, err = ioutil.TempDir("", testName)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return f.NewTestConsensusWithDataDir(dagParams, dataDir, isArchivalNode)
|
||||
}
|
||||
|
||||
func (f *factory) NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataDir string, isArchivalNode bool) (
|
||||
tc testapi.TestConsensus, teardown func(keepDataDir bool), err error) {
|
||||
|
||||
db, err := ldb.NewLevelDB(dataDir)
|
||||
db, err := ldb.NewLevelDB(datadir)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@ -404,9 +405,7 @@ func (f *factory) NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataD
|
||||
}
|
||||
|
||||
consensusAsImplementation := consensusAsInterface.(*consensus)
|
||||
|
||||
testConsensusStateManager := consensusstatemanager.NewTestConsensusStateManager(consensusAsImplementation.consensusStateManager)
|
||||
|
||||
testTransactionValidator := transactionvalidator.NewTestTransactionValidator(consensusAsImplementation.transactionValidator)
|
||||
|
||||
tstConsensus := &testConsensus{
|
||||
@ -422,12 +421,19 @@ func (f *factory) NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataD
|
||||
teardown = func(keepDataDir bool) {
|
||||
db.Close()
|
||||
if !keepDataDir {
|
||||
err := os.RemoveAll(dataDir)
|
||||
err := os.RemoveAll(f.dataDir)
|
||||
if err != nil {
|
||||
log.Errorf("Error removing data directory for test consensus: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tstConsensus, teardown, nil
|
||||
}
|
||||
|
||||
func (f *factory) SetTestDataDir(dataDir string) {
|
||||
f.dataDir = dataDir
|
||||
}
|
||||
|
||||
func (f *factory) SetTestGHOSTDAGManager(ghostdagConstructor GHOSTDAGManagerConstructor) {
|
||||
f.ghostdagConstructor = ghostdagConstructor
|
||||
}
|
||||
|
@ -32,8 +32,8 @@ func TestConsensusStateManager_pickVirtualParents(t *testing.T) {
|
||||
t.Fatalf("Consensus failed building a block: %v", err)
|
||||
}
|
||||
blockParents := block.Header.ParentHashes()
|
||||
sort.Sort(consensus.NewTestGhostDAGSorter(virtualRelations.Parents, tc, t))
|
||||
sort.Sort(consensus.NewTestGhostDAGSorter(blockParents, tc, t))
|
||||
sort.Sort(testutils.NewTestGhostDAGSorter(virtualRelations.Parents, tc, t))
|
||||
sort.Sort(testutils.NewTestGhostDAGSorter(blockParents, tc, t))
|
||||
if !externalapi.HashesEqual(virtualRelations.Parents, blockParents) {
|
||||
t.Fatalf("Block relations and BuildBlock return different parents for virtual, %s != %s", virtualRelations.Parents, blockParents)
|
||||
}
|
||||
@ -54,7 +54,7 @@ func TestConsensusStateManager_pickVirtualParents(t *testing.T) {
|
||||
}
|
||||
|
||||
virtualParents := getSortedVirtualParents(tc)
|
||||
sort.Sort(consensus.NewTestGhostDAGSorter(parents, tc, t))
|
||||
sort.Sort(testutils.NewTestGhostDAGSorter(parents, tc, t))
|
||||
|
||||
// Make sure the first half of the blocks are with highest blueWork
|
||||
// we use (max+1)/2 because the first "half" is rounded up, so `(dividend + (divisor - 1)) / divisor` = `(max + (2-1))/2` = `(max+1)/2`
|
||||
@ -102,7 +102,7 @@ func TestConsensusStateManager_pickVirtualParents(t *testing.T) {
|
||||
parents = append(parents, block)
|
||||
}
|
||||
|
||||
sort.Sort(consensus.NewTestGhostDAGSorter(parents, tc, t))
|
||||
sort.Sort(testutils.NewTestGhostDAGSorter(parents, tc, t))
|
||||
virtualParents = getSortedVirtualParents(tc)
|
||||
if !externalapi.HashesEqual(virtualParents, parents) {
|
||||
t.Fatalf("Expected VirtualParents and parents to be equal, instead: %s != %s", virtualParents, parents)
|
||||
|
@ -344,7 +344,7 @@ func TestBlueBlockWindow(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("BlueWindow: %s", err)
|
||||
}
|
||||
sort.Sort(consensus.NewTestGhostDAGSorter(window, tc, t))
|
||||
sort.Sort(testutils.NewTestGhostDAGSorter(window, tc, t))
|
||||
if err := checkWindowIDs(window, blockData.expectedWindowWithGenesisPadding, idByBlockMap); err != nil {
|
||||
t.Errorf("Unexpected values for window for block %s: %s", blockData.id, err)
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package consensus
|
||||
package testutils
|
||||
|
||||
import (
|
||||
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
Loading…
x
Reference in New Issue
Block a user