mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 04:52:59 +01:00
980e7b4f7c
d20d756752 rpc: faster getblockstats using BlockUndo data (Felix Weis) Pull request description: Using undo data for a block (rev?????.dat) we can retrieve value information about prevouts and calculate the final transaction fee (rate). This approach is about 80x faster, drops the requirement for `-txindex`, and works for all non-pruned blocks. ``` # 2018-11-25T16:36:19Z Bitcoin Core version v0.17.99.0-edc715240-dirty (release build) seq 550100 550200 0.00s user 0.00s system 62% cpu 0.004 total xargs -n1 src/bitcoin-cli getblockstats 0.21s user 0.19s system 17% cpu 2.302 total # 2018-11-25T16:39:17Z Bitcoin Core version v0.17.0 (release build) seq 550100 550200 0.00s user 0.00s system 87% cpu 0.002 total xargs -n1 src/bitcoin-cli getblockstats 0.24s user 0.22s system 0% cpu 3:19.42 total ``` ACKs for commit d20d75: MarcoFalke: re-utACK d20d7567528e216badb8475df298bb3cec008985 Tree-SHA512: 5babc3eb8d2fee2cb23dc12f522656b80737a540cbf2b13390a8f388304c46c064cca76f896b46a6e2abae8cc582d28e1ab20dd4bb17ad6142f20630c2d30c54
165 lines
6.7 KiB
Python
Executable File
165 lines
6.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2017-2017 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#
|
|
# Test getblockstats rpc call
|
|
#
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import (
|
|
assert_equal,
|
|
assert_raises_rpc_error,
|
|
)
|
|
import json
|
|
import os
|
|
|
|
TESTSDIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
class GetblockstatsTest(BitcoinTestFramework):
|
|
|
|
start_height = 101
|
|
max_stat_pos = 2
|
|
|
|
def add_options(self, parser):
|
|
parser.add_argument('--gen-test-data', dest='gen_test_data',
|
|
default=False, action='store_true',
|
|
help='Generate test data')
|
|
parser.add_argument('--test-data', dest='test_data',
|
|
default='data/rpc_getblockstats.json',
|
|
action='store', metavar='FILE',
|
|
help='Test data file')
|
|
|
|
# def set_test_params(self):
|
|
def set_test_params(self):
|
|
self.num_nodes = 1
|
|
self.setup_clean_chain = True
|
|
|
|
def get_stats(self):
|
|
return [self.nodes[0].getblockstats(hash_or_height=self.start_height + i) for i in range(self.max_stat_pos+1)]
|
|
|
|
def generate_test_data(self, filename):
|
|
self.nodes[0].setmocktime(self.mocktime)
|
|
self.nodes[0].generate(101)
|
|
|
|
address = self.nodes[0].get_deterministic_priv_key().address
|
|
self.nodes[0].sendtoaddress(address=address, amount=10, subtractfeefromamount=True)
|
|
self.nodes[0].generate(1)
|
|
self.sync_all()
|
|
|
|
self.nodes[0].sendtoaddress(address=address, amount=10, subtractfeefromamount=True)
|
|
self.nodes[0].sendtoaddress(address=address, amount=10, subtractfeefromamount=False)
|
|
self.nodes[0].settxfee(amount=0.003)
|
|
self.nodes[0].sendtoaddress(address=address, amount=1, subtractfeefromamount=True)
|
|
self.sync_all()
|
|
self.nodes[0].generate(1)
|
|
|
|
self.expected_stats = self.get_stats()
|
|
|
|
blocks = []
|
|
tip = self.nodes[0].getbestblockhash()
|
|
blockhash = None
|
|
height = 0
|
|
while tip != blockhash:
|
|
blockhash = self.nodes[0].getblockhash(height)
|
|
blocks.append(self.nodes[0].getblock(blockhash, 0))
|
|
height += 1
|
|
|
|
to_dump = {
|
|
'blocks': blocks,
|
|
'mocktime': int(self.mocktime),
|
|
'stats': self.expected_stats,
|
|
}
|
|
with open(filename, 'w', encoding="utf8") as f:
|
|
json.dump(to_dump, f, sort_keys=True, indent=2)
|
|
|
|
def load_test_data(self, filename):
|
|
with open(filename, 'r', encoding="utf8") as f:
|
|
d = json.load(f)
|
|
blocks = d['blocks']
|
|
self.mocktime = d['mocktime']
|
|
self.expected_stats = d['stats']
|
|
|
|
# Set the timestamps from the file so that the nodes can get out of Initial Block Download
|
|
self.nodes[0].setmocktime(self.mocktime)
|
|
self.sync_all()
|
|
|
|
for b in blocks:
|
|
self.nodes[0].submitblock(b)
|
|
|
|
|
|
def run_test(self):
|
|
test_data = os.path.join(TESTSDIR, self.options.test_data)
|
|
if self.options.gen_test_data:
|
|
self.generate_test_data(test_data)
|
|
else:
|
|
self.load_test_data(test_data)
|
|
|
|
self.sync_all()
|
|
stats = self.get_stats()
|
|
|
|
# Make sure all valid statistics are included but nothing else is
|
|
expected_keys = self.expected_stats[0].keys()
|
|
assert_equal(set(stats[0].keys()), set(expected_keys))
|
|
|
|
assert_equal(stats[0]['height'], self.start_height)
|
|
assert_equal(stats[self.max_stat_pos]['height'], self.start_height + self.max_stat_pos)
|
|
|
|
for i in range(self.max_stat_pos+1):
|
|
self.log.info('Checking block %d\n' % (i))
|
|
assert_equal(stats[i], self.expected_stats[i])
|
|
|
|
# Check selecting block by hash too
|
|
blockhash = self.expected_stats[i]['blockhash']
|
|
stats_by_hash = self.nodes[0].getblockstats(hash_or_height=blockhash)
|
|
assert_equal(stats_by_hash, self.expected_stats[i])
|
|
|
|
# Make sure each stat can be queried on its own
|
|
for stat in expected_keys:
|
|
for i in range(self.max_stat_pos+1):
|
|
result = self.nodes[0].getblockstats(hash_or_height=self.start_height + i, stats=[stat])
|
|
assert_equal(list(result.keys()), [stat])
|
|
if result[stat] != self.expected_stats[i][stat]:
|
|
self.log.info('result[%s] (%d) failed, %r != %r' % (
|
|
stat, i, result[stat], self.expected_stats[i][stat]))
|
|
assert_equal(result[stat], self.expected_stats[i][stat])
|
|
|
|
# Make sure only the selected statistics are included (more than one)
|
|
some_stats = {'minfee', 'maxfee'}
|
|
stats = self.nodes[0].getblockstats(hash_or_height=1, stats=list(some_stats))
|
|
assert_equal(set(stats.keys()), some_stats)
|
|
|
|
# Test invalid parameters raise the proper json exceptions
|
|
tip = self.start_height + self.max_stat_pos
|
|
assert_raises_rpc_error(-8, 'Target block height %d after current tip %d' % (tip+1, tip),
|
|
self.nodes[0].getblockstats, hash_or_height=tip+1)
|
|
assert_raises_rpc_error(-8, 'Target block height %d is negative' % (-1),
|
|
self.nodes[0].getblockstats, hash_or_height=-1)
|
|
|
|
# Make sure not valid stats aren't allowed
|
|
inv_sel_stat = 'asdfghjkl'
|
|
inv_stats = [
|
|
[inv_sel_stat],
|
|
['minfee' , inv_sel_stat],
|
|
[inv_sel_stat, 'minfee'],
|
|
['minfee', inv_sel_stat, 'maxfee'],
|
|
]
|
|
for inv_stat in inv_stats:
|
|
assert_raises_rpc_error(-8, 'Invalid selected statistic %s' % inv_sel_stat,
|
|
self.nodes[0].getblockstats, hash_or_height=1, stats=inv_stat)
|
|
|
|
# Make sure we aren't always returning inv_sel_stat as the culprit stat
|
|
assert_raises_rpc_error(-8, 'Invalid selected statistic aaa%s' % inv_sel_stat,
|
|
self.nodes[0].getblockstats, hash_or_height=1, stats=['minfee' , 'aaa%s' % inv_sel_stat])
|
|
# Mainchain's genesis block shouldn't be found on regtest
|
|
assert_raises_rpc_error(-5, 'Block not found', self.nodes[0].getblockstats,
|
|
hash_or_height='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')
|
|
|
|
# Invalid number of args
|
|
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, '00', 1, 2)
|
|
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
GetblockstatsTest().main()
|