diff --git a/test/functional/feature_block.py b/test/functional/feature_block.py --- a/test/functional/feature_block.py +++ b/test/functional/feature_block.py @@ -55,11 +55,6 @@ from test_framework.util import assert_equal -class PreviousSpendableOutput(): - def __init__(self, tx=CTransaction(), n=-1): - self.tx = tx - self.n = n # the output we're spending - # Use this class for tests that require behavior other than normal "mininode" behavior. # For now, it is used to serialize a bloated varint (b64). @@ -139,7 +134,7 @@ self.log.info("Don't reorg to a chain of the same length") self.move_tip(1) b3 = self.next_block(3, spend=out[1]) - txout_b3 = PreviousSpendableOutput(b3.vtx[1], 0) + txout_b3 = b3.vtx[1] self.sync_blocks([b3], False) # Now we add another block to make the alternative chain longer. @@ -424,8 +419,8 @@ "Reject a block spending transaction from a block which failed to connect") self.move_tip(35) b37 = self.next_block(37, spend=out[11]) - txout_b37 = PreviousSpendableOutput(b37.vtx[1], 0) - tx = self.create_and_sign_transaction(out[11].tx, out[11].n, 0) + txout_b37 = b37.vtx[1] + tx = self.create_and_sign_transaction(out[11], 0) b37 = self.update_block(37, [tx]) self.sync_blocks([b37], success=False, reject_reason='bad-txns-inputs-missingorspent', reconnect=True) @@ -462,10 +457,9 @@ # Create a transaction that spends one satoshi to the p2sh_script, the rest to OP_TRUE # This must be signed because it is spending a coinbase spend = out[11] - tx = self.create_tx(spend.tx, spend.n, 1, p2sh_script) - tx.vout.append( - CTxOut(spend.tx.vout[spend.n].nValue - 1, CScript([OP_TRUE]))) - self.sign_tx(tx, spend.tx, spend.n) + tx = self.create_tx(spend, 0, 1, p2sh_script) + tx.vout.append(CTxOut(spend.vout[0].nValue - 1, CScript([OP_TRUE]))) + self.sign_tx(tx, spend) tx.rehash() b39 = self.update_block(39, [tx]) b39_outputs += 1 @@ -593,7 +587,7 @@ self.sync_blocks([b44], True) self.log.info("Reject a block with a non-coinbase as the first tx") - non_coinbase = self.create_tx(out[15].tx, out[15].n, 1) + non_coinbase = self.create_tx(out[15], 0, 1) b45 = CBlock() b45.nTime = self.tip.nTime + 1 b45.hashPrevBlock = self.tip.sha256 @@ -724,7 +718,7 @@ # b57 - a good block with 2 txs, don't submit until end self.move_tip(55) b57 = self.next_block(57) - tx = self.create_and_sign_transaction(out[16].tx, out[16].n, 1) + tx = self.create_and_sign_transaction(out[16], 1) tx1 = self.create_tx(tx, 0, 1) b57 = self.update_block(57, [tx, tx1]) @@ -743,7 +737,7 @@ # b57p2 - a good block with 6 tx'es, don't submit until end self.move_tip(55) b57p2 = self.next_block("57p2") - tx = self.create_and_sign_transaction(out[16].tx, out[16].n, 1) + tx = self.create_and_sign_transaction(out[16], 1) tx1 = self.create_tx(tx, 0, 1) tx2 = self.create_tx(tx1, 0, 1) tx3 = self.create_tx(tx2, 0, 1) @@ -782,9 +776,9 @@ self.move_tip(57) b58 = self.next_block(58, spend=out[17]) tx = CTransaction() - assert len(out[17].tx.vout) < 42 + assert(len(out[17].vout) < 42) tx.vin.append( - CTxIn(COutPoint(out[17].tx.sha256, 42), CScript([OP_TRUE]), 0xffffffff)) + CTxIn(COutPoint(out[17].sha256, 42), CScript([OP_TRUE]), 0xffffffff)) tx.vout.append(CTxOut(0, b"")) pad_tx(tx) tx.calc_sha256() @@ -797,7 +791,7 @@ "Reject a block with a transaction with outputs > inputs") self.move_tip(57) b59 = self.next_block(59) - tx = self.create_and_sign_transaction(out[17].tx, out[17].n, 51 * COIN) + tx = self.create_and_sign_transaction(out[17], 51 * COIN) b59 = self.update_block(59, [tx]) self.sync_blocks([b59], success=False, reject_reason='bad-txns-in-belowout', reconnect=True) @@ -840,9 +834,8 @@ b62 = self.next_block(62) tx = CTransaction() tx.nLockTime = 0xffffffff # this locktime is non-final - assert out[18].n < len(out[18].tx.vout) # don't set nSequence - tx.vin.append(CTxIn(COutPoint(out[18].tx.sha256, out[18].n))) + tx.vin.append(CTxIn(COutPoint(out[18].sha256, 0))) tx.vout.append(CTxOut(0, CScript([OP_TRUE]))) assert tx.vin[0].nSequence < 0xffffffff tx.calc_sha256() @@ -928,9 +921,8 @@ "Accept a block with a transaction spending an output created in the same block") self.move_tip(64) b65 = self.next_block(65) - tx1 = self.create_and_sign_transaction( - out[19].tx, out[19].n, out[19].tx.vout[0].nValue) - tx2 = self.create_and_sign_transaction(tx1, 0, 0) + tx1 = self.create_and_sign_transaction(out[19], out[19].vout[0].nValue) + tx2 = self.create_and_sign_transaction(tx1, 0) b65 = self.update_block(65, [tx1, tx2]) self.sync_blocks([b65], True) self.save_spendable_output() @@ -945,10 +937,9 @@ "Reject a block with a transaction double spending a transaction created in the same block") self.move_tip(65) b67 = self.next_block(67) - tx1 = self.create_and_sign_transaction( - out[20].tx, out[20].n, out[20].tx.vout[0].nValue) - tx2 = self.create_and_sign_transaction(tx1, 0, 1) - tx3 = self.create_and_sign_transaction(tx1, 0, 2) + tx1 = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue) + tx2 = self.create_and_sign_transaction(tx1, 1) + tx3 = self.create_and_sign_transaction(tx1, 2) b67 = self.update_block(67, [tx1, tx2, tx3]) self.sync_blocks([b67], success=False, reject_reason='bad-txns-inputs-missingorspent', reconnect=True) @@ -970,7 +961,7 @@ self.move_tip(65) b68 = self.next_block(68, additional_coinbase_value=10) tx = self.create_and_sign_transaction( - out[20].tx, out[20].n, out[20].tx.vout[0].nValue - 9) + out[20], out[20].vout[0].nValue - 9) b68 = self.update_block(68, [tx]) self.sync_blocks([b68], success=False, reject_reason='bad-cb-amount', reconnect=True) @@ -980,7 +971,7 @@ self.move_tip(65) b69 = self.next_block(69, additional_coinbase_value=10) tx = self.create_and_sign_transaction( - out[20].tx, out[20].n, out[20].tx.vout[0].nValue - 10) + out[20], out[20].vout[0].nValue - 10) self.update_block(69, [tx]) self.sync_blocks([b69], True) self.save_spendable_output() @@ -1016,8 +1007,8 @@ "Reject a block containing a duplicate transaction but with the same Merkle root (Merkle tree malleability") self.move_tip(69) b72 = self.next_block(72) - tx1 = self.create_and_sign_transaction(out[21].tx, out[21].n, 2) - tx2 = self.create_and_sign_transaction(tx1, 0, 1) + tx1 = self.create_and_sign_transaction(out[21], 2) + tx2 = self.create_and_sign_transaction(tx1, 1) b72 = self.update_block(72, [tx1, tx2]) # now tip is 72 b71 = copy.deepcopy(b72) # add duplicate last transaction @@ -1068,7 +1059,7 @@ a[MAX_BLOCK_SIGOPS_PER_MB + 2] = 0 a[MAX_BLOCK_SIGOPS_PER_MB + 3] = 0 - tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a)) + tx = self.create_and_sign_transaction(out[22], 1, CScript(a)) b73 = self.update_block(73, [tx]) assert_equal(get_legacy_sigopcount_block( b73), MAX_BLOCK_SIGOPS_PER_MB + 1) @@ -1097,7 +1088,7 @@ a[MAX_BLOCK_SIGOPS_PER_MB + 2] = 0xff a[MAX_BLOCK_SIGOPS_PER_MB + 3] = 0xff a[MAX_BLOCK_SIGOPS_PER_MB + 4] = 0xff - tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a)) + tx = self.create_and_sign_transaction(out[22], 1, CScript(a)) b74 = self.update_block(74, [tx]) self.sync_blocks([b74], success=False, reject_reason='bad-blk-sigops', reconnect=True) @@ -1111,7 +1102,7 @@ a[MAX_BLOCK_SIGOPS_PER_MB + 1] = 0xff a[MAX_BLOCK_SIGOPS_PER_MB + 2] = 0xff a[MAX_BLOCK_SIGOPS_PER_MB + 3] = 0xff - tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a)) + tx = self.create_and_sign_transaction(out[22], 1, CScript(a)) b75 = self.update_block(75, [tx]) self.sync_blocks([b75], True) self.save_spendable_output() @@ -1123,7 +1114,7 @@ a = bytearray([OP_CHECKSIG] * size) # PUSHDATA4, but leave the following bytes as just checksigs a[MAX_BLOCK_SIGOPS_PER_MB - 1] = 0x4e - tx = self.create_and_sign_transaction(out[23].tx, 0, 1, CScript(a)) + tx = self.create_and_sign_transaction(out[23], 1, CScript(a)) b76 = self.update_block(76, [tx]) self.sync_blocks([b76], True) self.save_spendable_output() @@ -1148,8 +1139,7 @@ self.log.info("Test transaction resurrection during a re-org") self.move_tip(76) b77 = self.next_block(77) - tx77 = self.create_and_sign_transaction( - out[24].tx, out[24].n, 10 * COIN) + tx77 = self.create_and_sign_transaction(out[24], 10 * COIN) b77 = self.update_block(77, [tx77]) self.sync_blocks([b77], True) self.save_spendable_output() @@ -1199,9 +1189,9 @@ op_codes = [OP_IF, OP_INVALIDOPCODE, OP_ELSE, OP_TRUE, OP_ENDIF] script = CScript(op_codes) tx1 = self.create_and_sign_transaction( - out[28].tx, out[28].n, out[28].tx.vout[0].nValue, script) + out[28], out[28].vout[0].nValue, script) - tx2 = self.create_and_sign_transaction(tx1, 0, 0, CScript([OP_TRUE])) + tx2 = self.create_and_sign_transaction(tx1, 0, CScript([OP_TRUE])) tx2.vin[0].scriptSig = CScript([OP_FALSE]) tx2.rehash() @@ -1216,14 +1206,14 @@ # self.log.info("Test re-orging blocks with OP_RETURN in them") b84 = self.next_block(84) - tx1 = self.create_tx(out[29].tx, out[29].n, 0, CScript([OP_RETURN])) + tx1 = self.create_tx(out[29], 0, 0, CScript([OP_RETURN])) vout_offset = len(tx1.vout) tx1.vout.append(CTxOut(0, CScript([OP_TRUE]))) tx1.vout.append(CTxOut(0, CScript([OP_TRUE]))) tx1.vout.append(CTxOut(0, CScript([OP_TRUE]))) tx1.vout.append(CTxOut(0, CScript([OP_TRUE]))) tx1.calc_sha256() - self.sign_tx(tx1, out[29].tx, out[29].n) + self.sign_tx(tx1, out[29]) tx1.rehash() tx2 = self.create_tx(tx1, vout_offset, 0, CScript([OP_RETURN])) tx2.vout.append(CTxOut(0, CScript([OP_RETURN]))) @@ -1310,23 +1300,23 @@ # this is a little handier to use than the version in blocktools.py def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE])): - return create_tx_with_script(spend_tx, n, b"", value, script) + return create_tx_with_script(spend_tx, n, amount=value, script_pub_key=script) # sign a transaction, using the key we know about # this signs input 0 in tx, which is assumed to be spending output n in spend_tx - def sign_tx(self, tx, spend_tx, n): - scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey) + def sign_tx(self, tx, spend_tx): + scriptPubKey = bytearray(spend_tx.vout[0].scriptPubKey) if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend tx.vin[0].scriptSig = CScript() return sighash = SignatureHashForkId( - spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL | SIGHASH_FORKID, spend_tx.vout[n].nValue) + spend_tx.vout[0].scriptPubKey, tx, 0, SIGHASH_ALL | SIGHASH_FORKID, spend_tx.vout[0].nValue) tx.vin[0].scriptSig = CScript( [self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL | SIGHASH_FORKID]))]) - def create_and_sign_transaction(self, spend_tx, n, value, script=CScript([OP_TRUE])): - tx = self.create_tx(spend_tx, n, value, script) - self.sign_tx(tx, spend_tx, n) + def create_and_sign_transaction(self, spend_tx, value, script=CScript([OP_TRUE])): + tx = self.create_tx(spend_tx, 0, value, script) + self.sign_tx(tx, spend_tx) tx.rehash() return tx @@ -1346,12 +1336,12 @@ block = create_block(base_block_hash, coinbase, block_time) else: # all but one satoshi to fees - coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 + coinbase.vout[0].nValue += spend.vout[0].nValue - 1 coinbase.rehash() block = create_block(base_block_hash, coinbase, block_time) # spend 1 satoshi - tx = create_tx_with_script(spend.tx, spend.n, b"", 1, script) - self.sign_tx(tx, spend.tx, spend.n) + tx = self.create_tx(spend, 0, 1, script) + self.sign_tx(tx, spend) self.add_transactions_to_block(block, [tx]) block.hashMerkleRoot = block.calc_merkle_root() if solve: @@ -1371,7 +1361,7 @@ def get_spendable_output(self): self.log.debug("getting spendable output {}".format( self.spendable_outputs[0].vtx[0])) - return PreviousSpendableOutput(self.spendable_outputs.pop(0).vtx[0], 0) + return self.spendable_outputs.pop(0).vtx[0] # move the tip back to a previous block def move_tip(self, number):