Skip to content

Commit d548dc7

Browse files
committed
test: replace magic values by constants in rpc_blockchain.py
1 parent 78c3610 commit d548dc7

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

test/functional/rpc_blockchain.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@
4949
from test_framework.wallet import MiniWallet
5050

5151

52+
HEIGHT = 200 # blocks mined
5253
TIME_RANGE_STEP = 600 # ten-minute steps
53-
TIME_RANGE_MTP = TIME_GENESIS_BLOCK + 194 * TIME_RANGE_STEP
54-
TIME_RANGE_END = TIME_GENESIS_BLOCK + 200 * TIME_RANGE_STEP
54+
TIME_RANGE_MTP = TIME_GENESIS_BLOCK + (HEIGHT - 6) * TIME_RANGE_STEP
55+
TIME_RANGE_END = TIME_GENESIS_BLOCK + HEIGHT * TIME_RANGE_STEP
5556

5657

5758
class BlockchainTest(BitcoinTestFramework):
@@ -76,11 +77,11 @@ def run_test(self):
7677
assert self.nodes[0].verifychain(4, 0)
7778

7879
def mine_chain(self):
79-
self.log.info("Generate 200 blocks after the genesis block in ten-minute steps")
80+
self.log.info(f"Generate {HEIGHT} blocks after the genesis block in ten-minute steps")
8081
for t in range(TIME_GENESIS_BLOCK, TIME_RANGE_END, TIME_RANGE_STEP):
8182
self.nodes[0].setmocktime(t)
8283
self.nodes[0].generatetoaddress(1, ADDRESS_BCRT1_P2WSH_OP_TRUE)
83-
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)
84+
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], HEIGHT)
8485

8586
def _test_getblockchaininfo(self):
8687
self.log.info("Test getblockchaininfo")
@@ -153,8 +154,8 @@ def _test_getblockchaininfo(self):
153154
'statistics': {
154155
'period': 144,
155156
'threshold': 108,
156-
'elapsed': 57,
157-
'count': 57,
157+
'elapsed': HEIGHT - 143,
158+
'count': HEIGHT - 143,
158159
'possible': True,
159160
},
160161
'min_activation_height': 0,
@@ -191,33 +192,33 @@ def _test_getchaintxstats(self):
191192
assert_raises_rpc_error(-8, "blockhash must be of length 64 (not 1, for '0')", self.nodes[0].getchaintxstats, blockhash='0')
192193
assert_raises_rpc_error(-8, "blockhash must be hexadecimal string (not 'ZZZ0000000000000000000000000000000000000000000000000000000000000')", self.nodes[0].getchaintxstats, blockhash='ZZZ0000000000000000000000000000000000000000000000000000000000000')
193194
assert_raises_rpc_error(-5, "Block not found", self.nodes[0].getchaintxstats, blockhash='0000000000000000000000000000000000000000000000000000000000000000')
194-
blockhash = self.nodes[0].getblockhash(200)
195+
blockhash = self.nodes[0].getblockhash(HEIGHT)
195196
self.nodes[0].invalidateblock(blockhash)
196197
assert_raises_rpc_error(-8, "Block is not in main chain", self.nodes[0].getchaintxstats, blockhash=blockhash)
197198
self.nodes[0].reconsiderblock(blockhash)
198199

199200
chaintxstats = self.nodes[0].getchaintxstats(nblocks=1)
200201
# 200 txs plus genesis tx
201-
assert_equal(chaintxstats['txcount'], 201)
202+
assert_equal(chaintxstats['txcount'], HEIGHT + 1)
202203
# tx rate should be 1 per 10 minutes, or 1/600
203204
# we have to round because of binary math
204-
assert_equal(round(chaintxstats['txrate'] * 600, 10), Decimal(1))
205+
assert_equal(round(chaintxstats['txrate'] * TIME_RANGE_STEP, 10), Decimal(1))
205206

206207
b1_hash = self.nodes[0].getblockhash(1)
207208
b1 = self.nodes[0].getblock(b1_hash)
208-
b200_hash = self.nodes[0].getblockhash(200)
209+
b200_hash = self.nodes[0].getblockhash(HEIGHT)
209210
b200 = self.nodes[0].getblock(b200_hash)
210211
time_diff = b200['mediantime'] - b1['mediantime']
211212

212213
chaintxstats = self.nodes[0].getchaintxstats()
213214
assert_equal(chaintxstats['time'], b200['time'])
214-
assert_equal(chaintxstats['txcount'], 201)
215+
assert_equal(chaintxstats['txcount'], HEIGHT + 1)
215216
assert_equal(chaintxstats['window_final_block_hash'], b200_hash)
216-
assert_equal(chaintxstats['window_final_block_height'], 200)
217-
assert_equal(chaintxstats['window_block_count'], 199)
218-
assert_equal(chaintxstats['window_tx_count'], 199)
217+
assert_equal(chaintxstats['window_final_block_height'], HEIGHT )
218+
assert_equal(chaintxstats['window_block_count'], HEIGHT - 1)
219+
assert_equal(chaintxstats['window_tx_count'], HEIGHT - 1)
219220
assert_equal(chaintxstats['window_interval'], time_diff)
220-
assert_equal(round(chaintxstats['txrate'] * time_diff, 10), Decimal(199))
221+
assert_equal(round(chaintxstats['txrate'] * time_diff, 10), Decimal(HEIGHT - 1))
221222

222223
chaintxstats = self.nodes[0].getchaintxstats(blockhash=b1_hash)
223224
assert_equal(chaintxstats['time'], b1['time'])
@@ -234,11 +235,11 @@ def _test_gettxoutsetinfo(self):
234235
res = node.gettxoutsetinfo()
235236

236237
assert_equal(res['total_amount'], Decimal('8725.00000000'))
237-
assert_equal(res['transactions'], 200)
238-
assert_equal(res['height'], 200)
239-
assert_equal(res['txouts'], 200)
238+
assert_equal(res['transactions'], HEIGHT)
239+
assert_equal(res['height'], HEIGHT)
240+
assert_equal(res['txouts'], HEIGHT)
240241
assert_equal(res['bogosize'], 16800),
241-
assert_equal(res['bestblock'], node.getblockhash(200))
242+
assert_equal(res['bestblock'], node.getblockhash(HEIGHT))
242243
size = res['disk_size']
243244
assert size > 6400
244245
assert size < 64000
@@ -298,11 +299,11 @@ def _test_getblockheader(self):
298299
assert_raises_rpc_error(-5, "Block not found", node.getblockheader, "0cf7bb8b1697ea987f3b223ba7819250cae33efacb068d23dc24859824a77844")
299300

300301
besthash = node.getbestblockhash()
301-
secondbesthash = node.getblockhash(199)
302+
secondbesthash = node.getblockhash(HEIGHT - 1)
302303
header = node.getblockheader(blockhash=besthash)
303304

304305
assert_equal(header['hash'], besthash)
305-
assert_equal(header['height'], 200)
306+
assert_equal(header['height'], HEIGHT)
306307
assert_equal(header['confirmations'], 1)
307308
assert_equal(header['previousblockhash'], secondbesthash)
308309
assert_is_hex_string(header['chainwork'])
@@ -341,9 +342,9 @@ def _test_getnetworkhashps(self):
341342
assert abs(hashes_per_second * 300 - 1) < 0.0001
342343

343344
def _test_stopatheight(self):
344-
assert_equal(self.nodes[0].getblockcount(), 200)
345+
assert_equal(self.nodes[0].getblockcount(), HEIGHT)
345346
self.nodes[0].generatetoaddress(6, ADDRESS_BCRT1_P2WSH_OP_TRUE)
346-
assert_equal(self.nodes[0].getblockcount(), 206)
347+
assert_equal(self.nodes[0].getblockcount(), HEIGHT + 6)
347348
self.log.debug('Node should not stop at this height')
348349
assert_raises(subprocess.TimeoutExpired, lambda: self.nodes[0].process.wait(timeout=3))
349350
try:
@@ -353,7 +354,7 @@ def _test_stopatheight(self):
353354
self.log.debug('Node should stop at this height...')
354355
self.nodes[0].wait_until_stopped()
355356
self.start_node(0)
356-
assert_equal(self.nodes[0].getblockcount(), 207)
357+
assert_equal(self.nodes[0].getblockcount(), HEIGHT + 7)
357358

358359
def _test_waitforblockheight(self):
359360
self.log.info("Test waitforblockheight")

0 commit comments

Comments
 (0)