Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BEEFY: Define basic fisherman #4328

Merged
merged 3 commits into from
Apr 30, 2024
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
2 changes: 1 addition & 1 deletion polkadot/node/service/src/fake_runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ sp_api::impl_runtime_apis! {
}

fn submit_report_equivocation_unsigned_extrinsic(
_: beefy_primitives::EquivocationProof<
_: beefy_primitives::DoubleVotingProof<
BlockNumber,
BeefyId,
BeefySignature,
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,7 @@ sp_api::impl_runtime_apis! {
}

fn submit_report_equivocation_unsigned_extrinsic(
equivocation_proof: beefy_primitives::EquivocationProof<
equivocation_proof: beefy_primitives::DoubleVotingProof<
BlockNumber,
BeefyId,
BeefySignature,
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ sp_api::impl_runtime_apis! {
}

fn submit_report_equivocation_unsigned_extrinsic(
_equivocation_proof: beefy_primitives::EquivocationProof<
_equivocation_proof: beefy_primitives::DoubleVotingProof<
BlockNumber,
BeefyId,
BeefySignature,
Expand Down
2 changes: 1 addition & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,7 +1966,7 @@ sp_api::impl_runtime_apis! {
}

fn submit_report_equivocation_unsigned_extrinsic(
equivocation_proof: beefy_primitives::EquivocationProof<
equivocation_proof: beefy_primitives::DoubleVotingProof<
BlockNumber,
BeefyId,
BeefySignature,
Expand Down
2 changes: 1 addition & 1 deletion substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3053,7 +3053,7 @@ impl_runtime_apis! {
}

fn submit_report_equivocation_unsigned_extrinsic(
equivocation_proof: sp_consensus_beefy::EquivocationProof<
equivocation_proof: sp_consensus_beefy::DoubleVotingProof<
BlockNumber,
BeefyId,
BeefySignature,
Expand Down
2 changes: 1 addition & 1 deletion substrate/client/consensus/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ sp-consensus-beefy = { path = "../../../primitives/consensus/beefy" }
sp-core = { path = "../../../primitives/core" }
sp-crypto-hashing = { path = "../../../primitives/crypto/hashing" }
sp-keystore = { path = "../../../primitives/keystore" }
sp-mmr-primitives = { path = "../../../primitives/merkle-mountain-range" }
sp-runtime = { path = "../../../primitives/runtime" }
tokio = "1.37"

Expand All @@ -51,6 +50,7 @@ sc-block-builder = { path = "../../block-builder" }
sc-network-test = { path = "../../network/test" }
sp-consensus-grandpa = { path = "../../../primitives/consensus/grandpa" }
sp-keyring = { path = "../../../primitives/keyring" }
sp-mmr-primitives = { path = "../../../primitives/merkle-mountain-range" }
sp-tracing = { path = "../../../primitives/tracing" }
substrate-test-runtime-client = { path = "../../../test-utils/runtime/client" }

Expand Down
162 changes: 162 additions & 0 deletions substrate/client/consensus/beefy/src/fisherman.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{error::Error, keystore::BeefyKeystore, round::Rounds, LOG_TARGET};
use log::{debug, error, warn};
use sc_client_api::Backend;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_consensus_beefy::{
check_equivocation_proof,
ecdsa_crypto::{AuthorityId, Signature},
BeefyApi, BeefySignatureHasher, DoubleVotingProof, OpaqueKeyOwnershipProof, ValidatorSetId,
};
use sp_runtime::{
generic::BlockId,
traits::{Block, NumberFor},
};
use std::{marker::PhantomData, sync::Arc};

/// Helper struct containing the id and the key ownership proof for a validator.
pub struct ProvedValidator<'a> {
pub id: &'a AuthorityId,
pub key_owner_proof: OpaqueKeyOwnershipProof,
}

/// Helper used to check and report equivocations.
pub struct Fisherman<B, BE, RuntimeApi> {
backend: Arc<BE>,
runtime: Arc<RuntimeApi>,
key_store: Arc<BeefyKeystore<AuthorityId>>,

_phantom: PhantomData<B>,
}

impl<B: Block, BE: Backend<B>, RuntimeApi: ProvideRuntimeApi<B>> Fisherman<B, BE, RuntimeApi>
where
RuntimeApi::Api: BeefyApi<B, AuthorityId>,
{
pub fn new(
backend: Arc<BE>,
runtime: Arc<RuntimeApi>,
keystore: Arc<BeefyKeystore<AuthorityId>>,
) -> Self {
Self { backend, runtime, key_store: keystore, _phantom: Default::default() }
}

fn prove_offenders<'a>(
&self,
at: BlockId<B>,
offender_ids: impl Iterator<Item = &'a AuthorityId>,
validator_set_id: ValidatorSetId,
) -> Result<Vec<ProvedValidator<'a>>, Error> {
let hash = match at {
BlockId::Hash(hash) => hash,
BlockId::Number(number) => self
.backend
.blockchain()
.expect_block_hash_from_id(&BlockId::Number(number))
.map_err(|err| {
Error::Backend(format!(
"Couldn't get hash for block #{:?} (error: {:?}). \
Skipping report for equivocation",
at, err
))
})?,
};

let runtime_api = self.runtime.runtime_api();
let mut proved_offenders = vec![];
for offender_id in offender_ids {
match runtime_api.generate_key_ownership_proof(
hash,
validator_set_id,
offender_id.clone(),
) {
Ok(Some(key_owner_proof)) => {
proved_offenders.push(ProvedValidator { id: offender_id, key_owner_proof });
},
Ok(None) => {
debug!(
target: LOG_TARGET,
"🥩 Equivocation offender {} not part of the authority set {}.",
offender_id, validator_set_id
);
},
Err(e) => {
error!(
target: LOG_TARGET,
"🥩 Error generating key ownership proof for equivocation offender {} \
in authority set {}: {}",
offender_id, validator_set_id, e
);
},
};
}

Ok(proved_offenders)
}

/// Report the given equivocation to the BEEFY runtime module. This method
/// generates a session membership proof of the offender and then submits an
/// extrinsic to report the equivocation. In particular, the session membership
/// proof must be generated at the block at which the given set was active which
/// isn't necessarily the best block if there are pending authority set changes.
pub fn report_double_voting(
&self,
proof: DoubleVotingProof<NumberFor<B>, AuthorityId, Signature>,
active_rounds: &Rounds<B>,
) -> Result<(), Error> {
let (validators, validator_set_id) =
(active_rounds.validators(), active_rounds.validator_set_id());
let offender_id = proof.offender_id();

if !check_equivocation_proof::<_, _, BeefySignatureHasher>(&proof) {
debug!(target: LOG_TARGET, "🥩 Skipping report for bad equivocation {:?}", proof);
return Ok(())
}

if let Some(local_id) = self.key_store.authority_id(validators) {
if offender_id == &local_id {
warn!(target: LOG_TARGET, "🥩 Skipping report for own equivocation");
return Ok(())
}
}

let key_owner_proofs = self.prove_offenders(
BlockId::Number(*proof.round_number()),
vec![offender_id].into_iter(),
validator_set_id,
)?;

// submit equivocation report at **best** block
let best_block_hash = self.backend.blockchain().info().best_hash;
for ProvedValidator { key_owner_proof, .. } in key_owner_proofs {
self.runtime
.runtime_api()
.submit_report_equivocation_unsigned_extrinsic(
best_block_hash,
proof.clone(),
key_owner_proof,
)
.map_err(Error::RuntimeApi)?;
}

Ok(())
}
}
19 changes: 11 additions & 8 deletions substrate/client/consensus/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ use sp_api::ProvideRuntimeApi;
use sp_blockchain::{Backend as BlockchainBackend, HeaderBackend};
use sp_consensus::{Error as ConsensusError, SyncOracle};
use sp_consensus_beefy::{
ecdsa_crypto::AuthorityId, BeefyApi, ConsensusLog, MmrRootHash, PayloadProvider, ValidatorSet,
ecdsa_crypto::AuthorityId, BeefyApi, ConsensusLog, PayloadProvider, ValidatorSet,
BEEFY_ENGINE_ID,
};
use sp_keystore::KeystorePtr;
use sp_mmr_primitives::MmrApi;
use sp_runtime::traits::{Block, Header as HeaderT, NumberFor, Zero};
use std::{
collections::{BTreeMap, VecDeque},
Expand All @@ -69,6 +68,7 @@ pub mod justification;

use crate::{
communication::gossip::GossipValidator,
fisherman::Fisherman,
justification::BeefyVersionedFinalityProof,
keystore::BeefyKeystore,
metrics::VoterMetrics,
Expand All @@ -80,6 +80,7 @@ pub use communication::beefy_protocol_name::{
};
use sp_runtime::generic::OpaqueDigestItemId;

mod fisherman;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -305,14 +306,16 @@ where
pending_justifications: BTreeMap<NumberFor<B>, BeefyVersionedFinalityProof<B>>,
is_authority: bool,
) -> BeefyWorker<B, BE, P, R, S, N> {
let key_store = Arc::new(self.key_store);
BeefyWorker {
backend: self.backend,
runtime: self.runtime,
key_store: self.key_store,
metrics: self.metrics,
persisted_state: self.persisted_state,
backend: self.backend.clone(),
runtime: self.runtime.clone(),
key_store: key_store.clone(),
payload_provider,
sync,
fisherman: Arc::new(Fisherman::new(self.backend, self.runtime, key_store)),
metrics: self.metrics,
persisted_state: self.persisted_state,
comms,
links,
pending_justifications,
Expand Down Expand Up @@ -487,7 +490,7 @@ pub async fn start_beefy_gadget<B, BE, C, N, P, R, S>(
C: Client<B, BE> + BlockBackend<B>,
P: PayloadProvider<B> + Clone,
R: ProvideRuntimeApi<B>,
R::Api: BeefyApi<B, AuthorityId> + MmrApi<B, MmrRootHash, NumberFor<B>>,
R::Api: BeefyApi<B, AuthorityId>,
N: GossipNetwork<B> + NetworkRequest + Send + Sync + 'static,
S: GossipSyncing<B> + SyncOracle + 'static,
{
Expand Down
10 changes: 5 additions & 5 deletions substrate/client/consensus/beefy/src/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use codec::{Decode, Encode};
use log::{debug, info};
use sp_consensus_beefy::{
ecdsa_crypto::{AuthorityId, Signature},
Commitment, EquivocationProof, SignedCommitment, ValidatorSet, ValidatorSetId, VoteMessage,
Commitment, DoubleVotingProof, SignedCommitment, ValidatorSet, ValidatorSetId, VoteMessage,
};
use sp_runtime::traits::{Block, NumberFor};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -61,7 +61,7 @@ pub fn threshold(authorities: usize) -> usize {
pub enum VoteImportResult<B: Block> {
Ok,
RoundConcluded(SignedCommitment<NumberFor<B>, Signature>),
Equivocation(EquivocationProof<NumberFor<B>, AuthorityId, Signature>),
DoubleVoting(DoubleVotingProof<NumberFor<B>, AuthorityId, Signature>),
Invalid,
Stale,
}
Expand Down Expand Up @@ -153,7 +153,7 @@ where
target: LOG_TARGET,
"🥩 detected equivocated vote: 1st: {:?}, 2nd: {:?}", previous_vote, vote
);
return VoteImportResult::Equivocation(EquivocationProof {
return VoteImportResult::DoubleVoting(DoubleVotingProof {
first: previous_vote.clone(),
second: vote,
})
Expand Down Expand Up @@ -207,7 +207,7 @@ mod tests {
use sc_network_test::Block;

use sp_consensus_beefy::{
known_payloads::MMR_ROOT_ID, test_utils::Keyring, Commitment, EquivocationProof, Payload,
known_payloads::MMR_ROOT_ID, test_utils::Keyring, Commitment, DoubleVotingProof, Payload,
SignedCommitment, ValidatorSet, VoteMessage,
};

Expand Down Expand Up @@ -494,7 +494,7 @@ mod tests {
let mut alice_vote2 = alice_vote1.clone();
alice_vote2.commitment = commitment2;

let expected_result = VoteImportResult::Equivocation(EquivocationProof {
let expected_result = VoteImportResult::DoubleVoting(DoubleVotingProof {
first: alice_vote1.clone(),
second: alice_vote2.clone(),
});
Expand Down
6 changes: 3 additions & 3 deletions substrate/client/consensus/beefy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use sp_consensus_beefy::{
known_payloads,
mmr::{find_mmr_root_digest, MmrRootProvider},
test_utils::Keyring as BeefyKeyring,
BeefyApi, Commitment, ConsensusLog, EquivocationProof, MmrRootHash, OpaqueKeyOwnershipProof,
BeefyApi, Commitment, ConsensusLog, DoubleVotingProof, MmrRootHash, OpaqueKeyOwnershipProof,
Payload, SignedCommitment, ValidatorSet, ValidatorSetId, VersionedFinalityProof, VoteMessage,
BEEFY_ENGINE_ID,
};
Expand Down Expand Up @@ -259,7 +259,7 @@ pub(crate) struct TestApi {
pub validator_set: Option<BeefyValidatorSet>,
pub mmr_root_hash: MmrRootHash,
pub reported_equivocations:
Option<Arc<Mutex<Vec<EquivocationProof<NumberFor<Block>, AuthorityId, Signature>>>>>,
Option<Arc<Mutex<Vec<DoubleVotingProof<NumberFor<Block>, AuthorityId, Signature>>>>>,
}

impl TestApi {
Expand Down Expand Up @@ -313,7 +313,7 @@ sp_api::mock_impl_runtime_apis! {
}

fn submit_report_equivocation_unsigned_extrinsic(
proof: EquivocationProof<NumberFor<Block>, AuthorityId, Signature>,
proof: DoubleVotingProof<NumberFor<Block>, AuthorityId, Signature>,
_dummy: OpaqueKeyOwnershipProof,
) -> Option<()> {
if let Some(equivocations_buf) = self.inner.reported_equivocations.as_ref() {
Expand Down
Loading
Loading