Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions client/finality-grandpa/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::NewAuthoritySet;

const VERSION_KEY: &[u8] = b"grandpa_schema_version";
const SET_STATE_KEY: &[u8] = b"grandpa_completed_round";
const CONCLUDED_ROUNDS: &[u8] = b"grandpa_concluded_rounds";
const AUTHORITY_SET_KEY: &[u8] = b"grandpa_voters";
const CONSENSUS_CHANGES_KEY: &[u8] = b"grandpa_consensus_changes";

Expand Down Expand Up @@ -405,6 +406,18 @@ pub(crate) fn write_voter_set_state<Block: BlockT, B: AuxStore>(
)
}

/// Write concluded round.
pub(crate) fn write_concluded_round<Block: BlockT, B: AuxStore>(
backend: &B,
round_data: &CompletedRound<Block>,
) -> ClientResult<()> {
let mut key = CONCLUDED_ROUNDS.to_vec();
let round_number = round_data.number;
round_number.using_encoded(|n| key.extend(n));

backend.insert_aux(&[(&key[..], round_data.encode().as_slice())], &[])
}

/// Update the consensus changes.
pub(crate) fn update_consensus_changes<H, N, F, R>(
set: &ConsensusChanges<H, N>,
Expand Down Expand Up @@ -608,4 +621,29 @@ mod test {
},
);
}

#[test]
fn write_read_concluded_rounds() {
let client = test_client::new();
let hash = H256::random();
let round_state = RoundState::genesis((hash, 0));

let completed_round = CompletedRound::<test_client::runtime::Block> {
number: 42,
state: round_state.clone(),
base: round_state.prevote_ghost.unwrap(),
votes: vec![],
};

assert!(write_concluded_round(&client, &completed_round).is_ok());

let round_number = completed_round.number;
let mut key = CONCLUDED_ROUNDS.to_vec();
round_number.using_encoded(|n| key.extend(n));

assert_eq!(
load_decode::<_, CompletedRound::<test_client::runtime::Block>>(&client, &key).unwrap(),
Some(completed_round),
);
}
}
1 change: 1 addition & 0 deletions client/finality-grandpa/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ where
historical_votes.seen().iter().skip(n_existing_votes).cloned()
);
already_completed.state = state;
crate::aux_schema::write_concluded_round(&*self.client, &already_completed);
}

let set_state = VoterSetState::<Block>::Live {
Expand Down