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.
572 lines
14 KiB
Go
572 lines
14 KiB
Go
// Copyright (c) 2013-2017 The btcsuite developers
|
|
// Copyright (c) 2015-2019 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"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/decred/dcrd/wire"
|
|
)
|
|
|
|
// testScriptFlags are the script flags which are used in the tests when
|
|
// executing transaction scripts to enforce additional checks. Note these flags
|
|
// are different than what is required for the consensus rules in that they are
|
|
// more strict.
|
|
const testScriptFlags = ScriptDiscourageUpgradableNops |
|
|
ScriptVerifyCleanStack |
|
|
ScriptVerifyCheckLockTimeVerify |
|
|
ScriptVerifyCheckSequenceVerify |
|
|
ScriptVerifySHA256 |
|
|
ScriptVerifyTreasury
|
|
|
|
// TestOpcodeDisabled tests the opcodeDisabled function manually because all
|
|
// disabled opcodes result in a script execution failure when executed normally,
|
|
// so the function is not called under normal circumstances.
|
|
func TestOpcodeDisabled(t *testing.T) {
|
|
t.Parallel()
|
|
tests := []byte{OP_CAT, OP_SUBSTR, OP_LEFT, OP_RIGHT, OP_INVERT,
|
|
OP_AND, OP_OR, OP_2MUL, OP_2DIV, OP_MUL, OP_DIV, OP_MOD,
|
|
OP_LSHIFT, OP_RSHIFT,
|
|
}
|
|
for _, opcodeVal := range tests {
|
|
op := &opcodeArray[opcodeVal]
|
|
err := opcodeDisabled(op, nil, nil)
|
|
if !errors.Is(err, ErrDisabledOpcode) {
|
|
t.Errorf("opcodeDisabled: unexpected error - got %v, "+
|
|
"want %v", err, ErrDisabledOpcode)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestOpcodeDisasm tests the print function for all opcodes in both the oneline
|
|
// and full modes to ensure it provides the expected disassembly.
|
|
func TestOpcodeDisasm(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// First, test the oneline disassembly.
|
|
|
|
// The expected strings for the data push opcodes are replaced in the
|
|
// test loops below since they involve repeating bytes. Also, the
|
|
// OP_NOP# and OP_UNKNOWN# are replaced below too, since it's easier
|
|
// than manually listing them here.
|
|
oneBytes := []byte{0x01}
|
|
oneStr := "01"
|
|
expectedStrings := [256]string{0x00: "0", 0x4f: "-1",
|
|
0x50: "OP_RESERVED", 0x61: "OP_NOP", 0x62: "OP_VER",
|
|
0x63: "OP_IF", 0x64: "OP_NOTIF", 0x65: "OP_VERIF",
|
|
0x66: "OP_VERNOTIF", 0x67: "OP_ELSE", 0x68: "OP_ENDIF",
|
|
0x69: "OP_VERIFY", 0x6a: "OP_RETURN", 0x6b: "OP_TOALTSTACK",
|
|
0x6c: "OP_FROMALTSTACK", 0x6d: "OP_2DROP", 0x6e: "OP_2DUP",
|
|
0x6f: "OP_3DUP", 0x70: "OP_2OVER", 0x71: "OP_2ROT",
|
|
0x72: "OP_2SWAP", 0x73: "OP_IFDUP", 0x74: "OP_DEPTH",
|
|
0x75: "OP_DROP", 0x76: "OP_DUP", 0x77: "OP_NIP",
|
|
0x78: "OP_OVER", 0x79: "OP_PICK", 0x7a: "OP_ROLL",
|
|
0x7b: "OP_ROT", 0x7c: "OP_SWAP", 0x7d: "OP_TUCK",
|
|
0x7e: "OP_CAT", 0x7f: "OP_SUBSTR", 0x80: "OP_LEFT",
|
|
0x81: "OP_RIGHT", 0x82: "OP_SIZE", 0x83: "OP_INVERT",
|
|
0x84: "OP_AND", 0x85: "OP_OR", 0x86: "OP_XOR",
|
|
0x87: "OP_EQUAL", 0x88: "OP_EQUALVERIFY", 0x89: "OP_ROTR",
|
|
0x8a: "OP_ROTL", 0x8b: "OP_1ADD", 0x8c: "OP_1SUB",
|
|
0x8d: "OP_2MUL", 0x8e: "OP_2DIV", 0x8f: "OP_NEGATE",
|
|
0x90: "OP_ABS", 0x91: "OP_NOT", 0x92: "OP_0NOTEQUAL",
|
|
0x93: "OP_ADD", 0x94: "OP_SUB", 0x95: "OP_MUL", 0x96: "OP_DIV",
|
|
0x97: "OP_MOD", 0x98: "OP_LSHIFT", 0x99: "OP_RSHIFT",
|
|
0x9a: "OP_BOOLAND", 0x9b: "OP_BOOLOR", 0x9c: "OP_NUMEQUAL",
|
|
0x9d: "OP_NUMEQUALVERIFY", 0x9e: "OP_NUMNOTEQUAL",
|
|
0x9f: "OP_LESSTHAN", 0xa0: "OP_GREATERTHAN",
|
|
0xa1: "OP_LESSTHANOREQUAL", 0xa2: "OP_GREATERTHANOREQUAL",
|
|
0xa3: "OP_MIN", 0xa4: "OP_MAX", 0xa5: "OP_WITHIN",
|
|
0xa6: "OP_RIPEMD160", 0xa7: "OP_SHA1", 0xa8: "OP_BLAKE256",
|
|
0xa9: "OP_HASH160", 0xaa: "OP_HASH256", 0xab: "OP_CODESEPARATOR",
|
|
0xac: "OP_CHECKSIG", 0xad: "OP_CHECKSIGVERIFY",
|
|
0xae: "OP_CHECKMULTISIG", 0xaf: "OP_CHECKMULTISIGVERIFY",
|
|
0xf9: "OP_INVALID249", 0xfa: "OP_SMALLINTEGER",
|
|
0xfb: "OP_PUBKEYS", 0xfd: "OP_PUBKEYHASH", 0xfe: "OP_PUBKEY",
|
|
0xff: "OP_INVALIDOPCODE", 0xba: "OP_SSTX", 0xbb: "OP_SSGEN",
|
|
0xbc: "OP_SSRTX", 0xbd: "OP_SSTXCHANGE", 0xbe: "OP_CHECKSIGALT",
|
|
0xbf: "OP_CHECKSIGALTVERIFY", 0xc0: "OP_SHA256",
|
|
0xc1: "OP_TADD", 0xc2: "OP_TSPEND", 0xc3: "OP_TGEN",
|
|
}
|
|
for opcodeVal, expectedStr := range expectedStrings {
|
|
var data []byte
|
|
switch {
|
|
// OP_DATA_1 through OP_DATA_65 display the pushed data.
|
|
case opcodeVal >= 0x01 && opcodeVal < 0x4c:
|
|
data = bytes.Repeat(oneBytes, opcodeVal)
|
|
expectedStr = strings.Repeat(oneStr, opcodeVal)
|
|
|
|
// OP_PUSHDATA1.
|
|
case opcodeVal == 0x4c:
|
|
data = bytes.Repeat(oneBytes, 1)
|
|
expectedStr = strings.Repeat(oneStr, 1)
|
|
|
|
// OP_PUSHDATA2.
|
|
case opcodeVal == 0x4d:
|
|
data = bytes.Repeat(oneBytes, 2)
|
|
expectedStr = strings.Repeat(oneStr, 2)
|
|
|
|
// OP_PUSHDATA4.
|
|
case opcodeVal == 0x4e:
|
|
data = bytes.Repeat(oneBytes, 3)
|
|
expectedStr = strings.Repeat(oneStr, 3)
|
|
|
|
// OP_1 through OP_16 display the numbers themselves.
|
|
case opcodeVal >= 0x51 && opcodeVal <= 0x60:
|
|
val := byte(opcodeVal - (0x51 - 1))
|
|
data = []byte{val}
|
|
expectedStr = strconv.Itoa(int(val))
|
|
|
|
// OP_NOP1 through OP_NOP10.
|
|
case opcodeVal >= 0xb0 && opcodeVal <= 0xb9:
|
|
switch opcodeVal {
|
|
case 0xb1:
|
|
// OP_NOP2 is an alias of OP_CHECKLOCKTIMEVERIFY
|
|
expectedStr = "OP_CHECKLOCKTIMEVERIFY"
|
|
case 0xb2:
|
|
// OP_NOP3 is an alias of OP_CHECKSEQUENCEVERIFY
|
|
expectedStr = "OP_CHECKSEQUENCEVERIFY"
|
|
default:
|
|
val := byte(opcodeVal - (0xb0 - 1))
|
|
expectedStr = "OP_NOP" + strconv.Itoa(int(val))
|
|
}
|
|
|
|
// OP_UNKNOWN#.
|
|
case opcodeVal >= 0xc4 && opcodeVal <= 0xf8 || opcodeVal == 0xfc:
|
|
expectedStr = "OP_UNKNOWN" + strconv.Itoa(opcodeVal)
|
|
}
|
|
|
|
var buf strings.Builder
|
|
disasmOpcode(&buf, &opcodeArray[opcodeVal], data, true)
|
|
gotStr := buf.String()
|
|
if gotStr != expectedStr {
|
|
t.Errorf("pop.print (opcode %x): Unexpected disasm "+
|
|
"string - got %v, want %v", opcodeVal, gotStr,
|
|
expectedStr)
|
|
continue
|
|
}
|
|
}
|
|
|
|
// Now, replace the relevant fields and test the full disassembly.
|
|
expectedStrings[0x00] = "OP_0"
|
|
expectedStrings[0x4f] = "OP_1NEGATE"
|
|
for opcodeVal, expectedStr := range expectedStrings {
|
|
var data []byte
|
|
switch {
|
|
// OP_DATA_1 through OP_DATA_65 display the opcode followed by
|
|
// the pushed data.
|
|
case opcodeVal >= 0x01 && opcodeVal < 0x4c:
|
|
data = bytes.Repeat(oneBytes, opcodeVal)
|
|
expectedStr = fmt.Sprintf("OP_DATA_%d 0x%s", opcodeVal,
|
|
strings.Repeat(oneStr, opcodeVal))
|
|
|
|
// OP_PUSHDATA1.
|
|
case opcodeVal == 0x4c:
|
|
data = bytes.Repeat(oneBytes, 1)
|
|
expectedStr = fmt.Sprintf("OP_PUSHDATA1 0x%02x 0x%s",
|
|
len(data), strings.Repeat(oneStr, 1))
|
|
|
|
// OP_PUSHDATA2.
|
|
case opcodeVal == 0x4d:
|
|
data = bytes.Repeat(oneBytes, 2)
|
|
expectedStr = fmt.Sprintf("OP_PUSHDATA2 0x%04x 0x%s",
|
|
len(data), strings.Repeat(oneStr, 2))
|
|
|
|
// OP_PUSHDATA4.
|
|
case opcodeVal == 0x4e:
|
|
data = bytes.Repeat(oneBytes, 3)
|
|
expectedStr = fmt.Sprintf("OP_PUSHDATA4 0x%08x 0x%s",
|
|
len(data), strings.Repeat(oneStr, 3))
|
|
|
|
// OP_1 through OP_16.
|
|
case opcodeVal >= 0x51 && opcodeVal <= 0x60:
|
|
val := byte(opcodeVal - (0x51 - 1))
|
|
data = []byte{val}
|
|
expectedStr = "OP_" + strconv.Itoa(int(val))
|
|
|
|
// OP_NOP1 through OP_NOP10.
|
|
case opcodeVal >= 0xb0 && opcodeVal <= 0xb9:
|
|
switch opcodeVal {
|
|
case 0xb1:
|
|
// OP_NOP2 is an alias of OP_CHECKLOCKTIMEVERIFY
|
|
expectedStr = "OP_CHECKLOCKTIMEVERIFY"
|
|
case 0xb2:
|
|
// OP_NOP3 is an alias of OP_CHECKSEQUENCEVERIFY
|
|
expectedStr = "OP_CHECKSEQUENCEVERIFY"
|
|
default:
|
|
val := byte(opcodeVal - (0xb0 - 1))
|
|
expectedStr = "OP_NOP" + strconv.Itoa(int(val))
|
|
}
|
|
|
|
// OP_UNKNOWN#.
|
|
case opcodeVal >= 0xc4 && opcodeVal <= 0xf8 || opcodeVal == 0xfc:
|
|
expectedStr = "OP_UNKNOWN" + strconv.Itoa(opcodeVal)
|
|
}
|
|
|
|
var buf strings.Builder
|
|
disasmOpcode(&buf, &opcodeArray[opcodeVal], data, false)
|
|
gotStr := buf.String()
|
|
if gotStr != expectedStr {
|
|
t.Errorf("pop.print (opcode %x): Unexpected disasm "+
|
|
"string - got %v, want %v", opcodeVal, gotStr,
|
|
expectedStr)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewlyEnabledOpCodes(t *testing.T) {
|
|
sigScriptMath := []byte{
|
|
0x04,
|
|
0xff, 0xff, 0xff, 0x7f,
|
|
0x04,
|
|
0xee, 0xee, 0xee, 0x6e,
|
|
}
|
|
sigScriptShift := []byte{
|
|
0x04,
|
|
0xff, 0xff, 0xff, 0x7f,
|
|
0x53,
|
|
}
|
|
sigScriptRot := []byte{
|
|
0x04,
|
|
0x21, 0x12, 0x34, 0x56,
|
|
0x53,
|
|
}
|
|
sigScriptInv := []byte{
|
|
0x04,
|
|
0xff, 0x00, 0xf0, 0x0f,
|
|
}
|
|
sigScriptLogic := []byte{
|
|
0x04,
|
|
0x21, 0x12, 0x34, 0x56,
|
|
0x04,
|
|
0x0f, 0xf0, 0x00, 0xff,
|
|
}
|
|
sigScriptCat := []byte{
|
|
0x06,
|
|
0x21, 0x12, 0x34, 0x56, 0x44, 0x55,
|
|
0x06,
|
|
0x0f, 0xf0, 0x00, 0xff, 0x88, 0x99,
|
|
}
|
|
lotsOf01s := bytes.Repeat([]byte{0x01}, 2050)
|
|
builder := NewScriptBuilder()
|
|
builder.AddData(lotsOf01s).AddData(lotsOf01s)
|
|
sigScriptCatOverflow, _ := builder.Script()
|
|
sigScriptSubstr := []byte{
|
|
0x08,
|
|
0x21, 0x12, 0x34, 0x56, 0x59, 0x32, 0x40, 0x21,
|
|
0x56,
|
|
0x52,
|
|
}
|
|
sigScriptLR := []byte{
|
|
0x08,
|
|
0x21, 0x12, 0x34, 0x56, 0x59, 0x32, 0x40, 0x21,
|
|
0x54,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
pkScript []byte
|
|
sigScript []byte
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "add",
|
|
pkScript: []byte{
|
|
0x93, // OP_ADD
|
|
0x05, // Expected result push
|
|
0xed, 0xee, 0xee, 0xee, 0x00,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptMath,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "sub",
|
|
pkScript: []byte{
|
|
0x94, // OP_SUB
|
|
0x04, // Expected result push
|
|
0x11, 0x11, 0x11, 0x11,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptMath,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "mul",
|
|
pkScript: []byte{
|
|
0x95, // OP_MUL
|
|
0x04, // Expected result push
|
|
0xee, 0xee, 0xee, 0xee,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptMath,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "div",
|
|
pkScript: []byte{
|
|
0x96, // OP_DIV
|
|
0x51, // Expected result push
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptMath,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "mod",
|
|
pkScript: []byte{
|
|
0x97, // OP_MOD
|
|
0x04, // Expected result push
|
|
0x11, 0x11, 0x11, 0x11,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptMath,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "lshift",
|
|
pkScript: []byte{
|
|
0x98, // OP_LSHIFT
|
|
0x01, // Expected result push
|
|
0x88,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptShift,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "rshift",
|
|
pkScript: []byte{
|
|
0x99, // OP_RSHIFT
|
|
0x04, // Expected result push
|
|
0xff, 0xff, 0xff, 0x0f,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptShift,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "rotr",
|
|
pkScript: []byte{
|
|
0x89, // OP_ROTR
|
|
0x04, // Expected result push
|
|
0x44, 0x82, 0xc6, 0x2a,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptRot,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "rotl",
|
|
pkScript: []byte{
|
|
0x8a, // OP_ROTL
|
|
0x04, // Expected result push
|
|
0xf6, 0x6e, 0x5f, 0xce,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptRot,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "inv",
|
|
pkScript: []byte{
|
|
0x83, // OP_INV
|
|
0x04, // Expected result push
|
|
0x00, 0x01, 0xf0, 0x8f,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptInv,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "and",
|
|
pkScript: []byte{
|
|
0x84, // OP_AND
|
|
0x03, // Expected result push
|
|
0x21, 0x02, 0x34,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptLogic,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "or",
|
|
pkScript: []byte{
|
|
0x85, // OP_OR
|
|
0x04, // Expected result push
|
|
0x0f, 0xe0, 0x00, 0xa9,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptLogic,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "xor",
|
|
pkScript: []byte{
|
|
0x86, // OP_XOR
|
|
0x04, // Expected result push
|
|
0x30, 0xe2, 0x34, 0xa9,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptLogic,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "cat",
|
|
pkScript: []byte{
|
|
0x7e, // OP_CAT
|
|
0x0c, // Expected result push
|
|
0x21, 0x12, 0x34, 0x56, 0x44, 0x55,
|
|
0x0f, 0xf0, 0x00, 0xff, 0x88, 0x99,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptCat,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "catoverflow",
|
|
pkScript: []byte{
|
|
0x7e, // OP_CAT
|
|
0x0c, // Expected result push
|
|
0x21, 0x12, 0x34, 0x56, 0x44, 0x55,
|
|
0x0f, 0xf0, 0x00, 0xff, 0x88, 0x99,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptCatOverflow,
|
|
expected: false,
|
|
},
|
|
{
|
|
name: "substr",
|
|
pkScript: []byte{
|
|
0x7f, // OP_SUBSTR
|
|
0x04, // Expected result push
|
|
0x34, 0x56, 0x59, 0x32,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptSubstr,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "left",
|
|
pkScript: []byte{
|
|
0x80, // OP_LEFT
|
|
0x04, // Expected result push
|
|
0x21, 0x12, 0x34, 0x56,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptLR,
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "right",
|
|
pkScript: []byte{
|
|
0x81, // OP_RIGHT
|
|
0x04, // Expected result push
|
|
0x59, 0x32, 0x40, 0x21,
|
|
0x87, // OP_EQUAL
|
|
},
|
|
sigScript: sigScriptLR,
|
|
expected: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
msgTx := new(wire.MsgTx)
|
|
msgTx.AddTxIn(&wire.TxIn{
|
|
PreviousOutPoint: wire.OutPoint{},
|
|
SignatureScript: test.sigScript,
|
|
Sequence: 0xFFFFFFFF,
|
|
})
|
|
msgTx.AddTxOut(&wire.TxOut{
|
|
Value: 0x00FFFFFF00000000,
|
|
PkScript: []byte{0x01},
|
|
})
|
|
engine, err := NewEngine(test.pkScript, msgTx, 0,
|
|
testScriptFlags, 0, nil)
|
|
if err != nil {
|
|
t.Errorf("Bad script result for test %v because of error: %v",
|
|
test.name, err.Error())
|
|
continue
|
|
}
|
|
err = engine.Execute()
|
|
if err != nil && test.expected {
|
|
t.Errorf("Bad script exec for test %v because of error: %v",
|
|
test.name, err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func randByteSliceSlice(i int, maxLen int, src int) [][]byte {
|
|
r := rand.New(rand.NewSource(int64(src)))
|
|
|
|
slices := make([][]byte, i)
|
|
for j := 0; j < i; j++ {
|
|
for {
|
|
sz := r.Intn(maxLen) + 1
|
|
|
|
sl := make([]byte, sz)
|
|
for k := 0; k < sz; k++ {
|
|
randByte := r.Intn(255)
|
|
sl[k] = uint8(randByte)
|
|
}
|
|
|
|
// No duplicates allowed.
|
|
if j > 0 &&
|
|
(bytes.Equal(sl, slices[j-1])) {
|
|
r.Seed(int64(j) + r.Int63n(12345))
|
|
continue
|
|
}
|
|
|
|
slices[j] = sl
|
|
break
|
|
}
|
|
}
|
|
|
|
return slices
|
|
}
|
|
|
|
// TestForVMFailure feeds random scripts to the VMs to check and see if it
|
|
// crashes. Try increasing the number of iterations or the length of the
|
|
// byte string to sample a greater space.
|
|
func TestForVMFailure(t *testing.T) {
|
|
numTests := 2
|
|
bsLength := 11
|
|
|
|
for i := 0; i < numTests; i++ {
|
|
tests := randByteSliceSlice(65536, bsLength, i)
|
|
|
|
for j := range tests {
|
|
if j == 0 {
|
|
continue
|
|
}
|
|
|
|
msgTx := new(wire.MsgTx)
|
|
msgTx.AddTxIn(&wire.TxIn{
|
|
PreviousOutPoint: wire.OutPoint{},
|
|
SignatureScript: tests[j-1],
|
|
Sequence: 0xFFFFFFFF,
|
|
})
|
|
msgTx.AddTxOut(&wire.TxOut{
|
|
Value: 0x00FFFFFF00000000,
|
|
PkScript: []byte{0x01},
|
|
})
|
|
engine, err := NewEngine(tests[j], msgTx, 0,
|
|
testScriptFlags, 0, nil)
|
|
|
|
if err == nil {
|
|
engine.Execute()
|
|
}
|
|
}
|
|
}
|
|
}
|