Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Prev Previous commit
Next Next commit
sc-consensus-beefy: refactor persistent state
Move best-beefy and best-grandpa into VoterOracle instead
of passing them around as params.
VoterOracle ultimately needs to know best-beefy and/or best-grandpa
for most of its functions.
  • Loading branch information
acatangiu committed Mar 15, 2023
commit 17dfb4bc630263e6521d2f42f827f35280a659a1
6 changes: 3 additions & 3 deletions client/consensus/beefy/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sp_runtime::traits::Block as BlockT;
const VERSION_KEY: &[u8] = b"beefy_auxschema_version";
const WORKER_STATE_KEY: &[u8] = b"beefy_voter_state";

const CURRENT_VERSION: u32 = 2;
const CURRENT_VERSION: u32 = 3;

pub(crate) fn write_current_version<BE: AuxStore>(backend: &BE) -> ClientResult<()> {
info!(target: LOG_TARGET, "🥩 write aux schema version {:?}", CURRENT_VERSION);
Expand Down Expand Up @@ -63,8 +63,8 @@ where

match version {
None => (),
Some(1) => (), // version 1 is totally obsolete and should be simply ignored
Some(2) => return load_decode::<_, PersistedState<B>>(backend, WORKER_STATE_KEY),
Some(1) | Some(2) => (), // versions 1 & 2 are obsolete and should be simply ignored
Some(3) => return load_decode::<_, PersistedState<B>>(backend, WORKER_STATE_KEY),
other =>
return Err(ClientError::Backend(format!("Unsupported BEEFY DB version: {:?}", other))),
}
Expand Down
36 changes: 8 additions & 28 deletions client/consensus/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1017,13 +1017,8 @@ async fn should_initialize_voter_at_genesis() {

// verify next vote target is mandatory block 1
assert_eq!(persisted_state.best_beefy_block(), 0);
assert_eq!(persisted_state.best_grandpa_block(), 13);
assert_eq!(
persisted_state
.voting_oracle()
.voting_target(persisted_state.best_beefy_block(), 13),
Some(1)
);
assert_eq!(persisted_state.best_grandpa_number(), 13);
assert_eq!(persisted_state.voting_oracle().voting_target(), Some(1));

// verify state also saved to db
assert!(verify_persisted_version(&*backend));
Expand Down Expand Up @@ -1065,13 +1060,8 @@ async fn should_initialize_voter_at_custom_genesis() {

// verify next vote target is mandatory block 7
assert_eq!(persisted_state.best_beefy_block(), 0);
assert_eq!(persisted_state.best_grandpa_block(), 8);
assert_eq!(
persisted_state
.voting_oracle()
.voting_target(persisted_state.best_beefy_block(), 13),
Some(custom_pallet_genesis)
);
assert_eq!(persisted_state.best_grandpa_number(), 8);
assert_eq!(persisted_state.voting_oracle().voting_target(), Some(custom_pallet_genesis));

// verify state also saved to db
assert!(verify_persisted_version(&*backend));
Expand Down Expand Up @@ -1123,14 +1113,9 @@ async fn should_initialize_voter_when_last_final_is_session_boundary() {

// verify block 10 is correctly marked as finalized
assert_eq!(persisted_state.best_beefy_block(), 10);
assert_eq!(persisted_state.best_grandpa_block(), 13);
assert_eq!(persisted_state.best_grandpa_number(), 13);
// verify next vote target is diff-power-of-two block 12
assert_eq!(
persisted_state
.voting_oracle()
.voting_target(persisted_state.best_beefy_block(), 13),
Some(12)
);
assert_eq!(persisted_state.voting_oracle().voting_target(), Some(12));

// verify state also saved to db
assert!(verify_persisted_version(&*backend));
Expand Down Expand Up @@ -1181,13 +1166,8 @@ async fn should_initialize_voter_at_latest_finalized() {

// verify next vote target is 13
assert_eq!(persisted_state.best_beefy_block(), 12);
assert_eq!(persisted_state.best_grandpa_block(), 13);
assert_eq!(
persisted_state
.voting_oracle()
.voting_target(persisted_state.best_beefy_block(), 13),
Some(13)
);
assert_eq!(persisted_state.best_grandpa_number(), 13);
assert_eq!(persisted_state.voting_oracle().voting_target(), Some(13));

// verify state also saved to db
assert!(verify_persisted_version(&*backend));
Expand Down
Loading