Skip to content

Commit

Permalink
Merge branch 'autonomys:main' into tmp/opt/optimize-cluster-identific…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
tediou5 authored Jan 13, 2025
2 parents 1cfba5e + e0a3f16 commit b633d33
Show file tree
Hide file tree
Showing 34 changed files with 917 additions and 788 deletions.
26 changes: 0 additions & 26 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,32 +155,6 @@ jobs:
cargo -Zgitoxide -Zgit clippy --locked --all-targets --features rocm -- -D warnings
if: runner.os == 'Windows'

cargo-docs:
runs-on: ${{ fromJson(github.repository_owner == 'autonomys' && '["self-hosted", "ubuntu-22.04-x86-64"]' || '"ubuntu-22.04"') }}
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Install Protoc
uses: arduino/setup-protoc@c65c819552d16ad3c9b72d9dfd5ba5237b9c906b # v3.0.0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Configure cache
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: |
~/.cargo/registry
~/.cargo/git
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.toml') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Check Documentation
run: cargo -Zgitoxide -Zgit doc --locked --all --no-deps --lib
env:
RUSTDOCFLAGS: "-D rustdoc::broken-intra-doc-links -D rustdoc::private_intra_doc_links"

cargo-test:
strategy:
matrix:
Expand Down
8 changes: 6 additions & 2 deletions .github/workflows/rustdoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- main
pull_request:
workflow_dispatch:
merge_group:

concurrency:
group: rustdoc-${{ github.workflow }}-${{ github.ref }}
Expand Down Expand Up @@ -48,13 +51,14 @@ jobs:

# Build the rust crate docs
- name: Build Documentation
run: cargo -Zgitoxide -Zgit doc --all --no-deps --lib --features cuda
run: cargo -Zgitoxide -Zgit doc --locked --all --no-deps --lib --features cuda
env:
RUSTDOCFLAGS: "-Z unstable-options --enable-index-page"
RUSTDOCFLAGS: "-D rustdoc::broken-intra-doc-links -D rustdoc::private_intra_doc_links -Z unstable-options --enable-index-page"

- name: Deploy Docs
uses: JamesIves/github-pages-deploy-action@881db5376404c5c8d621010bcbec0310b58d5e29 # v4.6.8
with:
branch: gh-pages
single-commit: true
folder: target/doc
if: ${{ github.event_name == 'push' }}
1 change: 1 addition & 0 deletions Cargo.lock

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

117 changes: 88 additions & 29 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use frame_support::ensure;
use frame_support::pallet_prelude::StorageVersion;
use frame_support::traits::fungible::{Inspect, InspectHold};
use frame_support::traits::tokens::{Fortitude, Preservation};
use frame_support::traits::{Get, Randomness as RandomnessT};
use frame_support::traits::{Get, Randomness as RandomnessT, Time};
use frame_support::weights::Weight;
use frame_system::offchain::SubmitTransaction;
use frame_system::pallet_prelude::*;
Expand Down Expand Up @@ -86,6 +86,7 @@ use sp_subspace_mmr::{ConsensusChainMmrLeafProof, MmrProofVerifier};
pub use staking::OperatorConfig;
use subspace_core_primitives::pot::PotOutput;
use subspace_core_primitives::{BlockHash, SlotNumber, U256};
use subspace_runtime_primitives::{Balance, Moment, StorageFee};

/// Maximum number of nominators to slash within a give operator at a time.
pub const MAX_NOMINATORS_TO_SLASH: u32 = 10;
Expand Down Expand Up @@ -201,7 +202,7 @@ mod pallet {
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::staking_epoch::do_slash_operator;
use crate::staking_epoch::{do_finalize_domain_current_epoch, Error as StakingEpochError};
use crate::storage_proof::InvalidInherentExtrinsicData;
use crate::storage_proof::InherentExtrinsicData;
use crate::weights::WeightInfo;
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::DomainHashingFor;
Expand Down Expand Up @@ -248,10 +249,10 @@ mod pallet {
use sp_std::fmt::Debug;
use sp_subspace_mmr::MmrProofVerifier;
use subspace_core_primitives::{Randomness, U256};
use subspace_runtime_primitives::{Balance, StorageFee};
use subspace_runtime_primitives::StorageFee;

#[pallet::config]
pub trait Config: frame_system::Config<Hash: Into<H256>> {
pub trait Config: frame_system::Config<Hash: Into<H256> + From<H256>> {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

// TODO: `DomainHash` can be derived from `DomainHeader`, it is still needed just for
Expand Down Expand Up @@ -699,7 +700,7 @@ mod pallet {
#[pallet::storage]
pub(super) type AccumulatedTreasuryFunds<T> = StorageValue<_, BalanceOf<T>, ValueQuery>;

/// Storage used to keep track of which consensus block the domain runtime upgrade happen.
/// Storage used to keep track of which consensus block each domain runtime upgrade happens in.
#[pallet::storage]
pub(super) type DomainRuntimeUpgradeRecords<T: Config> = StorageMap<
_,
Expand All @@ -709,8 +710,8 @@ mod pallet {
ValueQuery,
>;

/// Temporary storage keep track of domain runtime upgrade happen in the current block, cleared
/// in the next block initialization.
/// Temporary storage to keep track of domain runtime upgrades which happened in the parent
/// block. Cleared in the current block's initialization.
#[pallet::storage]
pub type DomainRuntimeUpgrades<T> = StorageValue<_, Vec<RuntimeId>, ValueQuery>;

Expand Down Expand Up @@ -1856,7 +1857,7 @@ mod pallet {

/// Combined fraud proof data for the InvalidInherentExtrinsic fraud proof
#[pallet::storage]
pub type BlockInvalidInherentExtrinsicData<T> = StorageValue<_, InvalidInherentExtrinsicData>;
pub type BlockInherentExtrinsicData<T> = StorageValue<_, InherentExtrinsicData>;

#[pallet::hooks]
// TODO: proper benchmark
Expand All @@ -1865,8 +1866,7 @@ mod pallet {
let parent_number = block_number - One::one();
let parent_hash = frame_system::Pallet::<T>::block_hash(parent_number);

// Record any previous domain runtime upgrade in `DomainRuntimeUpgradeRecords` and then do the
// domain runtime upgrade scheduled in the current block
// Record any previous domain runtime upgrades in `DomainRuntimeUpgradeRecords`
for runtime_id in DomainRuntimeUpgrades::<T>::take() {
let reference_count = RuntimeRegistry::<T>::get(runtime_id)
.expect("Runtime object must be present since domain is insantiated; qed")
Expand All @@ -1883,9 +1883,14 @@ mod pallet {
});
}
}
// Set DomainRuntimeUpgrades to an empty list. (If there are no runtime upgrades
// scheduled in the current block, we can generate a proof the list is empty.)
DomainRuntimeUpgrades::<T>::set(Vec::new());
// Do the domain runtime upgrades scheduled in the current block, and record them in
// DomainRuntimeUpgrades
do_upgrade_runtimes::<T>(block_number);

// Store the hash of the parent consensus block for domain that have bundles submitted
// Store the hash of the parent consensus block for domains that have bundles submitted
// in that consensus block
for (domain_id, _) in SuccessfulBundles::<T>::drain() {
ConsensusBlockHash::<T>::insert(domain_id, parent_number, parent_hash);
Expand All @@ -1896,48 +1901,42 @@ mod pallet {
}

for (operator_id, slot_set) in OperatorBundleSlot::<T>::drain() {
// NOTE: `OperatorBundleSlot` use `BTreeSet` so `last` will return the maximum value in the set
// NOTE: `OperatorBundleSlot` uses `BTreeSet` so `last` will return the maximum
// value in the set
if let Some(highest_slot) = slot_set.last() {
OperatorHighestSlot::<T>::insert(operator_id, highest_slot);
}
}

BlockInvalidInherentExtrinsicData::<T>::kill();
BlockInherentExtrinsicData::<T>::kill();

Weight::zero()
}

fn on_finalize(_: BlockNumberFor<T>) {
// If this consensus block will derive any domain block, gather the necessary storage for potential fraud proof usage
// If this consensus block will derive any domain block, gather the necessary storage
// for potential fraud proof usage
if SuccessfulBundles::<T>::iter_keys().count() > 0
|| DomainRuntimeUpgrades::<T>::exists()
|| !DomainRuntimeUpgrades::<T>::get().is_empty()
{
let extrinsics_shuffling_seed = Randomness::from(
Into::<H256>::into(Self::extrinsics_shuffling_seed()).to_fixed_bytes(),
Into::<H256>::into(Self::extrinsics_shuffling_seed_value()).to_fixed_bytes(),
);

// There are no actual conversions here, but the trait bounds required to prove that
// (and debug-print the error in expect()) are very verbose.
let timestamp = T::BlockTimestamp::now()
.try_into()
.map_err(|_| ())
.expect("Moment is the same type in both pallets; qed");
let transaction_byte_fee: Balance = T::StorageFee::transaction_byte_fee()
.try_into()
.map_err(|_| ())
.expect("Balance is the same type in both pallets; qed");
let timestamp = Self::timestamp_value();

// The value returned by the consensus_chain_byte_fee() runtime API
let consensus_transaction_byte_fee =
sp_domains::DOMAIN_STORAGE_FEE_MULTIPLIER * transaction_byte_fee;
let consensus_transaction_byte_fee = Self::consensus_transaction_byte_fee_value();

let invalid_inherent_extrinsic_data = InvalidInherentExtrinsicData {
let inherent_extrinsic_data = InherentExtrinsicData {
extrinsics_shuffling_seed,
timestamp,
consensus_transaction_byte_fee,
};

BlockInvalidInherentExtrinsicData::<T>::set(Some(invalid_inherent_extrinsic_data));
BlockInherentExtrinsicData::<T>::set(Some(inherent_extrinsic_data));
}

let _ = LastEpochStakingDistribution::<T>::clear(u32::MAX, None);
Expand Down Expand Up @@ -2096,11 +2095,17 @@ impl<T: Config> Pallet<T> {
Ok(HeadDomainNumber::<T>::get(domain_id) + missed_upgrade.into())
}

/// Returns the runtime ID for the supplied `domain_id`, if that domain exists.
pub fn runtime_id(domain_id: DomainId) -> Option<RuntimeId> {
DomainRegistry::<T>::get(domain_id)
.map(|domain_object| domain_object.domain_config.runtime_id)
}

/// Returns the list of runtime upgrades in the current block.
pub fn runtime_upgrades() -> Vec<RuntimeId> {
DomainRuntimeUpgrades::<T>::get()
}

pub fn domain_instance_data(
domain_id: DomainId,
) -> Option<(DomainInstanceData, BlockNumberFor<T>)> {
Expand Down Expand Up @@ -2758,12 +2763,66 @@ impl<T: Config> Pallet<T> {
false
}

/// The external function used to access the extrinsics shuffling seed stored in
/// `BlockInherentExtrinsicData`.
pub fn extrinsics_shuffling_seed() -> T::Hash {
// Fall back to recalculating if it hasn't been stored yet.
BlockInherentExtrinsicData::<T>::get()
.map(|data| H256::from(*data.extrinsics_shuffling_seed).into())
.unwrap_or_else(|| Self::extrinsics_shuffling_seed_value())
}

/// The internal function used to calculate the extrinsics shuffling seed for storage into
/// `BlockInherentExtrinsicData`.
fn extrinsics_shuffling_seed_value() -> T::Hash {
let subject = DOMAIN_EXTRINSICS_SHUFFLING_SEED_SUBJECT;
let (randomness, _) = T::Randomness::random(subject);
randomness
}

/// The external function used to access the timestamp stored in
/// `BlockInherentExtrinsicData`.
pub fn timestamp() -> Moment {
// Fall back to recalculating if it hasn't been stored yet.
BlockInherentExtrinsicData::<T>::get()
.map(|data| data.timestamp)
.unwrap_or_else(|| Self::timestamp_value())
}

/// The internal function used to access the timestamp for storage into
/// `BlockInherentExtrinsicData`.
fn timestamp_value() -> Moment {
// There are no actual conversions here, but the trait bounds required to prove that
// (and debug-print the error in expect()) are very verbose.
T::BlockTimestamp::now()
.try_into()
.map_err(|_| ())
.expect("Moment is the same type in both pallets; qed")
}

/// The external function used to access the consensus transaction byte fee stored in
/// `BlockInherentExtrinsicData`.
/// This value is returned by the consensus_chain_byte_fee() runtime API
pub fn consensus_transaction_byte_fee() -> Balance {
// Fall back to recalculating if it hasn't been stored yet.
BlockInherentExtrinsicData::<T>::get()
.map(|data| data.consensus_transaction_byte_fee)
.unwrap_or_else(|| Self::consensus_transaction_byte_fee_value())
}

/// The internal function used to calculate the consensus transaction byte fee for storage into
/// `BlockInherentExtrinsicData`.
fn consensus_transaction_byte_fee_value() -> Balance {
// There are no actual conversions here, but the trait bounds required to prove that
// (and debug-print the error in expect()) are very verbose.
let transaction_byte_fee: Balance = T::StorageFee::transaction_byte_fee()
.try_into()
.map_err(|_| ())
.expect("Balance is the same type in both pallets; qed");

sp_domains::DOMAIN_STORAGE_FEE_MULTIPLIER * transaction_byte_fee
}

pub fn execution_receipt(receipt_hash: ReceiptHashFor<T>) -> Option<ExecutionReceiptOf<T>> {
BlockTreeNodes::<T>::get(receipt_hash).map(|db| db.execution_receipt)
}
Expand Down Expand Up @@ -2910,7 +2969,7 @@ impl<T: Config> Pallet<T> {
}

// Get the domain runtime code that used to derive `receipt`, if the runtime code still present in
// the state then get it from the state otherwise from the `maybe_domain_runtime_code_at` prood.
// the state then get it from the state otherwise from the `maybe_domain_runtime_code_at` proof.
pub fn get_domain_runtime_code_for_receipt(
domain_id: DomainId,
receipt: &ExecutionReceiptOf<T>,
Expand Down
16 changes: 11 additions & 5 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ use sp_domains::{
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_runtime::traits::{
AccountIdConversion, BlakeTwo256, BlockNumberProvider, Hash as HashT, IdentityLookup, One,
AccountIdConversion, BlakeTwo256, BlockNumberProvider, Hash as HashT, IdentityLookup, One, Zero,
};
use sp_runtime::transaction_validity::TransactionValidityError;
use sp_runtime::{BuildStorage, OpaqueExtrinsic, Saturating};
use sp_runtime::{BuildStorage, OpaqueExtrinsic};
use sp_version::RuntimeVersion;
use subspace_core_primitives::U256 as P256;
use subspace_runtime_primitives::{HoldIdentifier, Moment, StorageFee, SSC};
Expand Down Expand Up @@ -430,14 +430,20 @@ impl sp_core::traits::ReadRuntimeVersion for ReadRuntimeVersion {
}

pub(crate) fn run_to_block<T: Config>(block_number: BlockNumberFor<T>, parent_hash: T::Hash) {
// Finalize previous block
crate::Pallet::<T>::on_finalize(block_number.saturating_sub(One::one()));
// Finalize the previous block
// on_finalize() does not run on the genesis block
if block_number > One::one() {
crate::Pallet::<T>::on_finalize(block_number - One::one());
}
frame_system::Pallet::<T>::finalize();

// Initialize current block
frame_system::Pallet::<T>::set_block_number(block_number);
frame_system::Pallet::<T>::initialize(&block_number, &parent_hash, &Default::default());
crate::Pallet::<T>::on_initialize(block_number);
// on_initialize() does not run on the genesis block
if block_number > Zero::zero() {
crate::Pallet::<T>::on_initialize(block_number);
}
}

pub(crate) fn register_genesis_domain(creator: u128, operator_ids: Vec<OperatorId>) -> DomainId {
Expand Down
2 changes: 1 addition & 1 deletion crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1678,7 +1678,7 @@ fn check_vote<T: Config>(
(reward_address, _signature),
) in current_reward_receivers.iter_mut()
{
if public_key != &offender {
if public_key == &offender {
// Revoke reward if assigned in current block.
reward_address.take();
}
Expand Down
Loading

0 comments on commit b633d33

Please sign in to comment.