dcrd/blockchain/error_test.go
Dave Collins 42bc847479
multi: Cleanup and optimize tx input check code.
This optimizes and cleans up significant portions of the
CheckTransactionInputs function to avoid a lot extra allocations, remove
redundant checks, better document its semantics, and make the code
easier to reason about.  In addition, it renames a lot of the error
constants involved in said function to use the ticket/vote/revocation
terminology and improve their readability.  Even though several of the
checks have been rearranged for efficiency and readability purposes, all
consensus semantics have been retained.

One of the primary changes is to reduce the reliance on the stake package
for consensus validation.  Not only is more desirable to have the bulk
of the validation related to the blockchain in the blockchain package,
but it also allows the code to be more specific, which enables better
optimization opportunities as well as eliminates the need for a lot of
redundant checks that are simply unnecessary.

The stake identification functions are also part of consensus and have
not been modified, however, the actual validation related to all of
their inputs, such as ensuring commitments are observed, are now in the
blockchain package itself.

Since the only thing using the related verification functions is now
done in blockchain, this also removes the VerifyStakingPkhsAndAmounts,
VerifySStxAmounts, and related tests from the stake package.

Another significant change is the addition of new functions for
efficiently and specifically identifying the form of the scripts
required by stake transactions in order to reduce the dependence on the
standard code in txscript.  Standard code should _NOT_ be used in
consensus code as the two are not the same thing.

It should be noted that these changes break compatibility with the
current v1 blockchain and stake modules, so they will need a major
version bump prior to the next release.

Finally, all tests in the repository have been updated for the error
name changes as well as change in expected error in some cases due to
the reordering of the validation checks.
2018-09-26 09:23:03 -05:00

164 lines
5.9 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"},
{ErrTicketCommitment, "ErrTicketCommitment"},
{ErrInvalidVoteInput, "ErrInvalidVoteInput"},
{ErrBadNumPayees, "ErrBadNumPayees"},
{ErrBadPayeeScriptVersion, "ErrBadPayeeScriptVersion"},
{ErrBadPayeeScriptType, "ErrBadPayeeScriptType"},
{ErrMismatchedPayeeHash, "ErrMismatchedPayeeHash"},
{ErrBadPayeeValue, "ErrBadPayeeValue"},
{ErrSSGenSubsidy, "ErrSSGenSubsidy"},
{ErrImmatureTicketSpend, "ErrImmatureTicketSpend"},
{ErrTicketInputScript, "ErrTicketInputScript"},
{ErrInvalidRevokeInput, "ErrInvalidRevokeInput"},
{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
}
}
}