Skip to content

Commit

Permalink
Grandpa: save all completed prior rounds (#4203)
Browse files Browse the repository at this point in the history
* Save concluded rounds.

* Doc nit

* Fix cargo
  • Loading branch information
seerscode authored and gavofyork committed Nov 29, 2019
1 parent 6639e9b commit 1ceebb5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions substrate/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 substrate/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

0 comments on commit 1ceebb5

Please sign in to comment.