pub(crate) fn load_persistent<B, H, N, G>(
	backend: &B,
	genesis_hash: H,
	genesis_number: N,
	genesis_authorities: G,
)
	-> ClientResult<PersistentData<H, N>>
	where
		B: AuxStore,
		H: Debug + Decode + Encode + Clone + PartialEq,
		N: Debug + Decode + Encode + Clone + Ord,
		G: FnOnce() -> ClientResult<Vec<(AuthorityId, u64)>>
{
	let version: Option<u32> = load_decode(backend, VERSION_KEY)?;
	let consensus_changes = load_decode(backend, CONSENSUS_CHANGES_KEY)?
		.unwrap_or_else(ConsensusChanges::<H, N>::empty);

	let make_genesis_round = move || RoundState::genesis((genesis_hash, genesis_number));

	match version {
		None => {
			CURRENT_VERSION.using_encoded(|s|
				backend.insert_aux(&[(VERSION_KEY, s)], &[])
			)?;

			if let Some(old_set) = load_decode::<_, V0AuthoritySet<H, N>>(backend, AUTHORITY_SET_KEY)? {
				let new_set: AuthoritySet<H, N> = old_set.into();
				backend.insert_aux(&[(AUTHORITY_SET_KEY, new_set.encode().as_slice())], &[])?;

				let set_state = match load_decode::<_, V0VoterSetState<H, N>>(backend, SET_STATE_KEY)? {
					Some((number, state)) => {
						let set_state = VoterSetState::Live(number, state);
						backend.insert_aux(&[(SET_STATE_KEY, set_state.encode().as_slice())], &[])?;
						set_state
					},
					None => VoterSetState::Live(0, make_genesis_round()),
				};

				return Ok(PersistentData {
					authority_set: new_set.into(),
					consensus_changes: Arc::new(consensus_changes.into()),
					set_state,
				});
			}
		}
		Some(1) => {
			if let Some(set) = load_decode::<_, AuthoritySet<H, N>>(backend, AUTHORITY_SET_KEY)? {
				let set_state = match load_decode::<_, VoterSetState<H, N>>(backend, SET_STATE_KEY)? {
					Some(state) => state,
					None => VoterSetState::Live(0, make_genesis_round()),
				};

				return Ok(PersistentData {
					authority_set: set.into(),
					consensus_changes: Arc::new(consensus_changes.into()),
					set_state,
				});
			}
		}
		Some(other) => return Err(ClientErrorKind::Backend(
			format!("Unsupported GRANDPA DB version: {:?}", other)
		).into()),
	}

	// genesis.
	info!(target: "afg", "Loading GRANDPA authority set \
		from genesis on what appears to be first startup.");

	let genesis_set = AuthoritySet::genesis(genesis_authorities()?);
	let genesis_state = VoterSetState::Live(0, make_genesis_round());
	backend.insert_aux(
		&[
			(AUTHORITY_SET_KEY, genesis_set.encode().as_slice()),
			(SET_STATE_KEY, genesis_state.encode().as_slice()),
		],
		&[],
	)?;

	Ok(PersistentData {
		authority_set: genesis_set.into(),
		set_state: genesis_state,
		consensus_changes: Arc::new(consensus_changes.into()),
	})
}
/// Load or initialize persistent data from backend.
Load or initialize persistent data from backend.
Load or initialize persistent data from backend .
pub(crate) fn load_persistent<B, H, N, G>(
	backend: &B,
	genesis_hash: H,
	genesis_number: N,
	genesis_authorities: G,
)
	-> ClientResult<PersistentData<H, N>>
	where
		B: AuxStore,
		H: Debug + Decode + Encode + Clone + PartialEq,
		N: Debug + Decode + Encode + Clone + Ord,
		G: FnOnce() -> ClientResult<Vec<(AuthorityId, u64)>>
{
	let version: Option<u32> = load_decode(backend, VERSION_KEY)?;
	let consensus_changes = load_decode(backend, CONSENSUS_CHANGES_KEY)?
		.unwrap_or_else(ConsensusChanges::<H, N>::empty);
	let make_genesis_round = move || RoundState::genesis((genesis_hash, genesis_number));
	match version {
		None => {
			CURRENT_VERSION.using_encoded(|s|
				backend.insert_aux(&[(VERSION_KEY, s)], &[])
			)?;
			if let Some(old_set) = load_decode::<_, V0AuthoritySet<H, N>>(backend, AUTHORITY_SET_KEY)? {
				let new_set: AuthoritySet<H, N> = old_set.into();
				backend.insert_aux(&[(AUTHORITY_SET_KEY, new_set.encode().as_slice())], &[])?;
				let set_state = match load_decode::<_, V0VoterSetState<H, N>>(backend, SET_STATE_KEY)? {
					Some((number, state)) => {
						let set_state = VoterSetState::Live(number, state);
						backend.insert_aux(&[(SET_STATE_KEY, set_state.encode().as_slice())], &[])?;
						set_state
					},
					None => VoterSetState::Live(0, make_genesis_round()),
				};
				return Ok(PersistentData {
					authority_set: new_set.into(),
					consensus_changes: Arc::new(consensus_changes.into()),
					set_state,
				});
			}
		}
		Some(1) => {
			if let Some(set) = load_decode::<_, AuthoritySet<H, N>>(backend, AUTHORITY_SET_KEY)? {
				let set_state = match load_decode::<_, VoterSetState<H, N>>(backend, SET_STATE_KEY)? {
					Some(state) => state,
					None => VoterSetState::Live(0, make_genesis_round()),
				};
				return Ok(PersistentData {
					authority_set: set.into(),
					consensus_changes: Arc::new(consensus_changes.into()),
					set_state,
				});
			}
		}
		Some(other) => return Err(ClientErrorKind::Backend(
			format!("Unsupported GRANDPA DB version: {:?}", other)
		).into()),
	}
	// genesis.
	info!(target: "afg", "Loading GRANDPA authority set \
		from genesis on what appears to be first startup.");
	let genesis_set = AuthoritySet::genesis(genesis_authorities()?);
	let genesis_state = VoterSetState::Live(0, make_genesis_round());
	backend.insert_aux(
		&[
			(AUTHORITY_SET_KEY, genesis_set.encode().as_slice()),
			(SET_STATE_KEY, genesis_state.encode().as_slice()),
		],
		&[],
	)?;
	Ok(PersistentData {
		authority_set: genesis_set.into(),
		set_state: genesis_state,
		consensus_changes: Arc::new(consensus_changes.into()),
	})
}
pub ( crate ) fn load_persistent < B , H , N , G > ( backend : & B , genesis_hash : H , genesis_number : N , genesis_authorities : G , ) -> ClientResult < PersistentData < H , N > > where B : AuxStore , H : Debug + Decode + Encode + Clone + PartialEq , N : Debug + Decode + Encode + Clone + Ord , G : FnOnce ( ) -> ClientResult < Vec < ( AuthorityId , u64 ) > > { let version : Option < u32 > = load_decode ( backend , VERSION_KEY ) ? ; let consensus_changes = load_decode ( backend , CONSENSUS_CHANGES_KEY ) ? . unwrap_or_else ( ConsensusChanges :: < H , N > :: empty ) ; let make_genesis_round = move | | RoundState :: genesis ( ( genesis_hash , genesis_number ) ) ; match version { None => { CURRENT_VERSION . using_encoded ( | s | backend . insert_aux ( & [ ( VERSION_KEY , s ) ] , & [ ] ) ) ? ; if let Some ( old_set ) = load_decode :: < _ , V0AuthoritySet < H , N > > ( backend , AUTHORITY_SET_KEY ) ? { let new_set : AuthoritySet < H , N > = old_set . into ( ) ; backend . insert_aux ( & [ ( AUTHORITY_SET_KEY , new_set . encode ( ) . as_slice ( ) ) ] , & [ ] ) ? ; let set_state = match load_decode :: < _ , V0VoterSetState < H , N > > ( backend , SET_STATE_KEY ) ? { Some ( ( number , state ) ) => { let set_state = VoterSetState :: Live ( number , state ) ; backend . insert_aux ( & [ ( SET_STATE_KEY , set_state . encode ( ) . as_slice ( ) ) ] , & [ ] ) ? ; set_state } , None => VoterSetState :: Live ( 0 , make_genesis_round ( ) ) , } ; return Ok ( PersistentData { authority_set : new_set . into ( ) , consensus_changes : Arc :: new ( consensus_changes . into ( ) ) , set_state , } ) ; } } Some ( 1 ) => { if let Some ( set ) = load_decode :: < _ , AuthoritySet < H , N > > ( backend , AUTHORITY_SET_KEY ) ? { let set_state = match load_decode :: < _ , VoterSetState < H , N > > ( backend , SET_STATE_KEY ) ? { Some ( state ) => state , None => VoterSetState :: Live ( 0 , make_genesis_round ( ) ) , } ; return Ok ( PersistentData { authority_set : set . into ( ) , consensus_changes : Arc :: new ( consensus_changes . into ( ) ) , set_state , } ) ; } } Some ( other ) => return Err ( ClientErrorKind :: Backend ( format ! ( " " other ) ) . into ( ) ) , } // genesis. info ! ( target " " " \
 " ) ; let genesis_set = AuthoritySet :: genesis ( genesis_authorities ( ) ? ) ; let genesis_state = VoterSetState :: Live ( 0 , make_genesis_round ( ) ) ; backend . insert_aux ( & [ ( AUTHORITY_SET_KEY , genesis_set . encode ( ) . as_slice ( ) ) , ( SET_STATE_KEY , genesis_state . encode ( ) . as_slice ( ) ) , ] , & [ ] , ) ? ; Ok ( PersistentData { authority_set : genesis_set . into ( ) , set_state : genesis_state , consensus_changes : Arc :: new ( consensus_changes . into ( ) ) , } ) }
Load or initialize persistent data from backend.
Load or initialize persistent data from backend .
backend
&B
genesis_hash
H
genesis_number
N
genesis_authorities
G
backend
&B
genesis_hash
H
genesis_number
N
genesis_authorities
G