Skip to content

Commit dac7a11

Browse files
committed
refactor: test: use _ variable for unused loop counters
substitutes "for x in range(N):" by "for _ in range(N):" indicates to the reader that a block is just repeated N times, and that the loop counter is not used in the body
1 parent 82127d2 commit dac7a11

35 files changed

+83
-83
lines changed

test/functional/example_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ def run_test(self):
166166

167167
height = self.nodes[0].getblockcount()
168168

169-
for i in range(10):
169+
for _ in range(10):
170170
# Use the mininode and blocktools functionality to manually build a block
171171
# Calling the generate() rpc is easier, but this allows us to exactly
172172
# control the blocks and transactions.

test/functional/feature_assumevalid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def run_test(self):
123123
height += 1
124124

125125
# Bury the block 100 deep so the coinbase output is spendable
126-
for i in range(100):
126+
for _ in range(100):
127127
block = create_block(self.tip, create_coinbase(height), self.block_time)
128128
block.solve()
129129
self.blocks.append(block)
@@ -149,7 +149,7 @@ def run_test(self):
149149
height += 1
150150

151151
# Bury the assumed valid block 2100 deep
152-
for i in range(2100):
152+
for _ in range(2100):
153153
block = create_block(self.tip, create_coinbase(height), self.block_time)
154154
block.nVersion = 4
155155
block.solve()

test/functional/feature_bip68_sequence.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def test_sequence_lock_confirmed_inputs(self):
141141
# some of those inputs to be sequence locked (and randomly choose
142142
# between height/time locking). Small random chance of making the locks
143143
# all pass.
144-
for i in range(400):
144+
for _ in range(400):
145145
# Randomly choose up to 10 inputs
146146
num_inputs = random.randint(1, 10)
147147
random.shuffle(utxos)
@@ -260,7 +260,7 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
260260
# Use prioritisetransaction to lower the effective feerate to 0
261261
self.nodes[0].prioritisetransaction(txid=tx2.hash, fee_delta=int(-self.relayfee*COIN))
262262
cur_time = int(time.time())
263-
for i in range(10):
263+
for _ in range(10):
264264
self.nodes[0].setmocktime(cur_time + 600)
265265
self.nodes[0].generate(1)
266266
cur_time += 600

test/functional/feature_block.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def run_test(self):
125125

126126
# collect spendable outputs now to avoid cluttering the code later on
127127
out = []
128-
for i in range(NUM_OUTPUTS_TO_COLLECT):
128+
for _ in range(NUM_OUTPUTS_TO_COLLECT):
129129
out.append(self.get_spendable_output())
130130

131131
# Start by building a couple of blocks on top (which output is spent is

test/functional/feature_csv_activation.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def skip_test_if_missing_module(self):
161161

162162
def generate_blocks(self, number):
163163
test_blocks = []
164-
for i in range(number):
164+
for _ in range(number):
165165
block = self.create_test_block([])
166166
test_blocks.append(block)
167167
self.last_block_time += 600
@@ -209,22 +209,22 @@ def run_test(self):
209209
# Note we reuse inputs for v1 and v2 txs so must test these separately
210210
# 16 normal inputs
211211
bip68inputs = []
212-
for i in range(16):
212+
for _ in range(16):
213213
bip68inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
214214

215215
# 2 sets of 16 inputs with 10 OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
216216
bip112basicinputs = []
217-
for j in range(2):
217+
for _ in range(2):
218218
inputs = []
219-
for i in range(16):
219+
for _ in range(16):
220220
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
221221
bip112basicinputs.append(inputs)
222222

223223
# 2 sets of 16 varied inputs with (relative_lock_time) OP_CSV OP_DROP (actually will be prepended to spending scriptSig)
224224
bip112diverseinputs = []
225-
for j in range(2):
225+
for _ in range(2):
226226
inputs = []
227-
for i in range(16):
227+
for _ in range(16):
228228
inputs.append(send_generic_input_tx(self.nodes[0], self.coinbase_blocks, self.nodeaddress))
229229
bip112diverseinputs.append(inputs)
230230

test/functional/feature_dbcrash.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def generate_small_transactions(self, node, count, utxo_list):
195195
while len(utxo_list) >= 2 and num_transactions < count:
196196
tx = CTransaction()
197197
input_amount = 0
198-
for i in range(2):
198+
for _ in range(2):
199199
utxo = utxo_list.pop()
200200
tx.vin.append(CTxIn(COutPoint(int(utxo['txid'], 16), utxo['vout'])))
201201
input_amount += int(utxo['amount'] * COIN)
@@ -205,7 +205,7 @@ def generate_small_transactions(self, node, count, utxo_list):
205205
# Sanity check -- if we chose inputs that are too small, skip
206206
continue
207207

208-
for i in range(3):
208+
for _ in range(3):
209209
tx.vout.append(CTxOut(output_amount, hex_str_to_bytes(utxo['scriptPubKey'])))
210210

211211
# Sign and send the transaction to get into the mempool

test/functional/feature_fee_estimation.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ def transact_and_mine(self, numblocks, mining_node):
176176
# We shuffle our confirmed txout set before each set of transactions
177177
# small_txpuzzle_randfee will use the transactions that have inputs already in the chain when possible
178178
# resorting to tx's that depend on the mempool when those run out
179-
for i in range(numblocks):
179+
for _ in range(numblocks):
180180
random.shuffle(self.confutxo)
181-
for j in range(random.randrange(100 - 50, 100 + 50)):
181+
for _ in range(random.randrange(100 - 50, 100 + 50)):
182182
from_index = random.randint(1, 2)
183183
(txhex, fee) = small_txpuzzle_randfee(self.nodes[from_index], self.confutxo,
184184
self.memutxo, Decimal("0.005"), min_fee, min_fee)
@@ -243,7 +243,7 @@ def run_test(self):
243243
self.confutxo = self.txouts # Start with the set of confirmed txouts after splitting
244244
self.log.info("Will output estimates for 1/2/3/6/15/25 blocks")
245245

246-
for i in range(2):
246+
for _ in range(2):
247247
self.log.info("Creating transactions and mining them with a block size that can't keep up")
248248
# Create transactions and mine 10 small blocks with node 2, but create txs faster than we can mine
249249
self.transact_and_mine(10, self.nodes[2])

test/functional/feature_maxuploadtarget.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def run_test(self):
104104
assert_equal(len(self.nodes[0].getpeerinfo()), 3)
105105
# At most a couple more tries should succeed (depending on how long
106106
# the test has been running so far).
107-
for i in range(3):
107+
for _ in range(3):
108108
p2p_conns[0].send_message(getdata_request)
109109
p2p_conns[0].wait_for_disconnect()
110110
assert_equal(len(self.nodes[0].getpeerinfo()), 2)

test/functional/feature_pruning.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def create_chain_with_staleblocks(self):
147147
# Create stale blocks in manageable sized chunks
148148
self.log.info("Mine 24 (stale) blocks on Node 1, followed by 25 (main chain) block reorg from Node 0, for 12 rounds")
149149

150-
for j in range(12):
150+
for _ in range(12):
151151
# Disconnect node 0 so it can mine a longer reorg chain without knowing about node 1's soon-to-be-stale chain
152152
# Node 2 stays connected, so it hears about the stale blocks and then reorg's when node0 reconnects
153153
disconnect_nodes(self.nodes[0], 1)

test/functional/feature_rbf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def test_too_many_replacements(self):
376376
split_value = int((initial_nValue-fee)/(MAX_REPLACEMENT_LIMIT+1))
377377

378378
outputs = []
379-
for i in range(MAX_REPLACEMENT_LIMIT+1):
379+
for _ in range(MAX_REPLACEMENT_LIMIT+1):
380380
outputs.append(CTxOut(split_value, CScript([1])))
381381

382382
splitting_tx = CTransaction()

test/functional/feature_segwit.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ def run_test(self):
126126
assert_equal(bip173_ms_addr, script_to_p2wsh(multiscript))
127127
p2sh_ids.append([])
128128
wit_ids.append([])
129-
for v in range(2):
129+
for _ in range(2):
130130
p2sh_ids[i].append([])
131131
wit_ids[i].append([])
132132

133-
for i in range(5):
133+
for _ in range(5):
134134
for n in range(3):
135135
for v in range(2):
136136
wit_ids[n][v].append(send_to_witness(v, self.nodes[0], find_spendable_utxo(self.nodes[0], 50), self.pubkey[n], False, Decimal("49.999")))

test/functional/mempool_package_onemore.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def chain_transaction(self, node, parent_txids, vouts, value, fee, num_outputs):
3131
for (txid, vout) in zip(parent_txids, vouts):
3232
inputs.append({'txid' : txid, 'vout' : vout})
3333
outputs = {}
34-
for i in range(num_outputs):
34+
for _ in range(num_outputs):
3535
outputs[node.getnewaddress()] = send_value
3636
rawtx = node.createrawtransaction(inputs, outputs, 0, True)
3737
signedtx = node.signrawtransactionwithwallet(rawtx)

test/functional/mempool_packages.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def chain_transaction(self, node, parent_txid, vout, value, fee, num_outputs):
4848
send_value = satoshi_round((value - fee)/num_outputs)
4949
inputs = [ {'txid' : parent_txid, 'vout' : vout} ]
5050
outputs = {}
51-
for i in range(num_outputs):
51+
for _ in range(num_outputs):
5252
outputs[node.getnewaddress()] = send_value
5353
rawtx = node.createrawtransaction(inputs, outputs)
5454
signedtx = node.signrawtransactionwithwallet(rawtx)
@@ -70,7 +70,7 @@ def run_test(self):
7070
# MAX_ANCESTORS transactions off a confirmed tx should be fine
7171
chain = []
7272
witness_chain = []
73-
for i in range(MAX_ANCESTORS):
73+
for _ in range(MAX_ANCESTORS):
7474
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, 0, value, fee, 1)
7575
value = sent_value
7676
chain.append(txid)
@@ -245,7 +245,7 @@ def run_test(self):
245245

246246
# Sign and send up to MAX_DESCENDANT transactions chained off the parent tx
247247
chain = [] # save sent txs for the purpose of checking node1's mempool later (see below)
248-
for i in range(MAX_DESCENDANTS - 1):
248+
for _ in range(MAX_DESCENDANTS - 1):
249249
utxo = transaction_package.pop(0)
250250
(txid, sent_value) = self.chain_transaction(self.nodes[0], utxo['txid'], utxo['vout'], utxo['amount'], fee, 10)
251251
chain.append(txid)
@@ -312,7 +312,7 @@ def run_test(self):
312312
send_value = satoshi_round((value - fee)/2)
313313
inputs = [ {'txid' : txid, 'vout' : vout} ]
314314
outputs = {}
315-
for i in range(2):
315+
for _ in range(2):
316316
outputs[self.nodes[0].getnewaddress()] = send_value
317317
rawtx = self.nodes[0].createrawtransaction(inputs, outputs)
318318
signedtx = self.nodes[0].signrawtransactionwithwallet(rawtx)
@@ -326,7 +326,7 @@ def run_test(self):
326326
# Create tx2-7
327327
vout = 1
328328
txid = tx0_id
329-
for i in range(6):
329+
for _ in range(6):
330330
(txid, sent_value) = self.chain_transaction(self.nodes[0], txid, vout, value, fee, 1)
331331
vout = 0
332332
value = sent_value

test/functional/mempool_persist.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def skip_test_if_missing_module(self):
6262
def run_test(self):
6363
self.log.debug("Send 5 transactions from node2 (to its own address)")
6464
tx_creation_time_lower = int(time.time())
65-
for i in range(5):
65+
for _ in range(5):
6666
last_txid = self.nodes[2].sendtoaddress(self.nodes[2].getnewaddress(), Decimal("10"))
6767
node2_balance = self.nodes[2].getbalance()
6868
self.sync_all()

test/functional/mempool_updatefromblock.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def transaction_graph_test(self, size, n_tx_to_mine=None, start_input_txid='', e
7373
n_outputs = size - tx_count
7474
output_value = ((inputs_value - fee) / Decimal(n_outputs)).quantize(Decimal('0.00000001'))
7575
outputs = {}
76-
for n in range(0, n_outputs):
76+
for _ in range(n_outputs):
7777
outputs[self.nodes[0].getnewaddress()] = output_value
7878
else:
7979
output_value = (inputs_value - fee).quantize(Decimal('0.00000001'))

test/functional/p2p_compactblocks.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def make_utxos(self):
125125
out_value = total_value // 10
126126
tx = CTransaction()
127127
tx.vin.append(CTxIn(COutPoint(block.vtx[0].sha256, 0), b''))
128-
for i in range(10):
128+
for _ in range(10):
129129
tx.vout.append(CTxOut(out_value, CScript([OP_TRUE])))
130130
tx.rehash()
131131

@@ -266,7 +266,7 @@ def test_compactblock_construction(self, test_node, use_witness_address=True):
266266
address = node.getnewaddress()
267267

268268
segwit_tx_generated = False
269-
for i in range(num_transactions):
269+
for _ in range(num_transactions):
270270
txid = node.sendtoaddress(address, 0.1)
271271
hex_tx = node.gettransaction(txid)["hex"]
272272
tx = FromHex(CTransaction(), hex_tx)
@@ -418,7 +418,7 @@ def test_compactblock_requests(self, test_node, segwit=True):
418418
def build_block_with_transactions(self, node, utxo, num_transactions):
419419
block = self.build_block_on_tip(node)
420420

421-
for i in range(num_transactions):
421+
for _ in range(num_transactions):
422422
tx = CTransaction()
423423
tx.vin.append(CTxIn(COutPoint(utxo[0], utxo[1]), b''))
424424
tx.vout.append(CTxOut(utxo[2] - 1000, CScript([OP_TRUE, OP_DROP] * 15 + [OP_TRUE])))
@@ -627,7 +627,7 @@ def test_compactblocks_not_at_tip(self, test_node):
627627
# Test that requesting old compactblocks doesn't work.
628628
MAX_CMPCTBLOCK_DEPTH = 5
629629
new_blocks = []
630-
for i in range(MAX_CMPCTBLOCK_DEPTH + 1):
630+
for _ in range(MAX_CMPCTBLOCK_DEPTH + 1):
631631
test_node.clear_block_announcement()
632632
new_blocks.append(node.generate(1)[0])
633633
wait_until(test_node.received_block_announcement, timeout=30, lock=mininode_lock)

test/functional/p2p_feefilter.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def hashToHex(hash):
1919

2020
# Wait up to 60 secs to see if the testnode has received all the expected invs
2121
def allInvsMatch(invsExpected, testnode):
22-
for x in range(60):
22+
for _ in range(60):
2323
with mininode_lock:
2424
if (sorted(invsExpected) == sorted(testnode.txinvs)):
2525
return True
@@ -91,7 +91,7 @@ def test_feefilter(self):
9191
# Test that invs are received by test connection for all txs at
9292
# feerate of .2 sat/byte
9393
node1.settxfee(Decimal("0.00000200"))
94-
txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
94+
txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for _ in range(3)]
9595
assert allInvsMatch(txids, conn)
9696
conn.clear_invs()
9797

@@ -100,14 +100,14 @@ def test_feefilter(self):
100100

101101
# Test that txs are still being received by test connection (paying .15 sat/byte)
102102
node1.settxfee(Decimal("0.00000150"))
103-
txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
103+
txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for _ in range(3)]
104104
assert allInvsMatch(txids, conn)
105105
conn.clear_invs()
106106

107107
# Change tx fee rate to .1 sat/byte and test they are no longer received
108108
# by the test connection
109109
node1.settxfee(Decimal("0.00000100"))
110-
[node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
110+
[node1.sendtoaddress(node1.getnewaddress(), 1) for _ in range(3)]
111111
self.sync_mempools() # must be sure node 0 has received all txs
112112

113113
# Send one transaction from node0 that should be received, so that we
@@ -124,7 +124,7 @@ def test_feefilter(self):
124124

125125
# Remove fee filter and check that txs are received again
126126
conn.send_and_ping(msg_feefilter(0))
127-
txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for x in range(3)]
127+
txids = [node1.sendtoaddress(node1.getnewaddress(), 1) for _ in range(3)]
128128
assert allInvsMatch(txids, conn)
129129
conn.clear_invs()
130130

test/functional/p2p_segwit.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ def test_witness_block_size(self):
942942
parent_tx = CTransaction()
943943
parent_tx.vin.append(CTxIn(prevout, b""))
944944
child_value = int(value / NUM_OUTPUTS)
945-
for i in range(NUM_OUTPUTS):
945+
for _ in range(NUM_OUTPUTS):
946946
parent_tx.vout.append(CTxOut(child_value, script_pubkey))
947947
parent_tx.vout[0].nValue -= 50000
948948
assert parent_tx.vout[0].nValue > 0
@@ -952,7 +952,7 @@ def test_witness_block_size(self):
952952
for i in range(NUM_OUTPUTS):
953953
child_tx.vin.append(CTxIn(COutPoint(parent_tx.sha256, i), b""))
954954
child_tx.vout = [CTxOut(value - 100000, CScript([OP_TRUE]))]
955-
for i in range(NUM_OUTPUTS):
955+
for _ in range(NUM_OUTPUTS):
956956
child_tx.wit.vtxinwit.append(CTxInWitness())
957957
child_tx.wit.vtxinwit[-1].scriptWitness.stack = [b'a' * 195] * (2 * NUM_DROPS) + [witness_program]
958958
child_tx.rehash()
@@ -1199,7 +1199,7 @@ def test_witness_input_length(self):
11991199
tx = CTransaction()
12001200
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
12011201
value = self.utxo[0].nValue
1202-
for i in range(10):
1202+
for _ in range(10):
12031203
tx.vout.append(CTxOut(int(value / 10), script_pubkey))
12041204
tx.vout[0].nValue -= 1000
12051205
assert tx.vout[0].nValue >= 0
@@ -1372,7 +1372,7 @@ def test_segwit_versions(self):
13721372
tx = CTransaction()
13731373
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
13741374
split_value = (self.utxo[0].nValue - 4000) // NUM_SEGWIT_VERSIONS
1375-
for i in range(NUM_SEGWIT_VERSIONS):
1375+
for _ in range(NUM_SEGWIT_VERSIONS):
13761376
tx.vout.append(CTxOut(split_value, CScript([OP_TRUE])))
13771377
tx.rehash()
13781378
block = self.build_next_block()
@@ -1646,7 +1646,7 @@ def test_signature_version_1(self):
16461646
tx = CTransaction()
16471647
tx.vin.append(CTxIn(COutPoint(prev_utxo.sha256, prev_utxo.n), b""))
16481648
split_value = prev_utxo.nValue // NUM_SIGHASH_TESTS
1649-
for i in range(NUM_SIGHASH_TESTS):
1649+
for _ in range(NUM_SIGHASH_TESTS):
16501650
tx.vout.append(CTxOut(split_value, script_pubkey))
16511651
tx.wit.vtxinwit.append(CTxInWitness())
16521652
sign_p2pk_witness_input(witness_program, tx, 0, SIGHASH_ALL, prev_utxo.nValue, key)
@@ -1676,7 +1676,7 @@ def test_signature_version_1(self):
16761676
tx.wit.vtxinwit.append(CTxInWitness())
16771677
total_value += temp_utxos[i].nValue
16781678
split_value = total_value // num_outputs
1679-
for i in range(num_outputs):
1679+
for _ in range(num_outputs):
16801680
tx.vout.append(CTxOut(split_value, script_pubkey))
16811681
for i in range(num_inputs):
16821682
# Now try to sign each input, using a random hashtype.
@@ -1974,7 +1974,7 @@ def test_witness_sigops(self):
19741974
split_value = self.utxo[0].nValue // outputs
19751975
tx = CTransaction()
19761976
tx.vin.append(CTxIn(COutPoint(self.utxo[0].sha256, self.utxo[0].n), b""))
1977-
for i in range(outputs):
1977+
for _ in range(outputs):
19781978
tx.vout.append(CTxOut(split_value, script_pubkey))
19791979
tx.vout[-2].scriptPubKey = script_pubkey_toomany
19801980
tx.vout[-1].scriptPubKey = script_pubkey_justright
@@ -2060,7 +2060,7 @@ def serialize_with_bogus_witness(tx):
20602060
if (len(tx.wit.vtxinwit) != len(tx.vin)):
20612061
# vtxinwit must have the same length as vin
20622062
tx.wit.vtxinwit = tx.wit.vtxinwit[:len(tx.vin)]
2063-
for i in range(len(tx.wit.vtxinwit), len(tx.vin)):
2063+
for _ in range(len(tx.wit.vtxinwit), len(tx.vin)):
20642064
tx.wit.vtxinwit.append(CTxInWitness())
20652065
r += tx.wit.serialize()
20662066
r += struct.pack("<I", tx.nLockTime)

0 commit comments

Comments
 (0)