20
20
21
21
from decimal import Decimal
22
22
import http .client
23
+ import os
23
24
import subprocess
24
25
25
26
from test_framework .blocktools import (
42
43
assert_raises_rpc_error ,
43
44
assert_is_hex_string ,
44
45
assert_is_hash_string ,
46
+ get_datadir_path ,
45
47
)
48
+ from test_framework .wallet import MiniWallet
46
49
47
50
48
51
class BlockchainTest (BitcoinTestFramework ):
@@ -63,6 +66,7 @@ def run_test(self):
63
66
self ._test_getnetworkhashps ()
64
67
self ._test_stopatheight ()
65
68
self ._test_waitforblockheight ()
69
+ self ._test_getblock ()
66
70
assert self .nodes [0 ].verifychain (4 , 0 )
67
71
68
72
def mine_chain (self ):
@@ -352,6 +356,46 @@ def assert_waitforheight(height, timeout=2):
352
356
assert_waitforheight (current_height )
353
357
assert_waitforheight (current_height + 1 )
354
358
359
+ def _test_getblock (self ):
360
+ node = self .nodes [0 ]
361
+
362
+ miniwallet = MiniWallet (node )
363
+ miniwallet .generate (5 )
364
+ node .generate (100 )
365
+
366
+ fee_per_byte = Decimal ('0.00000010' )
367
+ fee_per_kb = 1000 * fee_per_byte
368
+
369
+ miniwallet .send_self_transfer (fee_rate = fee_per_kb , from_node = node )
370
+ blockhash = node .generate (1 )[0 ]
371
+
372
+ self .log .info ("Test that getblock with verbosity 1 doesn't include fee" )
373
+ block = node .getblock (blockhash , 1 )
374
+ assert 'fee' not in block ['tx' ][1 ]
375
+
376
+ self .log .info ('Test that getblock with verbosity 2 includes expected fee' )
377
+ block = node .getblock (blockhash , 2 )
378
+ tx = block ['tx' ][1 ]
379
+ assert 'fee' in tx
380
+ assert_equal (tx ['fee' ], tx ['vsize' ] * fee_per_byte )
381
+
382
+ self .log .info ("Test that getblock with verbosity 2 still works with pruned Undo data" )
383
+ datadir = get_datadir_path (self .options .tmpdir , 0 )
384
+
385
+ def move_block_file (old , new ):
386
+ old_path = os .path .join (datadir , self .chain , 'blocks' , old )
387
+ new_path = os .path .join (datadir , self .chain , 'blocks' , new )
388
+ os .rename (old_path , new_path )
389
+
390
+ # Move instead of deleting so we can restore chain state afterwards
391
+ move_block_file ('rev00000.dat' , 'rev_wrong' )
392
+
393
+ block = node .getblock (blockhash , 2 )
394
+ assert 'fee' not in block ['tx' ][1 ]
395
+
396
+ # Restore chain state
397
+ move_block_file ('rev_wrong' , 'rev00000.dat' )
398
+
355
399
356
400
if __name__ == '__main__' :
357
401
BlockchainTest ().main ()
0 commit comments