Repository: kaspanet/kaspad
Pull request #1225 (2020-12-15 07:34:26)

Enhancements needed by performance tests


Patch:
diff --git a/domain/consensus/consensus.go b/domain/consensus/consensus.go
index 3ecfe1b9..a2e8ec4d 100644
--- a/domain/consensus/consensus.go
+++ b/domain/consensus/consensus.go
@@ -11,7 +11,7 @@ import (
 
 type consensus struct {
 	lock            *sync.Mutex
-	databaseContext model.DBReader
+	databaseContext model.DBManager
 
 	blockProcessor        model.BlockProcessor
 	blockBuilder          model.BlockBuilder
diff --git a/domain/consensus/factory.go b/domain/consensus/factory.go
index dcfdf3a8..44e6a360 100644
--- a/domain/consensus/factory.go
+++ b/domain/consensus/factory.go
@@ -47,9 +47,10 @@ import (
 // Factory instantiates new Consensuses
 type Factory interface {
 	NewConsensus(dagParams *dagconfig.Params, db infrastructuredatabase.Database) (externalapi.Consensus, error)
-	NewTestConsensus(dagParams *dagconfig.Params, testName string) (tc testapi.TestConsensus, teardown func(), err error)
+	NewTestConsensus(dagParams *dagconfig.Params, testName string) (
+		tc testapi.TestConsensus, teardown func(keepDataDir bool), err error)
 	NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataDir string) (
-		tc testapi.TestConsensus, teardown func(), err error)
+		tc testapi.TestConsensus, teardown func(keepDataDir bool), err error)
 }
 
 type factory struct{}
@@ -344,7 +345,7 @@ func (f *factory) NewConsensus(dagParams *dagconfig.Params, db infrastructuredat
 }
 
 func (f *factory) NewTestConsensus(dagParams *dagconfig.Params, testName string) (
-	tc testapi.TestConsensus, teardown func(), err error) {
+	tc testapi.TestConsensus, teardown func(keepDataDir bool), err error) {
 
 	dataDir, err := ioutil.TempDir("", testName)
 	if err != nil {
@@ -355,7 +356,7 @@ func (f *factory) NewTestConsensus(dagParams *dagconfig.Params, testName string)
 }
 
 func (f *factory) NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataDir string) (
-	tc testapi.TestConsensus, teardown func(), err error) {
+	tc testapi.TestConsensus, teardown func(keepDataDir bool), err error) {
 
 	db, err := ldb.NewLevelDB(dataDir)
 	if err != nil {
@@ -380,9 +381,14 @@ func (f *factory) NewTestConsensusWithDataDir(dagParams *dagconfig.Params, dataD
 		testTransactionValidator: testTransactionValidator,
 	}
 	tstConsensus.testBlockBuilder = blockbuilder.NewTestBlockBuilder(consensusAsImplementation.blockBuilder, tstConsensus)
-	teardown = func() {
+	teardown = func(keepDataDir bool) {
 		db.Close()
-		os.RemoveAll(dataDir)
+		if !keepDataDir {
+			err := os.RemoveAll(dataDir)
+			if err != nil {
+				log.Errorf("Error removing data directory for test consensus: %s", err)
+			}
+		}
 	}
 
 	return tstConsensus, teardown, nil
diff --git a/domain/consensus/log.go b/domain/consensus/log.go
new file mode 100644
index 00000000..520dcce8
--- /dev/null
+++ b/domain/consensus/log.go
@@ -0,0 +1,7 @@
+package consensus
+
+import (
+	"github.com/kaspanet/kaspad/infrastructure/logger"
+)
+
+var log, _ = logger.Get(logger.SubsystemTags.BDAG)


Test patch:
diff --git a/domain/consensus/consensus_test.go b/domain/consensus/consensus_test.go
index 964580e6..25439472 100644
--- a/domain/consensus/consensus_test.go
+++ b/domain/consensus/consensus_test.go
@@ -19,7 +19,7 @@ func TestConsensus_GetBlockInfo(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		invalidBlock, _, err := consensus.BuildBlockWithParents([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
 		if err != nil {
diff --git a/domain/consensus/finality_test.go b/domain/consensus/finality_test.go
index 8eb2c873..1ee7ac03 100644
--- a/domain/consensus/finality_test.go
+++ b/domain/consensus/finality_test.go
@@ -24,7 +24,7 @@ func TestFinality(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		buildAndInsertBlock := func(parentHashes []*externalapi.DomainHash) (*externalapi.DomainBlock, error) {
 			block, _, err := consensus.BuildBlockWithParents(parentHashes, nil, nil)
@@ -241,7 +241,7 @@ func TestBoundedMergeDepth(t *testing.T) {
 		if err != nil {
 			t.Fatalf("TestBoundedMergeDepth: Error setting up consensus: %+v", err)
 		}
-		defer teardownFunc2()
+		defer teardownFunc2(false)
 
 		// Create a block on top on genesis
 		block1 := buildAndInsertBlock(consensusBuild, []*externalapi.DomainHash{params.GenesisHash})
@@ -266,7 +266,7 @@ func TestBoundedMergeDepth(t *testing.T) {
 		}
 
 		// Teardown and assign nil to make sure we use the right DAG from here on.
-		teardownFunc1()
+		teardownFunc1(false)
 		consensusBuild = nil
 
 		// Now test against the real DAG
diff --git a/domain/consensus/model/testapi/test_consensus.go b/domain/consensus/model/testapi/test_consensus.go
index e361bff4..33fcc859 100644
--- a/domain/consensus/model/testapi/test_consensus.go
+++ b/domain/consensus/model/testapi/test_consensus.go
@@ -11,7 +11,7 @@ type TestConsensus interface {
 	externalapi.Consensus
 
 	DAGParams() *dagconfig.Params
-	DatabaseContext() model.DBReader
+	DatabaseContext() model.DBManager
 
 	BuildBlockWithParents(parentHashes []*externalapi.DomainHash, coinbaseData *externalapi.DomainCoinbaseData,
 		transactions []*externalapi.DomainTransaction) (*externalapi.DomainBlock, model.UTXODiff, error)
diff --git a/domain/consensus/processes/blockvalidator/block_body_in_isolation_test.go b/domain/consensus/processes/blockvalidator/block_body_in_isolation_test.go
index 9f9966ee..31122ccf 100644
--- a/domain/consensus/processes/blockvalidator/block_body_in_isolation_test.go
+++ b/domain/consensus/processes/blockvalidator/block_body_in_isolation_test.go
@@ -24,7 +24,7 @@ func TestChainedTransactions(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		block1Hash, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
 		if err != nil {
@@ -86,7 +86,7 @@ func TestCheckBlockSanity(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 		blockHash := consensushashing.BlockHash(&exampleValidBlock)
 		if len(exampleValidBlock.Transactions) < 3 {
 			t.Fatalf("Too few transactions in block, expect at least 3, got %v", len(exampleValidBlock.Transactions))
diff --git a/domain/consensus/processes/blockvalidator/block_header_in_context_test.go b/domain/consensus/processes/blockvalidator/block_header_in_context_test.go
index e6c8238f..34390fde 100644
--- a/domain/consensus/processes/blockvalidator/block_header_in_context_test.go
+++ b/domain/consensus/processes/blockvalidator/block_header_in_context_test.go
@@ -20,7 +20,7 @@ func TestValidateMedianTime(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		addBlock := func(blockTime int64, parents []*externalapi.DomainHash, expectedErr error) (*externalapi.DomainBlock, *externalapi.DomainHash) {
 			block, _, err := tc.BuildBlockWithParents(parents, nil, nil)
@@ -87,7 +87,7 @@ func TestCheckParentsIncest(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		a, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
 		if err != nil {
diff --git a/domain/consensus/processes/consensusstatemanager/calculate_past_utxo_test.go b/domain/consensus/processes/consensusstatemanager/calculate_past_utxo_test.go
index b1769701..99ff74d8 100644
--- a/domain/consensus/processes/consensusstatemanager/calculate_past_utxo_test.go
+++ b/domain/consensus/processes/consensusstatemanager/calculate_past_utxo_test.go
@@ -23,7 +23,7 @@ func TestUTXOCommitment(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		// Build the following DAG:
 		// G <- A <- B <- C <- E
@@ -119,7 +119,7 @@ func TestPastUTXOMultiset(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		// Build a short chain
 		currentHash := params.GenesisHash
diff --git a/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go b/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go
index ad0f1eaa..bf0dce41 100644
--- a/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go
+++ b/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go
@@ -25,7 +25,7 @@ func TestDoubleSpends(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		// Mine chain of two blocks to fund our double spend
 		firstBlockHash, err := consensus.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
@@ -157,7 +157,7 @@ func TestResolveBlockStatusSanity(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		genesisHash := params.GenesisHash
 		allHashes := []*externalapi.DomainHash{genesisHash}
diff --git a/domain/consensus/processes/dagtopologymanager/dagtopologymanager_external_test.go b/domain/consensus/processes/dagtopologymanager/dagtopologymanager_external_test.go
index 9cb4bf34..3ab33082 100644
--- a/domain/consensus/processes/dagtopologymanager/dagtopologymanager_external_test.go
+++ b/domain/consensus/processes/dagtopologymanager/dagtopologymanager_external_test.go
@@ -16,7 +16,7 @@ func TestIsAncestorOf(t *testing.T) {
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %s", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		// Add a chain of two blocks above the genesis. This will be the
 		// selected parent chain.
diff --git a/domain/consensus/processes/dagtraversalmanager/window_test.go b/domain/consensus/processes/dagtraversalmanager/window_test.go
index 2f878cee..8947c14d 100644
--- a/domain/consensus/processes/dagtraversalmanager/window_test.go
+++ b/domain/consensus/processes/dagtraversalmanager/window_test.go
@@ -1,14 +1,15 @@
 package dagtraversalmanager_test
 
 import (
+	"reflect"
+	"testing"
+
 	"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"
-	"reflect"
-	"testing"
 )
 
 func TestBlueBlockWindow(t *testing.T) {
@@ -313,7 +314,7 @@ func TestBlueBlockWindow(t *testing.T) {
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %s", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		windowSize := 10
 		blockByIDMap := make(map[string]*externalapi.DomainHash)
diff --git a/domain/consensus/processes/difficultymanager/difficultymanager_test.go b/domain/consensus/processes/difficultymanager/difficultymanager_test.go
index 0714e58a..256ce46d 100644
--- a/domain/consensus/processes/difficultymanager/difficultymanager_test.go
+++ b/domain/consensus/processes/difficultymanager/difficultymanager_test.go
@@ -26,7 +26,7 @@ func TestDifficulty(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		addBlock := func(blockTime int64, parents ...*externalapi.DomainHash) (*externalapi.DomainBlock, *externalapi.DomainHash) {
 			bluestParent, err := tc.GHOSTDAGManager().ChooseSelectedParent(parents...)
diff --git a/domain/consensus/processes/pastmediantimemanager/pastmediantimemanager_test.go b/domain/consensus/processes/pastmediantimemanager/pastmediantimemanager_test.go
index 37f54f51..21f44612 100644
--- a/domain/consensus/processes/pastmediantimemanager/pastmediantimemanager_test.go
+++ b/domain/consensus/processes/pastmediantimemanager/pastmediantimemanager_test.go
@@ -17,7 +17,7 @@ func TestPastMedianTime(t *testing.T) {
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %s", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		numBlocks := uint32(300)
 		blockHashes := make([]*externalapi.DomainHash, numBlocks)
diff --git a/domain/consensus/processes/reachabilitymanager/reachability_external_test.go b/domain/consensus/processes/reachabilitymanager/reachability_external_test.go
index 62fe0796..1c7438c3 100644
--- a/domain/consensus/processes/reachabilitymanager/reachability_external_test.go
+++ b/domain/consensus/processes/reachabilitymanager/reachability_external_test.go
@@ -1,11 +1,12 @@
 package reachabilitymanager_test
 
 import (
+	"testing"
+
 	"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"
-	"testing"
 )
 
 func TestAddChildThatPointsDirectlyToTheSelectedParentChainBelowReindexRoot(t *testing.T) {
@@ -16,7 +17,7 @@ func TestAddChildThatPointsDirectlyToTheSelectedParentChainBelowReindexRoot(t *t
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %+v", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
 
@@ -71,7 +72,7 @@ func TestUpdateReindexRoot(t *testing.T) {
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %s", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
 
@@ -161,7 +162,7 @@ func TestReindexIntervalsEarlierThanReindexRoot(t *testing.T) {
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %+v", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
 
@@ -295,7 +296,7 @@ func TestTipsAfterReindexIntervalsEarlierThanReindexRoot(t *testing.T) {
 		if err != nil {
 			t.Fatalf("NewTestConsensus: %s", err)
 		}
-		defer tearDown()
+		defer tearDown(false)
 
 		tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
 
diff --git a/domain/consensus/processes/transactionvalidator/transaction_in_isolation_test.go b/domain/consensus/processes/transactionvalidator/transaction_in_isolation_test.go
index 912d8aa2..15d12ab6 100644
--- a/domain/consensus/processes/transactionvalidator/transaction_in_isolation_test.go
+++ b/domain/consensus/processes/transactionvalidator/transaction_in_isolation_test.go
@@ -1,6 +1,8 @@
 package transactionvalidator_test
 
 import (
+	"testing"
+
 	"github.com/kaspanet/kaspad/domain/consensus"
 	"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
 	"github.com/kaspanet/kaspad/domain/consensus/ruleerrors"
@@ -12,7 +14,6 @@ import (
 	"github.com/kaspanet/kaspad/domain/dagconfig"
 	"github.com/kaspanet/kaspad/util"
 	"github.com/pkg/errors"
-	"testing"
 )
 
 type txSubnetworkData struct {
@@ -28,7 +29,7 @@ func TestValidateTransactionInIsolation(t *testing.T) {
 		if err != nil {
 			t.Fatalf("Error setting up consensus: %+v", err)
 		}
-		defer teardown()
+		defer teardown(false)
 
 		tests := []struct {
 			name                   string
diff --git a/domain/consensus/test_consensus_getters.go b/domain/consensus/test_consensus_getters.go
index 58712718..0390be5b 100644
--- a/domain/consensus/test_consensus_getters.go
+++ b/domain/consensus/test_consensus_getters.go
@@ -5,7 +5,7 @@ import (
 	"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
 )
 
-func (tc *testConsensus) DatabaseContext() model.DBReader {
+func (tc *testConsensus) DatabaseContext() model.DBManager {
 	return tc.databaseContext
 }