Skip to content

Commit 44bbcee

Browse files
committed
[Tests] Cleanup feature_block.py, remove unnecessary PreviousSpendableOutput object
1 parent 736f941 commit 44bbcee

File tree

1 file changed

+44
-50
lines changed

1 file changed

+44
-50
lines changed

test/functional/feature_block.py

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@
4848

4949
MAX_BLOCK_SIGOPS = 20000
5050

51-
class PreviousSpendableOutput():
52-
def __init__(self, tx=CTransaction(), n=-1):
53-
self.tx = tx
54-
self.n = n # the output we're spending
55-
5651
# Use this class for tests that require behavior other than normal "mininode" behavior.
5752
# For now, it is used to serialize a bloated varint (b64).
5853
class CBrokenBlock(CBlock):
@@ -132,7 +127,7 @@ def run_test(self):
132127
self.log.info("Don't reorg to a chain of the same length")
133128
self.move_tip(1)
134129
b3 = self.next_block(3, spend=out[1])
135-
txout_b3 = PreviousSpendableOutput(b3.vtx[1], 0)
130+
txout_b3 = b3.vtx[1]
136131
self.sync_blocks([b3], False)
137132

138133
# Now we add another block to make the alternative chain longer.
@@ -397,8 +392,8 @@ def run_test(self):
397392
self.log.info("Reject a block spending transaction from a block which failed to connect")
398393
self.move_tip(35)
399394
b37 = self.next_block(37, spend=out[11])
400-
txout_b37 = PreviousSpendableOutput(b37.vtx[1], 0)
401-
tx = self.create_and_sign_transaction(out[11].tx, out[11].n, 0)
395+
txout_b37 = b37.vtx[1]
396+
tx = self.create_and_sign_transaction(out[11], 0)
402397
b37 = self.update_block(37, [tx])
403398
self.sync_blocks([b37], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
404399

@@ -432,9 +427,9 @@ def run_test(self):
432427
# Create a transaction that spends one satoshi to the p2sh_script, the rest to OP_TRUE
433428
# This must be signed because it is spending a coinbase
434429
spend = out[11]
435-
tx = self.create_tx(spend.tx, spend.n, 1, p2sh_script)
436-
tx.vout.append(CTxOut(spend.tx.vout[spend.n].nValue - 1, CScript([OP_TRUE])))
437-
self.sign_tx(tx, spend.tx, spend.n)
430+
tx = self.create_tx(spend, 0, 1, p2sh_script)
431+
tx.vout.append(CTxOut(spend.vout[0].nValue - 1, CScript([OP_TRUE])))
432+
self.sign_tx(tx, spend)
438433
tx.rehash()
439434
b39 = self.update_block(39, [tx])
440435
b39_outputs += 1
@@ -548,7 +543,7 @@ def run_test(self):
548543
self.sync_blocks([b44], True)
549544

550545
self.log.info("Reject a block with a non-coinbase as the first tx")
551-
non_coinbase = self.create_tx(out[15].tx, out[15].n, 1)
546+
non_coinbase = self.create_tx(out[15], 0, 1)
552547
b45 = CBlock()
553548
b45.nTime = self.tip.nTime + 1
554549
b45.hashPrevBlock = self.tip.sha256
@@ -675,7 +670,7 @@ def run_test(self):
675670
# b57 - a good block with 2 txs, don't submit until end
676671
self.move_tip(55)
677672
b57 = self.next_block(57)
678-
tx = self.create_and_sign_transaction(out[16].tx, out[16].n, 1)
673+
tx = self.create_and_sign_transaction(out[16], 1)
679674
tx1 = self.create_tx(tx, 0, 1)
680675
b57 = self.update_block(57, [tx, tx1])
681676

@@ -692,7 +687,7 @@ def run_test(self):
692687
# b57p2 - a good block with 6 tx'es, don't submit until end
693688
self.move_tip(55)
694689
b57p2 = self.next_block("57p2")
695-
tx = self.create_and_sign_transaction(out[16].tx, out[16].n, 1)
690+
tx = self.create_and_sign_transaction(out[16], 1)
696691
tx1 = self.create_tx(tx, 0, 1)
697692
tx2 = self.create_tx(tx1, 0, 1)
698693
tx3 = self.create_tx(tx2, 0, 1)
@@ -727,8 +722,8 @@ def run_test(self):
727722
self.move_tip(57)
728723
b58 = self.next_block(58, spend=out[17])
729724
tx = CTransaction()
730-
assert(len(out[17].tx.vout) < 42)
731-
tx.vin.append(CTxIn(COutPoint(out[17].tx.sha256, 42), CScript([OP_TRUE]), 0xffffffff))
725+
assert(len(out[17].vout) < 42)
726+
tx.vin.append(CTxIn(COutPoint(out[17].sha256, 42), CScript([OP_TRUE]), 0xffffffff))
732727
tx.vout.append(CTxOut(0, b""))
733728
tx.calc_sha256()
734729
b58 = self.update_block(58, [tx])
@@ -738,7 +733,7 @@ def run_test(self):
738733
self.log.info("Reject a block with a transaction with outputs > inputs")
739734
self.move_tip(57)
740735
b59 = self.next_block(59)
741-
tx = self.create_and_sign_transaction(out[17].tx, out[17].n, 51 * COIN)
736+
tx = self.create_and_sign_transaction(out[17], 51 * COIN)
742737
b59 = self.update_block(59, [tx])
743738
self.sync_blocks([b59], False, 16, b'bad-txns-in-belowout', reconnect=True)
744739

@@ -776,8 +771,7 @@ def run_test(self):
776771
b62 = self.next_block(62)
777772
tx = CTransaction()
778773
tx.nLockTime = 0xffffffff # this locktime is non-final
779-
assert(out[18].n < len(out[18].tx.vout))
780-
tx.vin.append(CTxIn(COutPoint(out[18].tx.sha256, out[18].n))) # don't set nSequence
774+
tx.vin.append(CTxIn(COutPoint(out[18].sha256, 0))) # don't set nSequence
781775
tx.vout.append(CTxOut(0, CScript([OP_TRUE])))
782776
assert(tx.vin[0].nSequence < 0xffffffff)
783777
tx.calc_sha256()
@@ -856,8 +850,8 @@ def run_test(self):
856850
self.log.info("Accept a block with a transaction spending an output created in the same block")
857851
self.move_tip(64)
858852
b65 = self.next_block(65)
859-
tx1 = self.create_and_sign_transaction(out[19].tx, out[19].n, out[19].tx.vout[0].nValue)
860-
tx2 = self.create_and_sign_transaction(tx1, 0, 0)
853+
tx1 = self.create_and_sign_transaction(out[19], out[19].vout[0].nValue)
854+
tx2 = self.create_and_sign_transaction(tx1, 0)
861855
b65 = self.update_block(65, [tx1, tx2])
862856
self.sync_blocks([b65], True)
863857
self.save_spendable_output()
@@ -869,8 +863,8 @@ def run_test(self):
869863
self.log.info("Reject a block with a transaction spending an output created later in the same block")
870864
self.move_tip(65)
871865
b66 = self.next_block(66)
872-
tx1 = self.create_and_sign_transaction(out[20].tx, out[20].n, out[20].tx.vout[0].nValue)
873-
tx2 = self.create_and_sign_transaction(tx1, 0, 1)
866+
tx1 = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue)
867+
tx2 = self.create_and_sign_transaction(tx1, 1)
874868
b66 = self.update_block(66, [tx2, tx1])
875869
self.sync_blocks([b66], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
876870

@@ -883,9 +877,9 @@ def run_test(self):
883877
self.log.info("Reject a block with a transaction double spending a transaction creted in the same block")
884878
self.move_tip(65)
885879
b67 = self.next_block(67)
886-
tx1 = self.create_and_sign_transaction(out[20].tx, out[20].n, out[20].tx.vout[0].nValue)
887-
tx2 = self.create_and_sign_transaction(tx1, 0, 1)
888-
tx3 = self.create_and_sign_transaction(tx1, 0, 2)
880+
tx1 = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue)
881+
tx2 = self.create_and_sign_transaction(tx1, 1)
882+
tx3 = self.create_and_sign_transaction(tx1, 2)
889883
b67 = self.update_block(67, [tx1, tx2, tx3])
890884
self.sync_blocks([b67], False, 16, b'bad-txns-inputs-missingorspent', reconnect=True)
891885

@@ -904,14 +898,14 @@ def run_test(self):
904898
self.log.info("Reject a block trying to claim too much subsidy in the coinbase transaction")
905899
self.move_tip(65)
906900
b68 = self.next_block(68, additional_coinbase_value=10)
907-
tx = self.create_and_sign_transaction(out[20].tx, out[20].n, out[20].tx.vout[0].nValue - 9)
901+
tx = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue - 9)
908902
b68 = self.update_block(68, [tx])
909903
self.sync_blocks([b68], False, 16, b'bad-cb-amount', reconnect=True)
910904

911905
self.log.info("Accept a block claiming the correct subsidy in the coinbase transaction")
912906
self.move_tip(65)
913907
b69 = self.next_block(69, additional_coinbase_value=10)
914-
tx = self.create_and_sign_transaction(out[20].tx, out[20].n, out[20].tx.vout[0].nValue - 10)
908+
tx = self.create_and_sign_transaction(out[20], out[20].vout[0].nValue - 10)
915909
self.update_block(69, [tx])
916910
self.sync_blocks([b69], True)
917911
self.save_spendable_output()
@@ -942,8 +936,8 @@ def run_test(self):
942936
self.log.info("Reject a block containing a duplicate transaction but with the same Merkle root (Merkle tree malleability")
943937
self.move_tip(69)
944938
b72 = self.next_block(72)
945-
tx1 = self.create_and_sign_transaction(out[21].tx, out[21].n, 2)
946-
tx2 = self.create_and_sign_transaction(tx1, 0, 1)
939+
tx1 = self.create_and_sign_transaction(out[21], 2)
940+
tx2 = self.create_and_sign_transaction(tx1, 1)
947941
b72 = self.update_block(72, [tx1, tx2]) # now tip is 72
948942
b71 = copy.deepcopy(b72)
949943
b71.vtx.append(tx2) # add duplicate tx2
@@ -990,7 +984,7 @@ def run_test(self):
990984
a[MAX_BLOCK_SIGOPS + 2] = 0
991985
a[MAX_BLOCK_SIGOPS + 3] = 0
992986

993-
tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a))
987+
tx = self.create_and_sign_transaction(out[22], 1, CScript(a))
994988
b73 = self.update_block(73, [tx])
995989
assert_equal(get_legacy_sigopcount_block(b73), MAX_BLOCK_SIGOPS + 1)
996990
self.sync_blocks([b73], False, 16, b'bad-blk-sigops', reconnect=True)
@@ -1015,7 +1009,7 @@ def run_test(self):
10151009
a[MAX_BLOCK_SIGOPS + 2] = 0xff
10161010
a[MAX_BLOCK_SIGOPS + 3] = 0xff
10171011
a[MAX_BLOCK_SIGOPS + 4] = 0xff
1018-
tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a))
1012+
tx = self.create_and_sign_transaction(out[22], 1, CScript(a))
10191013
b74 = self.update_block(74, [tx])
10201014
self.sync_blocks([b74], False, 16, b'bad-blk-sigops', reconnect=True)
10211015

@@ -1028,7 +1022,7 @@ def run_test(self):
10281022
a[MAX_BLOCK_SIGOPS + 1] = 0xff
10291023
a[MAX_BLOCK_SIGOPS + 2] = 0xff
10301024
a[MAX_BLOCK_SIGOPS + 3] = 0xff
1031-
tx = self.create_and_sign_transaction(out[22].tx, 0, 1, CScript(a))
1025+
tx = self.create_and_sign_transaction(out[22], 1, CScript(a))
10321026
b75 = self.update_block(75, [tx])
10331027
self.sync_blocks([b75], True)
10341028
self.save_spendable_output()
@@ -1039,7 +1033,7 @@ def run_test(self):
10391033
size = MAX_BLOCK_SIGOPS - 1 + MAX_SCRIPT_ELEMENT_SIZE + 1 + 5
10401034
a = bytearray([OP_CHECKSIG] * size)
10411035
a[MAX_BLOCK_SIGOPS - 1] = 0x4e # PUSHDATA4, but leave the following bytes as just checksigs
1042-
tx = self.create_and_sign_transaction(out[23].tx, 0, 1, CScript(a))
1036+
tx = self.create_and_sign_transaction(out[23], 1, CScript(a))
10431037
b76 = self.update_block(76, [tx])
10441038
self.sync_blocks([b76], True)
10451039
self.save_spendable_output()
@@ -1064,7 +1058,7 @@ def run_test(self):
10641058
self.log.info("Test transaction resurrection during a re-org")
10651059
self.move_tip(76)
10661060
b77 = self.next_block(77)
1067-
tx77 = self.create_and_sign_transaction(out[24].tx, out[24].n, 10 * COIN)
1061+
tx77 = self.create_and_sign_transaction(out[24], 10 * COIN)
10681062
b77 = self.update_block(77, [tx77])
10691063
self.sync_blocks([b77], True)
10701064
self.save_spendable_output()
@@ -1109,9 +1103,9 @@ def run_test(self):
11091103
b83 = self.next_block(83)
11101104
op_codes = [OP_IF, OP_INVALIDOPCODE, OP_ELSE, OP_TRUE, OP_ENDIF]
11111105
script = CScript(op_codes)
1112-
tx1 = self.create_and_sign_transaction(out[28].tx, out[28].n, out[28].tx.vout[0].nValue, script)
1106+
tx1 = self.create_and_sign_transaction(out[28], out[28].vout[0].nValue, script)
11131107

1114-
tx2 = self.create_and_sign_transaction(tx1, 0, 0, CScript([OP_TRUE]))
1108+
tx2 = self.create_and_sign_transaction(tx1, 0, CScript([OP_TRUE]))
11151109
tx2.vin[0].scriptSig = CScript([OP_FALSE])
11161110
tx2.rehash()
11171111

@@ -1126,13 +1120,13 @@ def run_test(self):
11261120
#
11271121
self.log.info("Test re-orging blocks with OP_RETURN in them")
11281122
b84 = self.next_block(84)
1129-
tx1 = self.create_tx(out[29].tx, out[29].n, 0, CScript([OP_RETURN]))
1123+
tx1 = self.create_tx(out[29], 0, 0, CScript([OP_RETURN]))
11301124
tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
11311125
tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
11321126
tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
11331127
tx1.vout.append(CTxOut(0, CScript([OP_TRUE])))
11341128
tx1.calc_sha256()
1135-
self.sign_tx(tx1, out[29].tx, out[29].n)
1129+
self.sign_tx(tx1, out[29])
11361130
tx1.rehash()
11371131
tx2 = self.create_tx(tx1, 1, 0, CScript([OP_RETURN]))
11381132
tx2.vout.append(CTxOut(0, CScript([OP_RETURN])))
@@ -1217,21 +1211,21 @@ def add_transactions_to_block(self, block, tx_list):
12171211

12181212
# this is a little handier to use than the version in blocktools.py
12191213
def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])):
1220-
return create_tx_with_script(spend_tx, n, b"", value, script)
1214+
return create_tx_with_script(spend_tx, n, amount=value, script_pub_key=script)
12211215

12221216
# sign a transaction, using the key we know about
12231217
# this signs input 0 in tx, which is assumed to be spending output n in spend_tx
1224-
def sign_tx(self, tx, spend_tx, n):
1225-
scriptPubKey = bytearray(spend_tx.vout[n].scriptPubKey)
1218+
def sign_tx(self, tx, spend_tx):
1219+
scriptPubKey = bytearray(spend_tx.vout[0].scriptPubKey)
12261220
if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend
12271221
tx.vin[0].scriptSig = CScript()
12281222
return
1229-
(sighash, err) = SignatureHash(spend_tx.vout[n].scriptPubKey, tx, 0, SIGHASH_ALL)
1223+
(sighash, err) = SignatureHash(spend_tx.vout[0].scriptPubKey, tx, 0, SIGHASH_ALL)
12301224
tx.vin[0].scriptSig = CScript([self.coinbase_key.sign(sighash) + bytes(bytearray([SIGHASH_ALL]))])
12311225

1232-
def create_and_sign_transaction(self, spend_tx, n, value, script=CScript([OP_TRUE])):
1233-
tx = self.create_tx(spend_tx, n, value, script)
1234-
self.sign_tx(tx, spend_tx, n)
1226+
def create_and_sign_transaction(self, spend_tx, value, script=CScript([OP_TRUE])):
1227+
tx = self.create_tx(spend_tx, 0, value, script)
1228+
self.sign_tx(tx, spend_tx)
12351229
tx.rehash()
12361230
return tx
12371231

@@ -1250,11 +1244,11 @@ def next_block(self, number, spend=None, additional_coinbase_value=0, script=CSc
12501244
if spend is None:
12511245
block = create_block(base_block_hash, coinbase, block_time)
12521246
else:
1253-
coinbase.vout[0].nValue += spend.tx.vout[spend.n].nValue - 1 # all but one satoshi to fees
1247+
coinbase.vout[0].nValue += spend.vout[0].nValue - 1 # all but one satoshi to fees
12541248
coinbase.rehash()
12551249
block = create_block(base_block_hash, coinbase, block_time)
1256-
tx = create_tx_with_script(spend.tx, spend.n, b"", 1, script) # spend 1 satoshi
1257-
self.sign_tx(tx, spend.tx, spend.n)
1250+
tx = self.create_tx(spend, 0, 1, script) # spend 1 satoshi
1251+
self.sign_tx(tx, spend)
12581252
self.add_transactions_to_block(block, [tx])
12591253
block.hashMerkleRoot = block.calc_merkle_root()
12601254
if solve:
@@ -1273,7 +1267,7 @@ def save_spendable_output(self):
12731267
# get an output that we previously marked as spendable
12741268
def get_spendable_output(self):
12751269
self.log.debug("getting spendable output %s" % self.spendable_outputs[0].vtx[0])
1276-
return PreviousSpendableOutput(self.spendable_outputs.pop(0).vtx[0], 0)
1270+
return self.spendable_outputs.pop(0).vtx[0]
12771271

12781272
# move the tip back to a previous block
12791273
def move_tip(self, number):

0 commit comments

Comments
 (0)