Skip to content

Commit 69fc867

Browse files
mzumsandetdb3
andcommitted
test: add coverage to getblock and getblockstats
also removes an unnecessary newline. Co-authored-by: tdb3 <[email protected]>
1 parent 5290cbd commit 69fc867

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

test/functional/rpc_blockchain.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,19 @@ def move_block_file(old, new):
633633
assert 'previousblockhash' not in node.getblock(node.getblockhash(0))
634634
assert 'nextblockhash' not in node.getblock(node.getbestblockhash())
635635

636+
self.log.info("Test getblock when only header is known")
637+
current_height = node.getblock(node.getbestblockhash())['height']
638+
block_time = node.getblock(node.getbestblockhash())['time'] + 1
639+
block = create_block(int(blockhash, 16), create_coinbase(current_height + 1, nValue=100), block_time)
640+
block.solve()
641+
node.submitheader(block.serialize().hex())
642+
assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", lambda: node.getblock(block.hash))
643+
644+
self.log.info("Test getblock when block is missing")
645+
move_block_file('blk00000.dat', 'blk00000.dat.bak')
646+
assert_raises_rpc_error(-1, "Block not found on disk", node.getblock, blockhash)
647+
move_block_file('blk00000.dat.bak', 'blk00000.dat')
648+
636649

637650
if __name__ == '__main__':
638651
BlockchainTest(__file__).main()

test/functional/rpc_getblockstats.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def run_test(self):
114114
assert_equal(stats[self.max_stat_pos]['height'], self.start_height + self.max_stat_pos)
115115

116116
for i in range(self.max_stat_pos+1):
117-
self.log.info('Checking block %d\n' % (i))
117+
self.log.info('Checking block %d' % (i))
118118
assert_equal(stats[i], self.expected_stats[i])
119119

120120
# Check selecting block by hash too
@@ -182,5 +182,16 @@ def run_test(self):
182182
assert_equal(tip_stats["utxo_increase_actual"], 4)
183183
assert_equal(tip_stats["utxo_size_inc_actual"], 300)
184184

185+
self.log.info("Test when only header is known")
186+
block = self.generateblock(self.nodes[0], output="raw(55)", transactions=[], submit=False)
187+
self.nodes[0].submitheader(block["hex"])
188+
assert_raises_rpc_error(-1, "Block not available (not fully downloaded)", lambda: self.nodes[0].getblockstats(block['hash']))
189+
190+
self.log.info('Test when block is missing')
191+
(self.nodes[0].blocks_path / 'blk00000.dat').rename(self.nodes[0].blocks_path / 'blk00000.dat.backup')
192+
assert_raises_rpc_error(-1, 'Block not found on disk', self.nodes[0].getblockstats, hash_or_height=1)
193+
(self.nodes[0].blocks_path / 'blk00000.dat.backup').rename(self.nodes[0].blocks_path / 'blk00000.dat')
194+
195+
185196
if __name__ == '__main__':
186197
GetblockstatsTest(__file__).main()

0 commit comments

Comments
 (0)