Skip to content

Commit 8d46c2c

Browse files
committed
Merge branch 'main' of github.com:subspace/subspace into pallet-domains-extension-benchmark
2 parents 3b989e6 + 7a0675a commit 8d46c2c

File tree

30 files changed

+1412
-34
lines changed

30 files changed

+1412
-34
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pallet-domains/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ hexlit.workspace = true
2121
hex-literal = { workspace = true, optional = true }
2222
log.workspace = true
2323
pallet-balances.workspace = true
24+
pallet-subspace = { workspace = true, optional = true }
2425
scale-info = { workspace = true, features = ["derive"] }
2526
schnorrkel = { workspace = true, optional = true }
2627
sp-consensus-slots.workspace = true
@@ -39,6 +40,7 @@ subspace-runtime-primitives.workspace = true
3940
[dev-dependencies]
4041
domain-pallet-executive.workspace = true
4142
hex-literal.workspace = true
43+
pallet-subspace.workspace = true
4244
pallet-timestamp.workspace = true
4345
pallet-block-fees.workspace = true
4446
sp-externalities.workspace = true
@@ -53,6 +55,7 @@ std = [
5355
"frame-system/std",
5456
"log/std",
5557
"pallet-balances/std",
58+
"pallet-subspace/std",
5659
"scale-info/std",
5760
"sp-consensus-slots/std",
5861
"sp-consensus-subspace/std",
@@ -73,6 +76,7 @@ runtime-benchmarks = [
7376
"frame-benchmarking",
7477
"frame-benchmarking/runtime-benchmarks",
7578
"sp-consensus-subspace/runtime-benchmarks",
79+
"pallet-subspace/runtime-benchmarks",
7680
"sp-domains/runtime-benchmarks",
7781
"sp-domains-fraud-proof/runtime-benchmarks",
7882
"sp-runtime/runtime-benchmarks",

crates/pallet-domains/src/benchmarking.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use frame_support::traits::fungible::{Inspect, Mutate};
2929
use frame_support::traits::Hooks;
3030
use frame_system::{Pallet as System, RawOrigin};
3131
use hex_literal::hex;
32+
use pallet_subspace::BlockRandomness;
3233
use sp_consensus_slots::Slot;
3334
use sp_core::crypto::{Ss58Codec, UncheckedFrom};
3435
use sp_core::sr25519::vrf::{VrfPreOutput, VrfProof, VrfSignature};
@@ -44,11 +45,16 @@ use sp_domains_fraud_proof::fraud_proof::FraudProof;
4445
use sp_runtime::traits::{CheckedAdd, One, Zero};
4546
use sp_std::collections::btree_set::BTreeSet;
4647
use subspace_core_primitives::pot::PotOutput;
48+
use subspace_core_primitives::Randomness;
4749

4850
const SEED: u32 = 0;
4951
const MAX_NOMINATORS_TO_SLASH_WITHOUT_OPERATOR: u32 = MAX_NOMINATORS_TO_SLASH - 1;
5052

51-
#[benchmarks(where <RuntimeCallFor<T> as sp_runtime::traits::Dispatchable>::RuntimeOrigin: From<DomainOrigin>)]
53+
#[allow(clippy::multiple_bound_locations)]
54+
#[benchmarks(where
55+
T: pallet_subspace::Config,
56+
<RuntimeCallFor<T> as sp_runtime::traits::Dispatchable>::RuntimeOrigin: From<DomainOrigin>,
57+
)]
5258
mod benchmarks {
5359
use super::*;
5460
use sp_std::vec;
@@ -859,8 +865,8 @@ mod benchmarks {
859865
#[benchmark]
860866
fn transfer_treasury_funds() {
861867
// Ensure the treasury account has balance
862-
let treasury_amount = 5000u32.into();
863-
let transfer_amount = 500u32.into();
868+
let transfer_amount = T::Currency::minimum_balance();
869+
let treasury_amount = T::Currency::minimum_balance() + transfer_amount;
864870
let account = account("slashed_account", 1, SEED);
865871
assert_eq!(T::Currency::balance(&account), 0u32.into());
866872
T::Currency::set_balance(&T::TreasuryAccount::get(), treasury_amount);
@@ -1234,12 +1240,16 @@ mod benchmarks {
12341240
}
12351241
}
12361242

1237-
fn run_to_block<T: Config>(block_number: BlockNumberFor<T>, parent_hash: T::Hash) {
1243+
fn run_to_block<T: Config + pallet_subspace::Config>(
1244+
block_number: BlockNumberFor<T>,
1245+
parent_hash: T::Hash,
1246+
) {
12381247
if let Some(parent_block_number) = block_number.checked_sub(&One::one()) {
12391248
Domains::<T>::on_finalize(parent_block_number);
12401249
}
12411250
System::<T>::set_block_number(block_number);
12421251
System::<T>::initialize(&block_number, &parent_hash, &Default::default());
1252+
BlockRandomness::<T>::put(Randomness::default());
12431253
Domains::<T>::on_initialize(block_number);
12441254
System::<T>::finalize();
12451255
}

crates/pallet-domains/src/tests.rs

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use frame_support::weights::{IdentityFee, Weight};
2020
use frame_support::{assert_err, assert_ok, derive_impl, parameter_types, PalletId};
2121
use frame_system::mocking::MockUncheckedExtrinsic;
2222
use frame_system::pallet_prelude::*;
23+
use pallet_subspace::NormalEraChange;
2324
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
2425
use scale_info::TypeInfo;
2526
use sp_core::crypto::Pair;
@@ -34,14 +35,18 @@ use sp_domains::{
3435
use sp_domains_fraud_proof::fraud_proof::FraudProof;
3536
use sp_runtime::generic::{Preamble, EXTRINSIC_FORMAT_VERSION};
3637
use sp_runtime::traits::{
37-
AccountIdConversion, BlakeTwo256, BlockNumberProvider, Bounded, Hash as HashT, IdentityLookup,
38-
One, Zero,
38+
AccountIdConversion, BlakeTwo256, BlockNumberProvider, Bounded, ConstU16, Hash as HashT,
39+
IdentityLookup, One, Zero,
3940
};
4041
use sp_runtime::transaction_validity::TransactionValidityError;
4142
use sp_runtime::type_with_default::TypeWithDefault;
4243
use sp_runtime::{BuildStorage, OpaqueExtrinsic};
4344
use sp_version::RuntimeVersion;
44-
use subspace_core_primitives::U256 as P256;
45+
use std::num::NonZeroU64;
46+
use subspace_core_primitives::pieces::Piece;
47+
use subspace_core_primitives::segments::HistorySize;
48+
use subspace_core_primitives::solutions::SolutionRange;
49+
use subspace_core_primitives::{SlotNumber, U256 as P256};
4550
use subspace_runtime_primitives::{
4651
ConsensusEventSegmentSize, HoldIdentifier, Moment, Nonce, StorageFee, SSC,
4752
};
@@ -61,6 +66,7 @@ frame_support::construct_runtime!(
6166
System: frame_system,
6267
Timestamp: pallet_timestamp,
6368
Balances: pallet_balances,
69+
Subspace: pallet_subspace,
6470
Domains: pallet_domains,
6571
DomainExecutive: domain_pallet_executive,
6672
BlockFees: pallet_block_fees,
@@ -313,6 +319,54 @@ impl pallet_block_fees::Config for Test {
313319
type DomainChainByteFee = DomainChainByteFee;
314320
}
315321

322+
pub const INITIAL_SOLUTION_RANGE: SolutionRange =
323+
u64::MAX / (1024 * 1024 * 1024 / Piece::SIZE as u64) * 3 / 10;
324+
325+
parameter_types! {
326+
pub const BlockAuthoringDelay: SlotNumber = 2;
327+
pub const PotEntropyInjectionInterval: BlockNumber = 5;
328+
pub const PotEntropyInjectionLookbackDepth: u8 = 2;
329+
pub const PotEntropyInjectionDelay: SlotNumber = 4;
330+
pub const EraDuration: u32 = 4;
331+
// 1GB
332+
pub const InitialSolutionRange: SolutionRange = INITIAL_SOLUTION_RANGE;
333+
pub const RecentSegments: HistorySize = HistorySize::new(NonZeroU64::new(5).unwrap());
334+
pub const RecentHistoryFraction: (HistorySize, HistorySize) = (
335+
HistorySize::new(NonZeroU64::new(1).unwrap()),
336+
HistorySize::new(NonZeroU64::new(10).unwrap()),
337+
);
338+
pub const MinSectorLifetime: HistorySize = HistorySize::new(NonZeroU64::new(4).unwrap());
339+
pub const RecordSize: u32 = 3840;
340+
pub const ExpectedVotesPerBlock: u32 = 9;
341+
pub const ReplicationFactor: u16 = 1;
342+
pub const ReportLongevity: u64 = 34;
343+
pub const ShouldAdjustSolutionRange: bool = false;
344+
pub const BlockSlotCount: u32 = 6;
345+
}
346+
347+
impl pallet_subspace::Config for Test {
348+
type RuntimeEvent = RuntimeEvent;
349+
type SubspaceOrigin = pallet_subspace::EnsureSubspaceOrigin;
350+
type BlockAuthoringDelay = BlockAuthoringDelay;
351+
type PotEntropyInjectionInterval = PotEntropyInjectionInterval;
352+
type PotEntropyInjectionLookbackDepth = PotEntropyInjectionLookbackDepth;
353+
type PotEntropyInjectionDelay = PotEntropyInjectionDelay;
354+
type EraDuration = EraDuration;
355+
type InitialSolutionRange = InitialSolutionRange;
356+
type SlotProbability = SlotProbability;
357+
type ConfirmationDepthK = ConfirmationDepthK;
358+
type RecentSegments = RecentSegments;
359+
type RecentHistoryFraction = RecentHistoryFraction;
360+
type MinSectorLifetime = MinSectorLifetime;
361+
type ExpectedVotesPerBlock = ExpectedVotesPerBlock;
362+
type MaxPiecesInSector = ConstU16<1>;
363+
type ShouldAdjustSolutionRange = ShouldAdjustSolutionRange;
364+
type EraChangeTrigger = NormalEraChange;
365+
type WeightInfo = ();
366+
type BlockSlotCount = BlockSlotCount;
367+
type ExtensionWeightInfo = pallet_subspace::extensions::weights::SubstrateWeight<Self>;
368+
}
369+
316370
pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
317371
let t = frame_system::GenesisConfig::<Test>::default()
318372
.build_storage()

crates/subspace-runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,6 @@ runtime-benchmarks = [
155155
"pallet-transporter/runtime-benchmarks",
156156
"pallet-utility/runtime-benchmarks",
157157
"sp-consensus-subspace/runtime-benchmarks",
158+
"sp-messenger/runtime-benchmarks",
158159
"sp-runtime/runtime-benchmarks",
159160
]

crates/subspace-runtime/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,9 @@ impl pallet_messenger::Config for Runtime {
754754
type OnXDMRewards = OnXDMRewards;
755755
type MmrHash = mmr::Hash;
756756
type MmrProofVerifier = MmrProofVerifier;
757+
#[cfg(feature = "runtime-benchmarks")]
758+
type StorageKeys = sp_messenger::BenchmarkStorageKeys;
759+
#[cfg(not(feature = "runtime-benchmarks"))]
757760
type StorageKeys = StorageKeys;
758761
type DomainOwner = Domains;
759762
type HoldIdentifier = HoldIdentifierWrapper;
@@ -763,6 +766,7 @@ impl pallet_messenger::Config for Runtime {
763766
type MaxOutgoingMessages = MaxOutgoingMessages;
764767
type MessengerOrigin = pallet_messenger::EnsureMessengerOrigin;
765768
type NoteChainTransfer = Transporter;
769+
type ExtensionWeightInfo = pallet_messenger::extensions::weights::SubstrateWeight<Runtime>;
766770
}
767771

768772
impl<C> frame_system::offchain::CreateTransactionBase<C> for Runtime
@@ -1259,6 +1263,7 @@ mod benches {
12591263
[pallet_messenger, Messenger]
12601264
[pallet_transporter, Transporter]
12611265
[pallet_subspace_extension, SubspaceExtensionBench::<Runtime>]
1266+
[pallet_messenger_from_domains_extension, MessengerFromDomainsExtensionBench::<Runtime>]
12621267
);
12631268
}
12641269

@@ -1781,6 +1786,7 @@ impl_runtime_apis! {
17811786
use frame_system_benchmarking::Pallet as SystemBench;
17821787
use baseline::Pallet as BaselineBench;
17831788
use pallet_subspace::extensions::benchmarking::Pallet as SubspaceExtensionBench;
1789+
use pallet_messenger::extensions::benchmarking_from_domains::Pallet as MessengerFromDomainsExtensionBench;
17841790

17851791
let mut list = Vec::<BenchmarkList>::new();
17861792
list_benchmarks!(list, extra);
@@ -1799,6 +1805,7 @@ impl_runtime_apis! {
17991805
use frame_system_benchmarking::Pallet as SystemBench;
18001806
use baseline::Pallet as BaselineBench;
18011807
use pallet_subspace::extensions::benchmarking::Pallet as SubspaceExtensionBench;
1808+
use pallet_messenger::extensions::benchmarking_from_domains::Pallet as MessengerFromDomainsExtensionBench;
18021809

18031810
use frame_support::traits::WhitelistedStorageKeys;
18041811
let whitelist: Vec<TrackedStorageKey> = AllPalletsWithSystem::whitelisted_storage_keys();

domains/client/block-preprocessor/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,11 @@ where
501501
let bundle_estimated_weight = bundle.estimated_weight();
502502
let mut maybe_invalid_bundle_type = None;
503503

504+
// Note: It is okay to use stateless here since all the bundle checks currently do not
505+
// require state. But of any checks in the future requires a state read that was part of the
506+
// genesis ex: `SelfChainId`, stateless runtime will panic.
507+
// So ideal to set the genesis storage as fraud proof already have access to that and would be
508+
// no different in terms of verification on both the sides
504509
let stateless_runtime_api = self.stateless_runtime_api(parent_domain_hash)?;
505510
let consensus_runtime_api = self.consensus_client.runtime_api();
506511

0 commit comments

Comments
 (0)