Skip to content

Commit 1e87a64

Browse files
committed
Add BlockProposal NamedTuple
1 parent 493f40a commit 1e87a64

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

eth/beacon/block_proposal.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import (
2+
NamedTuple,
3+
Tuple,
4+
TYPE_CHECKING,
5+
)
6+
7+
from eth.constants import (
8+
Hash32,
9+
)
10+
11+
if TYPE_CHECKING:
12+
from eth.beacon.types.blocks import AttestationRecord # noqa: F401
13+
from eth.beacon.types.blocks import BaseBeaconBlock # noqa: F401
14+
15+
16+
BlockProposal = NamedTuple(
17+
'BlockProposal',
18+
(
19+
('block', 'BaseBeaconBlock'),
20+
('shard_id', int),
21+
('shard_block_hash', Tuple[Hash32]),
22+
)
23+
)

eth/beacon/state_machines/base.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
Configurable,
3232
)
3333

34+
from eth.beacon.block_proposal import BlockProposal
3435
from eth.beacon.db.chain import BaseBeaconChainDB
3536
from eth.beacon.helpers import (
3637
create_signing_message,
@@ -379,9 +380,7 @@ def propose_block(
379380
self,
380381
crystallized_state: CrystallizedState,
381382
active_state: ActiveState,
382-
block: BaseBeaconBlock,
383-
shard_id: int,
384-
shard_block_hash: Hash32,
383+
block_proposal: 'BlockProposal',
385384
chaindb: BaseBeaconChainDB,
386385
config: BeaconConfig,
387386
private_key: int
@@ -392,7 +391,7 @@ def propose_block(
392391
block, post_crystallized_state, post_active_state = self.process_block(
393392
crystallized_state,
394393
active_state,
395-
block,
394+
block_proposal.block,
396395
chaindb,
397396
config,
398397
)
@@ -402,13 +401,16 @@ def propose_block(
402401
crystallized_state_root=post_crystallized_state.hash,
403402
active_state_root=post_active_state.hash,
404403
)
404+
block_proposal = BlockProposal(
405+
block=post_block,
406+
shard_id=block_proposal.shard_id,
407+
shard_block_hash=block_proposal.shard_block_hash,
408+
)
405409

406410
proposer_attestation = self.attest_proposed_block(
407411
post_crystallized_state,
408412
post_active_state,
409-
post_block,
410-
shard_id,
411-
shard_block_hash,
413+
block_proposal,
412414
chaindb,
413415
config.CYCLE_LENGTH,
414416
private_key,
@@ -424,9 +426,7 @@ def _update_the_states(self,
424426
def attest_proposed_block(self,
425427
post_crystallized_state: CrystallizedState,
426428
post_active_state: ActiveState,
427-
block: BaseBeaconBlock,
428-
shard_id: int,
429-
shard_block_hash: Hash32,
429+
block_proposal: 'BlockProposal',
430430
chaindb: BaseBeaconChainDB,
431431
cycle_length: int,
432432
private_key: int) -> 'AttestationRecord':
@@ -436,7 +436,7 @@ def attest_proposed_block(self,
436436
The proposer broadcasts their attestation with the proposed block.
437437
"""
438438
block_committees_info = get_block_committees_info(
439-
block,
439+
block_proposal.block,
440440
post_crystallized_state,
441441
cycle_length,
442442
)
@@ -453,14 +453,14 @@ def attest_proposed_block(self,
453453
# Get signing message and sign it
454454
parent_hashes = get_hashes_to_sign(
455455
post_active_state.recent_block_hashes,
456-
block,
456+
block_proposal.block,
457457
cycle_length,
458458
)
459459
message = create_signing_message(
460-
block.slot_number,
460+
block_proposal.block.slot_number,
461461
parent_hashes,
462-
shard_id,
463-
shard_block_hash,
462+
block_proposal.shard_id,
463+
block_proposal.shard_block_hash,
464464
justified_slot,
465465
)
466466
sigs = [
@@ -472,10 +472,10 @@ def attest_proposed_block(self,
472472
aggregate_sig = bls.aggregate_sigs(sigs)
473473

474474
return self.get_attestation_record_class()(
475-
slot=block.slot_number,
476-
shard_id=shard_id,
475+
slot=block_proposal.block.slot_number,
476+
shard_id=block_proposal.shard_id,
477477
oblique_parent_hashes=(),
478-
shard_block_hash=shard_block_hash,
478+
shard_block_hash=block_proposal.shard_block_hash,
479479
attester_bitfield=attester_bitfield,
480480
justified_slot=justified_slot,
481481
justified_block_hash=justified_block_hash,

tests/beacon/state_machines/test_proposer.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
ZERO_HASH32,
55
)
66

7+
from eth.beacon.block_proposal import BlockProposal
78
from eth.beacon.helpers import (
89
get_block_committees_info,
910
get_new_recent_block_hashes,
@@ -46,14 +47,17 @@ def test_propose_block_and_validate_attestation(fixture_sm_class,
4647
)
4748
# public_key = sm.crystallized_state.validators[block_committees_info.proposer_index].pubkey
4849
private_key = privkeys[block_committees_info.proposer_index]
50+
block_proposal = BlockProposal(
51+
block=block_1_shell,
52+
shard_id=block_committees_info.proposer_shard_id,
53+
shard_block_hash=ZERO_HASH32,
54+
)
4955

5056
(block_1, post_crystallized_state, post_active_state, proposer_attestation) = (
5157
sm.propose_block(
5258
crystallized_state=sm.crystallized_state,
5359
active_state=sm.active_state,
54-
block=block_1_shell,
55-
shard_id=block_committees_info.proposer_shard_id,
56-
shard_block_hash=ZERO_HASH32,
60+
block_proposal=block_proposal,
5761
chaindb=sm.chaindb,
5862
config=sm.config,
5963
private_key=private_key,

0 commit comments

Comments
 (0)