mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-06-05 21:56:50 +00:00

* [NOD-641] Upgrade to github.com/pkg/errors v0.9.1 and use errors.As where needed * [NOD-641] Fix find and replace error * [NOD-641] Use errors.As for error type checking * [NOD-641] Fix errors.As for pointer types * [NOD-641] Use errors.As where needed * [NOD-641] Rename rErr->ruleErr * [NOD-641] Rename derr->dbErr * [NOD-641] e->flagsErr where necessary * [NOD-641] change jerr to more appropriate name * [NOD-641] Rename cerr->bdRuleErr * [NOD-641] Rename serr->scriptErr * [NOD-641] Use errors.Is instead of testutil.AreErrorsEqual in TestNewHashFromStr * [NOD-641] Rename bdRuleErr->dagRuleErr * [NOD-641] Rename mErr->msgErr * [NOD-641] Rename dErr->deserializeErr
105 lines
3.8 KiB
Go
105 lines
3.8 KiB
Go
package blockdag
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/kaspanet/kaspad/dagconfig"
|
|
)
|
|
|
|
func TestMaybeAcceptBlockErrors(t *testing.T) {
|
|
// Create a new database and DAG instance to run tests against.
|
|
dag, teardownFunc, err := DAGSetup("TestMaybeAcceptBlockErrors", Config{
|
|
DAGParams: &dagconfig.SimnetParams,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("TestMaybeAcceptBlockErrors: Failed to setup DAG instance: %v", err)
|
|
}
|
|
defer teardownFunc()
|
|
|
|
dag.TestSetCoinbaseMaturity(0)
|
|
|
|
// Test rejecting the block if its parents are missing
|
|
orphanBlockFile := "blk_3B.dat"
|
|
loadedBlocks, err := LoadBlocks(filepath.Join("testdata/", orphanBlockFile))
|
|
if err != nil {
|
|
t.Fatalf("TestMaybeAcceptBlockErrors: "+
|
|
"Error loading file '%s': %s\n", orphanBlockFile, err)
|
|
}
|
|
block := loadedBlocks[0]
|
|
|
|
err = dag.maybeAcceptBlock(block, BFNone)
|
|
if err == nil {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block if its parents are missing: "+
|
|
"Expected: %s, got: <nil>", ErrParentBlockUnknown)
|
|
}
|
|
var ruleErr RuleError
|
|
if ok := errors.As(err, &ruleErr); !ok {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block if its parents are missing: "+
|
|
"Expected RuleError but got %s", err)
|
|
} else if ruleErr.ErrorCode != ErrParentBlockUnknown {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block if its parents are missing: "+
|
|
"Unexpected error code. Want: %s, got: %s", ErrParentBlockUnknown, ruleErr.ErrorCode)
|
|
}
|
|
|
|
// Test rejecting the block if its parents are invalid
|
|
blocksFile := "blk_0_to_4.dat"
|
|
blocks, err := LoadBlocks(filepath.Join("testdata/", blocksFile))
|
|
if err != nil {
|
|
t.Fatalf("TestMaybeAcceptBlockErrors: "+
|
|
"Error loading file '%s': %s\n", blocksFile, err)
|
|
}
|
|
|
|
// Add a valid block and mark it as invalid
|
|
block1 := blocks[1]
|
|
isOrphan, isDelayed, err := dag.ProcessBlock(block1, BFNone)
|
|
if err != nil {
|
|
t.Fatalf("TestMaybeAcceptBlockErrors: Valid block unexpectedly returned an error: %s", err)
|
|
}
|
|
if isDelayed {
|
|
t.Fatalf("TestMaybeAcceptBlockErrors: block 1 is too far in the future")
|
|
}
|
|
if isOrphan {
|
|
t.Fatalf("TestMaybeAcceptBlockErrors: incorrectly returned block 1 is an orphan")
|
|
}
|
|
blockNode1 := dag.index.LookupNode(block1.Hash())
|
|
dag.index.SetStatusFlags(blockNode1, statusValidateFailed)
|
|
|
|
block2 := blocks[2]
|
|
err = dag.maybeAcceptBlock(block2, BFNone)
|
|
if err == nil {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block if its parents are invalid: "+
|
|
"Expected: %s, got: <nil>", ErrInvalidAncestorBlock)
|
|
}
|
|
if ok := errors.As(err, &ruleErr); !ok {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block if its parents are invalid: "+
|
|
"Expected RuleError but got %s", err)
|
|
} else if ruleErr.ErrorCode != ErrInvalidAncestorBlock {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block if its parents are invalid: "+
|
|
"Unexpected error. Want: %s, got: %s", ErrInvalidAncestorBlock, ruleErr.ErrorCode)
|
|
}
|
|
|
|
// Set block1's status back to valid for next tests
|
|
dag.index.UnsetStatusFlags(blockNode1, statusValidateFailed)
|
|
|
|
// Test rejecting the block due to bad context
|
|
originalBits := block2.MsgBlock().Header.Bits
|
|
block2.MsgBlock().Header.Bits = 0
|
|
err = dag.maybeAcceptBlock(block2, BFNone)
|
|
if err == nil {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block due to bad context: "+
|
|
"Expected: %s, got: <nil>", ErrUnexpectedDifficulty)
|
|
}
|
|
if ok := errors.As(err, &ruleErr); !ok {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block due to bad context: "+
|
|
"Expected RuleError but got %s", err)
|
|
} else if ruleErr.ErrorCode != ErrUnexpectedDifficulty {
|
|
t.Errorf("TestMaybeAcceptBlockErrors: rejecting the block due to bad context: "+
|
|
"Unexpected error. Want: %s, got: %s", ErrUnexpectedDifficulty, ruleErr.ErrorCode)
|
|
}
|
|
|
|
// Set block2's bits back to valid for next tests
|
|
block2.MsgBlock().Header.Bits = originalBits
|
|
}
|