Repository: kaspanet/kaspad
Pull request #1666 (2021-04-08 07:26:15)

Support resolveBlockStatus without separate stagingAreas for usage of testConsensus

codecov[bot]: # [Codecov](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=h1) Report
> Merging [#1666](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=desc) (05b2c26) into [v0.10.0-dev](https://codecov.io/gh/kaspanet/kaspad/commit/d2cccd2829995c65e00c39ea0009627382ca3dd2?el=desc) (d2cccd2) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 05b2c26 differs from pull request most recent head 6c8bc82. Consider uploading reports for the commit 6c8bc82 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/kaspanet/kaspad/pull/1666/graphs/tree.svg?width=650&height=150&src=pr&token=2FBIEEJTNU)](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=tree)

```diff
@@               Coverage Diff               @@
##           v0.10.0-dev    #1666      +/-   ##
===============================================
+ Coverage        59.54%   59.56%   +0.01%     
===============================================
  Files              548      548              
  Lines            21695    21694       -1     
===============================================
+ Hits             12919    12922       +3     
+ Misses            6728     6723       -5     
- Partials          2048     2049       +1     
```


| [Impacted Files](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [domain/consensus/test\_consensus.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy90ZXN0X2NvbnNlbnN1cy5nbw==) | `65.59% <ø> (ø)` | |
| [...ensus/processes/blockbuilder/test\_block\_builder.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9wcm9jZXNzZXMvYmxvY2tidWlsZGVyL3Rlc3RfYmxvY2tfYnVpbGRlci5nbw==) | `70.53% <100.00%> (+0.26%)` | :arrow_up: |
| [...sses/consensusstatemanager/add\_block\_to\_virtual.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9wcm9jZXNzZXMvY29uc2Vuc3Vzc3RhdGVtYW5hZ2VyL2FkZF9ibG9ja190b192aXJ0dWFsLmdv) | `75.90% <100.00%> (ø)` | |
| [...esses/consensusstatemanager/calculate\_past\_utxo.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9wcm9jZXNzZXMvY29uc2Vuc3Vzc3RhdGVtYW5hZ2VyL2NhbGN1bGF0ZV9wYXN0X3V0eG8uZ28=) | `70.16% <100.00%> (ø)` | |
| [...sses/consensusstatemanager/resolve\_block\_status.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9wcm9jZXNzZXMvY29uc2Vuc3Vzc3RhdGVtYW5hZ2VyL3Jlc29sdmVfYmxvY2tfc3RhdHVzLmdv) | `72.66% <100.00%> (-0.20%)` | :arrow_down: |
| [...sensusstatemanager/test\_consensus\_state\_manager.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9wcm9jZXNzZXMvY29uc2Vuc3Vzc3RhdGVtYW5hZ2VyL3Rlc3RfY29uc2Vuc3VzX3N0YXRlX21hbmFnZXIuZ28=) | `100.00% <100.00%> (ø)` | |
| [...selectedchainstore/headers\_selected\_chain\_store.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9kYXRhc3RydWN0dXJlcy9oZWFkZXJzc2VsZWN0ZWRjaGFpbnN0b3JlL2hlYWRlcnNfc2VsZWN0ZWRfY2hhaW5fc3RvcmUuZ28=) | `70.00% <0.00%> (+4.28%)` | :arrow_up: |
| [...ain/consensus/database/binaryserialization/hash.go](https://codecov.io/gh/kaspanet/kaspad/pull/1666/diff?src=pr&el=tree#diff-ZG9tYWluL2NvbnNlbnN1cy9kYXRhYmFzZS9iaW5hcnlzZXJpYWxpemF0aW9uL2hhc2guZ28=) | `70.58% <0.00%> (+5.88%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=footer). Last update [d2cccd2...6c8bc82](https://codecov.io/gh/kaspanet/kaspad/pull/1666?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/domain/consensus/processes/consensusstatemanager/add_block_to_virtual.go b/domain/consensus/processes/consensusstatemanager/add_block_to_virtual.go
index 513f0c01..5e607864 100644
--- a/domain/consensus/processes/consensusstatemanager/add_block_to_virtual.go
+++ b/domain/consensus/processes/consensusstatemanager/add_block_to_virtual.go
@@ -37,7 +37,7 @@ func (csm *consensusStateManager) AddBlock(stagingArea *model.StagingArea, block
 
 		if !isViolatingFinality {
 			log.Debugf("Block %s doesn't violate finality. Resolving its block status", blockHash)
-			blockStatus, err := csm.resolveBlockStatus(stagingArea, blockHash)
+			blockStatus, err := csm.resolveBlockStatus(stagingArea, blockHash, true)
 			if err != nil {
 				return nil, nil, err
 			}
diff --git a/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go b/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go
index 39b36f61..c004a3e7 100644
--- a/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go
+++ b/domain/consensus/processes/consensusstatemanager/calculate_past_utxo.go
@@ -282,7 +282,7 @@ func (csm *consensusStateManager) RestorePastUTXOSetIterator(stagingArea *model.
 	onEnd := logger.LogAndMeasureExecutionTime(log, "RestorePastUTXOSetIterator")
 	defer onEnd()
 
-	blockStatus, err := csm.resolveBlockStatus(stagingArea, blockHash)
+	blockStatus, err := csm.resolveBlockStatus(stagingArea, blockHash, true)
 	if err != nil {
 		return nil, err
 	}
diff --git a/domain/consensus/processes/consensusstatemanager/resolve_block_status.go b/domain/consensus/processes/consensusstatemanager/resolve_block_status.go
index adbf5617..bc2a5e64 100644
--- a/domain/consensus/processes/consensusstatemanager/resolve_block_status.go
+++ b/domain/consensus/processes/consensusstatemanager/resolve_block_status.go
@@ -12,8 +12,8 @@ import (
 	"github.com/pkg/errors"
 )
 
-func (csm *consensusStateManager) resolveBlockStatus(
-	stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
+func (csm *consensusStateManager) resolveBlockStatus(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash,
+	useSeparateStagingAreasPerBlock bool) (externalapi.BlockStatus, error) {
 
 	onEnd := logger.LogAndMeasureExecutionTime(log, fmt.Sprintf("resolveBlockStatus for %s", blockHash))
 	defer onEnd()
@@ -53,8 +53,8 @@ func (csm *consensusStateManager) resolveBlockStatus(
 		unverifiedBlockHash := unverifiedBlocks[i]
 
 		stagingAreaForCurrentBlock := stagingArea
-		isCurrentBlockTopBlock := i == 0
-		if !isCurrentBlockTopBlock {
+		useSeparateStagingArea := useSeparateStagingAreasPerBlock && (i != 0)
+		if useSeparateStagingArea {
 			stagingAreaForCurrentBlock = model.NewStagingArea()
 		}
 
@@ -72,7 +72,7 @@ func (csm *consensusStateManager) resolveBlockStatus(
 		log.Debugf("Block %s status resolved to `%s`, finished %d/%d of unverified blocks",
 			unverifiedBlockHash, blockStatus, len(unverifiedBlocks)-i, len(unverifiedBlocks))
 
-		if !isCurrentBlockTopBlock {
+		if useSeparateStagingArea {
 			err := staging.CommitAllChanges(csm.databaseContext, stagingAreaForCurrentBlock)
 			if err != nil {
 				return 0, err


Test patch:
diff --git a/domain/consensus/model/testapi/test_consensus_state_manager.go b/domain/consensus/model/testapi/test_consensus_state_manager.go
index b119ee01..ff44e9d9 100644
--- a/domain/consensus/model/testapi/test_consensus_state_manager.go
+++ b/domain/consensus/model/testapi/test_consensus_state_manager.go
@@ -10,5 +10,6 @@ type TestConsensusStateManager interface {
 	model.ConsensusStateManager
 	AddUTXOToMultiset(multiset model.Multiset, entry externalapi.UTXOEntry,
 		outpoint *externalapi.DomainOutpoint) error
-	ResolveBlockStatus(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error)
+	ResolveBlockStatus(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash,
+		useSeparateStagingAreasPerBlock bool) (externalapi.BlockStatus, error)
 }
diff --git a/domain/consensus/processes/blockbuilder/test_block_builder.go b/domain/consensus/processes/blockbuilder/test_block_builder.go
index 461b9dc2..54ec4118 100644
--- a/domain/consensus/processes/blockbuilder/test_block_builder.go
+++ b/domain/consensus/processes/blockbuilder/test_block_builder.go
@@ -143,7 +143,8 @@ func (bb *testBlockBuilder) buildBlockWithParents(stagingArea *model.StagingArea
 		return nil, nil, err
 	}
 
-	selectedParentStatus, err := bb.testConsensus.ConsensusStateManager().ResolveBlockStatus(stagingArea, ghostdagData.SelectedParent())
+	selectedParentStatus, err := bb.testConsensus.ConsensusStateManager().ResolveBlockStatus(
+		stagingArea, ghostdagData.SelectedParent(), false)
 	if err != nil {
 		return nil, nil, err
 	}
diff --git a/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go b/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go
index c3b95834..ab17e0c8 100644
--- a/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go
+++ b/domain/consensus/processes/consensusstatemanager/resolve_block_status_test.go
@@ -147,7 +147,8 @@ func TestDoubleSpends(t *testing.T) {
 			t.Fatalf("Error adding goodBlock: %+v", err)
 		}
 		//use ResolveBlockStatus, since goodBlock2 might not be the selectedTip
-		goodBlock2Status, err := consensus.ConsensusStateManager().ResolveBlockStatus(stagingArea, goodBlock2Hash)
+		goodBlock2Status, err := consensus.ConsensusStateManager().ResolveBlockStatus(
+			stagingArea, goodBlock2Hash, true)
 		if err != nil {
 			t.Fatalf("Error getting status of goodBlock: %+v", err)
 		}
diff --git a/domain/consensus/processes/consensusstatemanager/test_consensus_state_manager.go b/domain/consensus/processes/consensusstatemanager/test_consensus_state_manager.go
index de1b70df..0eb9c2d5 100644
--- a/domain/consensus/processes/consensusstatemanager/test_consensus_state_manager.go
+++ b/domain/consensus/processes/consensusstatemanager/test_consensus_state_manager.go
@@ -21,8 +21,8 @@ func (csm *testConsensusStateManager) AddUTXOToMultiset(
 	return addUTXOToMultiset(multiset, entry, outpoint)
 }
 
-func (csm *testConsensusStateManager) ResolveBlockStatus(stagingArea *model.StagingArea,
-	blockHash *externalapi.DomainHash) (externalapi.BlockStatus, error) {
+func (csm *testConsensusStateManager) ResolveBlockStatus(stagingArea *model.StagingArea, blockHash *externalapi.DomainHash,
+	useSeparateStagingAreasPerBlock bool) (externalapi.BlockStatus, error) {
 
-	return csm.resolveBlockStatus(stagingArea, blockHash)
+	return csm.resolveBlockStatus(stagingArea, blockHash, useSeparateStagingAreasPerBlock)
 }
diff --git a/domain/consensus/test_consensus.go b/domain/consensus/test_consensus.go
index 4730bda3..f4c99889 100644
--- a/domain/consensus/test_consensus.go
+++ b/domain/consensus/test_consensus.go
@@ -21,6 +21,8 @@ type testConsensus struct {
 	testReachabilityManager   testapi.TestReachabilityManager
 	testConsensusStateManager testapi.TestConsensusStateManager
 	testTransactionValidator  testapi.TestTransactionValidator
+
+	buildBlockConsensus *consensus
 }
 
 func (tc *testConsensus) DAGParams() *dagconfig.Params {