Skip to content

Commit ec95bd5

Browse files
add beacon chain related NewTypes
1 parent 3b8fd86 commit ec95bd5

28 files changed

+235
-103
lines changed

eth/_utils/bitfield.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,33 @@
1010
from itertools import (
1111
zip_longest,
1212
)
13+
from eth.beacon.typing import Bitfield
1314

1415

1516
@curry
16-
def has_voted(bitfield: bytes, index: int) -> bool:
17+
def has_voted(bitfield: Bitfield, index: int) -> bool:
1718
return bool(bitfield[index // 8] & (128 >> (index % 8)))
1819

1920

2021
@curry
21-
def set_voted(bitfield: bytes, index: int) -> bytes:
22+
def set_voted(bitfield: Bitfield, index: int) -> Bitfield:
2223
byte_index = index // 8
2324
bit_index = index % 8
2425
new_byte_value = bitfield[byte_index] | (128 >> bit_index)
25-
return bitfield[:byte_index] + bytes([new_byte_value]) + bitfield[byte_index + 1:]
26+
new_bitfield = bitfield[:byte_index] + bytes([new_byte_value]) + bitfield[byte_index + 1:]
27+
return Bitfield(new_bitfield)
2628

2729

2830
def get_bitfield_length(bit_count: int) -> int:
2931
"""Return the length of the bitfield for a given number of attesters in bytes."""
3032
return (bit_count + 7) // 8
3133

3234

33-
def get_empty_bitfield(bit_count: int) -> bytes:
34-
return b"\x00" * get_bitfield_length(bit_count)
35+
def get_empty_bitfield(bit_count: int) -> Bitfield:
36+
return Bitfield(b"\x00" * get_bitfield_length(bit_count))
3537

3638

37-
def get_vote_count(bitfield: bytes) -> int:
39+
def get_vote_count(bitfield: Bitfield) -> int:
3840
return len(
3941
tuple(
4042
index
@@ -44,10 +46,10 @@ def get_vote_count(bitfield: bytes) -> int:
4446
)
4547

4648

47-
def or_bitfields(bitfields: List[bytes]) -> bytes:
49+
def or_bitfields(bitfields: List[Bitfield]) -> Bitfield:
4850
byte_slices = zip_longest(*bitfields)
4951

5052
if len(set((len(b) for b in bitfields))) != 1:
5153
raise ValueError("The bitfield sizes are different")
5254

53-
return bytes(map(reduce(operator.or_), byte_slices))
55+
return Bitfield(bytes(map(reduce(operator.or_), byte_slices)))

eth/_utils/bls.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
)
3434
from eth.beacon._utils.hash import hash_eth2
3535

36+
from eth.beacon.typing import (
37+
BLSPubkey,
38+
BLSPubkeyAggregated,
39+
BLSSignature,
40+
BLSSignatureAggregated,
41+
)
3642

3743
G2_cofactor = 305502333931268344200999753193121504214466019254188142667664032982267604182971884026507427359259977847832272839041616661285803823378372096355777062779109 # noqa: E501
3844
FQ2_order = q ** 2 - 1
@@ -156,20 +162,21 @@ def decompress_G2(p: bytes) -> Tuple[FQP, FQP, FQP]:
156162
#
157163
def sign(message: bytes,
158164
privkey: int,
159-
domain: int) -> Tuple[int, int]:
160-
return compress_G2(
161-
multiply(
162-
hash_to_G2(message, domain),
163-
privkey
164-
)
165-
)
165+
domain: int) -> BLSSignatureAggregated:
166+
return BLSSignatureAggregated(
167+
compress_G2(
168+
multiply(
169+
hash_to_G2(message, domain),
170+
privkey
171+
)
172+
))
166173

167174

168-
def privtopub(k: int) -> int:
169-
return compress_G1(multiply(G1, k))
175+
def privtopub(k: int) -> BLSPubkey:
176+
return BLSPubkey(compress_G1(multiply(G1, k)))
170177

171178

172-
def verify(message: bytes, pubkey: int, signature: bytes, domain: int) -> bool:
179+
def verify(message: bytes, pubkey: BLSPubkey, signature: BLSSignature, domain: int) -> bool:
173180
try:
174181
final_exponentiation = final_exponentiate(
175182
pairing(
@@ -188,23 +195,23 @@ def verify(message: bytes, pubkey: int, signature: bytes, domain: int) -> bool:
188195
return False
189196

190197

191-
def aggregate_signatures(signatures: Sequence[bytes]) -> Tuple[int, int]:
198+
def aggregate_signatures(signatures: Sequence[BLSSignature]) -> BLSSignatureAggregated:
192199
o = Z2
193200
for s in signatures:
194201
o = FQP_point_to_FQ2_point(add(o, decompress_G2(s)))
195-
return compress_G2(o)
202+
return BLSSignatureAggregated(compress_G2(o))
196203

197204

198-
def aggregate_pubkeys(pubkeys: Sequence[int]) -> int:
205+
def aggregate_pubkeys(pubkeys: Sequence[BLSPubkey]) -> BLSPubkeyAggregated:
199206
o = Z1
200207
for p in pubkeys:
201208
o = add(o, decompress_G1(p))
202-
return compress_G1(o)
209+
return BLSPubkeyAggregated(compress_G1(o))
203210

204211

205-
def verify_multiple(pubkeys: Sequence[int],
212+
def verify_multiple(pubkeys: Sequence[BLSPubkey],
206213
messages: Sequence[bytes],
207-
signature: bytes,
214+
signature: BLSSignature,
208215
domain: int) -> bool:
209216
len_msgs = len(messages)
210217

eth/beacon/aggregation.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
set_voted,
1212
)
1313
from eth.beacon.enums import SignatureDomain
14+
from eth.beacon.typing import (
15+
BLSPubkey,
16+
BLSSignature,
17+
BLSSignatureAggregated,
18+
Bitfield,
19+
)
1420

1521

1622
def verify_votes(
1723
message: bytes,
18-
votes: Iterable[Tuple[int, bytes, int]],
24+
votes: Iterable[Tuple[int, BLSSignature, BLSPubkey]],
1925
domain: SignatureDomain) -> Tuple[Tuple[bytes, ...], Tuple[int, ...]]:
2026
"""
2127
Verify the given votes.
@@ -37,10 +43,10 @@ def verify_votes(
3743
return sigs, committee_indices
3844

3945

40-
def aggregate_votes(bitfield: bytes,
41-
sigs: Iterable[bytes],
42-
voting_sigs: Iterable[bytes],
43-
voting_committee_indices: Iterable[int]) -> Tuple[bytes, Tuple[int, int]]:
46+
def aggregate_votes(bitfield: Bitfield,
47+
sigs: Iterable[BLSSignature],
48+
voting_sigs: Iterable[BLSSignature],
49+
voting_committee_indices: Iterable[int]) -> Tuple[Bitfield, BLSSignatureAggregated]:
4450
"""
4551
Aggregate the votes.
4652
"""

eth/beacon/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from eth.beacon.typing import BLSSignatureAggregated
2+
13
#
24
# shuffle function
35
#
@@ -11,4 +13,4 @@
1113
# The highest possible result of the RNG.
1214
RAND_MAX = 2 ** (RAND_BYTES * 8) - 1
1315

14-
EMPTY_SIGNATURE = (0, 0)
16+
EMPTY_SIGNATURE = BLSSignatureAggregated((0, 0))

eth/beacon/deposit_helpers.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
from eth.beacon.helpers import (
2828
get_domain,
2929
)
30+
from eth.beacon.typing import (
31+
BLSPubkey,
32+
BLSSignature,
33+
Gwei,
34+
)
3035

3136

3237
def get_min_empty_validator_index(validators: Sequence[ValidatorRecord],
@@ -44,8 +49,8 @@ def get_min_empty_validator_index(validators: Sequence[ValidatorRecord],
4449

4550

4651
def validate_proof_of_possession(state: BeaconState,
47-
pubkey: int,
48-
proof_of_possession: bytes,
52+
pubkey: BLSPubkey,
53+
proof_of_possession: BLSSignature,
4954
withdrawal_credentials: Hash32,
5055
randao_commitment: Hash32) -> None:
5156
deposit_input = DepositInput(
@@ -75,7 +80,7 @@ def validate_proof_of_possession(state: BeaconState,
7580

7681
def add_pending_validator(state: BeaconState,
7782
validator: ValidatorRecord,
78-
deposit: int,
83+
deposit: Gwei,
7984
zero_balance_validator_ttl: int) -> Tuple[BeaconState, int]:
8085
"""
8186
Add a validator to the existing minimum empty validator index or
@@ -108,9 +113,9 @@ def add_pending_validator(state: BeaconState,
108113

109114
def process_deposit(*,
110115
state: BeaconState,
111-
pubkey: int,
112-
deposit: int,
113-
proof_of_possession: bytes,
116+
pubkey: BLSPubkey,
117+
deposit: Gwei,
118+
proof_of_possession: BLSSignature,
114119
withdrawal_credentials: Hash32,
115120
randao_commitment: Hash32,
116121
zero_balance_validator_ttl: int) -> Tuple[BeaconState, int]:

eth/beacon/helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@
5151
from eth.beacon.types.fork_data import ForkData # noqa: F401
5252
from eth.beacon.types.slashable_vote_data import SlashableVoteData # noqa: F401
5353
from eth.beacon.types.validator_records import ValidatorRecord # noqa: F401
54-
54+
from eth.beacon.typing import (
55+
ShardNumber,
56+
BLSPubkey,
57+
)
5558

5659
def _get_element_from_recent_list(
5760
target_list: Sequence[Any],
@@ -161,7 +164,7 @@ def _get_shards_committees_for_shard_indices(
161164
"""
162165
for index, indices in enumerate(shard_indices):
163166
yield ShardCommittee(
164-
shard=(start_shard + index) % shard_count,
167+
shard=ShardNumber((start_shard + index) % shard_count),
165168
committee=indices,
166169
total_validator_count=total_validator_count,
167170
)
@@ -371,7 +374,7 @@ def get_effective_balance(validator_balances: Sequence[int], index: int, max_dep
371374

372375
def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_chain_tip: Hash32,
373376
validator_index: int,
374-
pubkey: int,
377+
pubkey: BLSPubkey,
375378
flag: int) -> Hash32:
376379
"""
377380
Compute the next hash in the validator registry delta hash chain.

eth/beacon/state_machines/configs.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
from eth.typing import (
66
Address,
77
)
8+
from eth.beacon.typing import (
9+
SlotNumber,
10+
ShardNumber,
11+
Ether,
12+
Seconds,
13+
)
814

915

1016
BeaconConfig = NamedTuple(
@@ -13,9 +19,9 @@
1319
# Misc
1420
('SHARD_COUNT', int),
1521
('TARGET_COMMITTEE_SIZE', int),
16-
('EJECTION_BALANCE', int),
22+
('EJECTION_BALANCE', Ether),
1723
('MAX_BALANCE_CHURN_QUOTIENT', int),
18-
('BEACON_CHAIN_SHARD_NUMBER', int),
24+
('BEACON_CHAIN_SHARD_NUMBER', ShardNumber),
1925
('BLS_WITHDRAWAL_PREFIX_BYTE', bytes),
2026
('MAX_CASPER_VOTES', int),
2127
('LATEST_BLOCK_ROOTS_LENGTH', int),
@@ -24,14 +30,14 @@
2430
# Deposit contract
2531
('DEPOSIT_CONTRACT_ADDRESS', Address),
2632
('DEPOSIT_CONTRACT_TREE_DEPTH', int),
27-
('MIN_DEPOSIT', int),
28-
('MAX_DEPOSIT', int),
33+
('MIN_DEPOSIT', Ether),
34+
('MAX_DEPOSIT', Ether),
2935
# ZERO_HASH (ZERO_HASH32) is defined in constants.py
3036
# Initial values
3137
('INITIAL_FORK_VERSION', int),
32-
('INITIAL_SLOT_NUMBER', int),
38+
('INITIAL_SLOT_NUMBER', SlotNumber),
3339
# Time parameters
34-
('SLOT_DURATION', int),
40+
('SLOT_DURATION', Seconds),
3541
('MIN_ATTESTATION_INCLUSION_DELAY', int),
3642
('EPOCH_LENGTH', int),
3743
('POW_RECEIPT_ROOT_VOTING_PERIOD', int),

eth/beacon/state_machines/forks/serenity/configs.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,35 @@
22
ZERO_ADDRESS,
33
)
44
from eth.beacon.state_machines.configs import BeaconConfig
5+
from eth.beacon.typing import (
6+
SlotNumber,
7+
ShardNumber,
8+
Ether,
9+
Seconds,
10+
)
511

612

713
SERENITY_CONFIG = BeaconConfig(
814
# Misc
915
SHARD_COUNT=2**10, # (= 1,024) shards
1016
TARGET_COMMITTEE_SIZE=2**7, # (= 128) validators
11-
EJECTION_BALANCE=2**4, # (= 16) ETH
17+
EJECTION_BALANCE=Ether(2**4), # (= 16) ETH
1218
MAX_BALANCE_CHURN_QUOTIENT=2**5, # (= 32)
13-
BEACON_CHAIN_SHARD_NUMBER=2**64 - 1,
19+
BEACON_CHAIN_SHARD_NUMBER=ShardNumber(2**64 - 1),
1420
BLS_WITHDRAWAL_PREFIX_BYTE=b'\x00',
1521
MAX_CASPER_VOTES=2**10, # (= 1,024) votes
1622
LATEST_BLOCK_ROOTS_LENGTH=2**13, # (= 8,192) block roots
1723
LATEST_RANDAO_MIXES_LENGTH=2**13, # (= 8,192) randao mixes
1824
# Deposit contract
1925
DEPOSIT_CONTRACT_ADDRESS=ZERO_ADDRESS, # TBD
2026
DEPOSIT_CONTRACT_TREE_DEPTH=2**5, # (= 32)
21-
MIN_DEPOSIT=2**0, # (= 1) ETH
22-
MAX_DEPOSIT=2**5, # (= 32) ETH
27+
MIN_DEPOSIT=Ether(2**0), # (= 1) ETH
28+
MAX_DEPOSIT=Ether(2**5), # (= 32) ETH
2329
# Initial values
2430
INITIAL_FORK_VERSION=0,
25-
INITIAL_SLOT_NUMBER=0,
31+
INITIAL_SLOT_NUMBER=SlotNumber(0),
2632
# Time parameters
27-
SLOT_DURATION=6, # seconds
33+
SLOT_DURATION=Seconds(6), # seconds
2834
MIN_ATTESTATION_INCLUSION_DELAY=2**2, # (= 4) slots
2935
EPOCH_LENGTH=2**6, # (= 64) slots
3036
POW_RECEIPT_ROOT_VOTING_PERIOD=2**10, # (= 1,024) slots

eth/beacon/state_machines/validation.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@
2323
verify,
2424
)
2525

26+
from eth.beacon.typing import (
27+
ShardNumber,
28+
)
29+
2630

2731
def validate_proposer_signature(state: BeaconState,
2832
block: BaseBeaconBlock,
29-
beacon_chain_shard_number: int,
33+
beacon_chain_shard_number: ShardNumber,
3034
epoch_length: int) -> None:
3135
block_without_signature_root = block.block_without_signature_root
3236

eth/beacon/types/attestation_data.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
hash32,
99
)
1010

11+
from eth.beacon.typing import (
12+
SlotNumber,
13+
ShardNumber,
14+
)
15+
1116

1217
class AttestationData(rlp.Serializable):
1318
"""
@@ -33,13 +38,13 @@ class AttestationData(rlp.Serializable):
3338
]
3439

3540
def __init__(self,
36-
slot: int,
37-
shard: int,
41+
slot: SlotNumber,
42+
shard: ShardNumber,
3843
beacon_block_root: Hash32,
3944
epoch_boundary_root: Hash32,
4045
shard_block_root: Hash32,
4146
latest_crosslink_root: Hash32,
42-
justified_slot: int,
47+
justified_slot: SlotNumber,
4348
justified_block_root: Hash32) -> None:
4449
super().__init__(
4550
slot,

0 commit comments

Comments
 (0)