kaspad/domain/domain_test.go
Ori Newman d207888b67
Implement pruned headers node (#1787)
* Pruning headers p2p basic structure

* Remove headers-first

* Fix consensus tests except TestValidateAndInsertPruningPointWithSideBlocks and TestValidateAndInsertImportedPruningPoint

* Add virtual genesis

* Implement PruningPointAndItsAnticoneWithMetaData

* Start fixing TestValidateAndInsertImportedPruningPoint

* Fix TestValidateAndInsertImportedPruningPoint

* Fix BlockWindow

* Update p2p and gRPC

* Fix all tests except TestHandleRelayInvs

* Delete TestHandleRelayInvs parts that cover the old IBD flow

* Fix lint errors

* Add p2p_request_ibd_blocks.go

* Clean code

* Make MsgBlockWithMetaData implement its own representation

* Remove redundant check if highest share block is below the pruning point

* Fix TestCheckLockTimeVerifyConditionedByAbsoluteTimeWithWrongLockTime

* Fix comments, errors ane names

* Fix window size to the real value

* Check reindex root after each block at TestUpdateReindexRoot

* Remove irrelevant check

* Renames and comments

* Remove redundant argument from sendGetBlockLocator

* Don't delete staging on non-recoverable errors

* Renames and comments

* Remove redundant code

* Commit changes inside ResolveVirtual

* Add comment to IsRecoverableError

* Remove blocksWithMetaDataGHOSTDAGDataStore

* Increase windows pagefile

* Move DeleteStagingConsensus outside of defer

* Get rid of mustAccepted in receiveBlockWithMetaData

* Ban on invalid pruning point

* Rename interface_datastructures_daawindowstore.go to interface_datastructures_blocks_with_meta_data_daa_window_store.go

* * Change GetVirtualSelectedParentChainFromBlockResponseMessage and VirtualSelectedParentChainChangedNotificationMessage to show only added block hashes
*  Remove ResolveVirtual
* Use externalapi.ConsensusWrapper inside MiningManager
* Fix pruningmanager.blockwithmetadata

* Set pruning point selected child when importing the pruning point UTXO set

* Change virtual genesis hash

* replace the selected parent with virtual genesis on removePrunedBlocksFromGHOSTDAGData

* Get rid of low hash in block locators

* Remove +1 from everywhere we use difficultyAdjustmentWindowSize and increase the default value by one

* Add comments about consensus wrapper

* Don't use separate staging area when resolving resolveBlockStatus

* Fix netsync stability test

* Fix checkResolveVirtual

* Rename ConsensusWrapper->ConsensusReference

* Get rid of blockHeapNode

* Add comment to defaultDifficultyAdjustmentWindowSize

* Add SelectedChild to DAGTraversalManager

* Remove redundant copy

* Rename blockWindowHeap->calculateBlockWindowHeap

* Move isVirtualGenesisOnlyParent to utils

* Change BlockWithMetaData->BlockWithTrustedData

* Get rid of maxReasonLength

* Split IBD to 100 blocks each time

* Fix a bug in calculateBlockWindowHeap

* Switch to trusted data when encountering virtual genesis in blockWithTrustedData

* Move ConsensusReference to domain

* Update ConsensusReference comment

* Add comment

* Rename shouldNotAddGenesis->skipAddingGenesis
2021-07-26 12:24:07 +03:00

158 lines
4.7 KiB
Go

package domain_test
import (
"fmt"
"math/big"
"github.com/kaspanet/kaspad/domain"
"github.com/kaspanet/kaspad/domain/consensus"
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/utils/testutils"
"github.com/kaspanet/kaspad/domain/miningmanager/mempool"
"github.com/kaspanet/kaspad/infrastructure/db/database/ldb"
"io/ioutil"
"os"
"strings"
"testing"
)
func TestCreateStagingConsensus(t *testing.T) {
testutils.ForAllNets(t, true, func(t *testing.T, consensusConfig *consensus.Config) {
dataDir, err := ioutil.TempDir("", fmt.Sprintf("TestCreateStagingConsensus-%s", consensusConfig.Name))
if err != nil {
t.Fatalf("ioutil.TempDir: %+v", err)
}
defer os.RemoveAll(dataDir)
db, err := ldb.NewLevelDB(dataDir, 8)
if err != nil {
t.Fatalf("NewLevelDB: %+v", err)
}
domainInstance, err := domain.New(consensusConfig, mempool.DefaultConfig(&consensusConfig.Params), db)
if err != nil {
t.Fatalf("New: %+v", err)
}
err = domainInstance.InitStagingConsensus()
if err != nil {
t.Fatalf("InitStagingConsensus: %+v", err)
}
err = domainInstance.InitStagingConsensus()
if !strings.Contains(err.Error(), "cannot create staging consensus when a staging consensus already exists") {
t.Fatalf("unexpected error: %+v", err)
}
addGenesisToStagingConsensus := func() {
genesisWithTrustedData := &externalapi.BlockWithTrustedData{
Block: consensusConfig.GenesisBlock,
DAAScore: 0,
DAAWindow: nil,
GHOSTDAGData: []*externalapi.BlockGHOSTDAGDataHashPair{
{
GHOSTDAGData: externalapi.NewBlockGHOSTDAGData(0, big.NewInt(0), model.VirtualGenesisBlockHash, nil, nil, make(map[externalapi.DomainHash]externalapi.KType)),
Hash: consensusConfig.GenesisHash,
},
},
}
_, err = domainInstance.StagingConsensus().ValidateAndInsertBlockWithTrustedData(genesisWithTrustedData, true)
if err != nil {
t.Fatalf("ValidateAndInsertBlockWithTrustedData: %+v", err)
}
}
addGenesisToStagingConsensus()
coinbaseData := &externalapi.DomainCoinbaseData{
ScriptPublicKey: &externalapi.ScriptPublicKey{},
ExtraData: []byte{},
}
block, err := domainInstance.StagingConsensus().BuildBlock(coinbaseData, nil)
if err != nil {
t.Fatalf("BuildBlock: %+v", err)
}
_, err = domainInstance.StagingConsensus().ValidateAndInsertBlock(block, true)
if err != nil {
t.Fatalf("ValidateAndInsertBlock: %+v", err)
}
blockHash := consensushashing.BlockHash(block)
blockInfo, err := domainInstance.StagingConsensus().GetBlockInfo(blockHash)
if err != nil {
t.Fatalf("GetBlockInfo: %+v", err)
}
if !blockInfo.Exists {
t.Fatalf("block not found on staging consensus")
}
blockInfo, err = domainInstance.Consensus().GetBlockInfo(blockHash)
if err != nil {
t.Fatalf("GetBlockInfo: %+v", err)
}
if blockInfo.Exists {
t.Fatalf("a block from staging consensus was found on consensus")
}
err = domainInstance.CommitStagingConsensus()
if err != nil {
t.Fatalf("CommitStagingConsensus: %+v", err)
}
blockInfo, err = domainInstance.Consensus().GetBlockInfo(blockHash)
if err != nil {
t.Fatalf("GetBlockInfo: %+v", err)
}
if !blockInfo.Exists {
t.Fatalf("a block from staging consensus was not found on consensus after commit")
}
// Now we create a new staging consensus and check that it's deleted once we init a new domain. We also
// validate that the main consensus persisted the data from the committed temp consensus.
err = domainInstance.InitStagingConsensus()
if err != nil {
t.Fatalf("InitStagingConsensus: %+v", err)
}
addGenesisToStagingConsensus()
_, err = domainInstance.StagingConsensus().ValidateAndInsertBlock(block, true)
if err != nil {
t.Fatalf("ValidateAndInsertBlock: %+v", err)
}
domainInstance2, err := domain.New(consensusConfig, mempool.DefaultConfig(&consensusConfig.Params), db)
if err != nil {
t.Fatalf("New: %+v", err)
}
blockInfo, err = domainInstance2.Consensus().GetBlockInfo(blockHash)
if err != nil {
t.Fatalf("GetBlockInfo: %+v", err)
}
if !blockInfo.Exists {
t.Fatalf("a block from committed staging consensus was not persisted to the active consensus")
}
err = domainInstance2.InitStagingConsensus()
if err != nil {
t.Fatalf("InitStagingConsensus: %+v", err)
}
blockInfo, err = domainInstance2.StagingConsensus().GetBlockInfo(blockHash)
if err != nil {
t.Fatalf("GetBlockInfo: %+v", err)
}
if blockInfo.Exists {
t.Fatalf("block from previous temp consensus shouldn't be found on a fresh temp consensus")
}
})
}