Skip to content

Commit ce8f5a2

Browse files
committed
Add more recipes to cookbook
1 parent 47047b3 commit ce8f5a2

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

docs/cookbooks/index.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,79 @@ Then to initialize, you can start it up with an in-memory database:
5151

5252
>>> # initialize a fresh chain
5353
>>> chain = chain_class.from_genesis_header(MemoryDB(), MAINNET_GENESIS_HEADER)
54+
55+
.. _evm_cookbook_recipe_creating_a_chain_with_custom_state:
56+
57+
Creating a chain with custom state
58+
----------------------------------
59+
60+
While the previous recipe demos how to create a chain from an existing genesis header, we can
61+
also create chains simply by specifing various genesis parameter as well as an optional genesis
62+
state.
63+
64+
.. doctest::
65+
66+
>>> from eth_keys import keys
67+
>>> from eth import constants
68+
>>> from eth.chains.mainnet import MainnetChain
69+
>>> from eth.db.backends.memory import MemoryDB
70+
>>> from eth_utils import to_wei, encode_hex
71+
72+
73+
74+
>>> # Giving funds to some address
75+
>>> SOME_ADDRESS = b'\x85\x82\xa2\x89V\xb9%\x93M\x03\xdd\xb4Xu\xe1\x8e\x85\x93\x12\xc1'
76+
>>> GENESIS_STATE = {
77+
... SOME_ADDRESS: {
78+
... "balance": to_wei(10000, 'ether'),
79+
... "nonce": 0,
80+
... "code": b'',
81+
... "storage": {}
82+
... }
83+
... }
84+
85+
>>> GENESIS_PARAMS = {
86+
... 'parent_hash': constants.GENESIS_PARENT_HASH,
87+
... 'uncles_hash': constants.EMPTY_UNCLE_HASH,
88+
... 'coinbase': constants.ZERO_ADDRESS,
89+
... 'transaction_root': constants.BLANK_ROOT_HASH,
90+
... 'receipt_root': constants.BLANK_ROOT_HASH,
91+
... 'difficulty': constants.GENESIS_DIFFICULTY,
92+
... 'block_number': constants.GENESIS_BLOCK_NUMBER,
93+
... 'gas_limit': constants.GENESIS_GAS_LIMIT,
94+
... 'extra_data': constants.GENESIS_EXTRA_DATA,
95+
... 'nonce': constants.GENESIS_NONCE
96+
... }
97+
98+
>>> chain = MainnetChain.from_genesis(MemoryDB(), GENESIS_PARAMS, GENESIS_STATE)
99+
100+
.. _evm_cookbook_recipe_getting_the_balance_from_an_account:
101+
102+
Getting the balance from an account
103+
-----------------------------------
104+
105+
Considering our previous example, we can get the balance of our pre-funded account as follows.
106+
107+
.. doctest::
108+
109+
>>> current_vm = chain.get_vm()
110+
>>> account_db = current_vm.state.account_db
111+
>>> account_db.get_balance(SOME_ADDRESS)
112+
10000000000000000000000
113+
114+
.. _evm_cookbook_recipe_building_blocks_incrementally:
115+
116+
Building blocks incrementally
117+
------------------------------
118+
119+
The default :class:`~eth.chains.chain.Chain` is stateless and thus does not keep a tip block open
120+
that would allow us to incrementally build a block. However, we can import the
121+
:class:`~eth.chains.chain.MiningChain` which does allow exactly that.
122+
123+
.. doctest::
124+
125+
>>> from eth.chains.base import MiningChain
126+
127+
Please check out the :doc:`Understanding the mining process
128+
</guides/eth/understanding_the_mining_process>` guide for a full example that demonstrates how
129+
to use the :class:`~eth.chains.chain.MiningChain`.

0 commit comments

Comments
 (0)