Skip to content

Commit 6fa7b83

Browse files
committed
Add get_fork_version and get_domain
1 parent 751c856 commit 6fa7b83

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

eth/beacon/enums/__init__.py

Whitespace-only changes.

eth/beacon/enums/signature_domain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import IntEnum
22

33

4-
class BLSDomain(IntEnum):
4+
class SignatureDomain(IntEnum):
55
DOMAIN_DEPOSIT = 0
66
DOMAIN_ATTESTATION = 1
77
DOMAIN_PROPOSAL = 2

eth/beacon/helpers.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
4141

4242

4343
if TYPE_CHECKING:
44+
from eth.beacon.enums.signature_domain import SignatureDomain # noqa: F401
4445
from eth.beacon.types.attestation_records import AttestationRecord # noqa: F401
4546
from eth.beacon.types.blocks import BaseBeaconBlock # noqa: F401
4647
from eth.beacon.types.states import BeaconState # noqa: F401
48+
from eth.beacon.types.fork_data import ForkData # noqa: F401
4749
from eth.beacon.types.validator_records import ValidatorRecord # noqa: F401
4850

4951

@@ -393,3 +395,27 @@ def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_
393395
# TODO: currently, we use 256-bit pubkey which is different form the spec
394396
pubkey.to_bytes(32, 'big')
395397
)
398+
399+
400+
def get_fork_version(fork_data: 'ForkData',
401+
slot: int) -> int:
402+
"""
403+
Return the current ``fork_version`` from the given ``fork_data`` and ``slot``.
404+
"""
405+
if slot < fork_data.fork_slot:
406+
return fork_data.pre_fork_version
407+
else:
408+
return fork_data.post_fork_version
409+
410+
411+
def get_domain(fork_data: 'ForkData',
412+
slot: int,
413+
domain_type: 'SignatureDomain') -> int:
414+
"""
415+
Return the domain number of the current fork and ``domain_type``.
416+
"""
417+
# 2 ** 32 = 4294967296
418+
return get_fork_version(
419+
fork_data,
420+
slot,
421+
) * 4294967296 + domain_type

tests/beacon/test_helpers.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ValidatorStatusCode,
1515
)
1616
from eth.beacon.types.blocks import BaseBeaconBlock
17+
from eth.beacon.types.fork_data import ForkData
1718
from eth.beacon.types.shard_committees import ShardCommittee
1819
from eth.beacon.types.states import BeaconState
1920
from eth.beacon.types.validator_records import ValidatorRecord
@@ -24,6 +25,8 @@
2425
get_beacon_proposer_index,
2526
get_block_hash,
2627
get_effective_balance,
28+
get_domain,
29+
get_fork_version,
2730
get_hashes_from_latest_block_hashes,
2831
get_new_shuffling,
2932
get_new_validator_registry_delta_chain_tip,
@@ -661,3 +664,68 @@ def test_get_new_validator_registry_delta_chain_tip(index,
661664
flag=flag,
662665
)
663666
assert result == expected
667+
668+
669+
@pytest.mark.parametrize(
670+
(
671+
'pre_fork_version,'
672+
'post_fork_version,'
673+
'fork_slot,'
674+
'current_slot,'
675+
'expected'
676+
),
677+
[
678+
(0, 0, 0, 0, 0),
679+
(0, 0, 0, 1, 0),
680+
(0, 0, 20, 10, 0),
681+
(0, 1, 20, 20, 1),
682+
(0, 1, 10, 20, 1),
683+
]
684+
)
685+
def test_get_fork_version(pre_fork_version,
686+
post_fork_version,
687+
fork_slot,
688+
current_slot,
689+
expected):
690+
fork_data = ForkData(
691+
pre_fork_version=pre_fork_version,
692+
post_fork_version=post_fork_version,
693+
fork_slot=fork_slot,
694+
)
695+
assert expected == get_fork_version(
696+
fork_data,
697+
current_slot,
698+
)
699+
700+
701+
@pytest.mark.parametrize(
702+
(
703+
'pre_fork_version,'
704+
'post_fork_version,'
705+
'fork_slot,'
706+
'current_slot,'
707+
'domain_type,'
708+
'expected'
709+
),
710+
[
711+
(1, 2, 20, 10, 10, 1 * 2 ** 32 + 10),
712+
(1, 2, 20, 20, 11, 2 * 2 ** 32 + 11),
713+
(1, 2, 10, 20, 12, 2 * 2 ** 32 + 12),
714+
]
715+
)
716+
def test_get_domain(pre_fork_version,
717+
post_fork_version,
718+
fork_slot,
719+
current_slot,
720+
domain_type,
721+
expected):
722+
fork_data = ForkData(
723+
pre_fork_version=pre_fork_version,
724+
post_fork_version=post_fork_version,
725+
fork_slot=fork_slot,
726+
)
727+
assert expected == get_domain(
728+
fork_data=fork_data,
729+
slot=current_slot,
730+
domain_type=domain_type,
731+
)

0 commit comments

Comments
 (0)