Skip to content

Commit 14c5b1e

Browse files
committed
Add helpers to create beacon block chains
1 parent 4e9f017 commit 14c5b1e

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

tests/trinity/core/p2p-proto/bcc/helpers.py

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import asyncio
22

3+
from cytoolz import (
4+
merge,
5+
)
6+
37
from cancel_token import CancelToken
48

9+
from eth_utils import (
10+
to_tuple,
11+
)
12+
513
from eth.db.atomic import AtomicDB
614
from eth.beacon.db.chain import BeaconChainDB
715
from eth.beacon.types.blocks import BaseBeaconBlock
@@ -12,6 +20,7 @@
1220
from trinity.protocol.bcc.context import BeaconContext
1321
from trinity.protocol.bcc.peer import (
1422
BCCPeerFactory,
23+
BCCPeerPool,
1524
)
1625

1726
from p2p import ecies
@@ -21,21 +30,46 @@
2130
)
2231

2332

33+
def create_test_block(parent=None, **kwargs):
34+
defaults = {
35+
"slot": 0,
36+
"randao_reveal": ZERO_HASH32,
37+
"candidate_pow_receipt_root": ZERO_HASH32,
38+
"ancestor_hashes": [ZERO_HASH32] * 32,
39+
"state_root": ZERO_HASH32, # note: not the actual genesis state root
40+
"attestations": [],
41+
"specials": [],
42+
"proposer_signature": None,
43+
}
44+
45+
if parent is not None:
46+
kwargs["ancestor_hashes"] = [parent.hash] + [ZERO_HASH32] * 31
47+
kwargs["slot"] = parent.slot + 1
48+
49+
return BaseBeaconBlock(**merge(defaults, kwargs))
50+
51+
52+
@to_tuple
53+
def create_branch(length, root, **start_kwargs):
54+
if length == 0:
55+
return
56+
57+
parent = create_test_block(parent=root, **start_kwargs)
58+
yield parent
59+
60+
for slot in range(root.slot + 2, root.slot + length + 1):
61+
child = create_test_block(parent)
62+
yield child
63+
parent = child
64+
65+
2466
def get_fresh_chain_db():
2567
db = AtomicDB()
26-
genesis_block = BaseBeaconBlock(
27-
slot=0,
28-
randao_reveal=ZERO_HASH32,
29-
candidate_pow_receipt_root=ZERO_HASH32,
30-
ancestor_hashes=[ZERO_HASH32] * 32,
31-
state_root=ZERO_HASH32, # note: not the actual genesis state root
32-
attestations=[],
33-
specials=[],
34-
proposer_signature=None,
35-
)
68+
genesis_block = create_test_block(slot=0)
3669

3770
chain_db = BeaconChainDB(db)
3871
chain_db.persist_block(genesis_block)
72+
3973
return chain_db
4074

4175

0 commit comments

Comments
 (0)