Skip to content

Commit

Permalink
migrate pallet-beefy and pallet-beefy-mmr to use umbrella crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathy-bajo committed Jan 26, 2025
1 parent 17ae062 commit c4f7ecf
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 178 deletions.
16 changes: 2 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion substrate/frame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ sp-session = { optional = true, workspace = true }
sp-storage = { optional = true, workspace = true }
sp-transaction-pool = { optional = true, workspace = true }
sp-version = { optional = true, workspace = true }

frame-executive = { optional = true, workspace = true }
frame-system-rpc-runtime-api = { optional = true, workspace = true }

Expand Down
28 changes: 4 additions & 24 deletions substrate/frame/beefy-mmr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,42 @@ workspace = true
array-bytes = { optional = true, workspace = true, default-features = true }
binary-merkle-tree = { workspace = true }
codec = { features = ["derive"], workspace = true }
frame-benchmarking = { optional = true, workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["runtime"] }
log = { workspace = true }
pallet-beefy = { workspace = true }
pallet-mmr = { workspace = true }
pallet-session = { workspace = true }
scale-info = { features = ["derive"], workspace = true }
serde = { optional = true, workspace = true, default-features = true }
sp-api = { workspace = true }
sp-consensus-beefy = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-state-machine = { workspace = true }

[dev-dependencies]
array-bytes = { workspace = true, default-features = true }
sp-staking = { workspace = true, default-features = true }

[features]
default = ["std"]
std = [
"array-bytes",
"binary-merkle-tree/std",
"codec/std",
"frame-benchmarking/std",
"frame-support/std",
"frame-system/std",
"frame/std",
"log/std",
"pallet-beefy/std",
"pallet-mmr/std",
"pallet-session/std",
"scale-info/std",
"serde",
"sp-api/std",
"sp-consensus-beefy/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-staking/std",
"sp-state-machine/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
"pallet-beefy/try-runtime",
"frame/try-runtime",
"pallet-mmr/try-runtime",
"pallet-session/try-runtime",
"sp-runtime/try-runtime",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-mmr/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"sp-staking/runtime-benchmarks",
"frame/runtime-benchmarks",
]
5 changes: 1 addition & 4 deletions substrate/frame/beefy-mmr/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@
use super::*;
use crate::Pallet as BeefyMmr;
use codec::Encode;
use frame_benchmarking::v2::*;
use frame_support::traits::Hooks;
use frame_system::{Config as SystemConfig, Pallet as System};
use frame::benchmarking::prelude::Pallet as System;
use pallet_mmr::{Nodes, Pallet as Mmr};
use sp_consensus_beefy::Payload;
use sp_runtime::traits::One;

pub trait Config:
pallet_mmr::Config<Hashing = sp_consensus_beefy::MmrHashing> + crate::Config
Expand Down
20 changes: 6 additions & 14 deletions substrate/frame/beefy-mmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@
extern crate alloc;

use sp_runtime::{
generic::OpaqueDigestItemId,
traits::{Convert, Header, Member},
SaturatedConversion,
};

use alloc::vec::Vec;
use codec::Decode;
use pallet_mmr::{primitives::AncestryProof, LeafDataProvider, NodesUtils, ParentNumberAndHash};
Expand All @@ -51,8 +45,7 @@ use sp_consensus_beefy::{
ValidatorSet as BeefyValidatorSet,
};

use frame_support::{crypto::ecdsa::ECDSAExt, pallet_prelude::Weight, traits::Get};
use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
use frame::{prelude::*, runtime::apis::decl_runtime_apis};

pub use pallet::*;
pub use weights::WeightInfo;
Expand All @@ -73,7 +66,7 @@ where
T: pallet_beefy::Config,
{
fn on_new_root(root: &sp_consensus_beefy::MmrRootHash) {
let digest = sp_runtime::generic::DigestItem::Consensus(
let digest = DigestItem::Consensus(
sp_consensus_beefy::BEEFY_ENGINE_ID,
codec::Encode::encode(&sp_consensus_beefy::ConsensusLog::<
<T as pallet_beefy::Config>::BeefyId,
Expand All @@ -87,7 +80,7 @@ where
pub struct BeefyEcdsaToEthereum;
impl Convert<sp_consensus_beefy::ecdsa_crypto::AuthorityId, Vec<u8>> for BeefyEcdsaToEthereum {
fn convert(beefy_id: sp_consensus_beefy::ecdsa_crypto::AuthorityId) -> Vec<u8> {
sp_core::ecdsa::Public::from(beefy_id)
Public::from(beefy_id)
.to_eth_address()
.map(|v| v.to_vec())
.map_err(|_| {
Expand All @@ -97,14 +90,13 @@ impl Convert<sp_consensus_beefy::ecdsa_crypto::AuthorityId, Vec<u8>> for BeefyEc
}
}

type MerkleRootOf<T> = <<T as pallet_mmr::Config>::Hashing as sp_runtime::traits::Hash>::Output;
type MerkleRootOf<T> = <<T as pallet_mmr::Config>::Hashing as Hash>::Output;

#[frame_support::pallet]
#[frame::pallet]
pub mod pallet {
#![allow(missing_docs)]

use super::*;
use frame_support::pallet_prelude::*;

/// BEEFY-MMR pallet.
#[pallet::pallet]
Expand Down Expand Up @@ -377,7 +369,7 @@ impl<T: Config> Pallet<T> {
}
}

sp_api::decl_runtime_apis! {
decl_runtime_apis! {
/// API useful for BEEFY light clients.
pub trait BeefyMmrApi<H>
where
Expand Down
22 changes: 6 additions & 16 deletions substrate/frame/beefy-mmr/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,24 @@
use std::vec;

use codec::Encode;
use frame_support::{
construct_runtime, derive_impl, parameter_types,
traits::{ConstU32, ConstU64},
};

use sp_consensus_beefy::mmr::MmrLeafVersion;
use sp_io::TestExternalities;
use sp_runtime::{
app_crypto::ecdsa::Public,
impl_opaque_keys,
traits::{ConvertInto, Keccak256, OpaqueKeys},
BuildStorage,
};

use sp_state_machine::BasicExternalities;

use crate as pallet_beefy_mmr;

pub use sp_consensus_beefy::{
ecdsa_crypto::AuthorityId as BeefyId, mmr::BeefyDataProvider, ConsensusLog, BEEFY_ENGINE_ID,
};
use sp_core::offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt};

impl_opaque_keys! {
pub struct MockSessionKeys {
pub dummy: pallet_beefy::Pallet<Test>,
}
}

type Block = frame_system::mocking::MockBlock<Test>;
type Block = MockBlock<Test>;

construct_runtime!(
pub enum Test
Expand Down Expand Up @@ -170,11 +160,11 @@ pub fn mock_authorities(vec: Vec<u8>) -> Vec<(u64, BeefyId)> {
vec.into_iter().map(|id| ((id as u64), mock_beefy_id(id))).collect()
}

pub fn new_test_ext(ids: Vec<u8>) -> TestExternalities {
pub fn new_test_ext(ids: Vec<u8>) -> TestState {
new_test_ext_raw_authorities(mock_authorities(ids))
}

pub fn new_test_ext_raw_authorities(authorities: Vec<(u64, BeefyId)>) -> TestExternalities {
pub fn new_test_ext_raw_authorities(authorities: Vec<(u64, BeefyId)>) -> TestState {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();

let session_keys: Vec<_> = authorities
Expand All @@ -193,7 +183,7 @@ pub fn new_test_ext_raw_authorities(authorities: Vec<(u64, BeefyId)>) -> TestExt
.assimilate_storage(&mut t)
.unwrap();

let mut ext: TestExternalities = t.into();
let mut ext: TestState = t.into();
let (offchain, _offchain_state) = TestOffchainExt::with_offchain_db(ext.offchain_db());
ext.register_extension(OffchainDbExt::new(offchain.clone()));
ext.register_extension(OffchainWorkerExt::new(offchain));
Expand Down
8 changes: 2 additions & 6 deletions substrate/frame/beefy-mmr/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ use sp_consensus_beefy::{
AncestryHelper, Commitment, Payload, ValidatorSet,
};

use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{traits::Keccak256, DigestItem};

use frame_support::traits::OnInitialize;
use frame::traits::OnInitialize;

use crate::mock::*;

Expand All @@ -43,7 +39,7 @@ pub fn beefy_log(log: ConsensusLog<BeefyId>) -> DigestItem {
DigestItem::Consensus(BEEFY_ENGINE_ID, log.encode())
}

fn read_mmr_leaf(ext: &mut TestExternalities, key: Vec<u8>) -> MmrLeaf {
fn read_mmr_leaf(ext: &mut TestState, key: Vec<u8>) -> MmrLeaf {
type Node = pallet_mmr::primitives::DataOrHash<Keccak256, MmrLeaf>;
ext.persist_offchain_overlay();
let offchain_db = ext.offchain_db();
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/beefy-mmr/src/weights.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 5 additions & 18 deletions substrate/frame/beefy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@ workspace = true

[dependencies]
codec = { features = ["derive"], workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
frame = { workspace = true, features = ["runtime"] }
log = { workspace = true }
pallet-authorship = { workspace = true }
pallet-session = { workspace = true }
scale-info = { features = ["derive", "serde"], workspace = true }
serde = { optional = true, workspace = true, default-features = true }
sp-consensus-beefy = { features = ["serde"], workspace = true }
sp-runtime = { features = ["serde"], workspace = true }
sp-session = { workspace = true }
sp-staking = { features = ["serde"], workspace = true }
sp-staking = { optional = true, workspace = true }

[dev-dependencies]
frame-election-provider-support = { workspace = true, default-features = true }
Expand All @@ -32,18 +29,14 @@ pallet-offences = { workspace = true, default-features = true }
pallet-staking = { workspace = true, default-features = true }
pallet-staking-reward-curve = { workspace = true, default-features = true }
pallet-timestamp = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-io = { workspace = true, default-features = true }
sp-staking = { workspace = true, default-features = true }
sp-state-machine = { workspace = true }

[features]
default = ["std"]
std = [
"codec/std",
"frame/std",
"frame-election-provider-support/std",
"frame-support/std",
"frame-system/std",
"log/std",
"pallet-authorship/std",
"pallet-balances/std",
Expand All @@ -54,22 +47,16 @@ std = [
"scale-info/std",
"serde/std",
"sp-consensus-beefy/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-session/std",
"sp-staking/std",
"sp-state-machine/std",
"sp-staking/std",
]
try-runtime = [
"frame-election-provider-support/try-runtime",
"frame-support/try-runtime",
"frame-system/try-runtime",
"frame/try-runtime",
"pallet-authorship/try-runtime",
"pallet-balances/try-runtime",
"pallet-offences/try-runtime",
"pallet-session/try-runtime",
"pallet-staking/try-runtime",
"pallet-timestamp/try-runtime",
"sp-runtime/try-runtime",
]
5 changes: 1 addition & 4 deletions substrate/frame/beefy/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
//! Default weights for the BEEFY Pallet
//! This file was not auto-generated.
use frame_support::weights::{
constants::{RocksDbWeight as DbWeight, WEIGHT_REF_TIME_PER_MICROS, WEIGHT_REF_TIME_PER_NANOS},
Weight,
};
use frame::weights_prelude::*;

impl crate::WeightInfo for () {
fn report_voting_equivocation(
Expand Down
13 changes: 3 additions & 10 deletions substrate/frame/beefy/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,14 @@
use alloc::{vec, vec::Vec};
use codec::{self as codec, Decode, Encode};
use frame_support::traits::{Get, KeyOwnerProofSystem};
use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
use log::{error, info};
use sp_consensus_beefy::{
check_commitment_signature, AncestryHelper, DoubleVotingProof, ForkVotingProof,
FutureBlockVotingProof, ValidatorSetId, KEY_TYPE as BEEFY_KEY_TYPE,
};
use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
TransactionValidityError, ValidTransaction,
},
DispatchError, KeyTypeId, Perbill, RuntimeAppPublic,
};
use sp_session::{GetSessionNumber, GetValidatorCount};

use frame::testing_prelude::*;
use frame::deps::sp_session::{GetSessionNumber, GetValidatorCount};
use sp_staking::{
offence::{Kind, Offence, OffenceReportSystem, ReportOffence},
SessionIndex,
Expand Down
Loading

0 comments on commit c4f7ecf

Please sign in to comment.