|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2015-2018 The Bitcoin Core developers |
| 3 | +# Distributed under the MIT software license, see the accompanying |
| 4 | +# file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 5 | +""" |
| 6 | +Templates for constructing various sorts of invalid transactions. |
| 7 | +
|
| 8 | +These templates (or an iterator over all of them) can be reused in different |
| 9 | +contexts to test using a number of invalid transaction types. |
| 10 | +
|
| 11 | +Hopefully this makes it easier to get coverage of a full variety of tx |
| 12 | +validation checks through different interfaces (AcceptBlock, AcceptToMemPool, |
| 13 | +etc.) without repeating ourselves. |
| 14 | +
|
| 15 | +Invalid tx cases not covered here can be found by running: |
| 16 | +
|
| 17 | + $ diff \ |
| 18 | + <(grep -IREho "bad-txns[a-zA-Z-]+" src | sort -u) \ |
| 19 | + <(grep -IEho "bad-txns[a-zA-Z-]+" test/functional/data/invalid_txs.py | sort -u) |
| 20 | +
|
| 21 | +""" |
| 22 | +import abc |
| 23 | + |
| 24 | +from test_framework.messages import CTransaction, CTxIn, CTxOut, COutPoint |
| 25 | +from test_framework import script as sc |
| 26 | +from test_framework.blocktools import create_tx_with_script, MAX_BLOCK_SIGOPS |
| 27 | + |
| 28 | +basic_p2sh = sc.CScript([sc.OP_HASH160, sc.hash160(sc.CScript([sc.OP_0])), sc.OP_EQUAL]) |
| 29 | + |
| 30 | + |
| 31 | +class BadTxTemplate: |
| 32 | + """Allows simple construction of a certain kind of invalid tx. Base class to be subclassed.""" |
| 33 | + __metaclass__ = abc.ABCMeta |
| 34 | + |
| 35 | + # The expected error code given by bitcoind upon submission of the tx. |
| 36 | + reject_reason = "" |
| 37 | + |
| 38 | + # Only specified if it differs from mempool acceptance error. |
| 39 | + block_reject_reason = "" |
| 40 | + |
| 41 | + # Do we expect to be disconnected after submitting this tx? |
| 42 | + expect_disconnect = False |
| 43 | + |
| 44 | + # Is this tx considered valid when included in a block, but not for acceptance into |
| 45 | + # the mempool (i.e. does it violate policy but not consensus)? |
| 46 | + valid_in_block = False |
| 47 | + |
| 48 | + def __init__(self, *, spend_tx=None, spend_block=None): |
| 49 | + self.spend_tx = spend_block.vtx[0] if spend_block else spend_tx |
| 50 | + self.spend_avail = sum(o.nValue for o in self.spend_tx.vout) |
| 51 | + self.valid_txin = CTxIn(COutPoint(self.spend_tx.sha256, 0), b"", 0xffffffff) |
| 52 | + |
| 53 | + @abc.abstractmethod |
| 54 | + def get_tx(self, *args, **kwargs): |
| 55 | + """Return a CTransaction that is invalid per the subclass.""" |
| 56 | + pass |
| 57 | + |
| 58 | + |
| 59 | +class OutputMissing(BadTxTemplate): |
| 60 | + reject_reason = "bad-txns-vout-empty" |
| 61 | + expect_disconnect = False |
| 62 | + |
| 63 | + def get_tx(self): |
| 64 | + tx = CTransaction() |
| 65 | + tx.vin.append(self.valid_txin) |
| 66 | + tx.calc_sha256() |
| 67 | + return tx |
| 68 | + |
| 69 | + |
| 70 | +class InputMissing(BadTxTemplate): |
| 71 | + reject_reason = "bad-txns-vin-empty" |
| 72 | + expect_disconnect = False |
| 73 | + |
| 74 | + def get_tx(self): |
| 75 | + tx = CTransaction() |
| 76 | + tx.vout.append(CTxOut(0, sc.CScript([sc.OP_TRUE] * 100))) |
| 77 | + tx.calc_sha256() |
| 78 | + return tx |
| 79 | + |
| 80 | + |
| 81 | +class SizeTooSmall(BadTxTemplate): |
| 82 | + reject_reason = "tx-size-small" |
| 83 | + expect_disconnect = False |
| 84 | + valid_in_block = True |
| 85 | + |
| 86 | + def get_tx(self): |
| 87 | + tx = CTransaction() |
| 88 | + tx.vin.append(self.valid_txin) |
| 89 | + tx.vout.append(CTxOut(0, sc.CScript([sc.OP_TRUE]))) |
| 90 | + tx.calc_sha256() |
| 91 | + return tx |
| 92 | + |
| 93 | + |
| 94 | +class BadInputOutpointIndex(BadTxTemplate): |
| 95 | + # Won't be rejected - nonexistent outpoint index is treated as an orphan since the coins |
| 96 | + # database can't distinguish between spent outpoints and outpoints which never existed. |
| 97 | + reject_reason = None |
| 98 | + expect_disconnect = False |
| 99 | + |
| 100 | + def get_tx(self): |
| 101 | + num_indices = len(self.spend_tx.vin) |
| 102 | + bad_idx = num_indices + 100 |
| 103 | + |
| 104 | + tx = CTransaction() |
| 105 | + tx.vin.append(CTxIn(COutPoint(self.spend_tx.sha256, bad_idx), b"", 0xffffffff)) |
| 106 | + tx.vout.append(CTxOut(0, basic_p2sh)) |
| 107 | + tx.calc_sha256() |
| 108 | + return tx |
| 109 | + |
| 110 | + |
| 111 | +class DuplicateInput(BadTxTemplate): |
| 112 | + reject_reason = 'bad-txns-inputs-duplicate' |
| 113 | + expect_disconnect = True |
| 114 | + |
| 115 | + def get_tx(self): |
| 116 | + tx = CTransaction() |
| 117 | + tx.vin.append(self.valid_txin) |
| 118 | + tx.vin.append(self.valid_txin) |
| 119 | + tx.vout.append(CTxOut(1, basic_p2sh)) |
| 120 | + tx.calc_sha256() |
| 121 | + return tx |
| 122 | + |
| 123 | + |
| 124 | +class NonexistentInput(BadTxTemplate): |
| 125 | + reject_reason = None # Added as an orphan tx. |
| 126 | + expect_disconnect = False |
| 127 | + |
| 128 | + def get_tx(self): |
| 129 | + tx = CTransaction() |
| 130 | + tx.vin.append(CTxIn(COutPoint(self.spend_tx.sha256 + 1, 0), b"", 0xffffffff)) |
| 131 | + tx.vin.append(self.valid_txin) |
| 132 | + tx.vout.append(CTxOut(1, basic_p2sh)) |
| 133 | + tx.calc_sha256() |
| 134 | + return tx |
| 135 | + |
| 136 | + |
| 137 | +class SpendTooMuch(BadTxTemplate): |
| 138 | + reject_reason = 'bad-txns-in-belowout' |
| 139 | + expect_disconnect = True |
| 140 | + |
| 141 | + def get_tx(self): |
| 142 | + return create_tx_with_script( |
| 143 | + self.spend_tx, 0, script_pub_key=basic_p2sh, amount=(self.spend_avail + 1)) |
| 144 | + |
| 145 | + |
| 146 | +class SpendNegative(BadTxTemplate): |
| 147 | + reject_reason = 'bad-txns-vout-negative' |
| 148 | + expect_disconnect = True |
| 149 | + |
| 150 | + def get_tx(self): |
| 151 | + return create_tx_with_script(self.spend_tx, 0, amount=-1) |
| 152 | + |
| 153 | + |
| 154 | +class InvalidOPIFConstruction(BadTxTemplate): |
| 155 | + reject_reason = "mandatory-script-verify-flag-failed (Invalid OP_IF construction)" |
| 156 | + expect_disconnect = True |
| 157 | + valid_in_block = True |
| 158 | + |
| 159 | + def get_tx(self): |
| 160 | + return create_tx_with_script( |
| 161 | + self.spend_tx, 0, script_sig=b'\x64' * 35, |
| 162 | + amount=(self.spend_avail // 2)) |
| 163 | + |
| 164 | + |
| 165 | +class TooManySigops(BadTxTemplate): |
| 166 | + reject_reason = "bad-txns-too-many-sigops" |
| 167 | + block_reject_reason = "bad-blk-sigops, out-of-bounds SigOpCount" |
| 168 | + expect_disconnect = False |
| 169 | + |
| 170 | + def get_tx(self): |
| 171 | + lotsa_checksigs = sc.CScript([sc.OP_CHECKSIG] * (MAX_BLOCK_SIGOPS)) |
| 172 | + return create_tx_with_script( |
| 173 | + self.spend_tx, 0, |
| 174 | + script_pub_key=lotsa_checksigs, |
| 175 | + amount=1) |
| 176 | + |
| 177 | + |
| 178 | +def iter_all_templates(): |
| 179 | + """Iterate through all bad transaction template types.""" |
| 180 | + return BadTxTemplate.__subclasses__() |
0 commit comments