dcrd/blockchain/error_test.go
Dave Collins 46b081c96d
blockchain: Optimize reorg to use known status.
This optimizes the chain reorganization logic by making use of the
block index status flags for known valid and invalid blocks.

In particular, validation is now skipped for blocks that are either
already known valid or invalid.  When validating blocks, the result is
stored into the block index for the block accordingly, and in the case
of blocks that fail validation, all of the descendants of the invalid
block are marked as having an invalid ancestor.

It also introduces a new error named ErrKnownInvalidBlock which is
returned in the case a forced reorg is attempted to an invalid block and
adds a test to ensure it works as intended.
2018-07-26 15:20:28 -05:00

162 lines
5.8 KiB
Go

// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2015-2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockchain
import (
"testing"
)
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
func TestErrorCodeStringer(t *testing.T) {
tests := []struct {
in ErrorCode
want string
}{
{ErrDuplicateBlock, "ErrDuplicateBlock"},
{ErrMissingParent, "ErrMissingParent"},
{ErrBlockTooBig, "ErrBlockTooBig"},
{ErrWrongBlockSize, "ErrWrongBlockSize"},
{ErrBlockVersionTooOld, "ErrBlockVersionTooOld"},
{ErrBadStakeVersion, "ErrBadStakeVersion"},
{ErrInvalidTime, "ErrInvalidTime"},
{ErrTimeTooOld, "ErrTimeTooOld"},
{ErrTimeTooNew, "ErrTimeTooNew"},
{ErrDifficultyTooLow, "ErrDifficultyTooLow"},
{ErrUnexpectedDifficulty, "ErrUnexpectedDifficulty"},
{ErrHighHash, "ErrHighHash"},
{ErrBadMerkleRoot, "ErrBadMerkleRoot"},
{ErrBadCheckpoint, "ErrBadCheckpoint"},
{ErrForkTooOld, "ErrForkTooOld"},
{ErrCheckpointTimeTooOld, "ErrCheckpointTimeTooOld"},
{ErrNoTransactions, "ErrNoTransactions"},
{ErrTooManyTransactions, "ErrTooManyTransactions"},
{ErrNoTxInputs, "ErrNoTxInputs"},
{ErrNoTxOutputs, "ErrNoTxOutputs"},
{ErrTxTooBig, "ErrTxTooBig"},
{ErrBadTxOutValue, "ErrBadTxOutValue"},
{ErrDuplicateTxInputs, "ErrDuplicateTxInputs"},
{ErrBadTxInput, "ErrBadTxInput"},
{ErrMissingTxOut, "ErrMissingTxOut"},
{ErrUnfinalizedTx, "ErrUnfinalizedTx"},
{ErrDuplicateTx, "ErrDuplicateTx"},
{ErrOverwriteTx, "ErrOverwriteTx"},
{ErrImmatureSpend, "ErrImmatureSpend"},
{ErrSpendTooHigh, "ErrSpendTooHigh"},
{ErrBadFees, "ErrBadFees"},
{ErrTooManySigOps, "ErrTooManySigOps"},
{ErrFirstTxNotCoinbase, "ErrFirstTxNotCoinbase"},
{ErrCoinbaseHeight, "ErrCoinbaseHeight"},
{ErrMultipleCoinbases, "ErrMultipleCoinbases"},
{ErrStakeTxInRegularTree, "ErrStakeTxInRegularTree"},
{ErrRegTxInStakeTree, "ErrRegTxInStakeTree"},
{ErrBadCoinbaseScriptLen, "ErrBadCoinbaseScriptLen"},
{ErrBadCoinbaseValue, "ErrBadCoinbaseValue"},
{ErrBadCoinbaseOutpoint, "ErrBadCoinbaseOutpoint"},
{ErrBadCoinbaseFraudProof, "ErrBadCoinbaseFraudProof"},
{ErrBadCoinbaseAmountIn, "ErrBadCoinbaseAmountIn"},
{ErrBadStakebaseAmountIn, "ErrBadStakebaseAmountIn"},
{ErrBadStakebaseScriptLen, "ErrBadStakebaseScriptLen"},
{ErrBadStakebaseScrVal, "ErrBadStakebaseScrVal"},
{ErrScriptMalformed, "ErrScriptMalformed"},
{ErrScriptValidation, "ErrScriptValidation"},
{ErrNotEnoughStake, "ErrNotEnoughStake"},
{ErrStakeBelowMinimum, "ErrStakeBelowMinimum"},
{ErrNonstandardStakeTx, "ErrNonstandardStakeTx"},
{ErrNotEnoughVotes, "ErrNotEnoughVotes"},
{ErrTooManyVotes, "ErrTooManyVotes"},
{ErrFreshStakeMismatch, "ErrFreshStakeMismatch"},
{ErrTooManySStxs, "ErrTooManySStxs"},
{ErrInvalidEarlyStakeTx, "ErrInvalidEarlyStakeTx"},
{ErrTicketUnavailable, "ErrTicketUnavailable"},
{ErrVotesOnWrongBlock, "ErrVotesOnWrongBlock"},
{ErrVotesMismatch, "ErrVotesMismatch"},
{ErrIncongruentVotebit, "ErrIncongruentVotebit"},
{ErrInvalidSSRtx, "ErrInvalidSSRtx"},
{ErrRevocationsMismatch, "ErrRevocationsMismatch"},
{ErrTooManyRevocations, "ErrTooManyRevocations"},
{ErrSStxCommitment, "ErrSStxCommitment"},
{ErrInvalidSSGenInput, "ErrInvalidSSGenInput"},
{ErrSSGenPayeeNum, "ErrSSGenPayeeNum"},
{ErrSSGenPayeeOuts, "ErrSSGenPayeeOuts"},
{ErrSSGenSubsidy, "ErrSSGenSubsidy"},
{ErrSStxInImmature, "ErrSStxInImmature"},
{ErrSStxInScrType, "ErrSStxInScrType"},
{ErrInvalidSSRtxInput, "ErrInvalidSSRtxInput"},
{ErrSSRtxPayeesMismatch, "ErrSSRtxPayeesMismatch"},
{ErrSSRtxPayees, "ErrSSRtxPayees"},
{ErrTxSStxOutSpend, "ErrTxSStxOutSpend"},
{ErrRegTxCreateStakeOut, "ErrRegTxCreateStakeOut"},
{ErrInvalidFinalState, "ErrInvalidFinalState"},
{ErrPoolSize, "ErrPoolSize"},
{ErrForceReorgWrongChain, "ErrForceReorgWrongChain"},
{ErrForceReorgMissingChild, "ErrForceReorgMissingChild"},
{ErrBadStakebaseValue, "ErrBadStakebaseValue"},
{ErrDiscordantTxTree, "ErrDiscordantTxTree"},
{ErrStakeFees, "ErrStakeFees"},
{ErrNoStakeTx, "ErrNoStakeTx"},
{ErrBadBlockHeight, "ErrBadBlockHeight"},
{ErrBlockOneTx, "ErrBlockOneTx"},
{ErrBlockOneInputs, "ErrBlockOneInputs"},
{ErrBlockOneOutputs, "ErrBlockOneOutputs"},
{ErrNoTax, "ErrNoTax"},
{ErrExpiredTx, "ErrExpiredTx"},
{ErrExpiryTxSpentEarly, "ErrExpiryTxSpentEarly"},
{ErrFraudAmountIn, "ErrFraudAmountIn"},
{ErrFraudBlockHeight, "ErrFraudBlockHeight"},
{ErrFraudBlockIndex, "ErrFraudBlockIndex"},
{ErrZeroValueOutputSpend, "ErrZeroValueOutputSpend"},
{ErrInvalidEarlyVoteBits, "ErrInvalidEarlyVoteBits"},
{ErrInvalidEarlyFinalState, "ErrInvalidEarlyFinalState"},
{ErrKnownInvalidBlock, "ErrKnownInvalidBlock"},
{ErrInvalidAncestorBlock, "ErrInvalidAncestorBlock"},
{ErrInvalidTemplateParent, "ErrInvalidTemplateParent"},
{0xffff, "Unknown ErrorCode (65535)"},
}
// Detect additional error codes that don't have the stringer added.
if len(tests)-1 != int(numErrorCodes) {
t.Errorf("It appears an error code was added without adding an " +
"associated stringer test")
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.String()
if result != test.want {
t.Errorf("String #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}
// TestRuleError tests the error output for the RuleError type.
func TestRuleError(t *testing.T) {
tests := []struct {
in RuleError
want string
}{
{
RuleError{Description: "duplicate block"},
"duplicate block",
},
{
RuleError{Description: "human-readable error"},
"human-readable error",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}