|
| 1 | +import pytest |
| 2 | + |
| 3 | +from eth.chains.base import MiningChain |
| 4 | +from eth.db.backends.memory import MemoryDB |
| 5 | + |
| 6 | + |
| 7 | +@pytest.fixture |
| 8 | +def chain(chain_without_block_validation): |
| 9 | + if not isinstance(chain_without_block_validation, MiningChain): |
| 10 | + pytest.skip('only valid on mining chains') |
| 11 | + return chain_without_block_validation |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def fork_chain(chain): |
| 16 | + # make a duplicate chain with no shared state |
| 17 | + fork_db = MemoryDB(chain.chaindb.db.kv_store.copy()) |
| 18 | + fork_chain = type(chain)(fork_db, chain.header) |
| 19 | + |
| 20 | + return fork_chain |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize( |
| 24 | + 'limit', |
| 25 | + (0, 1, 2, 5), |
| 26 | +) |
| 27 | +def test_chain_get_ancestors_from_genesis_block(chain, limit): |
| 28 | + header = chain.get_canonical_head() |
| 29 | + assert header.block_number == 0 |
| 30 | + |
| 31 | + ancestors = chain.get_ancestors(limit, header) |
| 32 | + assert ancestors == tuple() |
| 33 | + |
| 34 | + |
| 35 | +def test_chain_get_ancestors_from_block_1(chain): |
| 36 | + genesis = chain.get_canonical_block_by_number(0) |
| 37 | + block_1 = chain.mine_block() |
| 38 | + header = block_1.header |
| 39 | + assert header.block_number == 1 |
| 40 | + |
| 41 | + assert chain.get_ancestors(0, header) == tuple() |
| 42 | + assert chain.get_ancestors(1, header) == (genesis,) |
| 43 | + assert chain.get_ancestors(2, header) == (genesis,) |
| 44 | + assert chain.get_ancestors(5, header) == (genesis,) |
| 45 | + |
| 46 | + |
| 47 | +def test_chain_get_ancestors_from_block_5(chain): |
| 48 | + genesis = chain.get_canonical_block_by_number(0) |
| 49 | + ( |
| 50 | + block_1, |
| 51 | + block_2, |
| 52 | + block_3, |
| 53 | + block_4, |
| 54 | + block_5, |
| 55 | + ) = [chain.mine_block() for _ in range(5)] |
| 56 | + |
| 57 | + header = block_5.header |
| 58 | + assert header.block_number == 5 |
| 59 | + |
| 60 | + assert chain.get_ancestors(0, header) == tuple() |
| 61 | + assert chain.get_ancestors(1, header) == (block_4,) |
| 62 | + assert chain.get_ancestors(2, header) == (block_4, block_3) |
| 63 | + assert chain.get_ancestors(3, header) == (block_4, block_3, block_2) |
| 64 | + assert chain.get_ancestors(4, header) == (block_4, block_3, block_2, block_1) |
| 65 | + assert chain.get_ancestors(5, header) == (block_4, block_3, block_2, block_1, genesis) |
| 66 | + assert chain.get_ancestors(6, header) == (block_4, block_3, block_2, block_1, genesis) |
| 67 | + assert chain.get_ancestors(10, header) == (block_4, block_3, block_2, block_1, genesis) |
| 68 | + |
| 69 | + |
| 70 | +def test_chain_get_ancestors_for_fork_chains(chain, fork_chain): |
| 71 | + genesis = chain.get_canonical_block_by_number(0) |
| 72 | + ( |
| 73 | + block_1, |
| 74 | + block_2, |
| 75 | + block_3, |
| 76 | + ) = [chain.mine_block() for _ in range(3)] |
| 77 | + ( |
| 78 | + f_block_1, |
| 79 | + f_block_2, |
| 80 | + f_block_3, |
| 81 | + ) = [fork_chain.mine_block() for _ in range(3)] |
| 82 | + |
| 83 | + assert block_1 == f_block_1 |
| 84 | + assert block_2 == f_block_2 |
| 85 | + assert block_3 == f_block_3 |
| 86 | + |
| 87 | + # force the fork chain to diverge |
| 88 | + fork_chain.header = fork_chain.header.copy(extra_data=b'fork-it!') |
| 89 | + |
| 90 | + # mine ahead a bit further on both chains. |
| 91 | + ( |
| 92 | + block_4, |
| 93 | + block_5, |
| 94 | + block_6, |
| 95 | + ) = [chain.mine_block() for _ in range(3)] |
| 96 | + ( |
| 97 | + f_block_4, |
| 98 | + f_block_5, |
| 99 | + f_block_6, |
| 100 | + ) = [fork_chain.mine_block() for _ in range(3)] |
| 101 | + |
| 102 | + # import the fork blocks into the main chain (ensuring they don't cause a reorg) |
| 103 | + _, new_chain, _ = chain.import_block(f_block_4) |
| 104 | + assert new_chain == tuple() |
| 105 | + _, new_chain, _ = chain.import_block(f_block_5) |
| 106 | + assert new_chain == tuple() |
| 107 | + |
| 108 | + # check with a block that has been imported |
| 109 | + assert chain.get_ancestors(0, f_block_5.header) == tuple() |
| 110 | + assert chain.get_ancestors(1, f_block_5.header) == (f_block_4,) |
| 111 | + assert chain.get_ancestors(2, f_block_5.header) == (f_block_4, block_3) |
| 112 | + assert chain.get_ancestors(3, f_block_5.header) == (f_block_4, block_3, block_2) |
| 113 | + assert chain.get_ancestors(4, f_block_5.header) == (f_block_4, block_3, block_2, block_1) |
| 114 | + assert chain.get_ancestors(5, f_block_5.header) == (f_block_4, block_3, block_2, block_1, genesis) # noqa: E501 |
| 115 | + # check that when we hit genesis it self limits |
| 116 | + assert chain.get_ancestors(6, f_block_5.header) == (f_block_4, block_3, block_2, block_1, genesis) # noqa: E501 |
| 117 | + assert chain.get_ancestors(20, f_block_5.header) == (f_block_4, block_3, block_2, block_1, genesis) # noqa: E501 |
| 118 | + |
| 119 | + # check with a block that has NOT been imported |
| 120 | + assert chain.get_ancestors(0, f_block_6.header) == tuple() |
| 121 | + assert chain.get_ancestors(1, f_block_6.header) == (f_block_5,) |
| 122 | + assert chain.get_ancestors(2, f_block_6.header) == (f_block_5, f_block_4) |
| 123 | + assert chain.get_ancestors(3, f_block_6.header) == (f_block_5, f_block_4, block_3) |
| 124 | + assert chain.get_ancestors(4, f_block_6.header) == (f_block_5, f_block_4, block_3, block_2) |
| 125 | + assert chain.get_ancestors(5, f_block_6.header) == (f_block_5, f_block_4, block_3, block_2, block_1) # noqa: E501 |
| 126 | + assert chain.get_ancestors(6, f_block_6.header) == (f_block_5, f_block_4, block_3, block_2, block_1, genesis) # noqa: E501 |
| 127 | + # check that when we hit genesis it self limits |
| 128 | + assert chain.get_ancestors(7, f_block_6.header) == (f_block_5, f_block_4, block_3, block_2, block_1, genesis) # noqa: E501 |
| 129 | + assert chain.get_ancestors(20, f_block_6.header) == (f_block_5, f_block_4, block_3, block_2, block_1, genesis) # noqa: E501 |
0 commit comments