Skip to content

Commit dae7910

Browse files
authored
Merge pull request #1591 from hwwhww/get_fork
Add `get_fork_version` and `get_domain` helpers
2 parents 751c856 + 8bfef33 commit dae7910

File tree

9 files changed

+123
-39
lines changed

9 files changed

+123
-39
lines changed

eth/beacon/enums.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from enum import IntEnum
2+
3+
4+
class ValidatorStatusCode(IntEnum):
5+
PENDING_ACTIVATION = 0
6+
ACTIVE = 1
7+
ACTIVE_PENDING_EXIT = 2
8+
EXITED_WITHOUT_PENALTY = 3
9+
EXITED_WITH_PENALTY = 4
10+
11+
12+
class ValidatorRegistryDeltaFlag(IntEnum):
13+
ACTIVATION = 0
14+
EXIT = 1
15+
16+
17+
class SignatureDomain(IntEnum):
18+
DOMAIN_DEPOSIT = 0
19+
DOMAIN_ATTESTATION = 1
20+
DOMAIN_PROPOSAL = 2
21+
DOMAIN_EXIT = 3

eth/beacon/enums/signature_domain.py

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

eth/beacon/enums/special_record_types.py

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

eth/beacon/enums/validator_registry_delta_flags.py

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

eth/beacon/enums/validator_status_codes.py

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

eth/beacon/helpers.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929

3030
from eth.beacon.block_committees_info import BlockCommitteesInfo
31-
from eth.beacon.enums.validator_status_codes import (
31+
from eth.beacon.enums import (
3232
ValidatorStatusCode,
3333
)
3434
from eth.beacon.types.shard_committees import (
@@ -41,9 +41,11 @@
4141

4242

4343
if TYPE_CHECKING:
44+
from eth.beacon.enums 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

@@ -154,7 +156,7 @@ def get_active_validator_indices(validators: Sequence['ValidatorRecord']) -> Tup
154156
"""
155157
return tuple(
156158
i for i, v in enumerate(validators)
157-
if v.status in [ValidatorStatusCode.ACTIVE, ValidatorStatusCode.PENDING_EXIT]
159+
if v.status in [ValidatorStatusCode.ACTIVE, ValidatorStatusCode.ACTIVE_PENDING_EXIT]
158160
)
159161

160162

@@ -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/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import eth.utils.bls as bls
1010
from eth.utils.blake import blake
1111

12-
from eth.beacon.enums.validator_status_codes import (
12+
from eth.beacon.enums import (
1313
ValidatorStatusCode,
1414
)
1515
from eth.beacon.state_machines.forks.serenity.configs import SERENITY_CONFIG

tests/beacon/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from eth.beacon.enums.validator_status_codes import (
1+
from eth.beacon.enums import (
22
ValidatorStatusCode,
33
)
44
from eth.beacon.types.validator_records import (

tests/beacon/test_helpers.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
)
1111

1212

13-
from eth.beacon.enums.validator_status_codes import (
13+
from eth.beacon.enums import (
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,
@@ -500,14 +503,14 @@ def test_get_active_validator_indices(sample_validator_record_params):
500503
active_validator_indices = get_active_validator_indices(validators)
501504
assert len(active_validator_indices) == 3
502505

503-
# Make one validator becomes PENDING_EXIT.
506+
# Make one validator becomes ACTIVE_PENDING_EXIT.
504507
validators[0] = validators[0].copy(
505-
status=ValidatorStatusCode.PENDING_EXIT,
508+
status=ValidatorStatusCode.ACTIVE_PENDING_EXIT,
506509
)
507510
active_validator_indices = get_active_validator_indices(validators)
508511
assert len(active_validator_indices) == 3
509512

510-
# Make one validator becomes PENDING_EXIT.
513+
# Make one validator becomes EXITED_WITHOUT_PENALTY.
511514
validators[0] = validators[0].copy(
512515
status=ValidatorStatusCode.EXITED_WITHOUT_PENALTY,
513516
)
@@ -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, 1, 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)