Skip to content

Commit a6082b3

Browse files
Make some BEEFY keystore logic more generic (#10763)
This PR: 1. makes some BEEFY keystore methods more generic: - `sign()` - `public_keys()` This is done by implementing the specific logic in the `BeefyAuthorityId`. 2. Removes the `BeefyAuthorityId::SignatureHasher` since for some algorithms it doesn't make sense to have a hasher. Also since now the `BeefyAuthorityId` implements both the signing and the verification logic, we should have better consistency. Related to #8707 (comment) --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent f87563a commit a6082b3

File tree

12 files changed

+222
-231
lines changed

12 files changed

+222
-231
lines changed

bridges/modules/beefy/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ pub type BridgedBlockHash<T, I> = bp_runtime::HashOf<BridgedChain<T, I>>;
5858
/// Pallet initialization data.
5959
pub type InitializationDataOf<T, I> =
6060
InitializationData<BridgedBlockNumber<T, I>, bp_beefy::MmrHashOf<BridgedChain<T, I>>>;
61-
/// BEEFY commitment hasher, used by configured bridged chain.
62-
pub type BridgedBeefyCommitmentHasher<T, I> = bp_beefy::BeefyCommitmentHasher<BridgedChain<T, I>>;
6361
/// BEEFY validator id, used by configured bridged chain.
6462
pub type BridgedBeefyAuthorityId<T, I> = bp_beefy::BeefyAuthorityIdOf<BridgedChain<T, I>>;
6563
/// BEEFY validator set, used by configured bridged chain.

bridges/modules/beefy/src/mock.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use crate as beefy;
1818
use crate::{
1919
utils::get_authorities_mmr_root, BridgedBeefyAuthoritySet, BridgedBeefyAuthoritySetInfo,
20-
BridgedBeefyCommitmentHasher, BridgedBeefyMmrLeafExtra, BridgedBeefySignedCommitment,
21-
BridgedMmrHash, BridgedMmrHashing, BridgedMmrProof,
20+
BridgedBeefyMmrLeafExtra, BridgedBeefySignedCommitment, BridgedMmrHash, BridgedMmrHashing,
21+
BridgedMmrProof,
2222
};
2323

2424
use bp_beefy::{BeefyValidatorSignatureOf, ChainWithBeefy, Commitment, MmrDataOrHash};
@@ -33,7 +33,7 @@ use sp_runtime::{
3333
};
3434

3535
pub use sp_consensus_beefy::ecdsa_crypto::{AuthorityId as BeefyId, Pair as BeefyPair};
36-
use sp_core::crypto::Wraps;
36+
use sp_consensus_beefy::test_utils::BeefySignerAuthority;
3737
use sp_runtime::traits::Keccak256;
3838

3939
pub type TestAccountId = u64;
@@ -44,7 +44,6 @@ pub type TestBridgedAuthoritySetInfo = BridgedBeefyAuthoritySetInfo<TestRuntime,
4444
pub type TestBridgedValidatorSet = BridgedBeefyAuthoritySet<TestRuntime, ()>;
4545
pub type TestBridgedCommitment = BridgedBeefySignedCommitment<TestRuntime, ()>;
4646
pub type TestBridgedValidatorSignature = BeefyValidatorSignatureOf<TestBridgedChain>;
47-
pub type TestBridgedCommitmentHasher = BridgedBeefyCommitmentHasher<TestRuntime, ()>;
4847
pub type TestBridgedMmrHashing = BridgedMmrHashing<TestRuntime, ()>;
4948
pub type TestBridgedMmrHash = BridgedMmrHash<TestRuntime, ()>;
5049
pub type TestBridgedBeefyMmrLeafExtra = BridgedBeefyMmrLeafExtra<TestRuntime, ()>;
@@ -105,7 +104,6 @@ impl Chain for TestBridgedChain {
105104
}
106105

107106
impl ChainWithBeefy for TestBridgedChain {
108-
type CommitmentHasher = Keccak256;
109107
type MmrHashing = Keccak256;
110108
type MmrHash = <Keccak256 as Hash>::Output;
111109
type BeefyMmrLeafExtra = ();
@@ -184,12 +182,11 @@ pub fn sign_commitment(
184182
let random_validators =
185183
rand::seq::index::sample(&mut rand::thread_rng(), total_validators, signature_count);
186184

187-
let commitment_hash = TestBridgedCommitmentHasher::hash(&commitment.encode());
188185
let mut signatures = vec![None; total_validators];
189186
for validator_idx in random_validators.iter() {
190187
let validator = &validator_pairs[validator_idx];
191188
signatures[validator_idx] =
192-
Some(validator.as_inner_ref().sign_prehashed(commitment_hash.as_fixed_bytes()).into());
189+
Some(BeefySignerAuthority::sign(validator, &commitment.encode()));
193190
}
194191

195192
TestBridgedCommitment { commitment, signatures }

bridges/primitives/beefy/src/lib.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ use sp_std::prelude::*;
5252
/// primitives. Some of types can be configured in low-level pallets, but are constrained
5353
/// when BEEFY+MMR bundle is used.
5454
pub trait ChainWithBeefy: Chain {
55-
/// The hashing algorithm used to compute the digest of the BEEFY commitment.
56-
///
57-
/// Corresponds to the hashing algorithm, used by `sc_consensus_beefy::BeefyKeystore`.
58-
type CommitmentHasher: sp_runtime::traits::Hash;
59-
6055
/// The hashing algorithm used to build the MMR.
6156
///
6257
/// The same algorithm is also used to compute merkle roots in BEEFY
@@ -84,7 +79,7 @@ pub trait ChainWithBeefy: Chain {
8479
/// A way to identify a BEEFY validator.
8580
///
8681
/// Corresponds to the `BeefyId` field of the `pallet-beefy` configuration.
87-
type AuthorityId: BeefyAuthorityId<Self::CommitmentHasher> + Parameter;
82+
type AuthorityId: BeefyAuthorityId + Parameter;
8883

8984
/// A way to convert validator id to its raw representation in the BEEFY merkle tree.
9085
///
@@ -105,8 +100,6 @@ pub type BeefyValidatorSignatureOf<C> =
105100
/// Signed BEEFY commitment used by given Substrate chain.
106101
pub type BeefySignedCommitmentOf<C> =
107102
SignedCommitment<BlockNumberOf<C>, BeefyValidatorSignatureOf<C>>;
108-
/// Hash algorithm, used to compute the digest of the BEEFY commitment before signing it.
109-
pub type BeefyCommitmentHasher<C> = <C as ChainWithBeefy>::CommitmentHasher;
110103
/// Hash algorithm used in Beefy MMR construction by given Substrate chain.
111104
pub type MmrHashingOf<C> = <C as ChainWithBeefy>::MmrHashing;
112105
/// Hash type, used in MMR construction by given Substrate chain.

prdoc/pr_10763.prdoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
title: Make some BEEFY keystore logic more generic
2+
doc:
3+
- audience: Node Dev
4+
description: |-
5+
This PR:
6+
1. makes some BEEFY keystore methods more generic:
7+
- `sign()`
8+
- `public_keys()`
9+
This is done by implementing the specific logic in the `BeefyAuthorityId`.
10+
2. Removes the `BeefyAuthorityId::SignatureHasher` since for some algorithms it doesn't make sense to have a hasher.
11+
12+
Also since now the `BeefyAuthorityId` implements both the signing and the verification logic, we should have better consistency.
13+
14+
Related to https://github.com/paritytech/polkadot-sdk/pull/8707#discussion_r2673377834
15+
crates:
16+
- name: sp-consensus-beefy
17+
bump: major
18+
- name: sc-consensus-beefy
19+
bump: patch
20+
- name: pallet-beefy
21+
bump: patch

substrate/client/consensus/beefy/src/fisherman.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use sp_api::ProvideRuntimeApi;
2323
use sp_application_crypto::RuntimeAppPublic;
2424
use sp_blockchain::HeaderBackend;
2525
use sp_consensus_beefy::{
26-
check_double_voting_proof, AuthorityIdBound, BeefyApi, BeefySignatureHasher, DoubleVotingProof,
26+
check_double_voting_proof, AuthorityIdBound, BeefyApi, DoubleVotingProof,
2727
OpaqueKeyOwnershipProof, ValidatorSetId,
2828
};
2929
use sp_runtime::{
@@ -131,7 +131,7 @@ where
131131
(active_rounds.validators(), active_rounds.validator_set_id());
132132
let offender_id = proof.offender_id();
133133

134-
if !check_double_voting_proof::<_, _, BeefySignatureHasher>(&proof) {
134+
if !check_double_voting_proof::<_, _>(&proof) {
135135
debug!(target: LOG_TARGET, "🥩 Skipping report for bad equivocation {:?}", proof);
136136
return Ok(());
137137
}

substrate/client/consensus/beefy/src/justification.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ use codec::DecodeAll;
2020
use sp_application_crypto::RuntimeAppPublic;
2121
use sp_consensus::Error as ConsensusError;
2222
use sp_consensus_beefy::{
23-
AuthorityIdBound, BeefySignatureHasher, KnownSignature, ValidatorSet, ValidatorSetId,
24-
VersionedFinalityProof,
23+
AuthorityIdBound, KnownSignature, ValidatorSet, ValidatorSetId, VersionedFinalityProof,
2524
};
2625
use sp_runtime::traits::{Block as BlockT, NumberFor};
2726

@@ -61,11 +60,10 @@ pub(crate) fn verify_with_validator_set<'a, Block: BlockT, AuthorityId: Authorit
6160
> {
6261
match proof {
6362
VersionedFinalityProof::V1(signed_commitment) => {
64-
let signatories = signed_commitment
65-
.verify_signatures::<_, BeefySignatureHasher>(target_number, validator_set)
66-
.map_err(|checked_signatures| {
67-
(ConsensusError::InvalidJustification, checked_signatures)
68-
})?;
63+
let signatories =
64+
signed_commitment.verify_signatures::<_>(target_number, validator_set).map_err(
65+
|checked_signatures| (ConsensusError::InvalidJustification, checked_signatures),
66+
)?;
6967

7068
if signatories.len() >= crate::round::threshold(validator_set.len()) {
7169
Ok(signatories)

0 commit comments

Comments
 (0)