kaspad/domain/consensus/processes/reachabilitymanager/reachability_external_test.go
Ori Newman fc5e39f6cc [NOD-1535] fix reachability test (#1061)
* Revert "[NOD-1500] Delete integration tests"

This reverts commit fcb57a206690a884fa6afb69d5d493282954a8bf.

* [NOD-1518] hashserialization -> consenusserialization

* [NOD-1518] Fix add genesis to virtual

* [NOD-1518] Fix a bug in SerializeCoinbasePayload.

* [NOD-1518] Fix a loop error and make pastMedianTime behave correctly everywhere on genesis.

* [NOD-1518] Fix another bug and an infinite loop.

* [NOD-1518] Fix uninitialized slice.

* [NOD-1518] Fix bad should-commit checks and another infinite loop.

* [NOD-1518] Fix nil serialization.

* [NOD-1518] Rename blockHash to currentBlockHash.

* [NOD-1518] Move the check whether stagedVirtualUTXOSet != nil to the top of commitVirtualUTXODiff.

* [NOD-1518] Simplify utxoDiffStore.Commit.

* [NOD-1518] Unextract resolveBlockStatusAndCheckFinality.

* [NOD-1518] Move no-transactions logic into CalculateIDMerkleRoot.

* [NOD-1518] Remove redundant is-staged check.

* [NOD-1518] Fix merge errors.

* [NOD-1518] Don't write anything if utxoDiffChild is nil.

* [NOD-1518] Stage virtualAcceptanceData and virtualMultiset.

* [NOD-1518] Fix bugs in getBlockTemplate and submitBlock.

* [NOD-1518] Fix bad validation order in validateHeaderInContext.

* [NOD-1518] Fix bug in Next().

* [NOD-1518] Fix nil dereference of subnetworks in AddressCache.

* [NOD-1518] Fix multisetStore.Get returning a pointer to a multiset that is changed in place.

* [NOD-1518] Break on genesis in countSubtrees.

* [NOD-1518] Fix createBlockLocator.

* [NOD-1518] Fix MsgTxToDomainTransaction.

* [NOD-1518] Set MaxTxVersion to 1.

* [NOD-1518] Fix missing error handling, bug in MsgTxToDomainTransaction, and bad subnetwork equality check.

* [NOD-1518] Fix bug in hasUTXOByOutpointFromStagedVirtualUTXODiff.

* [NOD-1518] Remove irrelevant comments.

* [NOD-1518] Generate transactions with sufficient fee in tx_relay_test.

* [NOD-1518] Fix broken RPC handlers.

* [NOD-1518] Fix merge errors.

* [NOD-1518] Fix bad exists check in restorePastUTXO and missing genesis check in CalculatePastUTXOAndAcceptanceData.

* [NOD-1518] Add a comment.

* [NOD-1518] Use a regular mutex instead of a read-write mutex in consensus to avoid dealing with sneaky not-actually-read functions.

* [NOD-1518] Fix a deadlock in GetVirtualSelectedParent.

* [NOD-1518] Fix missing handler registration for CmdHeader.

* [NOD-1518] Fix processHeader calling OnNewBlock and LogBlock. Also fix conversion errors in IBDRootUTXOSetAndBlock.

* [NOD-1518] Fix bad Command() in MsgIBDRootUTXOSetAndBlock.

* [NOD-1518] Fix bad SyncStateMissingUTXOSet logic in resolveSyncState.

* [NOD-1518] Rename mode to syncState.

* [NOD-1518] Fix headers-only blocks coming in after the consensus thinks it's synced.

* [NOD-1518] Fix selectedChildIterator.Next not ignoring virtual, infinite loop in HashSet.Length().

* [NOD-1518] Fix not-properly wrapped IBD blocks.

* [NOD-1518] Fix bad conversion in RequestIBDBlocks.

* [NOD-1518] Fix bad string for CmdRequestHeaders.

* [NOD-1518] Fix bad string for CmdDoneHeaders.

* [NOD-1518] Fix bad Command() for MsgIBDRootNotFound.

* [NOD-1518] Fix bad areHeaderTipsSyncedMaxTimeDifference value.

* [NOD-1518] Add missing string for CmdRequestIBDBlocks.

* [NOD-1518] Fix bad check for SyncStateMissingBlockBodies.

* [NOD-1518] Fix bad timeout durations in tests.

* [NOD-1518] Fix IBD blocks not calling OnNewBlock.

* [NOD-1518] Change when IBD finishes.

* [NOD-1518] Properly clone utxoDiffChild.

* [NOD-1535] Fix reachability tests

* [NOD-1518] Fix merge errors.

* [NOD-1518] Move call to LogBlock to into OnNewBlock.

* [NOD-1518] Return "not implemented" in unimplemented RPC handlers.

* [NOD-1518] Extract cloning of hashes to a method over DomainHash.

* [NOD-1518] Use isHeaderOnlyBlock.

* [NOD-1518] Use constants.TransactionVersion.

* [NOD-1518] Break immediately if we reached the virtual in SelectedChildIterator.

* [NOD-1518] Don't stage nil utxoDiffChild.

* [NOD-1518] Properly check the genesis hash in CalculatePastUTXOAndAcceptanceData.

* [NOD-1518] Explain why we break on current == nil in countSubtrees.

* [NOD-1518] Add a comment explaining why we check against StatusValid in resolveSyncState.

* [NOD-1535] Add external reachability tests

* [NOD-1535] Fix reachability tests and fix related bugs

* [NOD-1535] Add setters fox reindex slack and window

* [NOD-1535] Remove redundant line

* [NOD-1535] Add comment

* [NOD-1535] Fix comments

* [NOD-1535] Rename DBReader->DatabaseContext

* [NOD-1535] Check that reindex root is changed

* [NOD-1535] Fix calculateNewTips

Co-authored-by: Mike Zak <feanorr@gmail.com>
Co-authored-by: stasatdaglabs <stas@daglabs.com>
2020-11-17 16:00:16 +02:00

325 lines
12 KiB
Go

package reachabilitymanager_test
import (
"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) {
reachabilityReindexWindow := uint64(2)
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
factory := consensus.NewFactory()
tc, tearDown, err := factory.NewTestConsensus(params, "TestAddChildThatPointsDirectlyToTheSelectedParentChainBelowReindexRoot")
if err != nil {
t.Fatalf("NewTestConsensus: %+v", err)
}
defer tearDown()
tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
reindexRoot, err := tc.ReachabilityDataStore().ReachabilityReindexRoot(tc.DatabaseContext())
if err != nil {
t.Fatalf("ReachabilityReindexRoot: %s", err)
}
if *reindexRoot != *params.GenesisHash {
t.Fatalf("reindex root is expected to initially be genesis")
}
// Add a block on top of the genesis block
chainRootBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
// Add chain of reachabilityReindexWindow blocks above chainRootBlock.
// This should move the reindex root
chainRootBlockTipHash := chainRootBlock
for i := uint64(0); i < reachabilityReindexWindow; i++ {
chainBlock, err := tc.AddBlock([]*externalapi.DomainHash{chainRootBlockTipHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
chainRootBlockTipHash = chainBlock
}
newReindexRoot, err := tc.ReachabilityDataStore().ReachabilityReindexRoot(tc.DatabaseContext())
if err != nil {
t.Fatalf("ReachabilityReindexRoot: %s", err)
}
if *newReindexRoot != *reindexRoot {
t.Fatalf("reindex root is expected to change")
}
// Add another block over genesis
_, err = tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
})
}
func TestUpdateReindexRoot(t *testing.T) {
reachabilityReindexWindow := uint64(2)
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
factory := consensus.NewFactory()
tc, tearDown, err := factory.NewTestConsensus(params, "TestUpdateReindexRoot")
if err != nil {
t.Fatalf("NewTestConsensus: %s", err)
}
defer tearDown()
tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
intervalSize := func(hash *externalapi.DomainHash) uint64 {
data, err := tc.ReachabilityDataStore().ReachabilityData(tc.DatabaseContext(), hash)
if err != nil {
t.Fatalf("ReachabilityData: %s", err)
}
return data.TreeNode.Interval.End - data.TreeNode.Interval.Start + 1
}
// Add two blocks on top of the genesis block
chain1RootBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
chain2RootBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
// Make two chains of size reachabilityReindexWindow and check that the reindex root is not changed.
for i := uint64(0); i < reachabilityReindexWindow-1; i++ {
_, err := tc.AddBlock([]*externalapi.DomainHash{chain1RootBlock}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
_, err = tc.AddBlock([]*externalapi.DomainHash{chain2RootBlock}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
reindexRoot, err := tc.ReachabilityDataStore().ReachabilityReindexRoot(tc.DatabaseContext())
if err != nil {
t.Fatalf("ReachabilityReindexRoot: %s", err)
}
if *reindexRoot != *params.GenesisHash {
t.Fatalf("reindex root unexpectedly moved")
}
}
// Add another block over chain1. This will move the reindex root to chain1RootBlock
_, err = tc.AddBlock([]*externalapi.DomainHash{chain1RootBlock}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
// Make sure that chain1RootBlock is now the reindex root
reindexRoot, err := tc.ReachabilityDataStore().ReachabilityReindexRoot(tc.DatabaseContext())
if err != nil {
t.Fatalf("ReachabilityReindexRoot: %s", err)
}
if *reindexRoot != *chain1RootBlock {
t.Fatalf("chain1RootBlock is not the reindex root after reindex")
}
// Make sure that tight intervals have been applied to chain2. Since
// we added reachabilityReindexWindow-1 blocks to chain2, the size
// of the interval at its root should be equal to reachabilityReindexWindow
if intervalSize(chain2RootBlock) != reachabilityReindexWindow {
t.Fatalf("got unexpected chain2RootBlock interval. Want: %d, got: %d",
intervalSize(chain2RootBlock), reachabilityReindexWindow)
}
// Make sure that the rest of the interval has been allocated to
// chain1RootNode, minus slack from both sides
expectedChain1RootIntervalSize := intervalSize(params.GenesisHash) - 1 -
intervalSize(chain2RootBlock) - 2*reachabilityReindexWindow
if intervalSize(chain1RootBlock) != expectedChain1RootIntervalSize {
t.Fatalf("got unexpected chain1RootBlock interval. Want: %d, got: %d",
intervalSize(chain1RootBlock), expectedChain1RootIntervalSize)
}
})
}
func TestReindexIntervalsEarlierThanReindexRoot(t *testing.T) {
reachabilityReindexWindow := uint64(2)
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
factory := consensus.NewFactory()
tc, tearDown, err := factory.NewTestConsensus(params, "TestUpdateReindexRoot")
if err != nil {
t.Fatalf("NewTestConsensus: %s", err)
}
defer tearDown()
tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
intervalSize := func(hash *externalapi.DomainHash) uint64 {
data, err := tc.ReachabilityDataStore().ReachabilityData(tc.DatabaseContext(), hash)
if err != nil {
t.Fatalf("ReachabilityData: %s", err)
}
return data.TreeNode.Interval.End - data.TreeNode.Interval.Start + 1
}
// Add three children to the genesis: leftBlock, centerBlock, rightBlock
leftBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
centerBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
rightBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
// Add a chain of reachabilityReindexWindow blocks above centerBlock.
// This will move the reindex root to centerBlock
centerTipHash := centerBlock
for i := uint64(0); i < reachabilityReindexWindow; i++ {
var err error
centerTipHash, err = tc.AddBlock([]*externalapi.DomainHash{centerTipHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
}
// Make sure that centerBlock is now the reindex root
reindexRoot, err := tc.ReachabilityDataStore().ReachabilityReindexRoot(tc.DatabaseContext())
if err != nil {
t.Fatalf("ReachabilityReindexRoot: %s", err)
}
if *reindexRoot != *centerBlock {
t.Fatalf("centerBlock is not the reindex root after reindex")
}
// Get the current interval for leftBlock. The reindex should have
// resulted in a tight interval there
if intervalSize(leftBlock) != 1 {
t.Fatalf("leftBlock interval not tight after reindex")
}
// Get the current interval for rightBlock. The reindex should have
// resulted in a tight interval there
if intervalSize(rightBlock) != 1 {
t.Fatalf("rightBlock interval not tight after reindex")
}
// Get the current interval for centerBlock. Its interval should be:
// genesisInterval - 1 - leftInterval - leftSlack - rightInterval - rightSlack
expectedCenterInterval := intervalSize(params.GenesisHash) - 1 -
intervalSize(leftBlock) - tc.ReachabilityManager().ReachabilityReindexSlack() -
intervalSize(rightBlock) - tc.ReachabilityManager().ReachabilityReindexSlack()
if intervalSize(centerBlock) != expectedCenterInterval {
t.Fatalf("unexpected centerBlock interval. Want: %d, got: %d",
expectedCenterInterval, intervalSize(centerBlock))
}
// Add a chain of reachabilityReindexWindow - 1 blocks above leftBlock.
// Each addition will trigger a low-than-reindex-root reindex. We
// expect the centerInterval to shrink by 1 each time, but its child
// to remain unaffected
cetnerData, err := tc.ReachabilityDataStore().ReachabilityData(tc.DatabaseContext(), centerBlock)
if err != nil {
t.Fatalf("ReachabilityData: %s", err)
}
treeChildOfCenterBlock := cetnerData.TreeNode.Children[0]
treeChildOfCenterBlockOriginalIntervalSize := intervalSize(treeChildOfCenterBlock)
leftTipHash := leftBlock
for i := uint64(0); i < reachabilityReindexWindow-1; i++ {
var err error
leftTipHash, err = tc.AddBlock([]*externalapi.DomainHash{leftTipHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
expectedCenterInterval--
if intervalSize(centerBlock) != expectedCenterInterval {
t.Fatalf("unexpected centerBlock interval. Want: %d, got: %d",
expectedCenterInterval, intervalSize(centerBlock))
}
if intervalSize(treeChildOfCenterBlock) != treeChildOfCenterBlockOriginalIntervalSize {
t.Fatalf("the interval of centerBlock's child unexpectedly changed")
}
}
// Add a chain of reachabilityReindexWindow - 1 blocks above rightBlock.
// Each addition will trigger a low-than-reindex-root reindex. We
// expect the centerInterval to shrink by 1 each time, but its child
// to remain unaffected
rightTipHash := rightBlock
for i := uint64(0); i < reachabilityReindexWindow-1; i++ {
var err error
rightTipHash, err = tc.AddBlock([]*externalapi.DomainHash{rightTipHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
expectedCenterInterval--
if intervalSize(centerBlock) != expectedCenterInterval {
t.Fatalf("unexpected centerBlock interval. Want: %d, got: %d",
expectedCenterInterval, intervalSize(centerBlock))
}
if intervalSize(treeChildOfCenterBlock) != treeChildOfCenterBlockOriginalIntervalSize {
t.Fatalf("the interval of centerBlock's child unexpectedly changed")
}
}
})
}
func TestTipsAfterReindexIntervalsEarlierThanReindexRoot(t *testing.T) {
reachabilityReindexWindow := uint64(2)
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
factory := consensus.NewFactory()
tc, tearDown, err := factory.NewTestConsensus(params, "TestUpdateReindexRoot")
if err != nil {
t.Fatalf("NewTestConsensus: %s", err)
}
defer tearDown()
tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
// Add a chain of reachabilityReindexWindow + 1 blocks above the genesis.
// This will set the reindex root to the child of genesis
chainTipHash := params.GenesisHash
for i := uint64(0); i < reachabilityReindexWindow+1; i++ {
chainTipHash, err = tc.AddBlock([]*externalapi.DomainHash{chainTipHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
}
// Add another block above the genesis block. This will trigger an
// earlier-than-reindex-root reindex
sideBlock, err := tc.AddBlock([]*externalapi.DomainHash{params.GenesisHash}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
// Add a block whose parents are the chain tip and the side block.
// We expect this not to fail
_, err = tc.AddBlock([]*externalapi.DomainHash{sideBlock}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
})
}