Skip to content

Commit facbfa5

Browse files
author
MarcoFalke
committed
[qa] Get rid of duplicate code
1 parent e22f409 commit facbfa5

File tree

3 files changed

+19
-82
lines changed

3 files changed

+19
-82
lines changed

qa/rpc-tests/maxuploadtarget.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -81,49 +81,16 @@ def received_pong():
8181

8282
class MaxUploadTest(BitcoinTestFramework):
8383

84-
def add_options(self, parser):
85-
parser.add_option("--testbinary", dest="testbinary",
86-
default=os.getenv("BITCOIND", "bitcoind"),
87-
help="bitcoind binary to test")
88-
8984
def __init__(self):
9085
super().__init__()
9186
self.setup_clean_chain = True
9287
self.num_nodes = 1
9388

94-
self.utxo = []
95-
self.txouts = gen_return_txouts()
96-
9789
def setup_network(self):
9890
# Start a node with maxuploadtarget of 200 MB (/24h)
9991
self.nodes = []
10092
self.nodes.append(start_node(0, self.options.tmpdir, ["-debug", "-maxuploadtarget=800", "-blockmaxsize=999000"]))
10193

102-
def mine_full_block(self, node, address):
103-
# Want to create a full block
104-
# We'll generate a 66k transaction below, and 14 of them is close to the 1MB block limit
105-
for j in range(14):
106-
if len(self.utxo) < 14:
107-
self.utxo = node.listunspent()
108-
inputs=[]
109-
outputs = {}
110-
t = self.utxo.pop()
111-
inputs.append({ "txid" : t["txid"], "vout" : t["vout"]})
112-
remchange = t["amount"] - Decimal("0.001000")
113-
outputs[address]=remchange
114-
# Create a basic transaction that will send change back to ourself after account for a fee
115-
# And then insert the 128 generated transaction outs in the middle rawtx[92] is where the #
116-
# of txouts is stored and is the only thing we overwrite from the original transaction
117-
rawtx = node.createrawtransaction(inputs, outputs)
118-
newtx = rawtx[0:92]
119-
newtx = newtx + self.txouts
120-
newtx = newtx + rawtx[94:]
121-
# Appears to be ever so slightly faster to sign with SIGHASH_NONE
122-
signresult = node.signrawtransaction(newtx,None,None,"NONE")
123-
txid = node.sendrawtransaction(signresult["hex"], True)
124-
# Mine a full sized block which will be these transactions we just created
125-
node.generate(1)
126-
12794
def run_test(self):
12895
# Before we connect anything, we first set the time on the node
12996
# to be in the past, otherwise things break because the CNode
@@ -151,7 +118,7 @@ def run_test(self):
151118
# Test logic begins here
152119

153120
# Now mine a big block
154-
self.mine_full_block(self.nodes[0], self.nodes[0].getnewaddress())
121+
mine_large_block(self.nodes[0])
155122

156123
# Store the hash; we'll request this later
157124
big_old_block = self.nodes[0].getbestblockhash()
@@ -162,11 +129,10 @@ def run_test(self):
162129
self.nodes[0].setmocktime(int(time.time()) - 2*60*60*24)
163130

164131
# Mine one more block, so that the prior block looks old
165-
self.mine_full_block(self.nodes[0], self.nodes[0].getnewaddress())
132+
mine_large_block(self.nodes[0])
166133

167134
# We'll be requesting this new block too
168135
big_new_block = self.nodes[0].getbestblockhash()
169-
new_block_size = self.nodes[0].getblock(big_new_block)['size']
170136
big_new_block = int(big_new_block, 16)
171137

172138
# test_nodes[0] will test what happens if we just keep requesting the

qa/rpc-tests/pruning.py

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ def __init__(self):
2424
self.setup_clean_chain = True
2525
self.num_nodes = 3
2626

27-
self.utxo = []
28-
self.address = ["",""]
29-
self.txouts = gen_return_txouts()
30-
3127
def setup_network(self):
3228
self.nodes = []
3329
self.is_network_split = False
@@ -40,12 +36,6 @@ def setup_network(self):
4036
self.nodes.append(start_node(2, self.options.tmpdir, ["-debug","-maxreceivebuffer=20000","-prune=550"], timewait=900))
4137
self.prunedir = self.options.tmpdir+"/node2/regtest/blocks/"
4238

43-
self.address[0] = self.nodes[0].getnewaddress()
44-
self.address[1] = self.nodes[1].getnewaddress()
45-
46-
# Determine default relay fee
47-
self.relayfee = self.nodes[0].getnetworkinfo()["relayfee"]
48-
4939
connect_nodes(self.nodes[0], 1)
5040
connect_nodes(self.nodes[1], 2)
5141
connect_nodes(self.nodes[2], 0)
@@ -58,7 +48,7 @@ def create_big_chain(self):
5848
self.nodes[0].generate(150)
5949
# Then mine enough full blocks to create more than 550MiB of data
6050
for i in range(645):
61-
self.mine_full_block(self.nodes[0], self.address[0])
51+
mine_large_block(self.nodes[0])
6252

6353
sync_blocks(self.nodes[0:3])
6454

@@ -70,7 +60,7 @@ def test_height_min(self):
7060
print("Mining 25 more blocks should cause the first block file to be pruned")
7161
# Pruning doesn't run until we're allocating another chunk, 20 full blocks past the height cutoff will ensure this
7262
for i in range(25):
73-
self.mine_full_block(self.nodes[0],self.address[0])
63+
mine_large_block(self.nodes[0])
7464

7565
waitstart = time.time()
7666
while os.path.isfile(self.prunedir+"blk00000.dat"):
@@ -95,17 +85,15 @@ def create_chain_with_staleblocks(self):
9585
stop_node(self.nodes[0],0)
9686
self.nodes[0]=start_node(0, self.options.tmpdir, ["-debug","-maxreceivebuffer=20000","-blockmaxsize=999000", "-checkblocks=5"], timewait=900)
9787
# Mine 24 blocks in node 1
98-
self.utxo = self.nodes[1].listunspent()
9988
for i in range(24):
10089
if j == 0:
101-
self.mine_full_block(self.nodes[1],self.address[1])
90+
mine_large_block(self.nodes[1])
10291
else:
10392
self.nodes[1].generate(1) #tx's already in mempool from previous disconnects
10493

10594
# Reorg back with 25 block chain from node 0
106-
self.utxo = self.nodes[0].listunspent()
10795
for i in range(25):
108-
self.mine_full_block(self.nodes[0],self.address[0])
96+
mine_large_block(self.nodes[0])
10997

11098
# Create connections in the order so both nodes can see the reorg at the same time
11199
connect_nodes(self.nodes[1], 0)
@@ -217,31 +205,6 @@ def reorg_back(self):
217205
# Verify we can now have the data for a block previously pruned
218206
assert(self.nodes[2].getblock(self.forkhash)["height"] == self.forkheight)
219207

220-
def mine_full_block(self, node, address):
221-
# Want to create a full block
222-
# We'll generate a 66k transaction below, and 14 of them is close to the 1MB block limit
223-
for j in range(14):
224-
if len(self.utxo) < 14:
225-
self.utxo = node.listunspent()
226-
inputs=[]
227-
outputs = {}
228-
t = self.utxo.pop()
229-
inputs.append({ "txid" : t["txid"], "vout" : t["vout"]})
230-
remchange = t["amount"] - 100*self.relayfee # Fee must be above min relay rate for 66kb tx
231-
outputs[address]=remchange
232-
# Create a basic transaction that will send change back to ourself after account for a fee
233-
# And then insert the 128 generated transaction outs in the middle rawtx[92] is where the #
234-
# of txouts is stored and is the only thing we overwrite from the original transaction
235-
rawtx = node.createrawtransaction(inputs, outputs)
236-
newtx = rawtx[0:92]
237-
newtx = newtx + self.txouts
238-
newtx = newtx + rawtx[94:]
239-
# Appears to be ever so slightly faster to sign with SIGHASH_NONE
240-
signresult = node.signrawtransaction(newtx,None,None,"NONE")
241-
txid = node.sendrawtransaction(signresult["hex"], True)
242-
# Mine a full sized block which will be these transactions we just created
243-
node.generate(1)
244-
245208

246209
def run_test(self):
247210
print("Warning! This test requires 4GB of disk space and takes over 30 mins (up to 2 hours)")

qa/rpc-tests/test_framework/util.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -657,13 +657,12 @@ def create_tx(node, coinbase, to_address, amount):
657657
def create_lots_of_big_transactions(node, txouts, utxos, fee):
658658
addr = node.getnewaddress()
659659
txids = []
660-
for i in range(len(utxos)):
660+
for _ in range(len(utxos)):
661661
t = utxos.pop()
662-
inputs = []
663-
inputs.append({ "txid" : t["txid"], "vout" : t["vout"]})
662+
inputs=[{ "txid" : t["txid"], "vout" : t["vout"]}]
664663
outputs = {}
665-
send_value = t['amount'] - fee
666-
outputs[addr] = satoshi_round(send_value)
664+
change = t['amount'] - fee
665+
outputs[addr] = satoshi_round(change)
667666
rawtx = node.createrawtransaction(inputs, outputs)
668667
newtx = rawtx[0:92]
669668
newtx = newtx + txouts
@@ -673,6 +672,15 @@ def create_lots_of_big_transactions(node, txouts, utxos, fee):
673672
txids.append(txid)
674673
return txids
675674

675+
def mine_large_block(node):
676+
# generate a 66k transaction,
677+
# and 14 of them is close to the 1MB block limit
678+
txouts = gen_return_txouts()
679+
utxos = node.listunspent()[:14]
680+
fee = 100 * node.getnetworkinfo()["relayfee"]
681+
create_lots_of_big_transactions(node, txouts, utxos, fee=fee)
682+
node.generate(1)
683+
676684
def get_bip9_status(node, key):
677685
info = node.getblockchaininfo()
678686
return info['bip9_softforks'][key]

0 commit comments

Comments
 (0)