v0.12 fork choice update (#1229)

* Incomplete scraps

* Add progress on new fork choice impl

* Further progress

* First complete compiling version

* Remove chain reference

* Add new lmd_ghost crate

* Start integrating into beacon chain

* Update `milagro_bls` to new release (#1183)

* Update milagro_bls to new release

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Tidy up fake cryptos

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* move SecretHash to bls and put plaintext back

Signed-off-by: Kirk Baird <baird.k@outlook.com>

* Update state processing for v0.12

* Fix EF test runners for v0.12

* Fix some tests

* Fix broken attestation verification test

* More test fixes

* Rough beacon chain impl working

* Remove fork_choice_2

* Remove checkpoint manager

* Half finished ssz impl

* Add missed file

* Add persistence

* Tidy, fix some compile errors

* Remove RwLock from ProtoArrayForkChoice

* Fix store-based compile errors

* Add comments, tidy

* Move function out of ForkChoice struct

* Start testing

* More testing

* Fix compile error

* Tidy beacon_chain::fork_choice

* Queue attestations from the current slot

* Allow fork choice to handle prior-to-genesis start

* Improve error granularity

* Test attestation dequeuing

* Process attestations during block

* Store target root in fork choice

* Move fork choice verification into new crate

* Update tests

* Consensus updates for v0.12 (#1228)

* Update state processing for v0.12

* Fix EF test runners for v0.12

* Fix some tests

* Fix broken attestation verification test

* More test fixes

* Fix typo found in review

* Add `Block` struct to ProtoArray

* Start fixing get_ancestor

* Add rough progress on testing

* Get fork choice tests working

* Progress with testing

* Fix partialeq impl

* Move slot clock from fc_store

* Improve testing

* Add testing for best justified

* Add clone back to SystemTimeSlotClock

* Add balances test

* Start adding balances cache again

* Wire-in balances cache

* Improve tests

* Remove commented-out tests

* Remove beacon_chain::ForkChoice

* Rename crates

* Update wider codebase to new fork_choice layout

* Move advance_slot in test harness

* Tidy ForkChoice::update_time

* Fix verification tests

* Fix compile error with iter::once

* Fix fork choice tests

* Ensure block attestations are processed

* Fix failing beacon_chain tests

* Add first invalid block check

* Add finalized block check

* Progress with testing, new store builder

* Add fixes to get_ancestor

* Fix old genesis justification test

* Fix remaining fork choice tests

* Change root iteration method

* Move on_verified_block

* Remove unused method

* Start adding attestation verification tests

* Add invalid ffg target test

* Add target epoch test

* Add queued attestation test

* Remove old fork choice verification tests

* Tidy, add test

* Move fork choice lock drop

* Rename BeaconForkChoiceStore

* Add comments, tidy BeaconForkChoiceStore

* Update metrics, rename fork_choice_store.rs

* Remove genesis_block_root from ForkChoice

* Tidy

* Update fork_choice comments

* Tidy, add comments

* Tidy, simplify ForkChoice, fix compile issue

* Tidy, removed dead file

* Increase http request timeout

* Fix failing rest_api test

* Set HTTP timeout back to 5s

* Apply fix to get_ancestor

* Address Michael's comments

* Fix typo

* Revert "Fix broken attestation verification test"

This reverts commit 722cdc903b.

Co-authored-by: Kirk Baird <baird.k@outlook.com>
Co-authored-by: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
Paul Hauner
2020-06-17 11:10:22 +10:00
committed by GitHub
parent 1a4de898bc
commit 764cb2d32a
51 changed files with 2641 additions and 1376 deletions

1
consensus/proto_array/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.yaml

View File

@@ -0,0 +1,18 @@
[package]
name = "proto_array"
version = "0.2.0"
authors = ["Paul Hauner <paul@sigmaprime.io>"]
edition = "2018"
[[bin]]
name = "proto_array"
path = "src/bin.rs"
[dependencies]
types = { path = "../types" }
itertools = "0.9.0"
eth2_ssz = "0.1.2"
eth2_ssz_derive = "0.1.0"
serde = "1.0.110"
serde_derive = "1.0.110"
serde_yaml = "0.8.11"

View File

@@ -0,0 +1,15 @@
use proto_array::fork_choice_test_definition::*;
use serde_yaml;
use std::fs::File;
fn main() {
write_test_def_to_yaml("votes.yaml", get_votes_test_definition());
write_test_def_to_yaml("no_votes.yaml", get_no_votes_test_definition());
write_test_def_to_yaml("ffg_01.yaml", get_ffg_case_01_test_definition());
write_test_def_to_yaml("ffg_02.yaml", get_ffg_case_02_test_definition());
}
fn write_test_def_to_yaml(filename: &str, def: ForkChoiceTestDefinition) {
let file = File::create(filename).expect("Should be able to open file");
serde_yaml::to_writer(file, &def).expect("Should be able to write YAML to file");
}

View File

@@ -0,0 +1,33 @@
use types::{Epoch, Hash256};
#[derive(Clone, PartialEq, Debug)]
pub enum Error {
FinalizedNodeUnknown(Hash256),
JustifiedNodeUnknown(Hash256),
InvalidFinalizedRootChange,
InvalidNodeIndex(usize),
InvalidParentIndex(usize),
InvalidBestChildIndex(usize),
InvalidJustifiedIndex(usize),
InvalidBestDescendant(usize),
InvalidParentDelta(usize),
InvalidNodeDelta(usize),
DeltaOverflow(usize),
IndexOverflow(&'static str),
InvalidDeltaLen {
deltas: usize,
indices: usize,
},
RevertedFinalizedEpoch {
current_finalized_epoch: Epoch,
new_finalized_epoch: Epoch,
},
InvalidBestNode {
start_root: Hash256,
justified_epoch: Epoch,
finalized_epoch: Epoch,
head_root: Hash256,
head_justified_epoch: Epoch,
head_finalized_epoch: Epoch,
},
}

View File

@@ -0,0 +1,189 @@
mod ffg_updates;
mod no_votes;
mod votes;
use crate::proto_array_fork_choice::{Block, ProtoArrayForkChoice};
use serde_derive::{Deserialize, Serialize};
use types::{Epoch, Hash256, Slot};
pub use ffg_updates::*;
pub use no_votes::*;
pub use votes::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Operation {
FindHead {
justified_epoch: Epoch,
justified_root: Hash256,
finalized_epoch: Epoch,
justified_state_balances: Vec<u64>,
expected_head: Hash256,
},
InvalidFindHead {
justified_epoch: Epoch,
justified_root: Hash256,
finalized_epoch: Epoch,
justified_state_balances: Vec<u64>,
},
ProcessBlock {
slot: Slot,
root: Hash256,
parent_root: Hash256,
justified_epoch: Epoch,
finalized_epoch: Epoch,
},
ProcessAttestation {
validator_index: usize,
block_root: Hash256,
target_epoch: Epoch,
},
Prune {
finalized_root: Hash256,
prune_threshold: usize,
expected_len: usize,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForkChoiceTestDefinition {
pub finalized_block_slot: Slot,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub finalized_root: Hash256,
pub operations: Vec<Operation>,
}
impl ForkChoiceTestDefinition {
pub fn run(self) {
let mut fork_choice = ProtoArrayForkChoice::new(
self.finalized_block_slot,
Hash256::zero(),
self.justified_epoch,
self.finalized_epoch,
self.finalized_root,
)
.expect("should create fork choice struct");
for (op_index, op) in self.operations.into_iter().enumerate() {
match op.clone() {
Operation::FindHead {
justified_epoch,
justified_root,
finalized_epoch,
justified_state_balances,
expected_head,
} => {
let head = fork_choice
.find_head(
justified_epoch,
justified_root,
finalized_epoch,
&justified_state_balances,
)
.expect(&format!(
"find_head op at index {} returned error",
op_index
));
assert_eq!(
head, expected_head,
"Operation at index {} failed checks. Operation: {:?}",
op_index, op
);
check_bytes_round_trip(&fork_choice);
}
Operation::InvalidFindHead {
justified_epoch,
justified_root,
finalized_epoch,
justified_state_balances,
} => {
let result = fork_choice.find_head(
justified_epoch,
justified_root,
finalized_epoch,
&justified_state_balances,
);
assert!(
result.is_err(),
"Operation at index {} . Operation: {:?}",
op_index,
op
);
check_bytes_round_trip(&fork_choice);
}
Operation::ProcessBlock {
slot,
root,
parent_root,
justified_epoch,
finalized_epoch,
} => {
let block = Block {
slot,
root,
parent_root: Some(parent_root),
state_root: Hash256::zero(),
target_root: Hash256::zero(),
justified_epoch,
finalized_epoch,
};
fork_choice.process_block(block).expect(&format!(
"process_block op at index {} returned error",
op_index
));
check_bytes_round_trip(&fork_choice);
}
Operation::ProcessAttestation {
validator_index,
block_root,
target_epoch,
} => {
fork_choice
.process_attestation(validator_index, block_root, target_epoch)
.expect(&format!(
"process_attestation op at index {} returned error",
op_index
));
check_bytes_round_trip(&fork_choice);
}
Operation::Prune {
finalized_root,
prune_threshold,
expected_len,
} => {
fork_choice.set_prune_threshold(prune_threshold);
fork_choice
.maybe_prune(finalized_root)
.expect("update_finalized_root op at index {} returned error");
// Ensure that no pruning happened.
assert_eq!(
fork_choice.len(),
expected_len,
"Prune op at index {} failed with {} instead of {}",
op_index,
fork_choice.len(),
expected_len
);
}
}
}
}
}
/// Gives a hash that is not the zero hash (unless i is `usize::max_value)`.
fn get_hash(i: u64) -> Hash256 {
Hash256::from_low_u64_be(i)
}
fn check_bytes_round_trip(original: &ProtoArrayForkChoice) {
let bytes = original.as_bytes();
let decoded =
ProtoArrayForkChoice::from_bytes(&bytes).expect("fork choice should decode from bytes");
assert!(
*original == decoded,
"fork choice should encode and decode without change"
);
}

View File

@@ -0,0 +1,452 @@
use super::*;
pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition {
let balances = vec![1; 2];
let mut ops = vec![];
// Ensure that the head starts at the finalized block.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(0),
});
// Build the following tree (stick? lol).
//
// 0 <- just: 0, fin: 0
// |
// 1 <- just: 0, fin: 0
// |
// 2 <- just: 1, fin: 0
// |
// 3 <- just: 2, fin: 1
ops.push(Operation::ProcessBlock {
slot: Slot::new(1),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(2),
root: get_hash(2),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(3),
root: get_hash(3),
parent_root: get_hash(2),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(1),
});
// Ensure that with justified epoch 0 we find 3
//
// 0 <- start
// |
// 1
// |
// 2
// |
// 3 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(3),
});
// Ensure that with justified epoch 1 we find 2
//
// 0
// |
// 1
// |
// 2 <- start
// |
// 3 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
// Ensure that with justified epoch 2 we find 3
//
// 0
// |
// 1
// |
// 2
// |
// 3 <- start + head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(3),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(3),
});
// END OF TESTS
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
operations: ops,
}
}
pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition {
let balances = vec![1; 2];
let mut ops = vec![];
// Ensure that the head starts at the finalized block.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(0),
});
// Build the following tree.
//
// 0
// / \
// just: 0, fin: 0 -> 1 2 <- just: 0, fin: 0
// | |
// just: 1, fin: 0 -> 3 4 <- just: 0, fin: 0
// | |
// just: 1, fin: 0 -> 5 6 <- just: 0, fin: 0
// | |
// just: 1, fin: 0 -> 7 8 <- just: 1, fin: 0
// | |
// just: 2, fin: 0 -> 9 10 <- just: 2, fin: 0
// Left branch
ops.push(Operation::ProcessBlock {
slot: Slot::new(1),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(2),
root: get_hash(3),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(3),
root: get_hash(5),
parent_root: get_hash(3),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
root: get_hash(7),
parent_root: get_hash(5),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
root: get_hash(9),
parent_root: get_hash(7),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(0),
});
// Right branch
ops.push(Operation::ProcessBlock {
slot: Slot::new(1),
root: get_hash(2),
parent_root: get_hash(0),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(2),
root: get_hash(4),
parent_root: get_hash(2),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(3),
root: get_hash(6),
parent_root: get_hash(4),
justified_epoch: Epoch::new(0),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
root: get_hash(8),
parent_root: get_hash(6),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(0),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(4),
root: get_hash(10),
parent_root: get_hash(8),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(0),
});
// Ensure that if we start at 0 we find 10 (just: 0, fin: 0).
//
// 0 <-- start
// / \
// 1 2
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// 9 10 <-- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above, but with justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above, but with justified epoch 3 (should be invalid).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
});
// Add a vote to 1.
//
// 0
// / \
// +1 vote -> 1 2
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// 9 10
ops.push(Operation::ProcessAttestation {
validator_index: 0,
block_root: get_hash(1),
target_epoch: Epoch::new(0),
});
// Ensure that if we start at 0 we find 9 (just: 0, fin: 0).
//
// 0 <-- start
// / \
// 1 2
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// head -> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Save as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Save as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
});
// Add a vote to 2.
//
// 0
// / \
// 1 2 <- +1 vote
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// 9 10
ops.push(Operation::ProcessAttestation {
validator_index: 1,
block_root: get_hash(2),
target_epoch: Epoch::new(0),
});
// Ensure that if we start at 0 we find 10 (just: 0, fin: 0).
//
// 0 <-- start
// / \
// 1 2
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// 9 10 <-- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
});
// Ensure that if we start at 1 we find 9 (just: 0, fin: 0).
//
// 0
// / \
// start-> 1 2
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// head -> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(1),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Same as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(1),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Same as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(1),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
});
// Ensure that if we start at 2 we find 10 (just: 0, fin: 0).
//
// 0
// / \
// 1 2 <- start
// | |
// 3 4
// | |
// 5 6
// | |
// 7 8
// | |
// 9 10 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(0),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 2.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Same as above but justified epoch 3 (should fail).
ops.push(Operation::InvalidFindHead {
justified_epoch: Epoch::new(3),
justified_root: get_hash(2),
finalized_epoch: Epoch::new(0),
justified_state_balances: balances.clone(),
});
// END OF TESTS
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
operations: ops,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ffg_case_01() {
let test = get_ffg_case_01_test_definition();
test.run();
}
#[test]
fn ffg_case_02() {
let test = get_ffg_case_02_test_definition();
test.run();
}
}

View File

@@ -0,0 +1,237 @@
use super::*;
pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
let balances = vec![0; 16];
let operations = vec![
// Check that the head is the finalized block.
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: Hash256::zero(),
},
// Add block 2
//
// 0
// /
// 2
Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(2),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
},
// Ensure the head is 2
//
// 0
// /
// 2 <- head
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
},
// Add block 1
//
// 0
// / \
// 2 1
Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
},
// Ensure the head is still 2
//
// 0
// / \
// head-> 2 1
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
},
// Add block 3
//
// 0
// / \
// 2 1
// |
// 3
Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(3),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
},
// Ensure 2 is still the head
//
// 0
// / \
// head-> 2 1
// |
// 3
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
},
// Add block 4
//
// 0
// / \
// 2 1
// | |
// 4 3
Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(4),
parent_root: get_hash(2),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
},
// Ensure the head is 4.
//
// 0
// / \
// 2 1
// | |
// head-> 4 3
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
},
// Add block 5 with a justified epoch of 2
//
// 0
// / \
// 2 1
// | |
// 4 3
// |
// 5 <- justified epoch = 2
Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(5),
parent_root: get_hash(4),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(1),
},
// Ensure the head is still 4 whilst the justified epoch is 0.
//
// 0
// / \
// 2 1
// | |
// head-> 4 3
// |
// 5
Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: Hash256::zero(),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
},
// Ensure there is an error when starting from a block that has the wrong justified epoch.
//
// 0
// / \
// 2 1
// | |
// 4 3
// |
// 5 <- starting from 5 with justified epoch 0 should error.
Operation::InvalidFindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
},
// Set the justified epoch to 2 and the start block to 5 and ensure 5 is the head.
//
// 0
// / \
// 2 1
// | |
// 4 3
// |
// 5 <- head
Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(5),
},
// Add block 6
//
// 0
// / \
// 2 1
// | |
// 4 3
// |
// 5
// |
// 6
Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(6),
parent_root: get_hash(5),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(1),
},
// Ensure 6 is the head
//
// 0
// / \
// 2 1
// | |
// 4 3
// |
// 5
// |
// 6 <- head
Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(6),
},
];
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
operations,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let test = get_no_votes_test_definition();
test.run();
}
}

View File

@@ -0,0 +1,698 @@
use super::*;
pub fn get_votes_test_definition() -> ForkChoiceTestDefinition {
let mut balances = vec![1; 2];
let mut ops = vec![];
// Ensure that the head starts at the finalized block.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(0),
});
// Add a block with a hash of 2.
//
// 0
// /
// 2
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(2),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
});
// Ensure that the head is 2
//
// 0
// /
// head-> 2
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
// Add a block with a hash of 1 that comes off the genesis block (this is a fork compared
// to the previous block).
//
// 0
// / \
// 2 1
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(1),
parent_root: get_hash(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
});
// Ensure that the head is still 2
//
// 0
// / \
// head-> 2 1
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
// Add a vote to block 1
//
// 0
// / \
// 2 1 <- +vote
ops.push(Operation::ProcessAttestation {
validator_index: 0,
block_root: get_hash(1),
target_epoch: Epoch::new(2),
});
// Ensure that the head is now 1, beacuse 1 has a vote.
//
// 0
// / \
// 2 1 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(1),
});
// Add a vote to block 2
//
// 0
// / \
// +vote-> 2 1
ops.push(Operation::ProcessAttestation {
validator_index: 1,
block_root: get_hash(2),
target_epoch: Epoch::new(2),
});
// Ensure that the head is 2 since 1 and 2 both have a vote
//
// 0
// / \
// head-> 2 1
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
// Add block 3.
//
// 0
// / \
// 2 1
// |
// 3
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(3),
parent_root: get_hash(1),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
});
// Ensure that the head is still 2
//
// 0
// / \
// head-> 2 1
// |
// 3
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
// Move validator #0 vote from 1 to 3
//
// 0
// / \
// 2 1 <- -vote
// |
// 3 <- +vote
ops.push(Operation::ProcessAttestation {
validator_index: 0,
block_root: get_hash(3),
target_epoch: Epoch::new(3),
});
// Ensure that the head is still 2
//
// 0
// / \
// head-> 2 1
// |
// 3
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(2),
});
// Move validator #1 vote from 2 to 1 (this is an equivocation, but fork choice doesn't
// care)
//
// 0
// / \
// -vote-> 2 1 <- +vote
// |
// 3
ops.push(Operation::ProcessAttestation {
validator_index: 1,
block_root: get_hash(1),
target_epoch: Epoch::new(3),
});
// Ensure that the head is now 3
//
// 0
// / \
// 2 1
// |
// 3 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(3),
});
// Add block 4.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(4),
parent_root: get_hash(3),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
});
// Ensure that the head is now 4
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
});
// Add block 5, which has a justified epoch of 2.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// /
// 5 <- justified epoch = 2
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(5),
parent_root: get_hash(4),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
});
// Ensure that 5 is filtered out and the head stays at 4.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4 <- head
// /
// 5
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(4),
});
// Add block 6, which has a justified epoch of 0.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6 <- justified epoch = 0
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(6),
parent_root: get_hash(4),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
});
// Move both votes to 5.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// +2 vote-> 5 6
ops.push(Operation::ProcessAttestation {
validator_index: 0,
block_root: get_hash(5),
target_epoch: Epoch::new(4),
});
ops.push(Operation::ProcessAttestation {
validator_index: 1,
block_root: get_hash(5),
target_epoch: Epoch::new(4),
});
// Add blocks 7, 8 and 9. Adding these blocks helps test the `best_descendant`
// functionality.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6
// |
// 7
// |
// 8
// /
// 9
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(7),
parent_root: get_hash(5),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(8),
parent_root: get_hash(7),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
});
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(9),
parent_root: get_hash(8),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
});
// Ensure that 6 is the head, even though 5 has all the votes. This is testing to ensure
// that 5 is filtered out due to a differing justified epoch.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6 <- head
// |
// 7
// |
// 8
// /
// 9
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(1),
justified_root: get_hash(0),
finalized_epoch: Epoch::new(1),
justified_state_balances: balances.clone(),
expected_head: get_hash(6),
});
// Change fork-choice justified epoch to 1, and the start block to 5 and ensure that 9 is
// the head.
//
// << Change justified epoch to 1 >>
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6
// |
// 7
// |
// 8
// /
// head-> 9
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Change fork-choice justified epoch to 1, and the start block to 5 and ensure that 9 is
// the head.
//
// << Change justified epoch to 1 >>
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6
// |
// 7
// |
// 8
// /
// 9 <- +2 votes
ops.push(Operation::ProcessAttestation {
validator_index: 0,
block_root: get_hash(9),
target_epoch: Epoch::new(5),
});
ops.push(Operation::ProcessAttestation {
validator_index: 1,
block_root: get_hash(9),
target_epoch: Epoch::new(5),
});
// Add block 10
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6
// |
// 7
// |
// 8
// / \
// 9 10
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(10),
parent_root: get_hash(8),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
});
// Double-check the head is still 9 (no diagram this time)
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Introduce 2 more validators into the system
balances = vec![1; 4];
// Have the two new validators vote for 10
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6
// |
// 7
// |
// 8
// / \
// 9 10 <- +2 votes
ops.push(Operation::ProcessAttestation {
validator_index: 2,
block_root: get_hash(10),
target_epoch: Epoch::new(5),
});
ops.push(Operation::ProcessAttestation {
validator_index: 3,
block_root: get_hash(10),
target_epoch: Epoch::new(5),
});
// Check the head is now 10.
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// / \
// 5 6
// |
// 7
// |
// 8
// / \
// 9 10 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Set the balances of the last two validators to zero
balances = vec![1, 1, 0, 0];
// Check the head is 9 again.
//
// .
// .
// .
// |
// 8
// / \
// head-> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Set the balances of the last two validators back to 1
balances = vec![1; 4];
// Check the head is 10.
//
// .
// .
// .
// |
// 8
// / \
// 9 10 <- head
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(10),
});
// Remove the last two validators
balances = vec![1; 2];
// Check the head is 9 again.
//
// (prior blocks omitted for brevity)
// .
// .
// .
// |
// 8
// / \
// head-> 9 10
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Ensure that pruning below the prune threshold does not prune.
ops.push(Operation::Prune {
finalized_root: get_hash(5),
prune_threshold: usize::max_value(),
expected_len: 11,
});
// Run find-head, ensure the no-op prune didn't change the head.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Ensure that pruning above the prune threshold does prune.
//
//
// 0
// / \
// 2 1
// |
// 3
// |
// 4
// -------pruned here ------
// 5 6
// |
// 7
// |
// 8
// / \
// 9 10
ops.push(Operation::Prune {
finalized_root: get_hash(5),
prune_threshold: 1,
expected_len: 6,
});
// Run find-head, ensure the prune didn't change the head.
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(9),
});
// Add block 11
//
// 5 6
// |
// 7
// |
// 8
// / \
// 9 10
// |
// 11
ops.push(Operation::ProcessBlock {
slot: Slot::new(0),
root: get_hash(11),
parent_root: get_hash(9),
justified_epoch: Epoch::new(2),
finalized_epoch: Epoch::new(2),
});
// Ensure the head is now 11
//
// 5 6
// |
// 7
// |
// 8
// / \
// 9 10
// |
// head-> 11
ops.push(Operation::FindHead {
justified_epoch: Epoch::new(2),
justified_root: get_hash(5),
finalized_epoch: Epoch::new(2),
justified_state_balances: balances.clone(),
expected_head: get_hash(11),
});
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
operations: ops,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
let test = get_votes_test_definition();
test.run();
}
}

View File

@@ -0,0 +1,12 @@
mod error;
pub mod fork_choice_test_definition;
mod proto_array;
mod proto_array_fork_choice;
mod ssz_container;
pub use crate::proto_array_fork_choice::{Block, ProtoArrayForkChoice};
pub use error::Error;
pub mod core {
pub use super::proto_array::ProtoArray;
}

View File

@@ -0,0 +1,448 @@
use crate::{error::Error, Block};
use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use types::{Epoch, Hash256, Slot};
#[derive(Clone, PartialEq, Debug, Encode, Decode, Serialize, Deserialize)]
pub struct ProtoNode {
/// The `slot` is not necessary for `ProtoArray`, it just exists so external components can
/// easily query the block slot. This is useful for upstream fork choice logic.
pub slot: Slot,
/// The `state_root` is not necessary for `ProtoArray` either, it also just exists for upstream
/// components (namely attestation verification).
pub state_root: Hash256,
/// The root that would be used for the `attestation.data.target.root` if a LMD vote was cast
/// for this block.
///
/// The `target_root` is not necessary for `ProtoArray` either, it also just exists for upstream
/// components (namely fork choice attestation verification).
pub target_root: Hash256,
pub root: Hash256,
pub parent: Option<usize>,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
weight: u64,
best_child: Option<usize>,
best_descendant: Option<usize>,
}
#[derive(PartialEq, Debug, Serialize, Deserialize)]
pub struct ProtoArray {
/// Do not attempt to prune the tree unless it has at least this many nodes. Small prunes
/// simply waste time.
pub prune_threshold: usize,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub nodes: Vec<ProtoNode>,
pub indices: HashMap<Hash256, usize>,
}
impl ProtoArray {
/// Iterate backwards through the array, touching all nodes and their parents and potentially
/// the best-child of each parent.
///
/// The structure of the `self.nodes` array ensures that the child of each node is always
/// touched before its parent.
///
/// For each node, the following is done:
///
/// - Update the node's weight with the corresponding delta.
/// - Back-propagate each node's delta to its parents delta.
/// - Compare the current node with the parents best-child, updating it if the current node
/// should become the best child.
/// - If required, update the parents best-descendant with the current node or its best-descendant.
pub fn apply_score_changes(
&mut self,
mut deltas: Vec<i64>,
justified_epoch: Epoch,
finalized_epoch: Epoch,
) -> Result<(), Error> {
if deltas.len() != self.indices.len() {
return Err(Error::InvalidDeltaLen {
deltas: deltas.len(),
indices: self.indices.len(),
});
}
if justified_epoch != self.justified_epoch || finalized_epoch != self.finalized_epoch {
self.justified_epoch = justified_epoch;
self.finalized_epoch = finalized_epoch;
}
// Iterate backwards through all indices in `self.nodes`.
for node_index in (0..self.nodes.len()).rev() {
let node = self
.nodes
.get_mut(node_index)
.ok_or_else(|| Error::InvalidNodeIndex(node_index))?;
// There is no need to adjust the balances or manage parent of the zero hash since it
// is an alias to the genesis block. The weight applied to the genesis block is
// irrelevant as we _always_ choose it and it's impossible for it to have a parent.
if node.root == Hash256::zero() {
continue;
}
let node_delta = deltas
.get(node_index)
.copied()
.ok_or_else(|| Error::InvalidNodeDelta(node_index))?;
// Apply the delta to the node.
if node_delta < 0 {
// Note: I am conflicted about whether to use `saturating_sub` or `checked_sub`
// here.
//
// I can't think of any valid reason why `node_delta.abs()` should be greater than
// `node.weight`, so I have chosen `checked_sub` to try and fail-fast if there is
// some error.
//
// However, I am not fully convinced that some valid case for `saturating_sub` does
// not exist.
node.weight = node
.weight
.checked_sub(node_delta.abs() as u64)
.ok_or_else(|| Error::DeltaOverflow(node_index))?;
} else {
node.weight = node
.weight
.checked_add(node_delta as u64)
.ok_or_else(|| Error::DeltaOverflow(node_index))?;
}
// If the node has a parent, try to update its best-child and best-descendant.
if let Some(parent_index) = node.parent {
let parent_delta = deltas
.get_mut(parent_index)
.ok_or_else(|| Error::InvalidParentDelta(parent_index))?;
// Back-propagate the nodes delta to its parent.
*parent_delta += node_delta;
self.maybe_update_best_child_and_descendant(parent_index, node_index)?;
}
}
Ok(())
}
/// Register a block with the fork choice.
///
/// It is only sane to supply a `None` parent for the genesis block.
pub fn on_block(&mut self, block: Block) -> Result<(), Error> {
// If the block is already known, simply ignore it.
if self.indices.contains_key(&block.root) {
return Ok(());
}
let node_index = self.nodes.len();
let node = ProtoNode {
slot: block.slot,
root: block.root,
target_root: block.target_root,
state_root: block.state_root,
parent: block
.parent_root
.and_then(|parent| self.indices.get(&parent).copied()),
justified_epoch: block.justified_epoch,
finalized_epoch: block.finalized_epoch,
weight: 0,
best_child: None,
best_descendant: None,
};
self.indices.insert(node.root, node_index);
self.nodes.push(node.clone());
if let Some(parent_index) = node.parent {
self.maybe_update_best_child_and_descendant(parent_index, node_index)?;
}
Ok(())
}
/// Follows the best-descendant links to find the best-block (i.e., head-block).
///
/// ## Notes
///
/// The result of this function is not guaranteed to be accurate if `Self::on_new_block` has
/// been called without a subsequent `Self::apply_score_changes` call. This is because
/// `on_new_block` does not attempt to walk backwards through the tree and update the
/// best-child/best-descendant links.
pub fn find_head(&self, justified_root: &Hash256) -> Result<Hash256, Error> {
let justified_index = self
.indices
.get(justified_root)
.copied()
.ok_or_else(|| Error::JustifiedNodeUnknown(*justified_root))?;
let justified_node = self
.nodes
.get(justified_index)
.ok_or_else(|| Error::InvalidJustifiedIndex(justified_index))?;
let best_descendant_index = justified_node
.best_descendant
.unwrap_or_else(|| justified_index);
let best_node = self
.nodes
.get(best_descendant_index)
.ok_or_else(|| Error::InvalidBestDescendant(best_descendant_index))?;
// Perform a sanity check that the node is indeed valid to be the head.
if !self.node_is_viable_for_head(&best_node) {
return Err(Error::InvalidBestNode {
start_root: *justified_root,
justified_epoch: self.justified_epoch,
finalized_epoch: self.finalized_epoch,
head_root: justified_node.root,
head_justified_epoch: justified_node.justified_epoch,
head_finalized_epoch: justified_node.finalized_epoch,
});
}
Ok(best_node.root)
}
/// Update the tree with new finalization information. The tree is only actually pruned if both
/// of the two following criteria are met:
///
/// - The supplied finalized epoch and root are different to the current values.
/// - The number of nodes in `self` is at least `self.prune_threshold`.
///
/// # Errors
///
/// Returns errors if:
///
/// - The finalized epoch is less than the current one.
/// - The finalized epoch is equal to the current one, but the finalized root is different.
/// - There is some internal error relating to invalid indices inside `self`.
pub fn maybe_prune(&mut self, finalized_root: Hash256) -> Result<(), Error> {
let finalized_index = *self
.indices
.get(&finalized_root)
.ok_or_else(|| Error::FinalizedNodeUnknown(finalized_root))?;
if finalized_index < self.prune_threshold {
// Pruning at small numbers incurs more cost than benefit.
return Ok(());
}
// Remove the `self.indices` key/values for all the to-be-deleted nodes.
for node_index in 0..finalized_index {
let root = &self
.nodes
.get(node_index)
.ok_or_else(|| Error::InvalidNodeIndex(node_index))?
.root;
self.indices.remove(root);
}
// Drop all the nodes prior to finalization.
self.nodes = self.nodes.split_off(finalized_index);
// Adjust the indices map.
for (_root, index) in self.indices.iter_mut() {
*index = index
.checked_sub(finalized_index)
.ok_or_else(|| Error::IndexOverflow("indices"))?;
}
// Iterate through all the existing nodes and adjust their indices to match the new layout
// of `self.nodes`.
for node in self.nodes.iter_mut() {
if let Some(parent) = node.parent {
// If `node.parent` is less than `finalized_index`, set it to `None`.
node.parent = parent.checked_sub(finalized_index);
}
if let Some(best_child) = node.best_child {
node.best_child = Some(
best_child
.checked_sub(finalized_index)
.ok_or_else(|| Error::IndexOverflow("best_child"))?,
);
}
if let Some(best_descendant) = node.best_descendant {
node.best_descendant = Some(
best_descendant
.checked_sub(finalized_index)
.ok_or_else(|| Error::IndexOverflow("best_descendant"))?,
);
}
}
Ok(())
}
/// Observe the parent at `parent_index` with respect to the child at `child_index` and
/// potentially modify the `parent.best_child` and `parent.best_descendant` values.
///
/// ## Detail
///
/// There are four outcomes:
///
/// - The child is already the best child but it's now invalid due to a FFG change and should be removed.
/// - The child is already the best child and the parent is updated with the new
/// best-descendant.
/// - The child is not the best child but becomes the best child.
/// - The child is not the best child and does not become the best child.
fn maybe_update_best_child_and_descendant(
&mut self,
parent_index: usize,
child_index: usize,
) -> Result<(), Error> {
let child = self
.nodes
.get(child_index)
.ok_or_else(|| Error::InvalidNodeIndex(child_index))?;
let parent = self
.nodes
.get(parent_index)
.ok_or_else(|| Error::InvalidNodeIndex(parent_index))?;
let child_leads_to_viable_head = self.node_leads_to_viable_head(&child)?;
// These three variables are aliases to the three options that we may set the
// `parent.best_child` and `parent.best_descendant` to.
//
// I use the aliases to assist readability.
let change_to_none = (None, None);
let change_to_child = (
Some(child_index),
child.best_descendant.or(Some(child_index)),
);
let no_change = (parent.best_child, parent.best_descendant);
let (new_best_child, new_best_descendant) =
if let Some(best_child_index) = parent.best_child {
if best_child_index == child_index && !child_leads_to_viable_head {
// If the child is already the best-child of the parent but it's not viable for
// the head, remove it.
change_to_none
} else if best_child_index == child_index {
// If the child is the best-child already, set it again to ensure that the
// best-descendant of the parent is updated.
change_to_child
} else {
let best_child = self
.nodes
.get(best_child_index)
.ok_or_else(|| Error::InvalidBestDescendant(best_child_index))?;
let best_child_leads_to_viable_head =
self.node_leads_to_viable_head(&best_child)?;
if child_leads_to_viable_head && !best_child_leads_to_viable_head {
// The child leads to a viable head, but the current best-child doesn't.
change_to_child
} else if !child_leads_to_viable_head && best_child_leads_to_viable_head {
// The best child leads to a viable head, but the child doesn't.
no_change
} else if child.weight == best_child.weight {
// Tie-breaker of equal weights by root.
if child.root >= best_child.root {
change_to_child
} else {
no_change
}
} else {
// Choose the winner by weight.
if child.weight >= best_child.weight {
change_to_child
} else {
no_change
}
}
}
} else {
if child_leads_to_viable_head {
// There is no current best-child and the child is viable.
change_to_child
} else {
// There is no current best-child but the child is not viable.
no_change
}
};
let parent = self
.nodes
.get_mut(parent_index)
.ok_or_else(|| Error::InvalidNodeIndex(parent_index))?;
parent.best_child = new_best_child;
parent.best_descendant = new_best_descendant;
Ok(())
}
/// Indicates if the node itself is viable for the head, or if it's best descendant is viable
/// for the head.
fn node_leads_to_viable_head(&self, node: &ProtoNode) -> Result<bool, Error> {
let best_descendant_is_viable_for_head =
if let Some(best_descendant_index) = node.best_descendant {
let best_descendant = self
.nodes
.get(best_descendant_index)
.ok_or_else(|| Error::InvalidBestDescendant(best_descendant_index))?;
self.node_is_viable_for_head(best_descendant)
} else {
false
};
Ok(best_descendant_is_viable_for_head || self.node_is_viable_for_head(node))
}
/// This is the equivalent to the `filter_block_tree` function in the eth2 spec:
///
/// https://github.com/ethereum/eth2.0-specs/blob/v0.10.0/specs/phase0/fork-choice.md#filter_block_tree
///
/// Any node that has a different finalized or justified epoch should not be viable for the
/// head.
fn node_is_viable_for_head(&self, node: &ProtoNode) -> bool {
(node.justified_epoch == self.justified_epoch || self.justified_epoch == Epoch::new(0))
&& (node.finalized_epoch == self.finalized_epoch
|| self.finalized_epoch == Epoch::new(0))
}
/// Return a reverse iterator over the nodes which comprise the chain ending at `block_root`.
pub fn iter_nodes<'a>(&'a self, block_root: &Hash256) -> Iter<'a> {
let next_node_index = self.indices.get(block_root).copied();
Iter {
next_node_index,
proto_array: self,
}
}
/// Return a reverse iterator over the block roots of the chain ending at `block_root`.
///
/// Note that unlike many other iterators, this one WILL NOT yield anything at skipped slots.
pub fn iter_block_roots<'a>(
&'a self,
block_root: &Hash256,
) -> impl Iterator<Item = (Hash256, Slot)> + 'a {
self.iter_nodes(block_root)
.map(|node| (node.root, node.slot))
}
}
/// Reverse iterator over one path through a `ProtoArray`.
pub struct Iter<'a> {
next_node_index: Option<usize>,
proto_array: &'a ProtoArray,
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a ProtoNode;
fn next(&mut self) -> Option<Self::Item> {
let next_node_index = self.next_node_index?;
let node = self.proto_array.nodes.get(next_node_index)?;
self.next_node_index = node.parent;
Some(node)
}
}

View File

@@ -0,0 +1,703 @@
use crate::error::Error;
use crate::proto_array::ProtoArray;
use crate::ssz_container::SszContainer;
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use types::{Epoch, Hash256, Slot};
pub const DEFAULT_PRUNE_THRESHOLD: usize = 256;
#[derive(Default, PartialEq, Clone, Encode, Decode)]
pub struct VoteTracker {
current_root: Hash256,
next_root: Hash256,
next_epoch: Epoch,
}
/// A block that is to be applied to the fork choice.
///
/// A simplified version of `types::BeaconBlock`.
pub struct Block {
pub slot: Slot,
pub root: Hash256,
pub parent_root: Option<Hash256>,
pub state_root: Hash256,
pub target_root: Hash256,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
}
/// A Vec-wrapper which will grow to match any request.
///
/// E.g., a `get` or `insert` to an out-of-bounds element will cause the Vec to grow (using
/// Default) to the smallest size required to fulfill the request.
#[derive(Default, Clone, Debug, PartialEq)]
pub struct ElasticList<T>(pub Vec<T>);
impl<T> ElasticList<T>
where
T: Default,
{
fn ensure(&mut self, i: usize) {
if self.0.len() <= i {
self.0.resize_with(i + 1, Default::default);
}
}
pub fn get_mut(&mut self, i: usize) -> &mut T {
self.ensure(i);
&mut self.0[i]
}
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.0.iter_mut()
}
}
#[derive(PartialEq)]
pub struct ProtoArrayForkChoice {
pub(crate) proto_array: ProtoArray,
pub(crate) votes: ElasticList<VoteTracker>,
pub(crate) balances: Vec<u64>,
}
impl ProtoArrayForkChoice {
pub fn new(
finalized_block_slot: Slot,
finalized_block_state_root: Hash256,
justified_epoch: Epoch,
finalized_epoch: Epoch,
finalized_root: Hash256,
) -> Result<Self, String> {
let mut proto_array = ProtoArray {
prune_threshold: DEFAULT_PRUNE_THRESHOLD,
justified_epoch,
finalized_epoch,
nodes: Vec::with_capacity(1),
indices: HashMap::with_capacity(1),
};
let block = Block {
slot: finalized_block_slot,
root: finalized_root,
parent_root: None,
state_root: finalized_block_state_root,
// We are using the finalized_root as the target_root, since it always lies on an
// epoch boundary.
target_root: finalized_root,
justified_epoch,
finalized_epoch,
};
proto_array
.on_block(block)
.map_err(|e| format!("Failed to add finalized block to proto_array: {:?}", e))?;
Ok(Self {
proto_array: proto_array,
votes: ElasticList::default(),
balances: vec![],
})
}
pub fn process_attestation(
&mut self,
validator_index: usize,
block_root: Hash256,
target_epoch: Epoch,
) -> Result<(), String> {
let vote = self.votes.get_mut(validator_index);
if target_epoch > vote.next_epoch || *vote == VoteTracker::default() {
vote.next_root = block_root;
vote.next_epoch = target_epoch;
}
Ok(())
}
pub fn process_block(&mut self, block: Block) -> Result<(), String> {
if block.parent_root.is_none() {
return Err("Missing parent root".to_string());
}
self.proto_array
.on_block(block)
.map_err(|e| format!("process_block_error: {:?}", e))
}
pub fn find_head(
&mut self,
justified_epoch: Epoch,
justified_root: Hash256,
finalized_epoch: Epoch,
justified_state_balances: &[u64],
) -> Result<Hash256, String> {
let old_balances = &mut self.balances;
let new_balances = justified_state_balances;
let deltas = compute_deltas(
&self.proto_array.indices,
&mut self.votes,
&old_balances,
&new_balances,
)
.map_err(|e| format!("find_head compute_deltas failed: {:?}", e))?;
self.proto_array
.apply_score_changes(deltas, justified_epoch, finalized_epoch)
.map_err(|e| format!("find_head apply_score_changes failed: {:?}", e))?;
*old_balances = new_balances.to_vec();
self.proto_array
.find_head(&justified_root)
.map_err(|e| format!("find_head failed: {:?}", e))
}
pub fn maybe_prune(&mut self, finalized_root: Hash256) -> Result<(), String> {
self.proto_array
.maybe_prune(finalized_root)
.map_err(|e| format!("find_head maybe_prune failed: {:?}", e))
}
pub fn set_prune_threshold(&mut self, prune_threshold: usize) {
self.proto_array.prune_threshold = prune_threshold;
}
pub fn len(&self) -> usize {
self.proto_array.nodes.len()
}
pub fn contains_block(&self, block_root: &Hash256) -> bool {
self.proto_array.indices.contains_key(block_root)
}
pub fn get_block(&self, block_root: &Hash256) -> Option<Block> {
let block_index = self.proto_array.indices.get(block_root)?;
let block = self.proto_array.nodes.get(*block_index)?;
let parent_root = block
.parent
.and_then(|i| self.proto_array.nodes.get(i))
.map(|parent| parent.root);
Some(Block {
slot: block.slot,
root: block.root,
parent_root,
state_root: block.state_root,
target_root: block.target_root,
justified_epoch: block.justified_epoch,
finalized_epoch: block.finalized_epoch,
})
}
pub fn latest_message(&self, validator_index: usize) -> Option<(Hash256, Epoch)> {
if validator_index < self.votes.0.len() {
let vote = &self.votes.0[validator_index];
if *vote == VoteTracker::default() {
None
} else {
Some((vote.next_root, vote.next_epoch))
}
} else {
None
}
}
pub fn as_bytes(&self) -> Vec<u8> {
SszContainer::from(self).as_ssz_bytes()
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, String> {
SszContainer::from_ssz_bytes(bytes)
.map(Into::into)
.map_err(|e| format!("Failed to decode ProtoArrayForkChoice: {:?}", e))
}
/// Returns a read-lock to core `ProtoArray` struct.
///
/// Should only be used when encoding/decoding during troubleshooting.
pub fn core_proto_array(&self) -> &ProtoArray {
&self.proto_array
}
}
/// Returns a list of `deltas`, where there is one delta for each of the indices in
/// `0..indices.len()`.
///
/// The deltas are formed by a change between `old_balances` and `new_balances`, and/or a change of vote in `votes`.
///
/// ## Errors
///
/// - If a value in `indices` is greater to or equal to `indices.len()`.
/// - If some `Hash256` in `votes` is not a key in `indices` (except for `Hash256::zero()`, this is
/// always valid).
fn compute_deltas(
indices: &HashMap<Hash256, usize>,
votes: &mut ElasticList<VoteTracker>,
old_balances: &[u64],
new_balances: &[u64],
) -> Result<Vec<i64>, Error> {
let mut deltas = vec![0_i64; indices.len()];
for (val_index, vote) in votes.iter_mut().enumerate() {
// There is no need to create a score change if the validator has never voted or both their
// votes are for the zero hash (alias to the genesis block).
if vote.current_root == Hash256::zero() && vote.next_root == Hash256::zero() {
continue;
}
// If the validator was not included in the _old_ balances (i.e., it did not exist yet)
// then say its balance was zero.
let old_balance = old_balances.get(val_index).copied().unwrap_or_else(|| 0);
// If the validators vote is not known in the _new_ balances, then use a balance of zero.
//
// It is possible that there is a vote for an unknown validator if we change our justified
// state to a new state with a higher epoch that is on a different fork because that fork may have
// on-boarded less validators than the prior fork.
let new_balance = new_balances.get(val_index).copied().unwrap_or_else(|| 0);
if vote.current_root != vote.next_root || old_balance != new_balance {
// We ignore the vote if it is not known in `indices`. We assume that it is outside
// of our tree (i.e., pre-finalization) and therefore not interesting.
if let Some(current_delta_index) = indices.get(&vote.current_root).copied() {
let delta = deltas
.get(current_delta_index)
.ok_or_else(|| Error::InvalidNodeDelta(current_delta_index))?
.checked_sub(old_balance as i64)
.ok_or_else(|| Error::DeltaOverflow(current_delta_index))?;
// Array access safe due to check on previous line.
deltas[current_delta_index] = delta;
}
// We ignore the vote if it is not known in `indices`. We assume that it is outside
// of our tree (i.e., pre-finalization) and therefore not interesting.
if let Some(next_delta_index) = indices.get(&vote.next_root).copied() {
let delta = deltas
.get(next_delta_index)
.ok_or_else(|| Error::InvalidNodeDelta(next_delta_index))?
.checked_add(new_balance as i64)
.ok_or_else(|| Error::DeltaOverflow(next_delta_index))?;
// Array access safe due to check on previous line.
deltas[next_delta_index] = delta;
}
vote.current_root = vote.next_root;
}
}
Ok(deltas)
}
#[cfg(test)]
mod test_compute_deltas {
use super::*;
/// Gives a hash that is not the zero hash (unless i is `usize::max_value)`.
fn hash_from_index(i: usize) -> Hash256 {
Hash256::from_low_u64_be(i as u64 + 1)
}
#[test]
fn zero_hash() {
let validator_count: usize = 16;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
let mut old_balances = vec![];
let mut new_balances = vec![];
for i in 0..validator_count {
indices.insert(hash_from_index(i), i);
votes.0.push(VoteTracker {
current_root: Hash256::zero(),
next_root: Hash256::zero(),
next_epoch: Epoch::new(0),
});
old_balances.push(0);
new_balances.push(0);
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(
deltas.len(),
validator_count,
"deltas should have expected length"
);
assert_eq!(
deltas,
vec![0; validator_count],
"deltas should all be zero"
);
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn all_voted_the_same() {
const BALANCE: u64 = 42;
let validator_count: usize = 16;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
let mut old_balances = vec![];
let mut new_balances = vec![];
for i in 0..validator_count {
indices.insert(hash_from_index(i), i);
votes.0.push(VoteTracker {
current_root: Hash256::zero(),
next_root: hash_from_index(0),
next_epoch: Epoch::new(0),
});
old_balances.push(BALANCE);
new_balances.push(BALANCE);
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(
deltas.len(),
validator_count,
"deltas should have expected length"
);
for (i, delta) in deltas.into_iter().enumerate() {
if i == 0 {
assert_eq!(
delta,
BALANCE as i64 * validator_count as i64,
"zero'th root should have a delta"
);
} else {
assert_eq!(delta, 0, "all other deltas should be zero");
}
}
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn different_votes() {
const BALANCE: u64 = 42;
let validator_count: usize = 16;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
let mut old_balances = vec![];
let mut new_balances = vec![];
for i in 0..validator_count {
indices.insert(hash_from_index(i), i);
votes.0.push(VoteTracker {
current_root: Hash256::zero(),
next_root: hash_from_index(i),
next_epoch: Epoch::new(0),
});
old_balances.push(BALANCE);
new_balances.push(BALANCE);
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(
deltas.len(),
validator_count,
"deltas should have expected length"
);
for delta in deltas.into_iter() {
assert_eq!(
delta, BALANCE as i64,
"each root should have the same delta"
);
}
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn moving_votes() {
const BALANCE: u64 = 42;
let validator_count: usize = 16;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
let mut old_balances = vec![];
let mut new_balances = vec![];
for i in 0..validator_count {
indices.insert(hash_from_index(i), i);
votes.0.push(VoteTracker {
current_root: hash_from_index(0),
next_root: hash_from_index(1),
next_epoch: Epoch::new(0),
});
old_balances.push(BALANCE);
new_balances.push(BALANCE);
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(
deltas.len(),
validator_count,
"deltas should have expected length"
);
let total_delta = BALANCE as i64 * validator_count as i64;
for (i, delta) in deltas.into_iter().enumerate() {
if i == 0 {
assert_eq!(
delta,
0 - total_delta,
"zero'th root should have a negative delta"
);
} else if i == 1 {
assert_eq!(delta, total_delta, "first root should have positive delta");
} else {
assert_eq!(delta, 0, "all other deltas should be zero");
}
}
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn move_out_of_tree() {
const BALANCE: u64 = 42;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
// There is only one block.
indices.insert(hash_from_index(1), 0);
// There are two validators.
let old_balances = vec![BALANCE; 2];
let new_balances = vec![BALANCE; 2];
// One validator moves their vote from the block to the zero hash.
votes.0.push(VoteTracker {
current_root: hash_from_index(1),
next_root: Hash256::zero(),
next_epoch: Epoch::new(0),
});
// One validator moves their vote from the block to something outside the tree.
votes.0.push(VoteTracker {
current_root: hash_from_index(1),
next_root: Hash256::from_low_u64_be(1337),
next_epoch: Epoch::new(0),
});
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(deltas.len(), 1, "deltas should have expected length");
assert_eq!(
deltas[0],
0 - BALANCE as i64 * 2,
"the block should have lost both balances"
);
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn changing_balances() {
const OLD_BALANCE: u64 = 42;
const NEW_BALANCE: u64 = OLD_BALANCE * 2;
let validator_count: usize = 16;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
let mut old_balances = vec![];
let mut new_balances = vec![];
for i in 0..validator_count {
indices.insert(hash_from_index(i), i);
votes.0.push(VoteTracker {
current_root: hash_from_index(0),
next_root: hash_from_index(1),
next_epoch: Epoch::new(0),
});
old_balances.push(OLD_BALANCE);
new_balances.push(NEW_BALANCE);
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(
deltas.len(),
validator_count,
"deltas should have expected length"
);
for (i, delta) in deltas.into_iter().enumerate() {
if i == 0 {
assert_eq!(
delta,
0 - OLD_BALANCE as i64 * validator_count as i64,
"zero'th root should have a negative delta"
);
} else if i == 1 {
assert_eq!(
delta,
NEW_BALANCE as i64 * validator_count as i64,
"first root should have positive delta"
);
} else {
assert_eq!(delta, 0, "all other deltas should be zero");
}
}
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn validator_appears() {
const BALANCE: u64 = 42;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
// There are two blocks.
indices.insert(hash_from_index(1), 0);
indices.insert(hash_from_index(2), 1);
// There is only one validator in the old balances.
let old_balances = vec![BALANCE; 1];
// There are two validators in the new balances.
let new_balances = vec![BALANCE; 2];
// Both validator move votes from block 1 to block 2.
for _ in 0..2 {
votes.0.push(VoteTracker {
current_root: hash_from_index(1),
next_root: hash_from_index(2),
next_epoch: Epoch::new(0),
});
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(deltas.len(), 2, "deltas should have expected length");
assert_eq!(
deltas[0],
0 - BALANCE as i64,
"block 1 should have only lost one balance"
);
assert_eq!(
deltas[1],
2 * BALANCE as i64,
"block 2 should have gained two balances"
);
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote shoulds should have been updated"
);
}
}
#[test]
fn validator_disappears() {
const BALANCE: u64 = 42;
let mut indices = HashMap::new();
let mut votes = ElasticList::default();
// There are two blocks.
indices.insert(hash_from_index(1), 0);
indices.insert(hash_from_index(2), 1);
// There are two validators in the old balances.
let old_balances = vec![BALANCE; 2];
// There is only one validator in the new balances.
let new_balances = vec![BALANCE; 1];
// Both validator move votes from block 1 to block 2.
for _ in 0..2 {
votes.0.push(VoteTracker {
current_root: hash_from_index(1),
next_root: hash_from_index(2),
next_epoch: Epoch::new(0),
});
}
let deltas = compute_deltas(&indices, &mut votes, &old_balances, &new_balances)
.expect("should compute deltas");
assert_eq!(deltas.len(), 2, "deltas should have expected length");
assert_eq!(
deltas[0],
0 - BALANCE as i64 * 2,
"block 1 should have lost both balances"
);
assert_eq!(
deltas[1], BALANCE as i64,
"block 2 should have only gained one balance"
);
for vote in votes.0 {
assert_eq!(
vote.current_root, vote.next_root,
"the vote should have been updated"
);
}
}
}

View File

@@ -0,0 +1,53 @@
use crate::{
proto_array::{ProtoArray, ProtoNode},
proto_array_fork_choice::{ElasticList, ProtoArrayForkChoice, VoteTracker},
};
use ssz_derive::{Decode, Encode};
use std::collections::HashMap;
use std::iter::FromIterator;
use types::{Epoch, Hash256};
#[derive(Encode, Decode)]
pub struct SszContainer {
votes: Vec<VoteTracker>,
balances: Vec<u64>,
prune_threshold: usize,
justified_epoch: Epoch,
finalized_epoch: Epoch,
nodes: Vec<ProtoNode>,
indices: Vec<(Hash256, usize)>,
}
impl From<&ProtoArrayForkChoice> for SszContainer {
fn from(from: &ProtoArrayForkChoice) -> Self {
let proto_array = &from.proto_array;
Self {
votes: from.votes.0.clone(),
balances: from.balances.clone(),
prune_threshold: proto_array.prune_threshold,
justified_epoch: proto_array.justified_epoch,
finalized_epoch: proto_array.finalized_epoch,
nodes: proto_array.nodes.clone(),
indices: proto_array.indices.iter().map(|(k, v)| (*k, *v)).collect(),
}
}
}
impl From<SszContainer> for ProtoArrayForkChoice {
fn from(from: SszContainer) -> Self {
let proto_array = ProtoArray {
prune_threshold: from.prune_threshold,
justified_epoch: from.justified_epoch,
finalized_epoch: from.finalized_epoch,
nodes: from.nodes,
indices: HashMap::from_iter(from.indices.into_iter()),
};
Self {
proto_array: proto_array,
votes: ElasticList(from.votes),
balances: from.balances,
}
}
}