-
Notifications
You must be signed in to change notification settings - Fork 0
/
genesis.py
85 lines (69 loc) · 2.21 KB
/
genesis.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
Genesis Configuration
^^^^^^^^^^^^^^^^^^^^^
.. contents:: Table of Contents
:backlinks: none
:local:
Introduction
------------
Functionalities and entities to obtain the genesis configurations for
different chains.
"""
import json
import pkgutil
from dataclasses import dataclass
from typing import Dict, cast
from ethereum.base_types import U256, Bytes, Bytes8, Uint, slotted_freezable
from ethereum.utils.hexadecimal import (
hex_to_bytes,
hex_to_bytes8,
hex_to_u256,
hex_to_uint,
)
from .eth_types import Address
from .utils.hexadecimal import hex_to_address
@slotted_freezable
@dataclass
class GenesisConfiguration:
"""
Configuration for the first block of an Ethereum chain.
Specifies the allocation of ether set out in the pre-sale, and some of
the fields of the genesis block.
"""
difficulty: Uint
extra_data: Bytes
gas_limit: Uint
nonce: Bytes8
timestamp: U256
# Mapping between address and their initial balance
initial_balances: Dict[Address, U256]
def genesis_configuration(genesis_file: str) -> GenesisConfiguration:
"""
Obtain the genesis configuration from the given genesis json file.
The genesis file should be present in the `assets` directory.
Parameters
----------
genesis_file :
The json file which contains the parameters for the genesis block
and the pre-sale allocation data.
Returns
-------
configuration : `GenesisConfiguration`
The genesis configuration obtained from the json genesis file.
"""
genesis_str_data = cast(
bytes, pkgutil.get_data("ethereum", f"assets/{genesis_file}")
).decode()
genesis_data = json.loads(genesis_str_data)
initial_balances = {
hex_to_address(address): hex_to_u256(account["balance"])
for address, account in genesis_data["alloc"].items()
}
return GenesisConfiguration(
difficulty=hex_to_uint(genesis_data["difficulty"]),
extra_data=hex_to_bytes(genesis_data["extraData"]),
gas_limit=hex_to_uint(genesis_data["gasLimit"]),
nonce=hex_to_bytes8(genesis_data["nonce"]),
timestamp=hex_to_u256(genesis_data["timestamp"]),
initial_balances=initial_balances,
)