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

Bump SSZ #499

Merged
merged 19 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions eth2/beacon/chains/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def import_block(
"Attempt to import block #{}. Cannot import block {} before importing "
"its parent block at {}".format(
block.slot,
block.signed_root,
block.signing_root,
block.previous_block_root,
)
)
Expand All @@ -395,7 +395,7 @@ def import_block(
self.logger.debug(
'IMPORTED_BLOCK: slot %s | signed root %s',
imported_block.slot,
encode_hex(imported_block.signed_root),
encode_hex(imported_block.signing_root),
)

return imported_block, new_canonical_blocks, old_canonical_blocks
49 changes: 25 additions & 24 deletions eth2/beacon/db/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def persist_block_chain(
# Beacon State
#
@abstractmethod
def get_state_by_root(self, state_root: Hash32) -> BeaconState:
def get_state_by_root(self, state_root: Hash32, state_class: Type[BeaconState]) -> BeaconState:
pass

@abstractmethod
Expand Down Expand Up @@ -209,7 +209,7 @@ def _get_canonical_block_root(db: BaseDB, slot: int) -> Hash32:
"No canonical block for block slot #{0}".format(slot)
)
else:
return ssz.decode(encoded_key, sedes=ssz.sedes.bytes_sedes)
return ssz.decode(encoded_key, sedes=ssz.sedes.byte_list)

def get_canonical_block_by_slot(self,
slot: int,
Expand Down Expand Up @@ -375,7 +375,7 @@ def _set_block_scores_to_db(
score = block.slot

db.set(
SchemaV1.make_block_root_to_score_lookup_key(block.signed_root),
SchemaV1.make_block_root_to_score_lookup_key(block.signing_root),
ssz.encode(score, sedes=ssz.sedes.uint64),
)
return score
Expand All @@ -395,7 +395,7 @@ def _persist_block_chain(
return tuple(), tuple()

try:
previous_canonical_head = cls._get_canonical_head(db, block_class).signed_root
previous_canonical_head = cls._get_canonical_head(db, block_class).signing_root
head_score = cls._get_score(db, previous_canonical_head)
except CanonicalHeadNotFound:
no_canonical_head = True
Expand All @@ -406,7 +406,7 @@ def _persist_block_chain(
if not is_genesis and not cls._block_exists(db, first_block.previous_block_root):
raise ParentNotFound(
"Cannot persist block ({}) with unknown parent ({})".format(
encode_hex(first_block.signed_root),
encode_hex(first_block.signing_root),
encode_hex(first_block.previous_block_root),
)
)
Expand All @@ -416,14 +416,14 @@ def _persist_block_chain(
# TODO: this should probably be done as part of the fork choice rule processing
db.set(
SchemaV1.make_finalized_head_root_lookup_key(),
first_block.signed_root,
first_block.signing_root,
)
else:
score = first_block.slot

curr_block_head = first_block
db.set(
curr_block_head.signed_root,
curr_block_head.signing_root,
ssz.encode(curr_block_head),
)
cls._add_block_root_to_slot_lookup(db, curr_block_head)
Expand All @@ -432,28 +432,28 @@ def _persist_block_chain(
orig_blocks_seq = concat([(first_block,), blocks_iterator])

for parent, child in sliding_window(2, orig_blocks_seq):
if parent.signed_root != child.previous_block_root:
if parent.signing_root != child.previous_block_root:
raise ValidationError(
"Non-contiguous chain. Expected {} to have {} as parent but was {}".format(
encode_hex(child.signed_root),
encode_hex(parent.signed_root),
encode_hex(child.signing_root),
encode_hex(parent.signing_root),
encode_hex(child.previous_block_root),
)
)

curr_block_head = child
db.set(
curr_block_head.signed_root,
curr_block_head.signing_root,
ssz.encode(curr_block_head),
)
cls._add_block_root_to_slot_lookup(db, curr_block_head)
score = cls._set_block_scores_to_db(db, curr_block_head)

if no_canonical_head:
return cls._set_as_canonical_chain_head(db, curr_block_head.signed_root, block_class)
return cls._set_as_canonical_chain_head(db, curr_block_head.signing_root, block_class)

if score > head_score:
return cls._set_as_canonical_chain_head(db, curr_block_head.signed_root, block_class)
return cls._set_as_canonical_chain_head(db, curr_block_head.signing_root, block_class)
else:
return tuple(), tuple()

Expand Down Expand Up @@ -494,7 +494,7 @@ def _set_as_canonical_chain_head(
for block in new_canonical_blocks:
cls._add_block_slot_to_root_lookup(db, block)

db.set(SchemaV1.make_canonical_head_root_lookup_key(), block.signed_root)
db.set(SchemaV1.make_canonical_head_root_lookup_key(), block.signing_root)

return new_canonical_blocks, tuple(old_canonical_blocks)

Expand Down Expand Up @@ -523,7 +523,7 @@ def _find_new_ancestors(
# This just means the block is not on the canonical chain.
pass
else:
if orig.signed_root == block.signed_root:
if orig.signing_root == block.signing_root:
# Found the common ancestor, stop.
break

Expand All @@ -546,7 +546,7 @@ def _add_block_slot_to_root_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
)
db.set(
block_slot_to_root_key,
ssz.encode(block.signed_root, sedes=ssz.sedes.bytes_sedes),
ssz.encode(block.signing_root, sedes=ssz.sedes.byte_list),
)

@staticmethod
Expand All @@ -556,7 +556,7 @@ def _add_block_root_to_slot_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
block root.
"""
block_root_to_slot_key = SchemaV1.make_block_root_to_slot_lookup_key(
block.signed_root
block.signing_root
)
db.set(
block_root_to_slot_key,
Expand All @@ -566,11 +566,13 @@ def _add_block_root_to_slot_lookup(db: BaseDB, block: BaseBeaconBlock) -> None:
#
# Beacon State API
#
def get_state_by_root(self, state_root: Hash32) -> BeaconState:
return self._get_state_by_root(self.db, state_root)
def get_state_by_root(self, state_root: Hash32, state_class: Type[BeaconState]) -> BeaconState:
return self._get_state_by_root(self.db, state_root, state_class)

@staticmethod
def _get_state_by_root(db: BaseDB, state_root: Hash32) -> BeaconState:
def _get_state_by_root(db: BaseDB,
state_root: Hash32,
state_class: Type[BeaconState]) -> BeaconState:
"""
Return the requested beacon state as specified by state hash.

Expand All @@ -581,7 +583,7 @@ def _get_state_by_root(db: BaseDB, state_root: Hash32) -> BeaconState:
state_ssz = db[state_root]
except KeyError:
raise StateRootNotFound(f"No state with root {encode_hex(state_root)} found")
return _decode_state(state_ssz)
return _decode_state(state_ssz, state_class)

def persist_state(self,
state: BeaconState) -> None:
Expand Down Expand Up @@ -625,6 +627,5 @@ def _decode_block(block_ssz: bytes, sedes: Type[BaseBeaconBlock]) -> BaseBeaconB


@functools.lru_cache(128)
def _decode_state(state_ssz: bytes) -> BeaconState:
# TODO: forkable BeaconState fields?
return ssz.decode(state_ssz, sedes=BeaconState)
def _decode_state(state_ssz: bytes, state_class: Type[BeaconState]) -> BeaconState:
return ssz.decode(state_ssz, sedes=state_class)
4 changes: 2 additions & 2 deletions eth2/beacon/deposit_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def process_deposit(state: BeaconState,
# Verify the proof of possession
proof_is_valid = bls.verify(
pubkey=pubkey,
message_hash=deposit_input.signed_root,
signature=deposit_input.proof_of_possession,
message_hash=deposit_input.signing_root,
signature=deposit_input.signature,
domain=get_domain(
state.fork,
state.current_epoch(slots_per_epoch),
Expand Down
2 changes: 1 addition & 1 deletion eth2/beacon/epoch_processing_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import (
from typing import ( # noqa: F401
Dict,
Iterable,
Sequence,
Expand Down
2 changes: 1 addition & 1 deletion eth2/beacon/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def get_temporary_block_header(block: BeaconBlock) -> BeaconBlockHeader:
slot=block.slot,
previous_block_root=block.previous_block_root,
state_root=ZERO_HASH32,
block_body_root=block.body.hash_tree_root,
block_body_root=block.body.root,
signature=EMPTY_SIGNATURE,
)

Expand Down
13 changes: 4 additions & 9 deletions eth2/beacon/on_genesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from eth.constants import (
ZERO_HASH32,
)
import ssz

from eth2.beacon.deposit_helpers import (
process_deposit,
Expand All @@ -29,7 +30,6 @@
from eth2.beacon.types.eth1_data import Eth1Data
from eth2.beacon.types.forks import Fork
from eth2.beacon.types.states import BeaconState
from eth2.beacon._utils.hash import hash_eth2
from eth2.beacon.typing import (
Epoch,
Gwei,
Expand Down Expand Up @@ -151,18 +151,13 @@ def get_genesis_beacon_state(*,
activation_exit_delay=activation_exit_delay,
)

# TODO: chanege to hash_tree_root
active_validator_indices = get_active_validator_indices(
state.validator_registry,
genesis_epoch,
)
genesis_active_index_root = hash_eth2(
b''.join(
[
index.to_bytes(32, 'little')
for index in active_validator_indices
]
)
genesis_active_index_root = ssz.hash_tree_root(
active_validator_indices,
ssz.sedes.List(ssz.uint64),
)
latest_active_index_roots = (genesis_active_index_root,) * latest_active_index_roots_length
state = state.copy(
Expand Down
5 changes: 4 additions & 1 deletion eth2/beacon/state_machines/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ def __init__(self,
@property
def state(self) -> BeaconState:
if self._state is None:
self._state = self.chaindb.get_state_by_root(self.block.state_root)
self._state = self.chaindb.get_state_by_root(
self.block.state_root,
self.get_state_class()
)
return self._state

@classmethod
Expand Down
14 changes: 6 additions & 8 deletions eth2/beacon/state_machines/forks/serenity/block_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def validate_block_slot(state: BeaconState,

def validate_block_previous_root(state: BeaconState,
block: BaseBeaconBlock) -> None:
expected_root = state.latest_block_header.signed_root
expected_root = state.latest_block_header.signing_root
previous_root = block.previous_block_root
if previous_root != expected_root:
raise ValidationError(
Expand All @@ -103,9 +103,7 @@ def validate_proposer_signature(state: BeaconState,
block: BaseBeaconBlock,
beacon_chain_shard_number: Shard,
committee_config: CommitteeConfig) -> None:

# TODO: Replace this with real signed_root
message_hash = block.signed_root
message_hash = block.signing_root

# Get the public key of proposer
beacon_proposer_index = get_beacon_proposer_index(
Expand Down Expand Up @@ -200,7 +198,7 @@ def validate_block_header_signature(header: BeaconBlockHeader,
slots_per_epoch: int) -> None:
header_signature_is_valid = bls.verify(
pubkey=pubkey,
message_hash=header.signed_root, # TODO: use signed_root
message_hash=header.signing_root,
signature=header.signature,
domain=get_domain(
fork,
Expand All @@ -211,7 +209,7 @@ def validate_block_header_signature(header: BeaconBlockHeader,
if not header_signature_is_valid:
raise ValidationError(
"Header signature is invalid: "
f"proposer pubkey: {pubkey}, message_hash: {header.signed_root}, "
f"proposer pubkey: {pubkey}, message_hash: {header.signing_root}, "
f"signature: {header.signature}"
)

Expand Down Expand Up @@ -766,14 +764,14 @@ def validate_voluntary_exit_signature(state: BeaconState,
domain = get_domain(state.fork, voluntary_exit.epoch, SignatureDomain.DOMAIN_VOLUNTARY_EXIT)
is_valid_signature = bls.verify(
pubkey=validator.pubkey,
message_hash=voluntary_exit.signed_root,
message_hash=voluntary_exit.signing_root,
signature=voluntary_exit.signature,
domain=domain,
)

if not is_valid_signature:
raise ValidationError(
f"Invalid VoluntaryExit signature, validator_index={voluntary_exit.validator_index}, "
f"pubkey={validator.pubkey}, message_hash={voluntary_exit.signed_root},"
f"pubkey={validator.pubkey}, message_hash={voluntary_exit.signing_root},"
f"signature={voluntary_exit.signature}, domain={domain}"
)
18 changes: 5 additions & 13 deletions eth2/beacon/state_machines/forks/serenity/epoch_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
curry,
first,
)
import ssz

from eth2.beacon import helpers
from eth2._utils.numeric import (
Expand Down Expand Up @@ -59,9 +60,6 @@
exit_validator,
prepare_validator_for_withdrawal,
)
from eth2.beacon._utils.hash import (
hash_eth2,
)
from eth2.beacon.datastructures.inclusion_info import InclusionInfo
from eth2.beacon.types.attestations import Attestation
from eth2.beacon.types.pending_attestation_records import PendingAttestationRecord
Expand Down Expand Up @@ -1041,18 +1039,13 @@ def _update_latest_active_index_roots(state: BeaconState,
"""
next_epoch = state.next_epoch(committee_config.SLOTS_PER_EPOCH)

# TODO: chanege to hash_tree_root
active_validator_indices = get_active_validator_indices(
state.validator_registry,
Epoch(next_epoch + committee_config.ACTIVATION_EXIT_DELAY),
)
index_root = hash_eth2(
b''.join(
[
index.to_bytes(32, 'little')
for index in active_validator_indices
]
)
index_root = ssz.hash_tree_root(
active_validator_indices,
ssz.sedes.List(ssz.uint64),
)

latest_active_index_roots = update_tuple_item(
Expand Down Expand Up @@ -1191,9 +1184,8 @@ def _update_historical_roots(state: BeaconState,
historical_batch = HistoricalBatch(
block_roots=state.latest_block_roots,
state_roots=state.latest_state_roots,
slots_per_historical_root=config.SLOTS_PER_HISTORICAL_ROOT,
)
updated_historical_roots += (historical_batch.hash_tree_root,)
updated_historical_roots += (historical_batch.root,)

return state.copy(
historical_roots=updated_historical_roots
Expand Down
3 changes: 1 addition & 2 deletions eth2/beacon/state_machines/forks/serenity/slot_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ def process_cache_state(state: BeaconState, config: Eth2Config) -> BeaconState:
state.latest_block_roots,
state.slot,
slots_per_historical_root,
# TODO make `signed_root`
state.latest_block_header.signed_root,
state.latest_block_header.signing_root,
)

state = state.copy(
Expand Down
3 changes: 1 addition & 2 deletions eth2/beacon/tools/builder/proposer.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ def create_block_on_state(
state, block = state_machine.import_block(block, check_proposer_signature=False)

# Sign
# TODO make sure we use the correct signed_root
signature = sign_transaction(
message_hash=block.signed_root,
message_hash=block.signing_root,
privkey=privkey,
fork=state.fork,
slot=slot,
Expand Down
Loading