Skip to content

Commit

Permalink
[BEEFY] Avoid missing voting sessions during node restart (paritytech…
Browse files Browse the repository at this point in the history
…#3074)

Related to paritytech#3003 and
paritytech#2842

---------

Co-authored-by: Adrian Catangiu <adrian@parity.io>
  • Loading branch information
serban300 and acatangiu authored Feb 1, 2024
1 parent a38dbfe commit a262da4
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 278 deletions.
20 changes: 10 additions & 10 deletions substrate/client/consensus/beefy/src/aux_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,44 @@

//! Schema for BEEFY state persisted in the aux-db.

use crate::{worker::PersistedState, LOG_TARGET};
use crate::{error::Error, worker::PersistedState, LOG_TARGET};
use codec::{Decode, Encode};
use log::{info, trace};
use sc_client_api::{backend::AuxStore, Backend};
use sp_blockchain::{Error as ClientError, Result as ClientResult};
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 = 4;

pub(crate) fn write_current_version<BE: AuxStore>(backend: &BE) -> ClientResult<()> {
pub(crate) fn write_current_version<BE: AuxStore>(backend: &BE) -> Result<(), Error> {
info!(target: LOG_TARGET, "🥩 write aux schema version {:?}", CURRENT_VERSION);
AuxStore::insert_aux(backend, &[(VERSION_KEY, CURRENT_VERSION.encode().as_slice())], &[])
.map_err(|e| Error::Backend(e.to_string()))
}

/// Write voter state.
pub(crate) fn write_voter_state<B: BlockT, BE: AuxStore>(
backend: &BE,
state: &PersistedState<B>,
) -> ClientResult<()> {
) -> Result<(), Error> {
trace!(target: LOG_TARGET, "🥩 persisting {:?}", state);
AuxStore::insert_aux(backend, &[(WORKER_STATE_KEY, state.encode().as_slice())], &[])
.map_err(|e| Error::Backend(e.to_string()))
}

fn load_decode<BE: AuxStore, T: Decode>(backend: &BE, key: &[u8]) -> ClientResult<Option<T>> {
match backend.get_aux(key)? {
fn load_decode<BE: AuxStore, T: Decode>(backend: &BE, key: &[u8]) -> Result<Option<T>, Error> {
match backend.get_aux(key).map_err(|e| Error::Backend(e.to_string()))? {
None => Ok(None),
Some(t) => T::decode(&mut &t[..])
.map_err(|e| ClientError::Backend(format!("BEEFY DB is corrupted: {}", e)))
.map_err(|e| Error::Backend(format!("BEEFY DB is corrupted: {}", e)))
.map(Some),
}
}

/// Load or initialize persistent data from backend.
pub(crate) fn load_persistent<B, BE>(backend: &BE) -> ClientResult<Option<PersistedState<B>>>
pub(crate) fn load_persistent<B, BE>(backend: &BE) -> Result<Option<PersistedState<B>>, Error>
where
B: BlockT,
BE: Backend<B>,
Expand All @@ -65,8 +66,7 @@ where
None => (),
Some(1) | Some(2) | Some(3) => (), // versions 1, 2 & 3 are obsolete and should be ignored
Some(4) => return load_decode::<_, PersistedState<B>>(backend, WORKER_STATE_KEY),
other =>
return Err(ClientError::Backend(format!("Unsupported BEEFY DB version: {:?}", other))),
other => return Err(Error::Backend(format!("Unsupported BEEFY DB version: {:?}", other))),
}

// No persistent state found in DB.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ where
let peer = request.peer;
match self.handle_request(request) {
Ok(()) => {
metric_inc!(self, beefy_successful_justification_responses);
metric_inc!(self.metrics, beefy_successful_justification_responses);
debug!(
target: BEEFY_SYNC_LOG_TARGET,
"🥩 Handled BEEFY justification request from {:?}.", peer
)
},
Err(e) => {
// peer reputation changes already applied in `self.handle_request()`
metric_inc!(self, beefy_failed_justification_responses);
metric_inc!(self.metrics, beefy_failed_justification_responses);
debug!(
target: BEEFY_SYNC_LOG_TARGET,
"🥩 Failed to handle BEEFY justification request from {:?}: {}", peer, e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
if let Some(peer) = self.try_next_peer() {
self.request_from_peer(peer, RequestInfo { block, active_set });
} else {
metric_inc!(self, beefy_on_demand_justification_no_peer_to_request_from);
metric_inc!(self.metrics, beefy_on_demand_justification_no_peer_to_request_from);
debug!(
target: BEEFY_SYNC_LOG_TARGET,
"🥩 no good peers to request justif #{:?} from", block
Expand Down Expand Up @@ -194,13 +194,13 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
);
match e {
RequestFailure::Refused => {
metric_inc!(self, beefy_on_demand_justification_peer_refused);
metric_inc!(self.metrics, beefy_on_demand_justification_peer_refused);
let peer_report =
PeerReport { who: *peer, cost_benefit: cost::REFUSAL_RESPONSE };
Error::InvalidResponse(peer_report)
},
_ => {
metric_inc!(self, beefy_on_demand_justification_peer_error);
metric_inc!(self.metrics, beefy_on_demand_justification_peer_error);
Error::ResponseError
},
}
Expand All @@ -212,7 +212,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
&req_info.active_set,
)
.map_err(|(err, signatures_checked)| {
metric_inc!(self, beefy_on_demand_justification_invalid_proof);
metric_inc!(self.metrics, beefy_on_demand_justification_invalid_proof);
debug!(
target: BEEFY_SYNC_LOG_TARGET,
"🥩 for on demand justification #{:?}, peer {:?} responded with invalid proof: {:?}",
Expand Down Expand Up @@ -261,7 +261,7 @@ impl<B: Block> OnDemandJustificationsEngine<B> {
}
},
Ok(proof) => {
metric_inc!(self, beefy_on_demand_justification_good_proof);
metric_inc!(self.metrics, beefy_on_demand_justification_good_proof);
debug!(
target: BEEFY_SYNC_LOG_TARGET,
"🥩 received valid on-demand justif #{:?} from {:?}", block, peer
Expand Down
4 changes: 2 additions & 2 deletions substrate/client/consensus/beefy/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
self.justification_sender
.notify(|| Ok::<_, ()>(proof))
.expect("the closure always returns Ok; qed.");
metric_inc!(self, beefy_good_justification_imports);
metric_inc!(self.metrics, beefy_good_justification_imports);
},
Err(err) => {
debug!(
Expand All @@ -174,7 +174,7 @@ where
number,
err,
);
metric_inc!(self, beefy_bad_justification_imports);
metric_inc!(self.metrics, beefy_bad_justification_imports);
},
}
},
Expand Down
Loading

0 comments on commit a262da4

Please sign in to comment.