kaspad/domain/consensus/test_consensus.go
Ori Newman dd35669861
Check that there are no prefilled fields when validating a block (#1329)
* Check that there are no prefilled fields when validating a block

* Use cleanBlockPrefilledFields in AddBlock

* Move cleanBlockPrefilledFields to BuildBlockWithParents

* Move cleanBlockPrefilledFields to func (bb *testBlockBuilder) BuildBlockWithParents
2020-12-30 18:31:17 +02:00

75 lines
2.3 KiB
Go

package consensus
import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/model/testapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/dagconfig"
)
type testConsensus struct {
*consensus
dagParams *dagconfig.Params
testBlockBuilder testapi.TestBlockBuilder
testReachabilityManager testapi.TestReachabilityManager
testConsensusStateManager testapi.TestConsensusStateManager
testTransactionValidator testapi.TestTransactionValidator
}
func (tc *testConsensus) DAGParams() *dagconfig.Params {
return tc.dagParams
}
func (tc *testConsensus) BuildBlockWithParents(parentHashes []*externalapi.DomainHash,
coinbaseData *externalapi.DomainCoinbaseData, transactions []*externalapi.DomainTransaction) (
*externalapi.DomainBlock, model.UTXODiff, error) {
// Require write lock because BuildBlockWithParents stages temporary data
tc.lock.Lock()
defer tc.lock.Unlock()
block, diff, err := tc.testBlockBuilder.BuildBlockWithParents(parentHashes, coinbaseData, transactions)
if err != nil {
return nil, nil, err
}
return block, diff, nil
}
func (tc *testConsensus) AddBlock(parentHashes []*externalapi.DomainHash, coinbaseData *externalapi.DomainCoinbaseData,
transactions []*externalapi.DomainTransaction) (*externalapi.DomainHash, *externalapi.BlockInsertionResult, error) {
// Require write lock because BuildBlockWithParents stages temporary data
tc.lock.Lock()
defer tc.lock.Unlock()
block, _, err := tc.testBlockBuilder.BuildBlockWithParents(parentHashes, coinbaseData, transactions)
if err != nil {
return nil, nil, err
}
blockInsertionResult, err := tc.blockProcessor.ValidateAndInsertBlock(block)
if err != nil {
return nil, nil, err
}
return consensushashing.BlockHash(block), blockInsertionResult, nil
}
func (tc *testConsensus) DiscardAllStores() {
tc.AcceptanceDataStore().Discard()
tc.BlockHeaderStore().Discard()
tc.BlockRelationStore().Discard()
tc.BlockStatusStore().Discard()
tc.BlockStore().Discard()
tc.ConsensusStateStore().Discard()
tc.GHOSTDAGDataStore().Discard()
tc.HeaderTipsStore().Discard()
tc.MultisetStore().Discard()
tc.PruningStore().Discard()
tc.ReachabilityDataStore().Discard()
tc.UTXODiffStore().Discard()
}