mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-01-12 11:44:41 +00:00
Cleanups
This commit is contained in:
parent
e0d814df32
commit
d73aa31ef6
@ -11,13 +11,12 @@
|
|||||||
- [Constants](#constants)
|
- [Constants](#constants)
|
||||||
- [Time parameters](#time-parameters)
|
- [Time parameters](#time-parameters)
|
||||||
- [Fork choice](#fork-choice)
|
- [Fork choice](#fork-choice)
|
||||||
- [Containers](#containers)
|
- [Helpers](#helpers)
|
||||||
- [`Target`](#target)
|
- [`Target`](#target)
|
||||||
- [`Store`](#store)
|
- [`Store`](#store)
|
||||||
- [Helpers](#helpers)
|
|
||||||
- [`get_genesis_store`](#get_genesis_store)
|
- [`get_genesis_store`](#get_genesis_store)
|
||||||
- [`get_ancestor`](#get_ancestor)
|
- [`get_ancestor`](#get_ancestor)
|
||||||
- [`get_attesting_balance_from_store`](#get_attesting_balance_from_store)
|
- [`get_latest_attesting_balance`](#get_latest_attesting_balance)
|
||||||
- [`get_head`](#get_head)
|
- [`get_head`](#get_head)
|
||||||
- [Handlers](#handlers)
|
- [Handlers](#handlers)
|
||||||
- [`on_tick`](#on_tick)
|
- [`on_tick`](#on_tick)
|
||||||
@ -51,9 +50,9 @@ The head block root associated with a `store` is defined as `get_head(store)`. A
|
|||||||
1) **Leap seconds**: Slots will last `SECONDS_PER_SLOT + 1` or `SECONDS_PER_SLOT - 1` seconds around leap seconds.
|
1) **Leap seconds**: Slots will last `SECONDS_PER_SLOT + 1` or `SECONDS_PER_SLOT - 1` seconds around leap seconds.
|
||||||
2) **Honest clocks**: Honest nodes are assumed to have clocks synchronized within `SECONDS_PER_SLOT` seconds of each other.
|
2) **Honest clocks**: Honest nodes are assumed to have clocks synchronized within `SECONDS_PER_SLOT` seconds of each other.
|
||||||
3) **Eth1 data**: The large `ETH1_FOLLOW_DISTANCE` specified in the [honest validator document](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/validator/0_beacon-chain-validator.md) should ensure that `state.latest_eth1_data` of the canonical Ethereum 2.0 chain remains consistent with the canonical Ethereum 1.0 chain. If not, emergency manual intervention will be required.
|
3) **Eth1 data**: The large `ETH1_FOLLOW_DISTANCE` specified in the [honest validator document](https://github.com/ethereum/eth2.0-specs/blob/dev/specs/validator/0_beacon-chain-validator.md) should ensure that `state.latest_eth1_data` of the canonical Ethereum 2.0 chain remains consistent with the canonical Ethereum 1.0 chain. If not, emergency manual intervention will be required.
|
||||||
4) **Manual forks**: Manual forks may arbitrarily change the fork choice rule but are expected to be enacted at epoch transitions, with the fork details reflected in `state.fork`.
|
4) **Manual forks**: Manual forks may arbitrarily change the fork choice rule but are expected to be enacted at epoch transitions, with the fork details reflected in `state.fork`.
|
||||||
|
|
||||||
### Containers
|
### Helpers
|
||||||
|
|
||||||
#### `Target`
|
#### `Target`
|
||||||
|
|
||||||
@ -61,7 +60,7 @@ The head block root associated with a `store` is defined as `get_head(store)`. A
|
|||||||
@dataclass
|
@dataclass
|
||||||
class Target(object):
|
class Target(object):
|
||||||
epoch: Epoch
|
epoch: Epoch
|
||||||
root: Bytes32
|
root: Hash
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `Store`
|
#### `Store`
|
||||||
@ -69,38 +68,36 @@ class Target(object):
|
|||||||
```python
|
```python
|
||||||
@dataclass
|
@dataclass
|
||||||
class Store(object):
|
class Store(object):
|
||||||
blocks: Dict[Bytes32, BeaconBlock] = field(default_factory=dict)
|
blocks: Dict[Hash, BeaconBlock] = field(default_factory=dict)
|
||||||
states: Dict[Bytes32, BeaconState] = field(default_factory=dict)
|
states: Dict[Hash, BeaconState] = field(default_factory=dict)
|
||||||
time: int = 0
|
time: int = 0
|
||||||
latest_targets: Dict[ValidatorIndex, Target] = field(default_factory=dict)
|
latest_targets: Dict[ValidatorIndex, Target] = field(default_factory=dict)
|
||||||
justified_root: Bytes32 = ZERO_HASH
|
justified_root: Hash = ZERO_HASH
|
||||||
finalized_root: Bytes32 = ZERO_HASH
|
finalized_root: Hash = ZERO_HASH
|
||||||
```
|
```
|
||||||
|
|
||||||
### Helpers
|
|
||||||
|
|
||||||
#### `get_genesis_store`
|
#### `get_genesis_store`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_genesis_store(genesis_state: BeaconState) -> Store:
|
def get_genesis_store(genesis_state: BeaconState) -> Store:
|
||||||
genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))
|
genesis_block = BeaconBlock(state_root=hash_tree_root(genesis_state))
|
||||||
root = signing_root(genesis_block)
|
root = signing_root(genesis_block)
|
||||||
return Store(blocks={root: genesis_block}, states={root: genesis_state}, finalized_root=root, justified_root=root)
|
return Store(blocks={root: genesis_block}, states={root: genesis_state}, justified_root=root, finalized_root=root)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `get_ancestor`
|
#### `get_ancestor`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_ancestor(store: Store, root: Bytes32, slot: Slot) -> Bytes32:
|
def get_ancestor(store: Store, root: Hash, slot: Slot) -> Hash:
|
||||||
block = store.blocks[root]
|
block = store.blocks[root]
|
||||||
assert block.slot >= slot
|
assert block.slot >= slot
|
||||||
return root if block.slot == slot else get_ancestor(store, block.parent_root, slot)
|
return root if block.slot == slot else get_ancestor(store, block.parent_root, slot)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `get_attesting_balance_from_store`
|
#### `get_latest_attesting_balance`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_attesting_balance_from_store(store: Store, root: Bytes32) -> Gwei:
|
def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
|
||||||
state = store.states[store.justified_root]
|
state = store.states[store.justified_root]
|
||||||
active_indices = get_active_validator_indices(state.validator_registry, slot_to_epoch(state.slot))
|
active_indices = get_active_validator_indices(state.validator_registry, slot_to_epoch(state.slot))
|
||||||
return Gwei(sum(
|
return Gwei(sum(
|
||||||
@ -112,15 +109,15 @@ def get_attesting_balance_from_store(store: Store, root: Bytes32) -> Gwei:
|
|||||||
#### `get_head`
|
#### `get_head`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def get_head(store: Store) -> Bytes32:
|
def get_head(store: Store) -> Hash:
|
||||||
# Execute the LMD-GHOST fork choice
|
# Execute the LMD-GHOST fork choice
|
||||||
head = store.justified_root
|
head = store.justified_root
|
||||||
while True:
|
while True:
|
||||||
children = [root for root in store.blocks.keys() if store.blocks[root].parent_root == head]
|
children = [root for root in store.blocks.keys() if store.blocks[root].parent_root == head]
|
||||||
if len(children) == 0:
|
if len(children) == 0:
|
||||||
return head
|
return head
|
||||||
# Sort by attesting balance with ties broken lexicographically
|
# Sort by latest attesting balance with ties broken lexicographically
|
||||||
head = max(children, key=lambda root: (get_attesting_balance_from_store(store, root), root))
|
head = max(children, key=lambda root: (get_latest_attesting_balance(store, root), root))
|
||||||
```
|
```
|
||||||
|
|
||||||
### Handlers
|
### Handlers
|
||||||
@ -147,13 +144,14 @@ def on_block(store: Store, block: BeaconBlock) -> None:
|
|||||||
state = state_transition(pre_state, block)
|
state = state_transition(pre_state, block)
|
||||||
# Add new state to the store
|
# Add new state to the store
|
||||||
store.states[signing_root(block)] = state
|
store.states[signing_root(block)] = state
|
||||||
# Update justified and finalized blocks
|
# Update justified block root
|
||||||
if state.finalized_epoch > slot_to_epoch(store.blocks[store.finalized_root].slot):
|
|
||||||
store.finalized_root = state.finalized_root
|
|
||||||
if state.current_justified_epoch > slot_to_epoch(store.blocks[store.justified_root].slot):
|
if state.current_justified_epoch > slot_to_epoch(store.blocks[store.justified_root].slot):
|
||||||
store.justified_root = state.current_justified_root
|
store.justified_root = state.current_justified_root
|
||||||
elif state.previous_justified_epoch > slot_to_epoch(store.blocks[store.justified_root].slot):
|
elif state.previous_justified_epoch > slot_to_epoch(store.blocks[store.justified_root].slot):
|
||||||
store.justified_root = state.previous_justified_root
|
store.justified_root = state.previous_justified_root
|
||||||
|
# Update finalized block root
|
||||||
|
if state.finalized_epoch > slot_to_epoch(store.blocks[store.finalized_root].slot):
|
||||||
|
store.finalized_root = state.finalized_root
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `on_attestation`
|
#### `on_attestation`
|
||||||
@ -165,8 +163,5 @@ def on_attestation(store: Store, attestation: Attestation) -> None:
|
|||||||
validate_indexed_attestation(state, indexed_attestation)
|
validate_indexed_attestation(state, indexed_attestation)
|
||||||
for i in indexed_attestation.custody_bit_0_indices + indexed_attestation.custody_bit_1_indices:
|
for i in indexed_attestation.custody_bit_0_indices + indexed_attestation.custody_bit_1_indices:
|
||||||
if i not in store.latest_targets or attestation.data.target_epoch > store.latest_targets[i].epoch:
|
if i not in store.latest_targets or attestation.data.target_epoch > store.latest_targets[i].epoch:
|
||||||
store.latest_targets[i] = Target(
|
store.latest_targets[i] = Target(attestation.data.target_epoch, attestation.data.target_root)
|
||||||
epoch=attestation.data.target_epoch,
|
|
||||||
root=attestation.data.target_root,
|
|
||||||
)
|
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user