mirror of
https://github.com/status-im/eth2.0-specs.git
synced 2025-02-26 01:05:15 +00:00
rework forkchoice to use Checkpoints
This commit is contained in:
parent
74eeb87936
commit
1b66a1a2bd
@ -12,7 +12,7 @@
|
|||||||
- [Time parameters](#time-parameters)
|
- [Time parameters](#time-parameters)
|
||||||
- [Fork choice](#fork-choice)
|
- [Fork choice](#fork-choice)
|
||||||
- [Helpers](#helpers)
|
- [Helpers](#helpers)
|
||||||
- [`Target`](#target)
|
- [`Checkpoint`](#checkpoint)
|
||||||
- [`Store`](#store)
|
- [`Store`](#store)
|
||||||
- [`get_genesis_store`](#get_genesis_store)
|
- [`get_genesis_store`](#get_genesis_store)
|
||||||
- [`get_ancestor`](#get_ancestor)
|
- [`get_ancestor`](#get_ancestor)
|
||||||
@ -55,39 +55,27 @@ The head block root associated with a `store` is defined as `get_head(store)`. A
|
|||||||
|
|
||||||
### Helpers
|
### Helpers
|
||||||
|
|
||||||
#### `Target`
|
#### `Checkpoint`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@dataclass
|
@dataclass
|
||||||
class Target(object):
|
class Checkpoint(object):
|
||||||
epoch: Epoch
|
epoch: Epoch
|
||||||
root: Hash
|
root: Hash
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `RootSlot`
|
|
||||||
|
|
||||||
```python
|
|
||||||
@dataclass
|
|
||||||
class RootSlot(object):
|
|
||||||
slot: Slot
|
|
||||||
root: Hash
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#### `Store`
|
#### `Store`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
@dataclass
|
@dataclass
|
||||||
class Store(object):
|
class Store(object):
|
||||||
blocks: Dict[Hash, BeaconBlock] = field(default_factory=dict)
|
blocks: Dict[Hash, BeaconBlock] = field(default_factory=dict)
|
||||||
states: Dict[RootSlot, BeaconState] = field(default_factory=dict)
|
block_states: Dict[Hash, BeaconState] = field(default_factory=dict)
|
||||||
time: int = 0
|
checkpoint_states: Dict[Checkpoint, BeaconState] = field(default_factory=dict)
|
||||||
latest_targets: Dict[ValidatorIndex, Target] = field(default_factory=dict)
|
time: int
|
||||||
justified_root: Hash = ZERO_HASH
|
latest_targets: Dict[ValidatorIndex, Checkpoint] = field(default_factory=dict)
|
||||||
justified_epoch: Epoch = 0
|
justified_checkpoint: Checkpoint
|
||||||
finalized_root: Hash = ZERO_HASH
|
finalized_checkpoint: Checkpoint
|
||||||
finalized_epoch: Epoch = 0
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `get_genesis_store`
|
#### `get_genesis_store`
|
||||||
@ -96,12 +84,15 @@ class Store(object):
|
|||||||
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)
|
||||||
|
justified_checkpoint = Checkpoint(GENESIS_EPOCH, root)
|
||||||
|
finalized_checkpoint = Checkpoint(GENESIS_EPOCH, root)
|
||||||
return Store(
|
return Store(
|
||||||
blocks={root: genesis_block},
|
blocks={root: genesis_block},
|
||||||
states={root: genesis_state},
|
block_states={root: genesis_state},
|
||||||
|
checkpoint_states={justified_checkpoint: genesis_state.copy()},
|
||||||
time=genesis_state.genesis_time,
|
time=genesis_state.genesis_time,
|
||||||
justified_root=root,
|
justified_checkpoint=justified_checkpoint,
|
||||||
finalized_root=root,
|
finalized_checkpoint=finalized_checkpoint,
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -118,7 +109,7 @@ def get_ancestor(store: Store, root: Hash, slot: Slot) -> Hash:
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
|
def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
|
||||||
state = store.states[RootSlot(store.justified_root, get_epoch_start_slot(store.justified_epoch)]
|
state = store.checkpoint_states[store.justified_checkpoint]
|
||||||
active_indices = get_active_validator_indices(state.validator_registry, get_current_epoch(state))
|
active_indices = get_active_validator_indices(state.validator_registry, get_current_epoch(state))
|
||||||
return Gwei(sum(
|
return Gwei(sum(
|
||||||
state.validator_registry[i].effective_balance for i in active_indices
|
state.validator_registry[i].effective_balance for i in active_indices
|
||||||
@ -131,8 +122,8 @@ def get_latest_attesting_balance(store: Store, root: Hash) -> Gwei:
|
|||||||
```python
|
```python
|
||||||
def get_head(store: Store) -> Hash:
|
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_checkpoint.root
|
||||||
justified_slot = get_epoch_start_slot(store.justified_epoch)
|
justified_slot = get_epoch_start_slot(store.justified_checkpoint.epoch)
|
||||||
while True:
|
while True:
|
||||||
children = [
|
children = [
|
||||||
root for root in store.blocks.keys()
|
root for root in store.blocks.keys()
|
||||||
@ -159,63 +150,52 @@ def on_tick(store: Store, time: int) -> None:
|
|||||||
def on_block(store: Store, block: BeaconBlock) -> None:
|
def on_block(store: Store, block: BeaconBlock) -> None:
|
||||||
# Make a copy of the state to avoid mutability issues
|
# Make a copy of the state to avoid mutability issues
|
||||||
parent_block = store.blocks[block.parent_root]
|
parent_block = store.blocks[block.parent_root]
|
||||||
pre_state = store.states[RootSlot(signing_root(parent_block), parent_block.slot)].copy()
|
pre_state = store.block_states[parent_block.root].copy()
|
||||||
# Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past.
|
# Blocks cannot be in the future. If they are, their consideration must be delayed until the are in the past.
|
||||||
assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT
|
assert store.time >= pre_state.genesis_time + block.slot * SECONDS_PER_SLOT
|
||||||
# Add new block to the store
|
# Add new block to the store
|
||||||
store.blocks[signing_root(block)] = block
|
store.blocks[signing_root(block)] = block
|
||||||
# Check block is a descendant of the finalized block
|
# Check block is a descendant of the finalized block
|
||||||
assert get_ancestor(store, signing_root(block), store.blocks[store.finalized_root].slot) == store.finalized_root
|
assert get_ancestor(store, signing_root(block), store.blocks[get_epoch_start_slot(store.finalized_checkpoint)].slot) == store.finalized_checkpoint.root
|
||||||
# Check that block is later than the finalized epoch slot
|
# Check that block is later than the finalized epoch slot
|
||||||
assert blocks.slot > get_epoch_start_slot(store.finalized_epoch)
|
assert block.slot > get_epoch_start_slot(store.finalized_checkpoint.epoch)
|
||||||
# Check the block is valid and compute the post-state
|
# Check the block is valid and compute the post-state
|
||||||
state = state_transition(pre_state, block)
|
state = state_transition(pre_state, block)
|
||||||
# Add new state for this block to the store
|
# Add new state for this block to the store
|
||||||
store.states[RootSlot(signing_root(block), block.slot)] = state
|
store.block_states[signing_root(block)] = state
|
||||||
# Update justified block root and epoch
|
|
||||||
previous_justified_epoch = store.justified_epoch
|
# Update justified checkpoint
|
||||||
justified_root_slot = None
|
if state.current_justified_epoch > store.justified_checkpoint.epoch:
|
||||||
if state.current_justified_epoch > store.justified_epoch:
|
store.justified_checkpoint = Checkpoint(state.current_justified_epoch, state.current_justified_root)
|
||||||
store.justified_root = state.current_justified_root
|
|
||||||
state.justified_epoch = state.current_justified_epoch
|
|
||||||
elif state.previous_justified_epoch > store.justified_epoch:
|
elif state.previous_justified_epoch > store.justified_epoch:
|
||||||
store.justified_root = state.previous_justified_root
|
store.justified_checkpoint = Checkpoint(state.previous_justified_epoch, state.previous_justified_root)
|
||||||
state.justified_epoch = state.previous_justified_epoch
|
|
||||||
|
|
||||||
# Store justified state
|
# Update finalized checkpoint
|
||||||
if previous_justified_epoch != store.justified_epoch:
|
|
||||||
justified_slot = get_epoch_start_slot(store.justified_epoch)
|
|
||||||
justified_root_slot = RootSlot(store.justified_root, justified_slot)
|
|
||||||
if justified_root_slot not in store.states:
|
|
||||||
base_state = store.states[RootSlot(store.justified_root, store.blocks[store.justified_root].slot)]
|
|
||||||
store.states[justified_root_slot] = process_slots(deepcopy(base_state), justified_slot)
|
|
||||||
|
|
||||||
# Update finalized block root and epoch, and store finalized state
|
|
||||||
if state.finalized_epoch > state.finalized_epoch:
|
if state.finalized_epoch > state.finalized_epoch:
|
||||||
store.finalized_root = state.finalized_root
|
store.finalized_checkpoint = Checkpoint(state.finalized_epoch, state.finalized_root)
|
||||||
store.finalized_epoch = state.finalized_epoch
|
|
||||||
finalized_slot = get_epoch_start_slot(store.finalized_epoch)
|
|
||||||
finalized_root_slot = RootSlot(store.finalized_root, finalized_slot)
|
|
||||||
if finalized_root_slot not in store.states:
|
|
||||||
base_state = store.states[RootSlot(store.finalized_root, store.blocks[store.finalized_root].slot)]
|
|
||||||
store.states[finalized_root_slot] = process_slots(deepcopy(base_state), finalized_slot)
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `on_attestation`
|
#### `on_attestation`
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def on_attestation(store: Store, attestation: Attestation) -> None:
|
def on_attestation(store: Store, attestation: Attestation) -> None:
|
||||||
# cannot calculate the current shuffling if have not seen the target
|
target_checkpoint = Checkpoint(attestation.data.target_epoch, attestation.data, target_root)
|
||||||
assert attestation.data.target_root in store.blocks
|
|
||||||
|
|
||||||
# get state at the `target_root`/`target_epoch` to validate attestation and calculate the committees
|
# Cannot calculate the current shuffling if have not seen the target
|
||||||
state = store.states[(attestation.data.target_root, get_epoch_start_slot(attestation.data.target_epoch))]
|
assert target_checkpoint.root in store.blocks
|
||||||
|
|
||||||
|
# Store target checkpoint state if not yet seen
|
||||||
|
if target_checkpoint not in store.checkpoint_states:
|
||||||
|
base_state = store.block_states[target_checkpoint.root].copy()
|
||||||
|
store.checkpoint_states[target_checkpoint] = process_slots(base_state, get_epoch_start_slot(target_checkpoint.epoch))
|
||||||
|
|
||||||
|
# Get state at the `target_checkpoint` to validate attestation and calculate the committees
|
||||||
|
state = store.checkpoint_states[target_checkpoint]
|
||||||
indexed_attestation = convert_to_indexed(state, attestation)
|
indexed_attestation = convert_to_indexed(state, attestation)
|
||||||
validate_indexed_attestation(state, indexed_attestation)
|
validate_indexed_attestation(state, indexed_attestation)
|
||||||
|
|
||||||
# update latest targets
|
# Update latest targets
|
||||||
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 target_checkpoint.epoch > store.latest_targets[i].epoch:
|
||||||
store.latest_targets[i] = Target(attestation.data.target_epoch, attestation.data.target_root)
|
store.latest_targets[i] = target_checkpoint
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user