This is based on https://proposals.decred.org/proposals/c96290a but was modified in order to deal with realities that were unknown at the time of the specification draft. It is large and could not really be broken apart due to the pervasive use of the isTreasuryEnabled flag. It was primarily authored by * Marco Peereboom <marco@peereboom.us> * Dave Collins <davec@conformal.com> * Matheus Degiovani <opensource@matheusd.com> With additional contributions from * Donald Adu-Poku <donald.adu@gmail.com> * Jamie Holdstock <jholdstock@decred.org> Major changes: * Add decentralized treasury agenda, as specified in DCP0006, to all supported nets. * Add functions to determine if the decentralized treasury agenda is active at given block. * Add new opcode OP_TADD that is a nop in txscript but is used to tag scripts that credit the treasury account. This opcode is overloaded for treasurybase and for normal transactions. * Add new opcode OP_TSPEND that is a nop in txscript but is used to tag scripts that debit the treasury account. * Add new opcode OP_TGEN that is a nop in txscript but is used to tag P2PKH and P2SH outputs in a TSpend transaction. * Add functions that detect if a transaction is a valid TAdd, TSpend or treasurybase transaction. * Add error codes that return specific treasurybase/TAdd/TSpend consensus violations. * Modify countSpentOutputs to deal with treasury opcodes accordingly. * Modify indexBlock to skip treasury transactions that do not have inputs. * Add IsTreasuryEnabled call to ChainQueryer interface. * Add treasury logger for debugging and logging the decentralized treasury subsystem. * Add IsTreasuryActive flag to BlockConnectedNtfnsData and BlockDisconnectedNtfnsData. * Modify OP_SSGEN to allow an optional output that contains votes for a TSpend transaction hash. * Add function that returns TSpend votes from an SSGen transaction. * Modify CalcStakeVoteSubsidy so that treasurybase, unlike coinbase, is always awarded the full percentage of the assigned block reward. * Add helper functions to do all TSpend math so that callers don't roll their own. * Modify IsCoinBaseTx to not mistake a TSpend transaction as a coinbase. * Add checkTreasuryBase function that verifies that a treasurybase is properly constructed and pays the right amount to the treasury account. * Add functions to calculate treasury balance for the provided block hash/node. * Add function that verifies if a TSpend has a valid signature. * Add functions to determine if a TSpend is not overspending. * Add function to determine if a TSpend has been mined on the provided chain. * Add functions that count and verifies treasury spend votes. * Modify connectTransaction and disconnectTransactions to deal with the various treasury transactions. * Split CheckTransactionSanity in two functions checkTransactionSanityContextFree and checkTransactionSanityContextual. This is done in order to keep the decentralized treasury, which is always contextual, from infecting the context free checks. * Modify checkTransactionSanityContextual to recognize and verify treasury transactions. * Modify CheckTransactionSanity to deal with treasury transactions. * Split checkBlockSanity in two functions checkBlockSanityContextFree and checkBlockSanityContextual. This is done in order to keep the decentralized treasury, which is always contextual, from infecting the context free checks. * Modify checkBlockSanityContextual to enforce treasurybase and TAdd consensus checks. * Modify checkBlockPositional by unindenting it and adding TSpend consensus enforcement. * Modify checkCoinbaseUniqueHeightWithAddress to deal with the removal of the project subsidy from output 0. * Add checkCoinbaseUniqueHeightWithTreasuryBase that verifies coinbase and treasurybase in the provided block. * Unindent checkBlockContext. * Modify checkTicketRedeemerCommitments and checkVoteInputs to deal with potential tspend votes. * Modify CheckTransactionInputs to skip treasurybase transactions. * Modify CheckTransactionInputs to deal with TSpend transactions. Ensure the provided Pi key is valid and that the signature is valid for the transaction. Ensure that treasury TAdd and TSpend transaction utxo can only be spent after coinbase maturity. * Modify CountSigOps to deal with treasury transactions. * Modify CountP2SHSigOps to deal with treasury transactions. * Modify getStakeTreeFees to skip treasury transactions. Modify totalOutputs to subtract ValueIn 0 for TSpend and treasurybase transactions. * Modify checkTransactionsAndConnect to deal with modified amounts. * Add tspendChecks function that verifies an entire TSpend transaction validity at the point of the provided block. It ensures a TSpend is on a TVI. It ensures the TSpend is in the valid window. It verifies that a TSpend In and Out amounts match. It ensures a TSpend has the ValueIn amount encoded in the OP_RETURN in Out 0. It ensures a TSpend has not been mined before on this chain. It ensures a TSpend has the requisite votes. It ensures a TSpend is not overspending. * Modify checkConnectBlock to call checkTreasuryBase and tspendChecks when treasury agenda is active. * Add two tables to the database. Table "treasury" records the balance as of this block and balance changes that occurred in this block which will become active in CoinbaseMaturity blocks. Table "tspend" records all block hashes where a TSpend has been mined this is to detect forks and prevent a Tspend from being mined more than once. * Modify handleBlockchainNotification to communicate if the treasury agenda is active and skip treasurybase transaction when needed. * Add various Treasury parameters to chaincfg params. * Add hardcoded Tspend signatures in dcr_tmux_simnet_setup.sh. * Add notifytspend and stoptspend calls to the RPC server. notifytspend notifies the mempool when a TSpend transaction arrives. * Modify commit filters V2 to recognize TAdd and TSpend transactions. It was possible to modify V2 instead of introducing V3 because nothing changes from the viewpoint of the wallet and treasury opcodes are disallowed prior to agenda activation. * Modify AddMemPoolTransaction to skip TSpend transactions that would throw the fee estimator off. * Add IsTreasuryAgendaActive, OnTSpendReceived and TSpendMinedOnAncestor to mempool.Config in order to reject/accept TSpends in the mempool. * Modify checkPoolDoubleSpend to ignore treasurybase. * Modify mempool.maybeAcceptTransaction to enforce treasury standardness rules. Don't allow TSpend transactions prior to stake validation height. Skip treasurybase and tspend transactions in the orphan test. Ensure a tspend is in a valid window. Ensure not more than 7 TSpends are active in the mempool. Ensure TSpend has a well-known Pi key. Ensure The provided Pi key was used to sign the transaction. Ensure TSpend was not mined in an ancestor block. Notify subscribers that a valid TSpend was received. * Add standardCoinbaseOpReturn and standardTreasurybaseOpReturn to create an OP_RETURN followed by a data push that little endian encodes the height of the block. Then there are a number of random bytes to ensure that the transaction hash is always random. * Modify createCoinbaseTx to create a coinbase that is valid when treasury is enabled or not. Additionally, alter the transaction version if treasury is enabled. * Add createTreasuryBaseTx that creates a standard treasurybase. * Modify maybeInsertStakeTx to recognize treasurybase and TSpend transactions. * Modify handleTooFewVoters to call createTreasuryBaseTx when the treasury agenda is active. Skip copying treasurybase. * Modify NewBlockTemplate to recognize and deal with treasury transactions. Skip TSpend transaction if block is not a TVI. Skip TSpend transaction if it is not in the proper window. Skip TSpend transaction if a TSpend does not have enough yes votes. Skip TSpend transaction if it overspends the treasury account. Skip TAdd if there are more than 20 TAdds in the block. Create treasurybase if required. Insert valid TAdd/TSpend transactions into stake tree. * Add TreasuryBalance and IsTreasuryAgendaActive to rpcserver Chain interface. * Add gettreasurybalance, sendfromtreasury and sendtotreasury calls to RPC server. * Add notifytspend and stopnotifytspend to RPC websocket commands. * Add simnet miner to generate large number of blocks during rpctests without triggering PoW difficulty increases. This is used to verify various treasury and tspend conditions during CI/CT. * Modify RPC voting wallet to also vote on TSpends. * Add json tests to verify all new opcodes and corner cases in the script engine. * Modify isStakeOpcode to recognize treasury opcodes. * Modify countSigOpsV0 to count TSpends. * Modify handleStakeOutSign to deal with TSpends. * Modify SignTxOutput to recognize TSpends. * Add TSpendSignatureScript that signs a TSpend transaction. * Add TreasuryAddTy and TreasurySpendTy types to the standard scripts. * Add isTreasuryAddScript and isTreasurySpendScript functions that recognize a form of TAdd and TSpend transactions. * Modify ExtractPkScriptAddrs to deal with TAdd and TSpend outputs. * Add TxVersionSeqLock = 2 and TxVersionTreasury = 3 to wire. This is used to discriminate between treasury and non-treasury scripts. * Rig up all functions that need the isTreasuryEnabledflag directly or indirectly. * Shuffle various functions around and export them when they were needed to be called from other packages. * Added and modified numerous tests to verify (hopefully) all corner cases that the decentralized treasury agenda has added.
932 lines
26 KiB
Go
932 lines
26 KiB
Go
// Copyright (c) 2013-2017 The btcsuite developers
|
|
// Copyright (c) 2015-2020 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package txscript
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
"github.com/decred/dcrd/dcrutil/v3"
|
|
"github.com/decred/dcrd/wire"
|
|
)
|
|
|
|
var (
|
|
// tokenRE is a regular expression used to parse tokens from short form
|
|
// scripts. It splits on repeated tokens and spaces. Repeated tokens are
|
|
// denoted by being wrapped in angular brackets followed by a suffix which
|
|
// consists of a number inside braces.
|
|
tokenRE = regexp.MustCompile(`\<.+?\>\{[0-9]+\}|[^\s]+`)
|
|
|
|
// repTokenRE is a regular expression used to parse short form scripts
|
|
// for a series of tokens repeated a specified number of times.
|
|
repTokenRE = regexp.MustCompile(`^\<(.+)\>\{([0-9]+)\}$`)
|
|
|
|
// repRawRE is a regular expression used to parse short form scripts
|
|
// for raw data that is to be repeated a specified number of times.
|
|
repRawRE = regexp.MustCompile(`^(0[xX][0-9a-fA-F]+)\{([0-9]+)\}$`)
|
|
|
|
// repQuoteRE is a regular expression used to parse short form scripts for
|
|
// quoted data that is to be repeated a specified number of times.
|
|
repQuoteRE = regexp.MustCompile(`^'(.*)'\{([0-9]+)\}$`)
|
|
)
|
|
|
|
// scriptTestName returns a descriptive test name for the given reference script
|
|
// test data.
|
|
func scriptTestName(test []string) (string, error) {
|
|
// The test must consist of at least a signature script, public key script,
|
|
// verification flags, and expected error. Finally, it may optionally
|
|
// contain a comment.
|
|
if len(test) < 4 || len(test) > 5 {
|
|
return "", fmt.Errorf("invalid test length %d", len(test))
|
|
}
|
|
|
|
// Use the comment for the test name if one is specified, otherwise,
|
|
// construct the name based on the signature script, public key script,
|
|
// and flags.
|
|
var name string
|
|
if len(test) == 5 {
|
|
name = fmt.Sprintf("test (%s)", test[4])
|
|
} else {
|
|
name = fmt.Sprintf("test ([%s, %s, %s])", test[0], test[1],
|
|
test[2])
|
|
}
|
|
return name, nil
|
|
}
|
|
|
|
// parse hex string into a []byte.
|
|
func parseHex(tok string) ([]byte, error) {
|
|
if !strings.HasPrefix(tok, "0x") {
|
|
return nil, errors.New("not a hex number")
|
|
}
|
|
return hex.DecodeString(tok[2:])
|
|
}
|
|
|
|
// shortFormOps holds a map of opcode names to values for use in short form
|
|
// parsing. It is declared here so it only needs to be created once.
|
|
var shortFormOps map[string]byte
|
|
|
|
// parseShortForm parses a string as used in the reference tests into the
|
|
// script it came from.
|
|
//
|
|
// The format used for these tests is pretty simple if ad-hoc:
|
|
// - Opcodes other than the push opcodes and unknown are present as either
|
|
// OP_NAME or just NAME
|
|
// - Plain numbers are made into push operations
|
|
// - Numbers beginning with 0x are inserted into the []byte as-is (so 0x14 is
|
|
// OP_DATA_20)
|
|
// - Numbers beginning with 0x which have a suffix which consists of a number
|
|
// in braces (e.g. 0x6161{10}) repeat the raw bytes the specified number of
|
|
// times and are inserted as-is
|
|
// - Single quoted strings are pushed as data
|
|
// - Single quoted strings that have a suffix which consists of a number in
|
|
// braces (e.g. 'b'{10}) repeat the data the specified number of times and
|
|
// are pushed as a single data push
|
|
// - Tokens inside of angular brackets with a suffix which consists of a
|
|
// number in braces (e.g. <0 0 CHECKMULTSIG>{5}) is parsed as if the tokens
|
|
// inside the angular brackets were manually repeated the specified number
|
|
// of times
|
|
// - Anything else is an error
|
|
func parseShortForm(script string) ([]byte, error) {
|
|
// Only create the short form opcode map once.
|
|
if shortFormOps == nil {
|
|
ops := make(map[string]byte)
|
|
for opcodeName, opcodeValue := range OpcodeByName {
|
|
if strings.Contains(opcodeName, "OP_UNKNOWN") {
|
|
continue
|
|
}
|
|
ops[opcodeName] = opcodeValue
|
|
|
|
// The opcodes named OP_# can't have the OP_ prefix
|
|
// stripped or they would conflict with the plain
|
|
// numbers. Also, since OP_FALSE and OP_TRUE are
|
|
// aliases for the OP_0, and OP_1, respectively, they
|
|
// have the same value, so detect those by name and
|
|
// allow them.
|
|
if (opcodeName == "OP_FALSE" || opcodeName == "OP_TRUE") ||
|
|
(opcodeValue != OP_0 && (opcodeValue < OP_1 ||
|
|
opcodeValue > OP_16)) {
|
|
|
|
ops[strings.TrimPrefix(opcodeName, "OP_")] = opcodeValue
|
|
}
|
|
}
|
|
shortFormOps = ops
|
|
}
|
|
|
|
builder := NewScriptBuilder()
|
|
|
|
var handleToken func(tok string) error
|
|
handleToken = func(tok string) error {
|
|
// Multiple repeated tokens.
|
|
if m := repTokenRE.FindStringSubmatch(tok); m != nil {
|
|
count, err := strconv.ParseInt(m[2], 10, 32)
|
|
if err != nil {
|
|
return fmt.Errorf("bad token %q", tok)
|
|
}
|
|
tokens := tokenRE.FindAllStringSubmatch(m[1], -1)
|
|
for i := 0; i < int(count); i++ {
|
|
for _, t := range tokens {
|
|
if err := handleToken(t[0]); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Plain number.
|
|
if num, err := strconv.ParseInt(tok, 10, 64); err == nil {
|
|
builder.AddInt64(num)
|
|
return nil
|
|
}
|
|
|
|
// Raw data.
|
|
if bts, err := parseHex(tok); err == nil {
|
|
// Concatenate the bytes manually since the test code
|
|
// intentionally creates scripts that are too large and
|
|
// would cause the builder to error otherwise.
|
|
if builder.err == nil {
|
|
builder.script = append(builder.script, bts...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Repeated raw bytes.
|
|
if m := repRawRE.FindStringSubmatch(tok); m != nil {
|
|
bts, err := parseHex(m[1])
|
|
if err != nil {
|
|
return fmt.Errorf("bad token %q", tok)
|
|
}
|
|
count, err := strconv.ParseInt(m[2], 10, 32)
|
|
if err != nil {
|
|
return fmt.Errorf("bad token %q", tok)
|
|
}
|
|
|
|
// Concatenate the bytes manually since the test code
|
|
// intentionally creates scripts that are too large and
|
|
// would cause the builder to error otherwise.
|
|
bts = bytes.Repeat(bts, int(count))
|
|
if builder.err == nil {
|
|
builder.script = append(builder.script, bts...)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Quoted data.
|
|
if len(tok) >= 2 && tok[0] == '\'' && tok[len(tok)-1] == '\'' {
|
|
builder.AddFullData([]byte(tok[1 : len(tok)-1]))
|
|
return nil
|
|
}
|
|
|
|
// Repeated quoted data.
|
|
if m := repQuoteRE.FindStringSubmatch(tok); m != nil {
|
|
count, err := strconv.ParseInt(m[2], 10, 32)
|
|
if err != nil {
|
|
return fmt.Errorf("bad token %q", tok)
|
|
}
|
|
data := strings.Repeat(m[1], int(count))
|
|
builder.AddFullData([]byte(data))
|
|
return nil
|
|
}
|
|
|
|
// Named opcode.
|
|
if opcode, ok := shortFormOps[tok]; ok {
|
|
builder.AddOp(opcode)
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("bad token %q", tok)
|
|
}
|
|
|
|
for _, tokens := range tokenRE.FindAllStringSubmatch(script, -1) {
|
|
if err := handleToken(tokens[0]); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return builder.Script()
|
|
}
|
|
|
|
// parseScriptFlags parses the provided flags string from the format used in the
|
|
// reference tests into ScriptFlags suitable for use in the script engine.
|
|
func parseScriptFlags(flagStr string) (ScriptFlags, error) {
|
|
var flags ScriptFlags
|
|
|
|
sFlags := strings.Split(flagStr, ",")
|
|
for _, flag := range sFlags {
|
|
switch flag {
|
|
case "":
|
|
// Nothing.
|
|
case "CHECKLOCKTIMEVERIFY":
|
|
flags |= ScriptVerifyCheckLockTimeVerify
|
|
case "CHECKSEQUENCEVERIFY":
|
|
flags |= ScriptVerifyCheckSequenceVerify
|
|
case "CLEANSTACK":
|
|
flags |= ScriptVerifyCleanStack
|
|
case "DISCOURAGE_UPGRADABLE_NOPS":
|
|
flags |= ScriptDiscourageUpgradableNops
|
|
case "NONE":
|
|
// Nothing.
|
|
case "SIGPUSHONLY":
|
|
flags |= ScriptVerifySigPushOnly
|
|
case "SHA256":
|
|
flags |= ScriptVerifySHA256
|
|
case "TREASURY":
|
|
flags |= ScriptVerifyTreasury
|
|
default:
|
|
return flags, fmt.Errorf("invalid flag: %s", flag)
|
|
}
|
|
}
|
|
return flags, nil
|
|
}
|
|
|
|
// parseExpectedResult parses the provided expected result string into allowed
|
|
// script error kinds. An error is returned if the expected result string is
|
|
// not supported.
|
|
func parseExpectedResult(expected string) ([]ErrorKind, error) {
|
|
switch expected {
|
|
case "OK":
|
|
return nil, nil
|
|
case "ERR_EARLY_RETURN":
|
|
return []ErrorKind{ErrEarlyReturn}, nil
|
|
case "ERR_EMPTY_STACK":
|
|
return []ErrorKind{ErrEmptyStack}, nil
|
|
case "ERR_EVAL_FALSE":
|
|
return []ErrorKind{ErrEvalFalse}, nil
|
|
case "ERR_SCRIPT_SIZE":
|
|
return []ErrorKind{ErrScriptTooBig}, nil
|
|
case "ERR_PUSH_SIZE":
|
|
return []ErrorKind{ErrElementTooBig}, nil
|
|
case "ERR_OP_COUNT":
|
|
return []ErrorKind{ErrTooManyOperations}, nil
|
|
case "ERR_STACK_SIZE":
|
|
return []ErrorKind{ErrStackOverflow}, nil
|
|
case "ERR_PUBKEY_COUNT":
|
|
return []ErrorKind{ErrInvalidPubKeyCount}, nil
|
|
case "ERR_SIG_COUNT":
|
|
return []ErrorKind{ErrInvalidSignatureCount}, nil
|
|
case "ERR_OUT_OF_RANGE":
|
|
return []ErrorKind{ErrNumOutOfRange}, nil
|
|
case "ERR_VERIFY":
|
|
return []ErrorKind{ErrVerify}, nil
|
|
case "ERR_EQUAL_VERIFY":
|
|
return []ErrorKind{ErrEqualVerify}, nil
|
|
case "ERR_DISABLED_OPCODE":
|
|
return []ErrorKind{ErrDisabledOpcode}, nil
|
|
case "ERR_RESERVED_OPCODE":
|
|
return []ErrorKind{ErrReservedOpcode}, nil
|
|
case "ERR_P2SH_STAKE_OPCODES":
|
|
return []ErrorKind{ErrP2SHStakeOpCodes}, nil
|
|
case "ERR_MALFORMED_PUSH":
|
|
return []ErrorKind{ErrMalformedPush}, nil
|
|
case "ERR_INVALID_STACK_OPERATION", "ERR_INVALID_ALTSTACK_OPERATION":
|
|
return []ErrorKind{ErrInvalidStackOperation}, nil
|
|
case "ERR_UNBALANCED_CONDITIONAL":
|
|
return []ErrorKind{ErrUnbalancedConditional}, nil
|
|
case "ERR_NEGATIVE_SUBSTR_INDEX":
|
|
return []ErrorKind{ErrNegativeSubstrIdx}, nil
|
|
case "ERR_OVERFLOW_SUBSTR_INDEX":
|
|
return []ErrorKind{ErrOverflowSubstrIdx}, nil
|
|
case "ERR_NEGATIVE_ROTATION":
|
|
return []ErrorKind{ErrNegativeRotation}, nil
|
|
case "ERR_OVERFLOW_ROTATION":
|
|
return []ErrorKind{ErrOverflowRotation}, nil
|
|
case "ERR_DIVIDE_BY_ZERO":
|
|
return []ErrorKind{ErrDivideByZero}, nil
|
|
case "ERR_NEGATIVE_SHIFT":
|
|
return []ErrorKind{ErrNegativeShift}, nil
|
|
case "ERR_OVERFLOW_SHIFT":
|
|
return []ErrorKind{ErrOverflowShift}, nil
|
|
case "ERR_MINIMAL_DATA":
|
|
return []ErrorKind{ErrMinimalData}, nil
|
|
case "ERR_SIG_HASH_TYPE":
|
|
return []ErrorKind{ErrInvalidSigHashType}, nil
|
|
case "ERR_SIG_TOO_SHORT":
|
|
return []ErrorKind{ErrSigTooShort}, nil
|
|
case "ERR_SIG_TOO_LONG":
|
|
return []ErrorKind{ErrSigTooLong}, nil
|
|
case "ERR_SIG_INVALID_SEQ_ID":
|
|
return []ErrorKind{ErrSigInvalidSeqID}, nil
|
|
case "ERR_SIG_INVALID_DATA_LEN":
|
|
return []ErrorKind{ErrSigInvalidDataLen}, nil
|
|
case "ERR_SIG_MISSING_S_TYPE_ID":
|
|
return []ErrorKind{ErrSigMissingSTypeID}, nil
|
|
case "ERR_SIG_MISSING_S_LEN":
|
|
return []ErrorKind{ErrSigMissingSLen}, nil
|
|
case "ERR_SIG_INVALID_S_LEN":
|
|
return []ErrorKind{ErrSigInvalidSLen}, nil
|
|
case "ERR_SIG_INVALID_R_INT_ID":
|
|
return []ErrorKind{ErrSigInvalidRIntID}, nil
|
|
case "ERR_SIG_ZERO_R_LEN":
|
|
return []ErrorKind{ErrSigZeroRLen}, nil
|
|
case "ERR_SIG_NEGATIVE_R":
|
|
return []ErrorKind{ErrSigNegativeR}, nil
|
|
case "ERR_SIG_TOO_MUCH_R_PADDING":
|
|
return []ErrorKind{ErrSigTooMuchRPadding}, nil
|
|
case "ERR_SIG_INVALID_S_INT_ID":
|
|
return []ErrorKind{ErrSigInvalidSIntID}, nil
|
|
case "ERR_SIG_ZERO_S_LEN":
|
|
return []ErrorKind{ErrSigZeroSLen}, nil
|
|
case "ERR_SIG_NEGATIVE_S":
|
|
return []ErrorKind{ErrSigNegativeS}, nil
|
|
case "ERR_SIG_TOO_MUCH_S_PADDING":
|
|
return []ErrorKind{ErrSigTooMuchSPadding}, nil
|
|
case "ERR_SIG_HIGH_S":
|
|
return []ErrorKind{ErrSigHighS}, nil
|
|
case "ERR_SIG_PUSHONLY":
|
|
return []ErrorKind{ErrNotPushOnly}, nil
|
|
case "ERR_PUBKEY_TYPE":
|
|
return []ErrorKind{ErrPubKeyType}, nil
|
|
case "ERR_CLEAN_STACK":
|
|
return []ErrorKind{ErrCleanStack}, nil
|
|
case "ERR_DISCOURAGE_UPGRADABLE_NOPS":
|
|
return []ErrorKind{ErrDiscourageUpgradableNOPs}, nil
|
|
case "ERR_NEGATIVE_LOCKTIME":
|
|
return []ErrorKind{ErrNegativeLockTime}, nil
|
|
case "ERR_UNSATISFIED_LOCKTIME":
|
|
return []ErrorKind{ErrUnsatisfiedLockTime}, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("unrecognized expected result in test data: %v",
|
|
expected)
|
|
}
|
|
|
|
// createSpendTx generates a basic spending transaction given the passed
|
|
// signature and public key scripts.
|
|
func createSpendingTx(sigScript, pkScript []byte) *wire.MsgTx {
|
|
coinbaseTx := wire.NewMsgTx()
|
|
|
|
outPoint := wire.NewOutPoint(&chainhash.Hash{}, ^uint32(0),
|
|
wire.TxTreeRegular)
|
|
txIn := wire.NewTxIn(outPoint, 0, []byte{OP_0, OP_0})
|
|
txOut := wire.NewTxOut(0, pkScript)
|
|
coinbaseTx.AddTxIn(txIn)
|
|
coinbaseTx.AddTxOut(txOut)
|
|
|
|
spendingTx := wire.NewMsgTx()
|
|
coinbaseTxHash := coinbaseTx.TxHash()
|
|
outPoint = wire.NewOutPoint(&coinbaseTxHash, 0, wire.TxTreeRegular)
|
|
txIn = wire.NewTxIn(outPoint, 0, sigScript)
|
|
txOut = wire.NewTxOut(0, nil)
|
|
|
|
spendingTx.AddTxIn(txIn)
|
|
spendingTx.AddTxOut(txOut)
|
|
|
|
return spendingTx
|
|
}
|
|
|
|
// testScripts ensures all of the passed script tests execute with the expected
|
|
// results with or without using a signature cache, as specified by the
|
|
// parameter.
|
|
func testScripts(t *testing.T, tests [][]string, useSigCache bool) {
|
|
// Create a signature cache to use only if requested.
|
|
var sigCache *SigCache
|
|
if useSigCache {
|
|
sigCache = NewSigCache(10)
|
|
}
|
|
|
|
// "Format is: [scriptSig, scriptPubKey, flags, expectedScriptError, ...
|
|
// comments]"
|
|
for i, test := range tests {
|
|
// Skip single line comments.
|
|
if len(test) == 1 {
|
|
continue
|
|
}
|
|
|
|
// Construct a name for the test based on the comment and test data.
|
|
name, err := scriptTestName(test)
|
|
if err != nil {
|
|
t.Errorf("TestScripts: invalid test #%d: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the signature script from the test fields.
|
|
scriptSig, err := parseShortForm(test[0])
|
|
if err != nil {
|
|
t.Errorf("%s: can't parse scriptSig; %v", name, err)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the public key script from the test fields.
|
|
scriptPubKey, err := parseShortForm(test[1])
|
|
if err != nil {
|
|
t.Errorf("%s: can't parse scriptPubkey; %v", name, err)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the script flags from the test fields.
|
|
flags, err := parseScriptFlags(test[2])
|
|
if err != nil {
|
|
t.Errorf("%s: %v", name, err)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the expected result from the test fields.
|
|
//
|
|
// Convert the expected result string into the allowed script errors.
|
|
// This allows txscript to be more fine grained with its errors than the
|
|
// reference test data by allowing some of the test data errors to map
|
|
// to more than one possibility.
|
|
resultStr := test[3]
|
|
allowErrorKinds, err := parseExpectedResult(resultStr)
|
|
if err != nil {
|
|
t.Errorf("%s: %v", name, err)
|
|
continue
|
|
}
|
|
|
|
// Generate a transaction pair such that one spends from the other and
|
|
// the provided signature and public key scripts are used, then create a
|
|
// new engine to execute the scripts.
|
|
tx := createSpendingTx(scriptSig, scriptPubKey)
|
|
vm, err := NewEngine(scriptPubKey, tx, 0, flags, 0, sigCache)
|
|
if err == nil {
|
|
err = vm.Execute()
|
|
}
|
|
|
|
// Ensure there were no errors when the expected result is OK.
|
|
if resultStr == "OK" {
|
|
if err != nil {
|
|
t.Errorf("%s failed to execute: %v", name, err)
|
|
}
|
|
continue
|
|
}
|
|
|
|
// At this point an error was expected so ensure the result of the
|
|
// execution matches it.
|
|
success := false
|
|
for _, kind := range allowErrorKinds {
|
|
if errors.Is(err, kind) {
|
|
success = true
|
|
break
|
|
}
|
|
}
|
|
if !success {
|
|
var serr Error
|
|
if errors.As(err, &serr) {
|
|
t.Errorf("%s: want error kinds %v, got %v", name,
|
|
allowErrorKinds, serr.Err)
|
|
continue
|
|
}
|
|
t.Errorf("%s: want error kinds %v, got err: %v (%T)", name,
|
|
allowErrorKinds, err, err)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestScripts ensures all of the tests in script_tests.json execute with the
|
|
// expected results as defined in the test data.
|
|
func TestScripts(t *testing.T) {
|
|
file, err := ioutil.ReadFile("data/script_tests.json")
|
|
if err != nil {
|
|
t.Fatalf("TestScripts: %v\n", err)
|
|
}
|
|
|
|
var tests [][]string
|
|
err = json.Unmarshal(file, &tests)
|
|
if err != nil {
|
|
t.Fatalf("TestScripts failed to unmarshal: %v", err)
|
|
}
|
|
|
|
// Run all script tests with and without the signature cache.
|
|
testScripts(t, tests, true)
|
|
testScripts(t, tests, false)
|
|
}
|
|
|
|
// testVecF64ToUint32 properly handles conversion of float64s read from the JSON
|
|
// test data to unsigned 32-bit integers. This is necessary because some of the
|
|
// test data uses -1 as a shortcut to mean max uint32 and direct conversion of a
|
|
// negative float to an unsigned int is implementation dependent and therefore
|
|
// doesn't result in the expected value on all platforms. This function works
|
|
// around that limitation by converting to a 32-bit signed integer first and
|
|
// then to a 32-bit unsigned integer which results in the expected behavior on
|
|
// all platforms.
|
|
func testVecF64ToUint32(f float64) uint32 {
|
|
return uint32(int32(f))
|
|
}
|
|
|
|
// TestTxInvalidTests ensures all of the tests in tx_invalid.json fail as
|
|
// expected.
|
|
func TestTxInvalidTests(t *testing.T) {
|
|
file, err := ioutil.ReadFile("data/tx_invalid.json")
|
|
if err != nil {
|
|
t.Errorf("TestTxInvalidTests: %v\n", err)
|
|
return
|
|
}
|
|
|
|
var tests [][]interface{}
|
|
err = json.Unmarshal(file, &tests)
|
|
if err != nil {
|
|
t.Errorf("TestTxInvalidTests couldn't Unmarshal: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// form is either:
|
|
// ["this is a comment "]
|
|
// or:
|
|
// [[[previous hash, previous index, previous scriptPubKey]...,]
|
|
// serializedTransaction, verifyFlags]
|
|
testloop:
|
|
for i, test := range tests {
|
|
inputs, ok := test[0].([]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
if len(test) != 3 {
|
|
t.Errorf("bad test (bad length) %d: %v", i, test)
|
|
continue
|
|
}
|
|
serializedhex, ok := test[1].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (arg 2 not string) %d: %v", i, test)
|
|
continue
|
|
}
|
|
serializedTx, err := hex.DecodeString(serializedhex)
|
|
if err != nil {
|
|
t.Errorf("bad test (arg 2 not hex %v) %d: %v", err, i,
|
|
test)
|
|
continue
|
|
}
|
|
|
|
tx, err := dcrutil.NewTxFromBytes(serializedTx)
|
|
if err != nil {
|
|
t.Errorf("bad test (arg 2 not msgtx %v) %d: %v", err,
|
|
i, test)
|
|
continue
|
|
}
|
|
|
|
verifyFlags, ok := test[2].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (arg 3 not string) %d: %v", i, test)
|
|
continue
|
|
}
|
|
|
|
flags, err := parseScriptFlags(verifyFlags)
|
|
if err != nil {
|
|
t.Errorf("bad test %d: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
prevOuts := make(map[wire.OutPoint][]byte)
|
|
for j, iinput := range inputs {
|
|
input, ok := iinput.([]interface{})
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input not array)"+
|
|
"%d: %v", j, i, test)
|
|
continue testloop
|
|
}
|
|
|
|
if len(input) != 3 {
|
|
t.Errorf("bad test (%dth input wrong length)"+
|
|
"%d: %v", j, i, test)
|
|
continue testloop
|
|
}
|
|
|
|
previoustx, ok := input[0].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input hash not string)"+
|
|
"%d: %v", j, i, test)
|
|
continue testloop
|
|
}
|
|
|
|
prevhash, err := chainhash.NewHashFromStr(previoustx)
|
|
if err != nil {
|
|
t.Errorf("bad test (%dth input hash not hash %v)"+
|
|
"%d: %v", j, err, i, test)
|
|
continue testloop
|
|
}
|
|
|
|
idxf, ok := input[1].(float64)
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input idx not number)"+
|
|
"%d: %v", j, i, test)
|
|
continue testloop
|
|
}
|
|
idx := testVecF64ToUint32(idxf)
|
|
|
|
oscript, ok := input[2].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input script not "+
|
|
"string) %d: %v", j, i, test)
|
|
continue testloop
|
|
}
|
|
|
|
script, err := parseShortForm(oscript)
|
|
if err != nil {
|
|
t.Errorf("bad test (%dth input script doesn't "+
|
|
"parse %v) %d: %v", j, err, i, test)
|
|
continue testloop
|
|
}
|
|
|
|
prevOuts[*wire.NewOutPoint(prevhash, idx, wire.TxTreeRegular)] = script
|
|
}
|
|
|
|
for k, txin := range tx.MsgTx().TxIn {
|
|
pkScript, ok := prevOuts[txin.PreviousOutPoint]
|
|
if !ok {
|
|
t.Errorf("bad test (missing %dth input) %d:%v",
|
|
k, i, test)
|
|
continue testloop
|
|
}
|
|
// These are meant to fail, so as soon as the first
|
|
// input fails the transaction has failed. (some of the
|
|
// test txns have good inputs, too..
|
|
vm, err := NewEngine(pkScript, tx.MsgTx(), k, flags, 0,
|
|
nil)
|
|
if err != nil {
|
|
continue testloop
|
|
}
|
|
|
|
err = vm.Execute()
|
|
if err != nil {
|
|
continue testloop
|
|
}
|
|
}
|
|
t.Errorf("test (%d:%v) succeeded when should fail",
|
|
i, test)
|
|
}
|
|
}
|
|
|
|
// TestTxValidTests ensures all of the tests in tx_valid.json pass as expected.
|
|
func TestTxValidTests(t *testing.T) {
|
|
file, err := ioutil.ReadFile("data/tx_valid.json")
|
|
if err != nil {
|
|
t.Errorf("TestTxValidTests: %v\n", err)
|
|
return
|
|
}
|
|
|
|
var tests [][]interface{}
|
|
err = json.Unmarshal(file, &tests)
|
|
if err != nil {
|
|
t.Errorf("TestTxValidTests couldn't Unmarshal: %v\n", err)
|
|
return
|
|
}
|
|
|
|
// form is either:
|
|
// ["this is a comment "]
|
|
// or:
|
|
// [[[previous hash, previous index, previous scriptPubKey]...,]
|
|
// serializedTransaction, verifyFlags]
|
|
testloop:
|
|
for i, test := range tests {
|
|
inputs, ok := test[0].([]interface{})
|
|
if !ok {
|
|
continue
|
|
}
|
|
|
|
if len(test) != 3 {
|
|
t.Errorf("bad test (bad length) %d: %v", i, test)
|
|
continue
|
|
}
|
|
serializedhex, ok := test[1].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (arg 2 not string) %d: %v", i, test)
|
|
continue
|
|
}
|
|
serializedTx, err := hex.DecodeString(serializedhex)
|
|
if err != nil {
|
|
t.Errorf("bad test (arg 2 not hex %v) %d: %v", err, i,
|
|
test)
|
|
continue
|
|
}
|
|
|
|
tx, err := dcrutil.NewTxFromBytes(serializedTx)
|
|
if err != nil {
|
|
t.Errorf("bad test (arg 2 not msgtx %v) %d: %v", err,
|
|
i, test)
|
|
continue
|
|
}
|
|
|
|
verifyFlags, ok := test[2].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (arg 3 not string) %d: %v", i, test)
|
|
continue
|
|
}
|
|
|
|
flags, err := parseScriptFlags(verifyFlags)
|
|
if err != nil {
|
|
t.Errorf("bad test %d: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
prevOuts := make(map[wire.OutPoint][]byte)
|
|
for j, iinput := range inputs {
|
|
input, ok := iinput.([]interface{})
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input not array)"+
|
|
"%d: %v", j, i, test)
|
|
continue
|
|
}
|
|
|
|
if len(input) != 3 {
|
|
t.Errorf("bad test (%dth input wrong length)"+
|
|
"%d: %v", j, i, test)
|
|
continue
|
|
}
|
|
|
|
previoustx, ok := input[0].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input hash not string)"+
|
|
"%d: %v", j, i, test)
|
|
continue
|
|
}
|
|
|
|
prevhash, err := chainhash.NewHashFromStr(previoustx)
|
|
if err != nil {
|
|
t.Errorf("bad test (%dth input hash not hash %v)"+
|
|
"%d: %v", j, err, i, test)
|
|
continue
|
|
}
|
|
|
|
idxf, ok := input[1].(float64)
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input idx not number)"+
|
|
"%d: %v", j, i, test)
|
|
continue
|
|
}
|
|
idx := testVecF64ToUint32(idxf)
|
|
|
|
oscript, ok := input[2].(string)
|
|
if !ok {
|
|
t.Errorf("bad test (%dth input script not "+
|
|
"string) %d: %v", j, i, test)
|
|
continue
|
|
}
|
|
|
|
script, err := parseShortForm(oscript)
|
|
if err != nil {
|
|
t.Errorf("bad test (%dth input script doesn't "+
|
|
"parse %v) %d: %v", j, err, i, test)
|
|
continue
|
|
}
|
|
|
|
prevOuts[*wire.NewOutPoint(prevhash, idx, wire.TxTreeRegular)] = script
|
|
}
|
|
|
|
for k, txin := range tx.MsgTx().TxIn {
|
|
pkScript, ok := prevOuts[txin.PreviousOutPoint]
|
|
if !ok {
|
|
t.Errorf("bad test (missing %dth input) %d:%v",
|
|
k, i, test)
|
|
continue testloop
|
|
}
|
|
vm, err := NewEngine(pkScript, tx.MsgTx(), k, flags, 0,
|
|
nil)
|
|
if err != nil {
|
|
t.Errorf("test (%d:%v:%d) failed to create "+
|
|
"script: %v", i, test, k, err)
|
|
continue
|
|
}
|
|
|
|
err = vm.Execute()
|
|
if err != nil {
|
|
t.Errorf("test (%d:%v:%d) failed to execute: "+
|
|
"%v", i, test, k, err)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// parseSigHashExpectedResult parses the provided expected result string into
|
|
// allowed error kinds. An error is returned if the expected result string is
|
|
// not supported.
|
|
func parseSigHashExpectedResult(expected string) (error, error) {
|
|
switch expected {
|
|
case "OK":
|
|
return nil, nil
|
|
case "SIGHASH_SINGLE_IDX":
|
|
return ErrInvalidSigHashSingleIndex, nil
|
|
}
|
|
|
|
return nil, fmt.Errorf("unrecognized expected result in test data: %v",
|
|
expected)
|
|
}
|
|
|
|
// TestCalcSignatureHashReference runs the reference signature hash calculation
|
|
// tests in sighash.json.
|
|
func TestCalcSignatureHashReference(t *testing.T) {
|
|
file, err := ioutil.ReadFile("data/sighash.json")
|
|
if err != nil {
|
|
t.Fatalf("TestCalcSignatureHash: %v\n", err)
|
|
}
|
|
|
|
var tests [][]interface{}
|
|
err = json.Unmarshal(file, &tests)
|
|
if err != nil {
|
|
t.Fatalf("TestCalcSignatureHash couldn't Unmarshal: %v\n", err)
|
|
}
|
|
|
|
const scriptVersion = 0
|
|
for i, test := range tests {
|
|
// Skip comment lines.
|
|
if len(test) == 1 {
|
|
continue
|
|
}
|
|
|
|
// Ensure test is well formed.
|
|
if len(test) < 6 || len(test) > 7 {
|
|
t.Fatalf("Test #%d: wrong length %d", i, len(test))
|
|
}
|
|
|
|
// Extract and parse the transaction from the test fields.
|
|
txHex, ok := test[0].(string)
|
|
if !ok {
|
|
t.Errorf("Test #%d: transaction is not a string", i)
|
|
continue
|
|
}
|
|
rawTx, err := hex.DecodeString(txHex)
|
|
if err != nil {
|
|
t.Errorf("Test #%d: unable to parse transaction: %v", i, err)
|
|
continue
|
|
}
|
|
var tx wire.MsgTx
|
|
err = tx.Deserialize(bytes.NewReader(rawTx))
|
|
if err != nil {
|
|
t.Errorf("Test #%d: unable to deserialize transaction: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the script from the test fields.
|
|
subScriptStr, ok := test[1].(string)
|
|
if !ok {
|
|
t.Errorf("Test #%d: script is not a string", i)
|
|
continue
|
|
}
|
|
subScript, err := hex.DecodeString(subScriptStr)
|
|
if err != nil {
|
|
t.Errorf("Test #%d: unable to decode script: %v", i, err)
|
|
continue
|
|
}
|
|
err = checkScriptParses(scriptVersion, subScript)
|
|
if err != nil {
|
|
t.Errorf("Test #%d: unable to parse script: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
// Extract the input index from the test fields.
|
|
inputIdxF64, ok := test[2].(float64)
|
|
if !ok {
|
|
t.Errorf("Test #%d: input idx is not numeric", i)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the hash type from the test fields.
|
|
hashTypeF64, ok := test[3].(float64)
|
|
if !ok {
|
|
t.Errorf("Test #%d: hash type is not numeric", i)
|
|
continue
|
|
}
|
|
hashType := SigHashType(testVecF64ToUint32(hashTypeF64))
|
|
|
|
// Extract and parse the signature hash from the test fields.
|
|
expectedHashStr, ok := test[4].(string)
|
|
if !ok {
|
|
t.Errorf("Test #%d: signature hash is not a string", i)
|
|
continue
|
|
}
|
|
expectedHash, err := hex.DecodeString(expectedHashStr)
|
|
if err != nil {
|
|
t.Errorf("Test #%d: unable to sig hash: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
// Extract and parse the expected result from the test fields.
|
|
expectedErrStr, ok := test[5].(string)
|
|
if !ok {
|
|
t.Errorf("Test #%d: result field is not a string", i)
|
|
continue
|
|
}
|
|
expectedErr, err := parseSigHashExpectedResult(expectedErrStr)
|
|
if err != nil {
|
|
t.Errorf("Test #%d: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
// Calculate the signature hash and verify expected result.
|
|
hash, err := CalcSignatureHash(subScript, hashType, &tx,
|
|
int(inputIdxF64), nil)
|
|
if !errors.Is(err, expectedErr) {
|
|
t.Errorf("Test #%d: want error kind %v, got err: %v (%T)", i,
|
|
expectedErr, err, err)
|
|
continue
|
|
}
|
|
if !bytes.Equal(hash, expectedHash) {
|
|
t.Errorf("Test #%d: signature hash mismatch - got %x, want %x", i,
|
|
hash, expectedHash)
|
|
continue
|
|
}
|
|
}
|
|
}
|