Skip to content

Commit 24b0749

Browse files
committed
Add get_effective_balance and get_new_validator_registry_delta_chain_tip
1 parent 700fe16 commit 24b0749

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

eth/beacon/helpers.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
get_bitfield_length,
2323
has_voted,
2424
)
25+
from eth.utils.blake import (
26+
blake,
27+
)
2528
from eth.utils.numeric import (
2629
clamp,
2730
)
@@ -412,3 +415,29 @@ def get_attestation_participants(state: 'BeaconState',
412415
for bitfield_index, validator_index in enumerate(shard_committee.committee):
413416
if has_voted(participation_bitfield, bitfield_index):
414417
yield validator_index
418+
419+
420+
#
421+
# Misc
422+
#
423+
def get_effective_balance(validator: 'ValidatorRecord', max_deposit: int) -> int:
424+
"""
425+
Return the effective balance (also known as "balance at stake") for the ``validator``.
426+
"""
427+
return min(validator.balance, max_deposit)
428+
429+
430+
def get_new_validator_registry_delta_chain_tip(current_validator_registry_delta_chain_tip: Hash32,
431+
index: int,
432+
pubkey: int,
433+
flag: int) -> Hash32:
434+
"""
435+
Compute the next hash in the validator registry delta hash chain.
436+
"""
437+
return blake(
438+
current_validator_registry_delta_chain_tip +
439+
flag.to_bytes(1, 'big') +
440+
index.to_bytes(3, 'big') +
441+
# TODO: currently, we use 256-bit pubkey which is different form the spec
442+
pubkey.to_bytes(32, 'big')
443+
)

tests/beacon/test_helpers.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import pytest
22

3+
from eth.constants import (
4+
ZERO_HASH32,
5+
)
6+
7+
38
from eth.beacon.enums.validator_status_codes import (
49
ValidatorStatusCode,
510
)
@@ -13,13 +18,16 @@
1318
get_attestation_participants,
1419
get_beacon_proposer_index,
1520
get_block_hash,
21+
get_effective_balance,
1622
get_hashes_from_latest_block_hashes,
1723
get_hashes_to_sign,
1824
get_new_shuffling,
25+
get_new_validator_registry_delta_chain_tip,
1926
_get_shard_committees_at_slot,
2027
get_block_committees_info,
2128
)
2229

30+
2331
from tests.beacon.helpers import (
2432
get_pseudo_chain,
2533
)
@@ -256,7 +264,7 @@ def test_get_hashes_to_sign(sample_block, epoch_length):
256264
),
257265
],
258266
)
259-
def test_get_shard_committee_for_slot(
267+
def test_get_shard_committees_at_slot(
260268
num_validators,
261269
cycle_length,
262270
latest_state_recalculation_slot,
@@ -606,3 +614,59 @@ def mock_get_shard_committees_at_slot(state,
606614
)
607615

608616
assert result == expected
617+
618+
619+
@pytest.mark.parametrize(
620+
(
621+
'balance,'
622+
'max_deposit,'
623+
'expected'
624+
),
625+
[
626+
(
627+
1,
628+
32,
629+
1,
630+
),
631+
(
632+
33,
633+
32,
634+
32,
635+
)
636+
]
637+
)
638+
def test_get_effective_balance(balance, max_deposit, expected, sample_validator_record_params):
639+
validator = ValidatorRecord(**sample_validator_record_params).copy(
640+
balance=balance,
641+
)
642+
result = get_effective_balance(validator, max_deposit)
643+
assert result == expected
644+
645+
646+
@pytest.mark.parametrize(
647+
(
648+
'index,'
649+
'pubkey,'
650+
'flag,'
651+
'expected'
652+
),
653+
[
654+
(
655+
1,
656+
2 * 256 - 1,
657+
1,
658+
b')\x8a4^\xc5\xb4\x06\r\xf3\x0cX\xb8\xdd\x05\x94\xcfY+qF\xbe\xf1\x04\xe3\xe8\xbd\xe5\xef\xfaGY\t' # noqa: E501
659+
),
660+
]
661+
)
662+
def test_get_new_validator_registry_delta_chain_tip(index,
663+
pubkey,
664+
flag,
665+
expected):
666+
result = get_new_validator_registry_delta_chain_tip(
667+
current_validator_registry_delta_chain_tip=ZERO_HASH32,
668+
index=index,
669+
pubkey=pubkey,
670+
flag=flag,
671+
)
672+
assert result == expected

0 commit comments

Comments
 (0)