Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
Misc sync: state.deposit_index BLS-based RANDAO (#286)
Browse files Browse the repository at this point in the history
* Rename

1. `get_initial_beacon_state` -> `get_genesis_beacon_state`
2. `initial_validator_deposits` -> `genesis_validator_deposits`

* Add BeaconState.deposit_index (ethereum/consensus-specs#594)

* Use BLS-based RANDAO types and modify `process_deposit`

* Remove outdated per-slot `proposer.randao_layers` and `state.latest_randao_mixes` update

* Update eth2/beacon/types/blocks.py

Co-Authored-By: hwwhww <hwwang156@gmail.com>
  • Loading branch information
hwwhww committed Feb 18, 2019
1 parent b6667b6 commit caf9f38
Show file tree
Hide file tree
Showing 16 changed files with 35 additions and 126 deletions.
5 changes: 0 additions & 5 deletions eth2/beacon/deposit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ def validate_proof_of_possession(state: BeaconState,
pubkey: BLSPubkey,
proof_of_possession: BLSSignature,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
epoch_length: int) -> None:
deposit_input = DepositInput(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
proof_of_possession=EMPTY_SIGNATURE,
)

Expand Down Expand Up @@ -76,7 +74,6 @@ def process_deposit(*,
amount: Gwei,
proof_of_possession: BLSSignature,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
epoch_length: int) -> BeaconState:
"""
Process a deposit from Ethereum 1.0.
Expand All @@ -86,7 +83,6 @@ def process_deposit(*,
pubkey=pubkey,
proof_of_possession=proof_of_possession,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
epoch_length=epoch_length,
)

Expand All @@ -95,7 +91,6 @@ def process_deposit(*,
validator = ValidatorRecord.create_pending_validator(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
)

# Note: In phase 2 registry indices that has been withdrawn for a long time
Expand Down
10 changes: 5 additions & 5 deletions eth2/beacon/on_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def get_genesis_block(startup_state_root: Hash32,
slot=genesis_slot,
parent_root=ZERO_HASH32,
state_root=startup_state_root,
randao_reveal=ZERO_HASH32,
randao_reveal=EMPTY_SIGNATURE,
eth1_data=Eth1Data.create_empty_data(),
signature=EMPTY_SIGNATURE,
body=BeaconBlockBody.create_empty_body(),
)


def get_initial_beacon_state(*,
initial_validator_deposits: Sequence[Deposit],
def get_genesis_beacon_state(*,
genesis_validator_deposits: Sequence[Deposit],
genesis_time: Timestamp,
latest_eth1_data: Eth1Data,
genesis_slot: SlotNumber,
Expand Down Expand Up @@ -119,17 +119,17 @@ def get_initial_beacon_state(*,
# Ethereum 1.0 chain data
latest_eth1_data=latest_eth1_data,
eth1_data_votes=(),
deposit_index=len(genesis_validator_deposits),
)

# Process initial deposits
for deposit in initial_validator_deposits:
for deposit in genesis_validator_deposits:
state = process_deposit(
state=state,
pubkey=deposit.deposit_data.deposit_input.pubkey,
amount=deposit.deposit_data.amount,
proof_of_possession=deposit.deposit_data.deposit_input.proof_of_possession,
withdrawal_credentials=deposit.deposit_data.deposit_input.withdrawal_credentials,
randao_commitment=deposit.deposit_data.deposit_input.randao_commitment,
epoch_length=epoch_length,
)

Expand Down
19 changes: 2 additions & 17 deletions eth2/beacon/state_machines/forks/serenity/state_transitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
)

from eth2._utils.merkle import get_merkle_root
from eth2.beacon.committee_helpers import (
get_beacon_proposer_index,
)
from eth2.beacon.configs import (
BeaconConfig,
CommitteeConfig,
Expand Down Expand Up @@ -57,29 +54,17 @@ def per_slot_transition(self,
slot=state.slot + 1
)

# Update proposer.randao_layers
updated_validator_registry = list(state.validator_registry)
beacon_proposer_index = get_beacon_proposer_index(
state,
state.slot,
CommitteeConfig(self.config),
)
old_validator_record = updated_validator_registry[beacon_proposer_index]
updated_validator_record = old_validator_record.copy(
randao_layers=old_validator_record.randao_layers + 1,
)
updated_validator_registry[beacon_proposer_index] = updated_validator_record

# Update state.latest_block_roots
updated_latest_block_roots = list(state.latest_block_roots)
previous_block_root_index = (state.slot - 1) % LATEST_BLOCK_ROOTS_LENGTH
updated_latest_block_roots[previous_block_root_index] = previous_block_root

# Update state.batched_block_roots
updated_batched_block_roots = state.batched_block_roots
if state.slot % LATEST_BLOCK_ROOTS_LENGTH == 0:
updated_batched_block_roots += (get_merkle_root(updated_latest_block_roots),)

state = state.copy(
validator_registry=tuple(updated_validator_registry),
latest_block_roots=tuple(updated_latest_block_roots),
batched_block_roots=updated_batched_block_roots,
)
Expand Down
17 changes: 7 additions & 10 deletions eth2/beacon/tools/builder/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from eth2.beacon.configs import BeaconConfig
from eth2.beacon.on_startup import (
get_genesis_block,
get_initial_beacon_state,
get_genesis_beacon_state,
)
from eth2.beacon.types.blocks import (
BaseBeaconBlock,
Expand All @@ -29,22 +29,21 @@
)


def create_mock_initial_validator_deposits(
def create_mock_genesis_validator_deposits(
num_validators: int,
config: BeaconConfig,
pubkeys: Sequence[BLSPubkey],
keymap: Dict[BLSPubkey, int]) -> Tuple[Deposit, ...]:
# Mock data
withdrawal_credentials = b'\x22' * 32
randao_commitment = b'\x33' * 32
deposit_timestamp = 0
fork = Fork(
previous_version=config.GENESIS_FORK_VERSION,
current_version=config.GENESIS_FORK_VERSION,
epoch=config.GENESIS_EPOCH,
)

initial_validator_deposits = tuple(
genesis_validator_deposits = tuple(
Deposit(
branch=(
b'\x11' * 32
Expand All @@ -55,12 +54,10 @@ def create_mock_initial_validator_deposits(
deposit_input=DepositInput(
pubkey=pubkeys[i],
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
proof_of_possession=sign_proof_of_possession(
deposit_input=DepositInput(
pubkey=pubkeys[i],
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
),
privkey=keymap[pubkeys[i]],
fork=fork,
Expand All @@ -75,7 +72,7 @@ def create_mock_initial_validator_deposits(
for i in range(num_validators)
)

return initial_validator_deposits
return genesis_validator_deposits


def create_mock_genesis(
Expand All @@ -90,14 +87,14 @@ def create_mock_genesis(

pubkeys = list(keymap)[:num_validators]

initial_validator_deposits = create_mock_initial_validator_deposits(
genesis_validator_deposits = create_mock_genesis_validator_deposits(
num_validators=num_validators,
config=config,
pubkeys=pubkeys,
keymap=keymap,
)
state = get_initial_beacon_state(
initial_validator_deposits=initial_validator_deposits,
state = get_genesis_beacon_state(
genesis_validator_deposits=genesis_validator_deposits,
genesis_time=genesis_time,
latest_eth1_data=latest_eth1_data,
genesis_slot=config.GENESIS_SLOT,
Expand Down
9 changes: 3 additions & 6 deletions eth2/beacon/types/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
from eth._utils.datatypes import (
Configurable,
)
from eth.constants import (
ZERO_HASH32,
)

from eth2.beacon._utils.hash import hash_eth2
from eth2.beacon.constants import EMPTY_SIGNATURE
Expand Down Expand Up @@ -116,7 +113,7 @@ class BaseBeaconBlock(rlp.Serializable, Configurable, ABC):
('slot', uint64),
('parent_root', hash32),
('state_root', hash32),
('randao_reveal', hash32),
('randao_reveal', binary), # TODO: should be bytes96 once we transition to SSZ
('eth1_data', Eth1Data),
('signature', binary),

Expand All @@ -130,7 +127,7 @@ def __init__(self,
slot: SlotNumber,
parent_root: Hash32,
state_root: Hash32,
randao_reveal: Hash32,
randao_reveal: BLSSignature,
eth1_data: Eth1Data,
body: BeaconBlockBody,
signature: BLSSignature=EMPTY_SIGNATURE) -> None:
Expand Down Expand Up @@ -227,7 +224,7 @@ def from_parent(cls,
slot=slot,
parent_root=parent_block.root,
state_root=parent_block.state_root,
randao_reveal=ZERO_HASH32,
randao_reveal=EMPTY_SIGNATURE,
eth1_data=parent_block.eth1_data,
signature=EMPTY_SIGNATURE,
body=cls.block_body_class.create_empty_body(),
Expand Down
4 changes: 0 additions & 4 deletions eth2/beacon/types/deposit_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,17 @@ class DepositInput(rlp.Serializable):
('pubkey', binary),
# Withdrawal credentials
('withdrawal_credentials', hash32),
# Initial RANDAO commitment
('randao_commitment', hash32),
# BLS proof of possession (a BLS signature)
('proof_of_possession', binary),
]

def __init__(self,
pubkey: BLSPubkey,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
proof_of_possession: BLSSignature=EMPTY_SIGNATURE) -> None:
super().__init__(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
proof_of_possession=proof_of_possession,
)

Expand Down
6 changes: 5 additions & 1 deletion eth2/beacon/types/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class BeaconState(rlp.Serializable):
# Ethereum 1.0 chain
('latest_eth1_data', Eth1Data),
('eth1_data_votes', CountableList(Eth1DataVote)),
('deposit_index', uint64),
]

def __init__(
Expand Down Expand Up @@ -122,7 +123,8 @@ def __init__(
latest_attestations: Sequence[PendingAttestationRecord],
# Ethereum 1.0 chain
latest_eth1_data: Eth1Data,
eth1_data_votes: Sequence[Eth1DataVote]) -> None:
eth1_data_votes: Sequence[Eth1DataVote],
deposit_index: int) -> None:
if len(validator_registry) != len(validator_balances):
raise ValueError(
"The length of validator_registry and validator_balances should be the same."
Expand Down Expand Up @@ -159,6 +161,7 @@ def __init__(
# Ethereum 1.0 chain
latest_eth1_data=latest_eth1_data,
eth1_data_votes=eth1_data_votes,
deposit_index=deposit_index,
)

def __repr__(self) -> str:
Expand Down Expand Up @@ -244,6 +247,7 @@ def create_filled_state(cls,
# Ethereum 1.0 chain data
latest_eth1_data=Eth1Data.create_empty_data(),
eth1_data_votes=(),
deposit_index=len(activated_genesis_validators),
)

def update_validator_registry(self,
Expand Down
13 changes: 1 addition & 12 deletions eth2/beacon/types/validator_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class ValidatorRecord(rlp.Serializable):
('pubkey', binary),
# Withdrawal credentials
('withdrawal_credentials', hash32),
# RANDAO commitment
('randao_commitment', hash32),
# Slot the proposer has skipped (ie. layers of RANDAO expected)
('randao_layers', uint64),
# Epoch when validator activated
('activation_epoch', uint64),
# Epoch when validator exited
Expand All @@ -47,8 +43,6 @@ class ValidatorRecord(rlp.Serializable):
def __init__(self,
pubkey: BLSPubkey,
withdrawal_credentials: Hash32,
randao_commitment: Hash32,
randao_layers: int,
activation_epoch: EpochNumber,
exit_epoch: EpochNumber,
withdrawal_epoch: EpochNumber,
Expand All @@ -57,8 +51,6 @@ def __init__(self,
super().__init__(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
randao_layers=randao_layers,
activation_epoch=activation_epoch,
exit_epoch=exit_epoch,
withdrawal_epoch=withdrawal_epoch,
Expand All @@ -75,16 +67,13 @@ def is_active(self, epoch: EpochNumber) -> bool:
@classmethod
def create_pending_validator(cls,
pubkey: BLSPubkey,
withdrawal_credentials: Hash32,
randao_commitment: Hash32) -> 'ValidatorRecord':
withdrawal_credentials: Hash32) -> 'ValidatorRecord':
"""
Return a new pending ``ValidatorRecord`` with the given fields.
"""
return cls(
pubkey=pubkey,
withdrawal_credentials=withdrawal_credentials,
randao_commitment=randao_commitment,
randao_layers=0,
activation_epoch=FAR_FUTURE_EPOCH,
exit_epoch=FAR_FUTURE_EPOCH,
withdrawal_epoch=FAR_FUTURE_EPOCH,
Expand Down
2 changes: 1 addition & 1 deletion tests/core/p2p-proto/bcc/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_test_block(parent=None, **kwargs):
"slot": 0,
"parent_root": ZERO_HASH32,
"state_root": ZERO_HASH32, # note: not the actual genesis state root
"randao_reveal": ZERO_HASH32,
"randao_reveal": EMPTY_SIGNATURE,
"eth1_data": Eth1Data.create_empty_data(),
"signature": EMPTY_SIGNATURE,
"body": BeaconBlockBody.create_empty_body()
Expand Down
4 changes: 2 additions & 2 deletions tests/core/p2p-proto/bcc/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_send_single_block(request, event_loop):
slot=1,
parent_root=ZERO_HASH32,
state_root=ZERO_HASH32,
randao_reveal=ZERO_HASH32,
randao_reveal=EMPTY_SIGNATURE,
eth1_data=Eth1Data.create_empty_data(),
signature=EMPTY_SIGNATURE,
body=BeaconBlockBody.create_empty_body(),
Expand All @@ -95,7 +95,7 @@ async def test_send_multiple_blocks(request, event_loop):
slot=slot,
parent_root=ZERO_HASH32,
state_root=ZERO_HASH32,
randao_reveal=ZERO_HASH32,
randao_reveal=EMPTY_SIGNATURE,
eth1_data=Eth1Data.create_empty_data(),
signature=EMPTY_SIGNATURE,
body=BeaconBlockBody.create_empty_body(),
Expand Down
6 changes: 2 additions & 4 deletions tests/eth2/beacon/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def sample_beacon_block_params(sample_beacon_block_body_params,
'slot': 10,
'parent_root': ZERO_HASH32,
'state_root': b'\x55' * 32,
'randao_reveal': b'\x55' * 32,
'randao_reveal': EMPTY_SIGNATURE,
'eth1_data': Eth1Data(**sample_eth1_data_params),
'signature': EMPTY_SIGNATURE,
'body': BeaconBlockBody(**sample_beacon_block_body_params)
Expand Down Expand Up @@ -169,6 +169,7 @@ def sample_beacon_state_params(sample_fork_params, sample_eth1_data_params):
'batched_block_roots': (),
'latest_eth1_data': Eth1Data(**sample_eth1_data_params),
'eth1_data_votes': (),
'deposit_index': 0,
}


Expand Down Expand Up @@ -201,7 +202,6 @@ def sample_deposit_input_params():
return {
'pubkey': 123,
'withdrawal_credentials': b'\11' * 32,
'randao_commitment': b'\11' * 32,
'proof_of_possession': (0, 0),
}

Expand Down Expand Up @@ -294,8 +294,6 @@ def sample_validator_record_params():
return {
'pubkey': 123,
'withdrawal_credentials': b'\x01' * 32,
'randao_commitment': b'\x01' * 32,
'randao_layers': 1,
'activation_epoch': FAR_FUTURE_EPOCH,
'exit_epoch': FAR_FUTURE_EPOCH,
'withdrawal_epoch': FAR_FUTURE_EPOCH,
Expand Down
Loading

0 comments on commit caf9f38

Please sign in to comment.