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] Add runtime support for reporting fork voting #4522

Merged
merged 17 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions polkadot/runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,7 @@ impl pallet_beefy::Config for Runtime {
type MaxNominators = ConstU32<0>;
type MaxSetIdSessionEntries = BeefySetIdSessionEntries;
type OnNewValidatorSet = MmrLeaf;
type AncestryHelper = MmrLeaf;
type WeightInfo = ();
type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, BeefyId)>>::Proof;
type EquivocationReportSystem =
Expand Down
1 change: 1 addition & 0 deletions polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ impl pallet_beefy::Config for Runtime {
type MaxNominators = MaxNominators;
type MaxSetIdSessionEntries = BeefySetIdSessionEntries;
type OnNewValidatorSet = BeefyMmrLeaf;
type AncestryHelper = BeefyMmrLeaf;
type WeightInfo = ();
type KeyOwnerProof = sp_session::MembershipProof;
type EquivocationReportSystem =
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,7 @@ impl pallet_beefy::Config for Runtime {
type MaxNominators = ConstU32<0>;
type MaxSetIdSessionEntries = BeefySetIdSessionEntries;
type OnNewValidatorSet = MmrLeaf;
type AncestryHelper = MmrLeaf;
type WeightInfo = ();
type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, BeefyId)>>::Proof;
type EquivocationReportSystem =
Expand Down
49 changes: 47 additions & 2 deletions substrate/frame/beefy-mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ use sp_runtime::traits::{Convert, Member};
use sp_std::prelude::*;

use codec::Decode;
use pallet_mmr::{LeafDataProvider, ParentNumberAndHash};
use pallet_mmr::{primitives::AncestryProof, LeafDataProvider, ParentNumberAndHash};
use sp_consensus_beefy::{
known_payloads,
mmr::{BeefyAuthoritySet, BeefyDataProvider, BeefyNextAuthoritySet, MmrLeaf, MmrLeafVersion},
ValidatorSet as BeefyValidatorSet,
AncestryHelper, Commitment, ValidatorSet as BeefyValidatorSet,
};

use frame_support::{crypto::ecdsa::ECDSAExt, traits::Get};
Expand Down Expand Up @@ -172,6 +173,50 @@ where
}
}

impl<T: Config> AncestryHelper<BlockNumberFor<T>> for Pallet<T> {
type Proof = AncestryProof<MerkleRootOf<T>>;

fn is_non_canonical(commitment: &Commitment<BlockNumberFor<T>>, proof: Self::Proof) -> bool {
let commitment_leaf_count =
match pallet_mmr::Pallet::<T>::block_num_to_leaf_count(commitment.block_number) {
Ok(commitment_leaf_count) => commitment_leaf_count,
Err(_) => {
// We consider the commitment non-canonical if the `commitment.block_number`
serban300 marked this conversation as resolved.
Show resolved Hide resolved
// is invalid.
return true
},
};
if commitment_leaf_count != proof.prev_leaf_count {
// Can't prove that the commitment is non-canonical if the `commitment.block_number`
// doesn't match the ancestry proof.
return false;
}

let canonical_mmr_root = pallet_mmr::Pallet::<T>::mmr_root();
let canonical_prev_root =
match pallet_mmr::Pallet::<T>::verify_ancestry_proof(canonical_mmr_root, proof) {
Ok(canonical_prev_root) => canonical_prev_root,
Err(_) => {
// Can't prove that the commitment is non-canonical if the proof
// is invalid.
return false
},
};

let commitment_root =
match commitment.payload.get_decoded::<MerkleRootOf<T>>(&known_payloads::MMR_ROOT_ID) {
Some(commitment_root) => commitment_root,
None => {
// If the commitment doesn't contain any MMR root, while the proof is valid,
// the commitment is invalid
return true
},
};

canonical_prev_root != commitment_root
}
}

impl<T: Config> Pallet<T> {
/// Return the currently active BEEFY authority set proof.
pub fn authority_set_proof() -> BeefyAuthoritySet<MerkleRootOf<T>> {
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/beefy-mmr/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl pallet_beefy::Config for Test {
type MaxNominators = ConstU32<1000>;
type MaxSetIdSessionEntries = ConstU64<100>;
type OnNewValidatorSet = BeefyMmr;
type AncestryHelper = BeefyMmr;
type WeightInfo = ();
type KeyOwnerProof = sp_core::Void;
type EquivocationReportSystem = ();
Expand Down
162 changes: 143 additions & 19 deletions substrate/frame/beefy-mmr/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ use std::vec;

use codec::{Decode, Encode};
use sp_consensus_beefy::{
known_payloads,
mmr::{BeefyNextAuthoritySet, MmrLeafVersion},
ValidatorSet,
AncestryHelper, Commitment, Payload, ValidatorSet,
};

use sp_core::H256;
use sp_core::{
offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt},
H256,
};
use sp_io::TestExternalities;
use sp_runtime::{traits::Keccak256, DigestItem};

Expand All @@ -32,7 +36,8 @@ use frame_support::traits::OnInitialize;
use crate::mock::*;

fn init_block(block: u64) {
System::set_block_number(block);
let hash = H256::repeat_byte(block as u8);
System::initialize(&block, &hash, &Default::default());
Session::on_initialize(block);
Mmr::on_initialize(block);
Beefy::on_initialize(block);
Expand Down Expand Up @@ -62,37 +67,31 @@ fn should_contain_mmr_digest() {
let mut ext = new_test_ext(vec![1, 2, 3, 4]);
ext.execute_with(|| {
init_block(1);

assert_eq!(
System::digest().logs,
vec![
beefy_log(ConsensusLog::AuthoritiesChange(
ValidatorSet::new(vec![mock_beefy_id(1), mock_beefy_id(2)], 1).unwrap()
)),
beefy_log(ConsensusLog::MmrRoot(array_bytes::hex_n_into_unchecked(
"95803defe6ea9f41e7ec6afa497064f21bfded027d8812efacbdf984e630cbdc"
)))
beefy_log(ConsensusLog::MmrRoot(H256::from_slice(&[
117, 0, 56, 25, 185, 195, 71, 232, 67, 213, 27, 178, 64, 168, 137, 220, 64,
184, 64, 240, 83, 245, 18, 93, 185, 202, 125, 205, 17, 254, 18, 143
])))
]
);

// unique every time
init_block(2);

assert_eq!(
System::digest().logs,
vec![
beefy_log(ConsensusLog::AuthoritiesChange(
ValidatorSet::new(vec![mock_beefy_id(1), mock_beefy_id(2)], 1).unwrap()
)),
beefy_log(ConsensusLog::MmrRoot(array_bytes::hex_n_into_unchecked(
"95803defe6ea9f41e7ec6afa497064f21bfded027d8812efacbdf984e630cbdc"
))),
beefy_log(ConsensusLog::AuthoritiesChange(
ValidatorSet::new(vec![mock_beefy_id(3), mock_beefy_id(4)], 2).unwrap()
)),
beefy_log(ConsensusLog::MmrRoot(array_bytes::hex_n_into_unchecked(
"a73271a0974f1e67d6e9b8dd58e506177a2e556519a330796721e98279a753e2"
))),
beefy_log(ConsensusLog::MmrRoot(H256::from_slice(&[
193, 246, 48, 7, 89, 204, 186, 109, 167, 226, 188, 211, 8, 243, 203, 154, 234,
235, 136, 210, 245, 7, 209, 27, 241, 90, 156, 113, 137, 65, 191, 139
]))),
]
);
});
Expand All @@ -115,7 +114,7 @@ fn should_contain_valid_leaf_data() {
mmr_leaf,
MmrLeaf {
version: MmrLeafVersion::new(1, 5),
parent_number_and_hash: (0_u64, H256::repeat_byte(0x45)),
parent_number_and_hash: (0_u64, H256::repeat_byte(1)),
beefy_next_authority_set: BeefyNextAuthoritySet {
id: 2,
len: 2,
Expand All @@ -140,7 +139,7 @@ fn should_contain_valid_leaf_data() {
mmr_leaf,
MmrLeaf {
version: MmrLeafVersion::new(1, 5),
parent_number_and_hash: (1_u64, H256::repeat_byte(0x45)),
parent_number_and_hash: (1_u64, H256::repeat_byte(2)),
beefy_next_authority_set: BeefyNextAuthoritySet {
id: 3,
len: 2,
Expand Down Expand Up @@ -207,3 +206,128 @@ fn should_update_authorities() {
assert_eq!(want, next_auth_set.keyset_commitment);
});
}

#[test]
fn is_non_canonical_should_work_correctly() {
let mut ext = new_test_ext(vec![1, 2]);

let mut prev_roots = vec![];
ext.execute_with(|| {
for block_num in 1..=500 {
init_block(block_num);
prev_roots.push(Mmr::mmr_root())
}
});
ext.persist_offchain_overlay();

// Register offchain ext.
let (offchain, _offchain_state) = TestOffchainExt::with_offchain_db(ext.offchain_db());
ext.register_extension(OffchainDbExt::new(offchain.clone()));
ext.register_extension(OffchainWorkerExt::new(offchain));

ext.execute_with(|| {
let valid_proof = Mmr::generate_ancestry_proof(250, None).unwrap();
let mut invalid_proof = valid_proof.clone();
invalid_proof.items.push((300, Default::default()));

// The commitment is invalid if it has no MMR root payload and the proof is valid.
assert_eq!(
BeefyMmr::is_non_canonical(
&Commitment {
payload: Payload::from_single_entry([0, 0], vec![]),
block_number: 250,
validator_set_id: 0
},
valid_proof.clone(),
),
true
);

// If the `commitment.payload` contains an MMR root that doesn't match the ancestry proof,
// it's non-canonical.
assert_eq!(
BeefyMmr::is_non_canonical(
&Commitment {
payload: Payload::from_single_entry(
known_payloads::MMR_ROOT_ID,
H256::repeat_byte(0).encode(),
),
block_number: 250,
validator_set_id: 0,
},
valid_proof.clone(),
),
true
);

// Should return false if the proof is invalid, no matter the payload.
assert_eq!(
BeefyMmr::is_non_canonical(
&Commitment {
payload: Payload::from_single_entry(
known_payloads::MMR_ROOT_ID,
H256::repeat_byte(0).encode(),
),
block_number: 250,
validator_set_id: 0
},
invalid_proof,
),
false
);

// Can't prove that the commitment is non-canonical if the `commitment.block_number`
// doesn't match the ancestry proof.
assert_eq!(
BeefyMmr::is_non_canonical(
&Commitment {
payload: Payload::from_single_entry(
known_payloads::MMR_ROOT_ID,
prev_roots[250 - 1].encode(),
),
block_number: 300,
validator_set_id: 0,
},
valid_proof,
),
false
);

// For each previous block, the check:
// - should return false, if the commitment is targeting the canonical chain
// - should return true if the commitment is NOT targeting the canonical chain
for prev_block_number in 1usize..=500 {
let proof = Mmr::generate_ancestry_proof(prev_block_number as u64, None).unwrap();

assert_eq!(
BeefyMmr::is_non_canonical(
&Commitment {
payload: Payload::from_single_entry(
known_payloads::MMR_ROOT_ID,
prev_roots[prev_block_number - 1].encode(),
),
block_number: prev_block_number as u64,
validator_set_id: 0,
},
proof.clone(),
),
false
);

assert_eq!(
BeefyMmr::is_non_canonical(
&Commitment {
payload: Payload::from_single_entry(
known_payloads::MMR_ROOT_ID,
H256::repeat_byte(0).encode(),
),
block_number: prev_block_number as u64,
validator_set_id: 0,
},
proof,
),
true
)
}
});
}
7 changes: 5 additions & 2 deletions substrate/frame/beefy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use sp_staking::{offence::OffenceReportSystem, SessionIndex};
use sp_std::prelude::*;

use sp_consensus_beefy::{
AuthorityIndex, BeefyAuthorityId, ConsensusLog, DoubleVotingProof, OnNewValidatorSet,
ValidatorSet, BEEFY_ENGINE_ID, GENESIS_AUTHORITY_SET_ID,
AncestryHelper, AuthorityIndex, BeefyAuthorityId, ConsensusLog, DoubleVotingProof,
OnNewValidatorSet, ValidatorSet, BEEFY_ENGINE_ID, GENESIS_AUTHORITY_SET_ID,
};

mod default_weights;
Expand Down Expand Up @@ -98,6 +98,9 @@ pub mod pallet {
/// weight MMR root over validators and make it available for Light Clients.
type OnNewValidatorSet: OnNewValidatorSet<<Self as Config>::BeefyId>;

/// Hook for checking commitment canonicity.
type AncestryHelper: AncestryHelper<BlockNumberFor<Self>>;

/// Weights for this pallet.
type WeightInfo: WeightInfo;

Expand Down
14 changes: 14 additions & 0 deletions substrate/frame/beefy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use sp_state_machine::BasicExternalities;
use crate as pallet_beefy;

pub use sp_consensus_beefy::{ecdsa_crypto::AuthorityId as BeefyId, ConsensusLog, BEEFY_ENGINE_ID};
use sp_consensus_beefy::{AncestryHelper, Commitment};

impl_opaque_keys! {
pub struct MockSessionKeys {
Expand Down Expand Up @@ -80,6 +81,18 @@ parameter_types! {
pub const ReportLongevity: u64 =
BondingDuration::get() as u64 * SessionsPerEra::get() as u64 * Period::get();
pub const MaxSetIdSessionEntries: u32 = BondingDuration::get() * SessionsPerEra::get();

pub storage CommitmentIsTargetingFork: bool = true;
}

pub struct MockAncestryHelper;

impl<BlockNumber> AncestryHelper<BlockNumber> for MockAncestryHelper {
type Proof = ();

fn is_non_canonical(_commitment: &Commitment<BlockNumber>, _proof: Self::Proof) -> bool {
CommitmentIsTargetingFork::get()
}
}

impl pallet_beefy::Config for Test {
Expand All @@ -88,6 +101,7 @@ impl pallet_beefy::Config for Test {
type MaxNominators = ConstU32<1000>;
type MaxSetIdSessionEntries = MaxSetIdSessionEntries;
type OnNewValidatorSet = ();
type AncestryHelper = MockAncestryHelper;
type WeightInfo = ();
type KeyOwnerProof = <Historical as KeyOwnerProofSystem<(KeyTypeId, BeefyId)>>::Proof;
type EquivocationReportSystem =
Expand Down
Loading