Skip to content

Commit 071aa2a

Browse files
Fix #1582, Special -> operations and rework beacon block (#1585)
* delete special_records and add casper_slashings * add a crappy Deposit * fix attestation block exit proposer_slashings and add tests * fix linting error by removing unused block_proposal.py * split deposits, deposit_data, deposit_parameters, casper_slashings, and slashable_vote_data out * fix validator_index in Exit * replace ancestor_hashes and parent_hash with parent_root, reorder BeaconBlockBody attestation field, and fix comments * use tuple as default parameter * remove kwargs for performance * make flake8 happy * more ancestor_hashes to parent_root, and rework get_pseudo_chain * update casper_slashings to reflect ethereum/consensus-specs/pull/301 * refactor get_pseudo_chain to yield * flake8 * fix create_test_block * fix child.parent_root
1 parent 0b0b6ec commit 071aa2a

33 files changed

+528
-263
lines changed

eth/beacon/block_proposal.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

eth/beacon/db/chain.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -278,25 +278,25 @@ def _persist_block_chain(
278278
return tuple(), tuple()
279279
else:
280280
for parent, child in sliding_window(2, blocks):
281-
if parent.hash != child.parent_hash:
281+
if parent.hash != child.parent_root:
282282
raise ValidationError(
283283
"Non-contiguous chain. Expected {} to have {} as parent but was {}".format(
284284
encode_hex(child.hash),
285285
encode_hex(parent.hash),
286-
encode_hex(child.parent_hash),
286+
encode_hex(child.parent_root),
287287
)
288288
)
289289

290-
is_genesis = first_block.parent_hash == GENESIS_PARENT_HASH
291-
if not is_genesis and not cls._block_exists(db, first_block.parent_hash):
290+
is_genesis = first_block.parent_root == GENESIS_PARENT_HASH
291+
if not is_genesis and not cls._block_exists(db, first_block.parent_root):
292292
raise ParentNotFound(
293293
"Cannot persist block ({}) with unknown parent ({})".format(
294-
encode_hex(first_block.hash), encode_hex(first_block.parent_hash)))
294+
encode_hex(first_block.hash), encode_hex(first_block.parent_root)))
295295

296296
if is_genesis:
297297
score = 0
298298
else:
299-
score = cls._get_score(db, first_block.parent_hash)
299+
score = cls._get_score(db, first_block.parent_root)
300300

301301
for block in blocks:
302302
db.set(
@@ -389,10 +389,10 @@ def _find_new_ancestors(cls, db: BaseDB, block: BaseBeaconBlock) -> Iterable[Bas
389389
# Found a new ancestor
390390
yield block
391391

392-
if block.parent_hash == GENESIS_PARENT_HASH:
392+
if block.parent_root == GENESIS_PARENT_HASH:
393393
break
394394
else:
395-
block = cls._get_block_by_hash(db, block.parent_hash)
395+
block = cls._get_block_by_hash(db, block.parent_root)
396396

397397
@staticmethod
398398
def _add_block_slot_to_hash_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:

eth/beacon/types/attestation_data.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ def __init__(self,
4242
justified_slot: int,
4343
justified_block_hash: Hash32) -> None:
4444
super().__init__(
45-
slot=slot,
46-
shard=shard,
47-
beacon_block_hash=beacon_block_hash,
48-
epoch_boundary_hash=epoch_boundary_hash,
49-
shard_block_hash=shard_block_hash,
50-
latest_crosslink_hash=latest_crosslink_hash,
51-
justified_slot=justified_slot,
52-
justified_block_hash=justified_block_hash,
45+
slot,
46+
shard,
47+
beacon_block_hash,
48+
epoch_boundary_hash,
49+
shard_block_hash,
50+
latest_crosslink_hash,
51+
justified_slot,
52+
justified_block_hash,
5353
)

eth/beacon/types/attestation_records.py renamed to eth/beacon/types/attestations.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919

2020

21-
class AttestationRecord(rlp.Serializable):
21+
class Attestation(rlp.Serializable):
2222
"""
2323
Note: using RLP until we have standardized serialization format.
2424
"""
@@ -36,13 +36,10 @@ def __init__(self,
3636
data: AttestationData,
3737
participation_bitfield: bytes,
3838
custody_bitfield: bytes,
39-
aggregate_sig: Sequence[int]=None) -> None:
40-
if aggregate_sig is None:
41-
aggregate_sig = (0, 0)
42-
39+
aggregate_sig: Sequence[int]=(0, 0)) -> None:
4340
super().__init__(
44-
data=data,
45-
participation_bitfield=participation_bitfield,
46-
custody_bitfield=custody_bitfield,
47-
aggregate_sig=aggregate_sig,
41+
data,
42+
participation_bitfield,
43+
custody_bitfield,
44+
aggregate_sig,
4845
)

eth/beacon/types/blocks.py

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,73 @@
2121
)
2222
from eth.utils.blake import blake
2323

24-
from .attestation_records import AttestationRecord
25-
from .special_records import SpecialRecord
24+
from .attestations import Attestation
25+
from .proposer_slashings import ProposerSlashing
26+
from .casper_slashings import CasperSlashing
27+
from .deposits import Deposit
28+
from .exits import Exit
29+
30+
31+
class BeaconBlockBody(rlp.Serializable):
32+
fields = [
33+
('proposer_slashings', CountableList(ProposerSlashing)),
34+
('casper_slashings', CountableList(CasperSlashing)),
35+
('attestations', CountableList(Attestation)),
36+
('deposits', CountableList(Deposit)),
37+
('exits', CountableList(Exit)),
38+
]
39+
40+
def __init__(self,
41+
proposer_slashings: Sequence[int],
42+
casper_slashings: Sequence[int],
43+
attestations: Sequence[int],
44+
deposits: Sequence[int],
45+
exits: Sequence[int])-> None:
46+
super().__init__(
47+
proposer_slashings=proposer_slashings,
48+
casper_slashings=casper_slashings,
49+
attestations=attestations,
50+
deposits=deposits,
51+
exits=exits,
52+
)
2653

2754

2855
class BaseBeaconBlock(rlp.Serializable):
2956
fields = [
30-
# Slot number
57+
#
58+
# Header
59+
#
3160
('slot', uint64),
32-
# Proposer RANDAO reveal
33-
('randao_reveal', hash32),
34-
# Recent PoW receipt root
35-
('candidate_pow_receipt_root', hash32),
3661
# Skip list of previous beacon block hashes
3762
# i'th item is the most recent ancestor whose slot is a multiple of 2**i for i = 0, ..., 31
38-
('ancestor_hashes', CountableList(hash32)),
39-
# State root
63+
('parent_root', hash32),
4064
('state_root', hash32),
41-
# Attestations
42-
('attestations', CountableList(AttestationRecord)),
43-
# Specials (e.g. logouts, penalties)
44-
('specials', CountableList(SpecialRecord)),
45-
# Proposer signature
46-
('proposer_signature', CountableList(uint256)),
65+
('randao_reveal', hash32),
66+
('candidate_pow_receipt_root', hash32),
67+
('signature', CountableList(uint256)),
68+
69+
#
70+
# Body
71+
#
72+
('body', BeaconBlockBody)
4773
]
4874

4975
def __init__(self,
5076
slot: int,
77+
parent_root: Hash32,
78+
state_root: Hash32,
5179
randao_reveal: Hash32,
5280
candidate_pow_receipt_root: Hash32,
53-
ancestor_hashes: Sequence[Hash32],
54-
state_root: Hash32,
55-
attestations: Sequence[AttestationRecord],
56-
specials: Sequence[SpecialRecord],
57-
proposer_signature: Sequence[int]=None) -> None:
58-
if proposer_signature is None:
59-
proposer_signature = (0, 0)
81+
body: BeaconBlockBody,
82+
signature: Sequence[int]=(0, 0)) -> None:
6083
super().__init__(
61-
slot=slot,
62-
randao_reveal=randao_reveal,
63-
candidate_pow_receipt_root=candidate_pow_receipt_root,
64-
ancestor_hashes=ancestor_hashes,
65-
state_root=state_root,
66-
attestations=attestations,
67-
specials=specials,
68-
proposer_signature=proposer_signature,
84+
slot,
85+
parent_root,
86+
state_root,
87+
randao_reveal,
88+
candidate_pow_receipt_root,
89+
signature,
90+
body
6991
)
7092

7193
def __repr__(self) -> str:
@@ -84,11 +106,4 @@ def hash(self) -> Hash32:
84106

85107
@property
86108
def num_attestations(self) -> int:
87-
return len(self.attestations)
88-
89-
@property
90-
def parent_hash(self) -> Hash32:
91-
if not self.ancestor_hashes:
92-
return None
93-
else:
94-
return self.ancestor_hashes[0]
109+
return len(self.body.attestations)

eth/beacon/types/casper_slashings.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import rlp
2+
from .slashable_vote_data import SlashableVoteData
3+
4+
5+
class CasperSlashing(rlp.Serializable):
6+
"""
7+
Note: using RLP until we have standardized serialization format.
8+
"""
9+
fields = [
10+
# First batch of votes
11+
('slashable_vote_data_1', SlashableVoteData),
12+
# Second batch of votes
13+
('slashable_vote_data_2', SlashableVoteData),
14+
]
15+
16+
def __init__(self,
17+
slashable_vote_data_1: SlashableVoteData,
18+
slashable_vote_data_2: SlashableVoteData)-> None:
19+
super().__init__(
20+
slashable_vote_data_1,
21+
slashable_vote_data_2,
22+
)

eth/beacon/types/deposit_data.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
import rlp
3+
from eth.rlp.sedes import uint64
4+
from .deposit_parameters import DepositParameters
5+
6+
7+
class DepositData(rlp.Serializable):
8+
"""
9+
Not in spec, this is for fields in Deposit
10+
"""
11+
fields = [
12+
# Deposit parameters
13+
('deposit_parameters', DepositParameters),
14+
# Value in Gwei
15+
('value', uint64),
16+
# Timestamp from deposit contract
17+
('timestamp', uint64),
18+
]
19+
20+
def __init__(self,
21+
deposit_parameters: DepositParameters,
22+
value: int,
23+
timestamp: int) -> None:
24+
25+
super().__init__(
26+
deposit_parameters,
27+
value,
28+
timestamp,
29+
)

eth/beacon/types/deposit_parameters_records.py renamed to eth/beacon/types/deposit_parameters.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
)
1717

1818

19-
class DepositParametersRecord(rlp.Serializable):
19+
class DepositParameters(rlp.Serializable):
2020
"""
2121
Note: using RLP until we have standardized serialization format.
2222
"""
@@ -35,13 +35,10 @@ def __init__(self,
3535
pubkey: int,
3636
withdrawal_credentials: Hash32,
3737
randao_commitment: Hash32,
38-
proof_of_possession: Sequence[int]=None) -> None:
39-
if proof_of_possession is None:
40-
proof_of_possession = (0, 0)
41-
38+
proof_of_possession: Sequence[int]=(0, 0)) -> None:
4239
super().__init__(
43-
pubkey=pubkey,
44-
proof_of_possession=proof_of_possession,
45-
withdrawal_credentials=withdrawal_credentials,
46-
randao_commitment=randao_commitment,
40+
pubkey,
41+
proof_of_possession,
42+
withdrawal_credentials,
43+
randao_commitment,
4744
)

eth/beacon/types/deposits.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import (
2+
Sequence,
3+
)
4+
5+
from eth_typing import (
6+
Hash32,
7+
)
8+
import rlp
9+
from rlp.sedes import (
10+
CountableList,
11+
)
12+
13+
from eth.rlp.sedes import (
14+
hash32,
15+
uint64,
16+
)
17+
18+
from .deposit_data import DepositData
19+
20+
21+
class Deposit(rlp.Serializable):
22+
"""
23+
Note: using RLP until we have standardized serialization format.
24+
"""
25+
26+
fields = [
27+
# Receipt Merkle branch
28+
('merkle_branch', CountableList(hash32)),
29+
# Merkle tree index
30+
('merkle_tree_index', uint64),
31+
# Deposit data
32+
('deposit_data', DepositData),
33+
]
34+
35+
def __init__(self,
36+
merkle_branch: Sequence[Hash32],
37+
merkle_tree_index: int,
38+
deposit_data: DepositData)-> None:
39+
super().__init__(
40+
merkle_branch,
41+
merkle_tree_index,
42+
deposit_data,
43+
)

eth/beacon/types/exits.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from typing import Sequence
2+
import rlp
3+
from rlp.sedes import (
4+
CountableList,
5+
)
6+
from eth.rlp.sedes import (
7+
uint24,
8+
uint64,
9+
uint256,
10+
)
11+
12+
13+
class Exit(rlp.Serializable):
14+
"""
15+
Note: using RLP until we have standardized serialization format.
16+
"""
17+
fields = [
18+
# Minimum slot for processing exit
19+
('slot', uint64),
20+
# Index of the exiting validator
21+
('validator_index', uint24),
22+
# Validator signature
23+
('signature', CountableList(uint256)),
24+
]
25+
26+
def __init__(self,
27+
slot: int,
28+
validator_index: int,
29+
signature: Sequence[int]) -> None:
30+
super().__init__(
31+
slot,
32+
validator_index,
33+
signature,
34+
)

0 commit comments

Comments
 (0)