Add TestPruning (#1222)

* Add TestPruning

* Add missing argument to teardown

* Add missing return value to AddBlock
This commit is contained in:
Ori Newman 2020-12-16 14:49:55 +02:00 committed by GitHub
parent 99a14c5999
commit bf67c6351e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17376 additions and 0 deletions

View File

@ -0,0 +1,176 @@
package pruningmanager_test
import (
"encoding/json"
"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"
"os"
"path/filepath"
"testing"
"time"
)
type jsonBlock struct {
ID string `json:"ID"`
Parents []string `json:"Parents"`
}
type testJSON struct {
MergeSetSizeLimit uint64 `json:"mergeSetSizeLimit"`
FinalityDepth uint64 `json:"finalityDepth"`
Blocks []*jsonBlock `json:"blocks"`
}
func TestPruning(t *testing.T) {
expectedPruningPointByNet := map[string]map[string]string{
"chain-for-test-pruning.json": {
"kaspa-mainnet": "84",
"kaspa-simnet": "84",
"kaspa-devnet": "84",
"kaspa-testnet": "84",
},
"dag-for-test-pruning.json": {
"kaspa-mainnet": "503",
"kaspa-simnet": "502",
"kaspa-devnet": "502",
"kaspa-testnet": "502",
},
}
testutils.ForAllNets(t, true, func(t *testing.T, params *dagconfig.Params) {
err := filepath.Walk("./testdata", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
jsonFile, err := os.Open(path)
if err != nil {
t.Fatalf("TestPruning : failed opening json file %s: %s", path, err)
}
defer jsonFile.Close()
test := &testJSON{}
decoder := json.NewDecoder(jsonFile)
decoder.DisallowUnknownFields()
err = decoder.Decode(&test)
if err != nil {
t.Fatalf("TestPruning: failed decoding json: %v", err)
}
params.FinalityDuration = time.Duration(test.FinalityDepth) * params.TargetTimePerBlock
params.MergeSetSizeLimit = test.MergeSetSizeLimit
factory := consensus.NewFactory()
tc, teardown, err := factory.NewTestConsensus(params, "TestPruning")
if err != nil {
t.Fatalf("Error setting up consensus: %+v", err)
}
defer teardown(false)
blockIDToHash := map[string]*externalapi.DomainHash{
"0": params.GenesisHash,
}
blockHashToID := map[externalapi.DomainHash]string{
*params.GenesisHash: "0",
}
for _, dagBlock := range test.Blocks {
if dagBlock.ID == "0" {
continue
}
parentHashes := make([]*externalapi.DomainHash, 0, len(dagBlock.Parents))
for _, parentID := range dagBlock.Parents {
parentHash, ok := blockIDToHash[parentID]
if !ok {
t.Fatalf("No hash was found for block with ID %s", parentID)
}
parentHashes = append(parentHashes, parentHash)
}
blockHash, _, err := tc.AddBlock(parentHashes, nil, nil)
if err != nil {
t.Fatalf("AddBlock: %+v", err)
}
blockIDToHash[dagBlock.ID] = blockHash
blockHashToID[*blockHash] = dagBlock.ID
}
pruningPoint, err := tc.PruningStore().PruningPoint(tc.DatabaseContext())
if err != nil {
t.Fatalf("PruningPoint: %+v", err)
}
pruningPointID := blockHashToID[*pruningPoint]
expectedPruningPoint := expectedPruningPointByNet[info.Name()][params.Name]
if expectedPruningPoint != pruningPointID {
t.Fatalf("%s: Expected pruning point to be %s but got %s", info.Name(), expectedPruningPoint, pruningPointID)
}
for _, jsonBlock := range test.Blocks {
id := jsonBlock.ID
blockHash := blockIDToHash[id]
isPruningPointAncestorOfBlock, err := tc.DAGTopologyManager().IsAncestorOf(pruningPoint, blockHash)
if err != nil {
t.Fatalf("IsAncestorOf: %+v", err)
}
expectsBlock := true
if !isPruningPointAncestorOfBlock {
isBlockAncestorOfPruningPoint, err := tc.DAGTopologyManager().IsAncestorOf(blockHash, pruningPoint)
if err != nil {
t.Fatalf("IsAncestorOf: %+v", err)
}
if isBlockAncestorOfPruningPoint {
expectsBlock = false
} else {
virtualInfo, err := tc.GetVirtualInfo()
if err != nil {
t.Fatalf("GetVirtualInfo: %+v", err)
}
isInPastOfVirtual := false
for _, virtualParent := range virtualInfo.ParentHashes {
isAncestorOfVirtualParent, err := tc.DAGTopologyManager().IsAncestorOf(blockHash, virtualParent)
if err != nil {
t.Fatalf("IsAncestorOf: %+v", err)
}
if isAncestorOfVirtualParent {
isInPastOfVirtual = true
break
}
}
if !isInPastOfVirtual {
expectsBlock = false
}
}
}
hasBlock, err := tc.BlockStore().HasBlock(tc.DatabaseContext(), blockHash)
if err != nil {
t.Fatalf("HasBlock: %+v", err)
}
if expectsBlock != hasBlock {
t.Fatalf("expected hasBlock to be %t for block %s but got %t", expectsBlock, id, hasBlock)
}
}
return nil
})
if err != nil {
t.Fatalf("Walk: %+v", err)
}
})
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff