| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
from __future__ import print_function |
| 33 |
import time |
| 34 |
|
| 35 |
"""Test daemon blockchain RPC calls |
| 36 |
|
| 37 |
Test the following RPCs: |
| 38 |
- get_info |
| 39 |
- generateblocks |
| 40 |
- misc block retrieval |
| 41 |
- pop_blocks |
| 42 |
- [TODO: many tests still need to be written] |
| 43 |
|
| 44 |
""" |
| 45 |
|
| 46 |
from framework.daemon import Daemon |
| 47 |
|
| 48 |
class BlockchainTest(): |
| 49 |
def run_test(self): |
| 50 |
self.reset() |
| 51 |
self._test_generateblocks(5) |
| 52 |
self._test_alt_chains() |
| 53 |
|
| 54 |
def reset(self): |
| 55 |
print('Resetting blockchain') |
| 56 |
daemon = Daemon() |
| 57 |
res = daemon.get_height() |
| 58 |
daemon.pop_blocks(res.height - 1) |
| 59 |
daemon.flush_txpool() |
| 60 |
|
| 61 |
def _test_generateblocks(self, blocks): |
| 62 |
assert blocks >= 2 |
| 63 |
|
| 64 |
print("Test generating", blocks, 'blocks') |
| 65 |
|
| 66 |
daemon = Daemon() |
| 67 |
|
| 68 |
|
| 69 |
res_info = daemon.get_info() |
| 70 |
height = res_info.height |
| 71 |
prev_block = res_info.top_block_hash |
| 72 |
res_height = daemon.get_height() |
| 73 |
assert res_height.height == height |
| 74 |
assert int(res_info.wide_cumulative_difficulty, 16) == (res_info.cumulative_difficulty_top64 << 64) + res_info.cumulative_difficulty |
| 75 |
cumulative_difficulty = int(res_info.wide_cumulative_difficulty, 16) |
| 76 |
|
| 77 |
|
| 78 |
ok = False |
| 79 |
try: daemon.getblock(height = height) |
| 80 |
except: ok = True |
| 81 |
assert ok |
| 82 |
|
| 83 |
res = daemon.get_fee_estimate() |
| 84 |
assert res.fee == 1200000 |
| 85 |
assert res.quantization_mask == 10000 |
| 86 |
res = daemon.get_fee_estimate(10) |
| 87 |
assert res.fee <= 1200000 |
| 88 |
|
| 89 |
|
| 90 |
res_generateblocks = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', blocks) |
| 91 |
|
| 92 |
|
| 93 |
assert res_generateblocks.height == height + blocks - 1 |
| 94 |
res_info = daemon.get_info() |
| 95 |
assert res_info.height == height + blocks |
| 96 |
assert res_info.top_block_hash != prev_block |
| 97 |
res_height = daemon.get_height() |
| 98 |
assert res_height.height == height + blocks |
| 99 |
|
| 100 |
|
| 101 |
res_getblock = [] |
| 102 |
for n in range(blocks): |
| 103 |
res_getblock.append(daemon.getblock(height = height + n)) |
| 104 |
block_header = res_getblock[n].block_header |
| 105 |
assert abs(block_header.timestamp - time.time()) < 60 |
| 106 |
assert block_header.height == height + n |
| 107 |
assert block_header.orphan_status == False |
| 108 |
assert block_header.depth == blocks - n - 1 |
| 109 |
assert block_header.prev_hash == prev_block, prev_block |
| 110 |
assert int(block_header.wide_difficulty, 16) == (block_header.difficulty_top64 << 64) + block_header.difficulty |
| 111 |
assert int(block_header.wide_cumulative_difficulty, 16) == (block_header.cumulative_difficulty_top64 << 64) + block_header.cumulative_difficulty |
| 112 |
assert block_header.reward >= 600000000000 |
| 113 |
cumulative_difficulty += int(block_header.wide_difficulty, 16) |
| 114 |
assert cumulative_difficulty == int(block_header.wide_cumulative_difficulty, 16) |
| 115 |
assert block_header.block_size > 0 |
| 116 |
assert block_header.block_weight >= block_header.block_size |
| 117 |
assert block_header.long_term_weight > 0 |
| 118 |
prev_block = block_header.hash |
| 119 |
|
| 120 |
|
| 121 |
ok = False |
| 122 |
try: daemon.getblock(height = height + blocks) |
| 123 |
except: ok = True |
| 124 |
assert ok |
| 125 |
|
| 126 |
|
| 127 |
res_getlastblockheader = daemon.getlastblockheader() |
| 128 |
assert res_getlastblockheader.block_header == block_header |
| 129 |
res_getblockheaderbyhash = daemon.getblockheaderbyhash(prev_block) |
| 130 |
assert res_getblockheaderbyhash.block_header == block_header |
| 131 |
res_getblockheaderbyheight = daemon.getblockheaderbyheight(height + blocks - 1) |
| 132 |
assert res_getblockheaderbyheight.block_header == block_header |
| 133 |
|
| 134 |
|
| 135 |
res_getblocktemplate = daemon.getblocktemplate('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm') |
| 136 |
assert res_getblocktemplate.height == height + blocks |
| 137 |
assert res_getblocktemplate.reserved_offset > 0 |
| 138 |
assert res_getblocktemplate.prev_hash == res_info.top_block_hash |
| 139 |
assert res_getblocktemplate.expected_reward >= 600000000000 |
| 140 |
assert len(res_getblocktemplate.blocktemplate_blob) > 0 |
| 141 |
assert len(res_getblocktemplate.blockhashing_blob) > 0 |
| 142 |
assert int(res_getblocktemplate.wide_difficulty, 16) == (res_getblocktemplate.difficulty_top64 << 64) + res_getblocktemplate.difficulty |
| 143 |
|
| 144 |
|
| 145 |
assert res_getblocktemplate.prev_hash == res_info.top_block_hash |
| 146 |
|
| 147 |
res_getlastblockheader = daemon.getlastblockheader() |
| 148 |
|
| 149 |
|
| 150 |
res_popblocks = daemon.pop_blocks(1) |
| 151 |
assert res_popblocks.height == height + blocks - 1 |
| 152 |
|
| 153 |
res_info = daemon.get_info() |
| 154 |
assert res_info.height == height + blocks - 1 |
| 155 |
|
| 156 |
|
| 157 |
block_header = res_getblock[blocks - 2].block_header |
| 158 |
block_header.depth = 0 |
| 159 |
res_getlastblockheader = daemon.getlastblockheader() |
| 160 |
assert res_getlastblockheader.block_header == block_header |
| 161 |
res_getblockheaderbyhash = daemon.getblockheaderbyhash(block_header.hash) |
| 162 |
assert res_getblockheaderbyhash.block_header == block_header |
| 163 |
res_getblockheaderbyheight = daemon.getblockheaderbyheight(height + blocks - 2) |
| 164 |
assert res_getblockheaderbyheight.block_header == block_header |
| 165 |
|
| 166 |
|
| 167 |
ok = False |
| 168 |
try: daemon.getblock(height = height + blocks - 1) |
| 169 |
except: ok = True |
| 170 |
assert ok |
| 171 |
|
| 172 |
|
| 173 |
res = daemon.get_info() |
| 174 |
assert res.height == height + blocks - 1 |
| 175 |
nblocks = height + blocks - 1 |
| 176 |
res = daemon.getblockheadersrange(0, nblocks - 1) |
| 177 |
assert len(res.headers) == nblocks |
| 178 |
assert res.headers[-1] == block_header |
| 179 |
txids = [x.miner_tx_hash for x in res.headers] |
| 180 |
res = daemon.get_transactions(txs_hashes = txids) |
| 181 |
assert len(res.txs) == nblocks |
| 182 |
assert not 'missed_txs' in res or len(res.missed_txs) == 0 |
| 183 |
running_output_index = 0 |
| 184 |
for i in range(len(txids)): |
| 185 |
tx = res.txs[i] |
| 186 |
assert tx.tx_hash == txids[i] |
| 187 |
assert not tx.double_spend_seen |
| 188 |
assert not tx.in_pool |
| 189 |
assert tx.block_height == i |
| 190 |
if i > 0: |
| 191 |
for idx in tx.output_indices: |
| 192 |
assert idx == running_output_index |
| 193 |
running_output_index += 1 |
| 194 |
res_out = daemon.get_outs([{'amount': 0, 'index': idx} for idx in tx.output_indices], get_txid = True) |
| 195 |
assert len(res_out.outs) == len(tx.output_indices) |
| 196 |
for out in res_out.outs: |
| 197 |
assert len(out.key) == 64 |
| 198 |
assert len(out.mask) == 64 |
| 199 |
assert not out.unlocked |
| 200 |
assert out.height == i |
| 201 |
assert out.txid == txids[i] |
| 202 |
|
| 203 |
for i in range(height + nblocks - 1): |
| 204 |
res_sum = daemon.get_coinbase_tx_sum(i, 1) |
| 205 |
res_header = daemon.getblockheaderbyheight(i) |
| 206 |
assert res_sum.emission_amount == res_header.block_header.reward |
| 207 |
assert res_sum.emission_amount_top64 == 0 |
| 208 |
assert res_sum.emission_amount == int(res_sum.wide_emission_amount, 16) |
| 209 |
assert res_sum.fee_amount == int(res_sum.wide_fee_amount, 16) |
| 210 |
|
| 211 |
res = daemon.get_coinbase_tx_sum(0, 1) |
| 212 |
assert res.emission_amount == 17592186044415 |
| 213 |
assert res.emission_amount_top64 == 0 |
| 214 |
assert res.fee_amount == 0 |
| 215 |
assert res.fee_amount_top64 == 0 |
| 216 |
sum_blocks = height + nblocks - 1 |
| 217 |
res = daemon.get_coinbase_tx_sum(0, sum_blocks) |
| 218 |
extrapolated = 17592186044415 + 17592186044415 * 2 * (sum_blocks - 1) |
| 219 |
assert res.emission_amount < extrapolated and res.emission_amount > extrapolated - 1e12 |
| 220 |
assert res.fee_amount == 0 |
| 221 |
sum_blocks_emission = res.emission_amount |
| 222 |
res = daemon.get_coinbase_tx_sum(1, sum_blocks) |
| 223 |
assert res.emission_amount == sum_blocks_emission - 17592186044415 |
| 224 |
assert res.fee_amount == 0 |
| 225 |
|
| 226 |
res = daemon.get_output_distribution([0, 1, 17592186044415], 0, 0) |
| 227 |
assert len(res.distributions) == 3 |
| 228 |
for a in range(3): |
| 229 |
assert res.distributions[a].amount == [0, 1, 17592186044415][a] |
| 230 |
assert res.distributions[a].start_height == 0 |
| 231 |
assert res.distributions[a].base == 0 |
| 232 |
assert len(res.distributions[a].distribution) == height + nblocks - 1 |
| 233 |
assert res.distributions[a].binary == False |
| 234 |
for i in range(height + nblocks - 1): |
| 235 |
assert res.distributions[a].distribution[i] == (1 if i > 0 and a == 0 else 1 if a == 2 and i == 0 else 0) |
| 236 |
|
| 237 |
res = daemon.get_output_histogram([], min_count = 0, max_count = 0) |
| 238 |
assert len(res.histogram) == 2 |
| 239 |
for i in range(2): |
| 240 |
assert res.histogram[i].amount in [0, 17592186044415] |
| 241 |
assert res.histogram[i].total_instances in [height + nblocks - 2, 1] |
| 242 |
assert res.histogram[i].unlocked_instances == 0 |
| 243 |
assert res.histogram[i].recent_instances == 0 |
| 244 |
|
| 245 |
res = daemon.get_fee_estimate() |
| 246 |
assert res.fee == 1200000 |
| 247 |
assert res.quantization_mask == 10000 |
| 248 |
res = daemon.get_fee_estimate(10) |
| 249 |
assert res.fee <= 1200000 |
| 250 |
|
| 251 |
def _test_alt_chains(self): |
| 252 |
print('Testing alt chains') |
| 253 |
daemon = Daemon() |
| 254 |
res = daemon.get_alt_blocks_hashes() |
| 255 |
starting_alt_blocks = res.blks_hashes if 'blks_hashes' in res else [] |
| 256 |
res = daemon.get_info() |
| 257 |
root_block_hash = res.top_block_hash |
| 258 |
height = res.height |
| 259 |
prev_hash = res.top_block_hash |
| 260 |
res_template = daemon.getblocktemplate('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm') |
| 261 |
nonce = 0 |
| 262 |
|
| 263 |
|
| 264 |
alt_blocks = [None] * 5 |
| 265 |
for i in range(len(alt_blocks)): |
| 266 |
res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1, prev_block = prev_hash, starting_nonce = nonce) |
| 267 |
assert res.height == height |
| 268 |
assert len(res.blocks) == 1 |
| 269 |
txid = res.blocks[0] |
| 270 |
res = daemon.getblockheaderbyhash(txid) |
| 271 |
nonce = res.block_header.nonce |
| 272 |
print('mined ' + ('alt' if res.block_header.orphan_status else 'tip') + ' block ' + str(height) + ', nonce ' + str(nonce)) |
| 273 |
assert res.block_header.prev_hash == prev_hash |
| 274 |
assert res.block_header.orphan_status == (i > 0) |
| 275 |
alt_blocks[i] = txid |
| 276 |
nonce += 1 |
| 277 |
|
| 278 |
print('mining 3 on 1') |
| 279 |
|
| 280 |
chain1 = [] |
| 281 |
res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 3, prev_block = alt_blocks[1], starting_nonce = nonce) |
| 282 |
assert res.height == height + 3 |
| 283 |
assert len(res.blocks) == 3 |
| 284 |
blk_hash = res.blocks[2] |
| 285 |
res = daemon.getblockheaderbyhash(blk_hash) |
| 286 |
nonce = res.block_header.nonce |
| 287 |
assert not res.block_header.orphan_status |
| 288 |
nonce += 1 |
| 289 |
chain1.append(blk_hash) |
| 290 |
chain1.append(res.block_header.prev_hash) |
| 291 |
|
| 292 |
print('Checking alt blocks match') |
| 293 |
res = daemon.get_alt_blocks_hashes() |
| 294 |
assert len(res.blks_hashes) == len(starting_alt_blocks) + 4 |
| 295 |
for txid in alt_blocks: |
| 296 |
assert txid in res.blks_hashes or txid == alt_blocks[1] |
| 297 |
|
| 298 |
print('mining 4 on 3') |
| 299 |
|
| 300 |
top_block_hash = blk_hash |
| 301 |
prev_block = alt_blocks[3] |
| 302 |
for i in range(4): |
| 303 |
res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 1, prev_block = prev_block) |
| 304 |
assert res.height == height + 1 + i |
| 305 |
assert len(res.blocks) == 1 |
| 306 |
prev_block = res.blocks[-1] |
| 307 |
res = daemon.getblockheaderbyhash(res.blocks[-1]) |
| 308 |
assert res.block_header.orphan_status == (i < 3) |
| 309 |
|
| 310 |
res = daemon.get_info() |
| 311 |
assert res.height == ((height + 4) if i < 3 else height + 5) |
| 312 |
assert res.top_block_hash == (top_block_hash if i < 3 else prev_block) |
| 313 |
|
| 314 |
res = daemon.get_info() |
| 315 |
assert res.height == height + 5 |
| 316 |
assert res.top_block_hash == prev_block |
| 317 |
|
| 318 |
print('Checking alt blocks match') |
| 319 |
res = daemon.get_alt_blocks_hashes() |
| 320 |
blks_hashes = res.blks_hashes |
| 321 |
assert len(blks_hashes) == len(starting_alt_blocks) + 7 |
| 322 |
for txid in alt_blocks: |
| 323 |
assert txid in blks_hashes or txid == alt_blocks[3] |
| 324 |
for txid in chain1: |
| 325 |
assert txid in blks_hashes |
| 326 |
|
| 327 |
res = daemon.get_alternate_chains() |
| 328 |
assert len(res.chains) == 4 |
| 329 |
tips = [chain.block_hash for chain in res.chains] |
| 330 |
for txid in tips: |
| 331 |
assert txid in blks_hashes |
| 332 |
for chain in res.chains: |
| 333 |
assert chain.length in [1, 4] |
| 334 |
assert chain.length == len(chain.block_hashes) |
| 335 |
assert chain.height == height + chain.length - 1 |
| 336 |
assert chain.main_chain_parent_block == root_block_hash |
| 337 |
for txid in [alt_blocks[0], alt_blocks[2], alt_blocks[4]]: |
| 338 |
assert len([chain for chain in res.chains if chain.block_hash == txid]) == 1 |
| 339 |
|
| 340 |
print('Saving blockchain explicitely') |
| 341 |
daemon.save_bc() |
| 342 |
|
| 343 |
|
| 344 |
if __name__ == '__main__': |
| 345 |
BlockchainTest().run_test() |