kaspad/domain/consensus/processes/reachabilitymanager/reachability_external_test.go
Ori Newman cb9d7e313d
Implement Clone and Equal for all model types (#1155)
* [NOD-1575] Implement Clone and Equal for all model types

* [NOD-1575] Add assertion for transaction ID equality

* [NOD-1575] Use DomainTransaction.Equal to compare to expected coinbase transaction

* [NOD-1575] Add TestDomainBlockHeader_Clone

* [NOD-1575] Don't clone nil values

* [NOD-1575] Add type assertions

* [NOD-1575] Don't clone nil values

* [NOD-1575] Add missing Equals

* [NOD-1575] Add length checks

* [NOD-1575] Update comment

* [NOD-1575] Check length for TransactionAcceptanceData

* [NOD-1575] Explicitly clone nils where needed

* [NOD-1575] Clone tx id

* [NOD-1575] Flip condition

* Nod 1576 make coverage tests for equal clone inside model externalapi (#1177)

* [NOD-1576] Make coverage tests for equal and clone inside model and externalapi

* Some formatting and naming fixes

* Made transactionToCompare type exported

* Added some tests and made some changes to the tests code

* No changes made

* Some formatting and naming changes made

* Made better test coverage for externalapi clone and equal functions

* Changed expected result for two cases

* Added equal and clone functions tests for ghostdag and utxodiff

* Added tests

* [NOD-1576] Implement reachabilitydata equal/clone unit tests

* [NOD-1576]  Full coverage of reachabilitydata equal/clone unit tests

* Made changes and handling panic to transaction_equal_clone_test.go and formating of utxodiff_equal_clone_test.go

* Added recoverForEqual2 for handling panic to transaction_equal_clone_test.go

* [NOD-1576]  Full coverage of transaction equal unit test

* [NOD-1576] Add expects panic

* [NOD-1576] Allow composites in go vet

* [NOD-1576] Code review fixes (#1223)

* [NOD-1576] Code review fixes

* [NOD-1576] Code review fixes part 2

* [NOD-1576] Fix wrong name

Co-authored-by: karim1king <karimkaspersky@yahoo.com>
Co-authored-by: Ori Newman <orinewman1@gmail.com>
Co-authored-by: Karim <karim1king@users.noreply.github.com>

* Fix merge errors

* Use Equal where possible

* Use Equal where possible

* Use Equal where possible

Co-authored-by: andrey-hash <74914043+andrey-hash@users.noreply.github.com>
Co-authored-by: karim1king <karimkaspersky@yahoo.com>
Co-authored-by: Karim <karim1king@users.noreply.github.com>
2020-12-22 17:38:54 +02:00

328 lines
12 KiB
Go

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"
)
func TestAddChildThatPointsDirectlyToTheSelectedParentChainBelowReindexRoot(t *testing.T) {
reachabilityReindexWindow := uint64(10)
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(false)
tc.ReachabilityManager().SetReachabilityReindexWindow(reachabilityReindexWindow)
reindexRoot, err := tc.ReachabilityDataStore().ReachabilityReindexRoot(tc.DatabaseContext())
if err != nil {
t.Fatalf("ReachabilityReindexRoot: %s", err)
}
if !reindexRoot.Equal(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.Equal(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(10)
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(false)
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.
chain1Tip, chain2Tip := chain1RootBlock, chain2RootBlock
for i := uint64(0); i < reachabilityReindexWindow-1; i++ {
var err error
chain1Tip, _, err = tc.AddBlock([]*externalapi.DomainHash{chain1Tip}, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
chain2Tip, _, err = tc.AddBlock([]*externalapi.DomainHash{chain2Tip}, 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.Equal(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{chain1Tip}, 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.Equal(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*tc.ReachabilityManager().ReachabilityReindexSlack()
if intervalSize(chain1RootBlock) != expectedChain1RootIntervalSize {
t.Fatalf("got unexpected chain1RootBlock interval. Want: %d, got: %d",
intervalSize(chain1RootBlock), expectedChain1RootIntervalSize)
}
})
}
func TestReindexIntervalsEarlierThanReindexRoot(t *testing.T) {
reachabilityReindexWindow := uint64(10)
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: %+v", err)
}
defer tearDown(false)
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.Equal(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
centerData, err := tc.ReachabilityDataStore().ReachabilityData(tc.DatabaseContext(), centerBlock)
if err != nil {
t.Fatalf("ReachabilityData: %s", err)
}
treeChildOfCenterBlock := centerData.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(10)
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(false)
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)
}
})
}