Skip to content

Commit 66d012a

Browse files
committed
test: RPC: getblock fee calculations
1 parent bf7d6e3 commit 66d012a

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

test/functional/rpc_blockchain.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from decimal import Decimal
2222
import http.client
23+
import os
2324
import subprocess
2425

2526
from test_framework.blocktools import (
@@ -42,7 +43,9 @@
4243
assert_raises_rpc_error,
4344
assert_is_hex_string,
4445
assert_is_hash_string,
46+
get_datadir_path,
4547
)
48+
from test_framework.wallet import MiniWallet
4649

4750

4851
class BlockchainTest(BitcoinTestFramework):
@@ -63,6 +66,7 @@ def run_test(self):
6366
self._test_getnetworkhashps()
6467
self._test_stopatheight()
6568
self._test_waitforblockheight()
69+
self._test_getblock()
6670
assert self.nodes[0].verifychain(4, 0)
6771

6872
def mine_chain(self):
@@ -352,6 +356,46 @@ def assert_waitforheight(height, timeout=2):
352356
assert_waitforheight(current_height)
353357
assert_waitforheight(current_height + 1)
354358

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+
355399

356400
if __name__ == '__main__':
357401
BlockchainTest().main()

0 commit comments

Comments
 (0)