Skip to content

Commit fad51dc

Browse files
authored
Chain builder tool (#1209)
* start building chain builder * move docs to docstrings * PR feedback * pr feedback * fix doctests * fix chain_split and docs
1 parent 1a3d64f commit fad51dc

File tree

18 files changed

+1237
-190
lines changed

18 files changed

+1237
-190
lines changed

docs/api/eth/api.tools.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Tools
77
:name: toc-eth-api-tools
88
:caption: Tools
99

10+
tools/api.tools.builders
1011
tools/api.tools.fixtures
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Builders
2+
========
3+
4+
5+
Chain Builder
6+
-------------
7+
8+
The chain builder utils are intended to reduce common boilerplace for both
9+
construction of chain classes as well as building up some desired chain state.
10+
11+
.. note:: These tools are best used in conjunction with ``cytoolz.pipe``.
12+
13+
14+
Constructing Chain Classes
15+
~~~~~~~~~~~~~~~~~~~~~~~~~~
16+
17+
The following utilities are provided to assist with constructing a chain class.
18+
19+
20+
.. autofunction:: eth.tools.builder.chain.fork_at
21+
22+
23+
.. autofunction:: eth.tools.builder.chain.dao_fork_at
24+
25+
26+
.. autofunction:: eth.tools.builder.chain.disable_dao_fork
27+
28+
29+
.. autofunction:: eth.tools.builder.chain.enable_pow_mining
30+
31+
32+
.. autofunction:: eth.tools.builder.chain.disable_pow_check
33+
34+
35+
.. autofunction:: eth.tools.builder.chain.name
36+
37+
38+
Initializing Chains
39+
~~~~~~~~~~~~~~~~~~~
40+
41+
The following utilities are provided to assist with initializing a chain into
42+
the genesis state.
43+
44+
.. autofunction:: eth.tools.builder.chain.genesis
45+
46+
47+
Building Chains
48+
~~~~~~~~~~~~~~~
49+
50+
The following utilities are provided to assist with building out chains of
51+
blocks.
52+
53+
54+
.. autofunction:: eth.tools.builder.chain.copy
55+
56+
57+
.. autofunction:: eth.tools.builder.chain.import_block
58+
59+
60+
.. autofunction:: eth.tools.builder.chain.import_blocks
61+
62+
63+
.. autofunction:: eth.tools.builder.chain.mine_block
64+
65+
66+
.. autofunction:: eth.tools.builder.chain.mine_blocks
67+
68+
69+
.. autofunction:: eth.tools.builder.chain.chain_split
70+
71+
72+
.. autofunction:: eth.tools.builder.chain.at_block_number

docs/api/eth/tools/api.tools.fixtures.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ sequence of functions.
155155
support only a single expectation with no specified network and no transaction (here, its role is
156156
played by :func:`~eth.tools.fixtures.fillers.execution`).
157157

158-
* ``post_state`` is a list of state definition in the same form as expected by `pre_state`. State items
159-
that are not set explicitly default to their pre state.
158+
* ``post_state`` is a list of state definition in the same form as expected
159+
by :func:`~eth.tools.fixtures.fillers.pre_state`. State items that are
160+
not set explicitly default to their pre state.
160161

161162
* ``networks`` defines the forks under which the expectation is applicable. It should be a sublist of
162163
the following identifiers (also available in `ALL_FORKS`):

eth/chains/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ def mine_block(self, *args: Any, **kwargs: Any) -> BaseBlock:
849849
self.validate_block(mined_block)
850850

851851
self.chaindb.persist_block(mined_block)
852-
self.header = self.ensure_header()
852+
self.header = self.create_header_from_parent(mined_block.header)
853853
return mined_block
854854

855855
def get_vm(self, at_header: BlockHeader=None) -> 'BaseVM':

eth/chains/mainnet/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,22 @@ class MainnetHomesteadVM(MainnetDAOValidatorVM, HomesteadVM):
6262
pass
6363

6464

65-
MAINNET_VM_CONFIGURATION = (
66-
(0, FrontierVM),
67-
(HOMESTEAD_MAINNET_BLOCK, MainnetHomesteadVM),
68-
(TANGERINE_WHISTLE_MAINNET_BLOCK, TangerineWhistleVM),
69-
(SPURIOUS_DRAGON_MAINNET_BLOCK, SpuriousDragonVM),
70-
(BYZANTIUM_MAINNET_BLOCK, ByzantiumVM),
65+
MAINNET_FORK_BLOCKS = (
66+
0,
67+
HOMESTEAD_MAINNET_BLOCK,
68+
TANGERINE_WHISTLE_MAINNET_BLOCK,
69+
SPURIOUS_DRAGON_MAINNET_BLOCK,
70+
BYZANTIUM_MAINNET_BLOCK,
71+
)
72+
MAINNET_VMS = (
73+
FrontierVM,
74+
MainnetHomesteadVM,
75+
TangerineWhistleVM,
76+
SpuriousDragonVM,
77+
ByzantiumVM,
7178
)
7279

80+
MAINNET_VM_CONFIGURATION = tuple(zip(MAINNET_FORK_BLOCKS, MAINNET_VMS))
7381

7482
MAINNET_NETWORK_ID = 1
7583

eth/tools/builder/__init__.py

Whitespace-only changes.

eth/tools/builder/chain/__init__.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
from .builders import ( # noqa: F401
2+
at_block_number,
3+
build,
4+
chain_split,
5+
copy,
6+
dao_fork_at,
7+
disable_dao_fork,
8+
disable_pow_check,
9+
enable_pow_mining,
10+
fork_at,
11+
genesis,
12+
import_block,
13+
import_blocks,
14+
mine_block,
15+
mine_blocks,
16+
name,
17+
)
18+
from .builders import ( # noqa: F401
19+
byzantium_at,
20+
frontier_at,
21+
homestead_at,
22+
spurious_dragon_at,
23+
tangerine_whistle_at,
24+
constantinople_at,
25+
)
26+
27+
28+
mainnet_fork_at_fns = (
29+
byzantium_at,
30+
frontier_at,
31+
homestead_at,
32+
spurious_dragon_at,
33+
tangerine_whistle_at,
34+
constantinople_at,
35+
)
36+
37+
38+
class API:
39+
#
40+
# Chain Class Construction
41+
#
42+
43+
# Primary wrapper function
44+
build = staticmethod(build)
45+
46+
# Configure chain vm_configuration
47+
fork_at = staticmethod(fork_at)
48+
49+
# Mainnet Forks
50+
frontier_at = staticmethod(frontier_at)
51+
homestead_at = staticmethod(homestead_at)
52+
tangerine_whistle_at = staticmethod(tangerine_whistle_at)
53+
spurious_dragon_at = staticmethod(spurious_dragon_at)
54+
byzantium_at = staticmethod(byzantium_at)
55+
constantinople_at = staticmethod(constantinople_at)
56+
57+
# iterable of the fork specific functions
58+
mainnet_fork_at_fns = staticmethod(mainnet_fork_at_fns)
59+
60+
# DAO Fork specific
61+
dao_fork_at = staticmethod(dao_fork_at)
62+
disable_dao_fork = staticmethod(disable_dao_fork)
63+
64+
# Chain Mining config
65+
enable_pow_mining = staticmethod(enable_pow_mining)
66+
disable_pow_check = staticmethod(disable_pow_check)
67+
68+
#
69+
# Chain Instance Initialization
70+
#
71+
name = staticmethod(name)
72+
genesis = staticmethod(genesis)
73+
74+
#
75+
# Chain Building
76+
#
77+
copy = staticmethod(copy)
78+
79+
import_block = staticmethod(import_block)
80+
import_blocks = staticmethod(import_blocks)
81+
82+
mine_block = staticmethod(mine_block)
83+
mine_blocks = staticmethod(mine_blocks)
84+
85+
chain_split = staticmethod(chain_split)
86+
at_block_number = staticmethod(at_block_number)
87+
88+
89+
api = API()

0 commit comments

Comments
 (0)