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

Introduce basic storage proof for the stateless fraud proof #2746

Merged
merged 7 commits into from
May 16, 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
22 changes: 11 additions & 11 deletions crates/pallet-domains/src/domain_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::pallet::{DomainStakingSummary, NextEVMChainId};
use crate::runtime_registry::DomainRuntimeInfo;
use crate::staking::StakingSummary;
use crate::{
BalanceOf, Config, DomainHashingFor, DomainRegistry, ExecutionReceiptOf, HoldIdentifier,
NextDomainId, RuntimeRegistry,
into_complete_raw_genesis, BalanceOf, Config, DomainHashingFor, DomainRegistry,
ExecutionReceiptOf, HoldIdentifier, NextDomainId, RuntimeRegistry,
};
#[cfg(not(feature = "std"))]
use alloc::string::String;
Expand Down Expand Up @@ -236,14 +236,14 @@ pub(crate) fn do_instantiate_domain<T: Config>(

let genesis_receipt = {
let state_version = runtime_obj.version.state_version();
let raw_genesis = runtime_obj
.into_complete_raw_genesis::<T>(
domain_id,
domain_runtime_info,
total_issuance,
domain_config.initial_balances.clone(),
)
.map_err(Error::FailedToGenerateRawGenesis)?;
let raw_genesis = into_complete_raw_genesis::<T>(
runtime_obj,
domain_id,
domain_runtime_info,
total_issuance,
domain_config.initial_balances.clone(),
)
.map_err(Error::FailedToGenerateRawGenesis)?;
let state_root = raw_genesis.state_root::<DomainHashingFor<T>>(state_version);
let genesis_block_hash = derive_domain_block_hash::<T::DomainHeader>(
Zero::zero(),
Expand Down Expand Up @@ -347,13 +347,13 @@ pub(crate) fn calculate_max_bundle_weight_and_size(
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime_registry::RuntimeObject;
use crate::tests::{new_test_ext, Test};
use domain_runtime_primitives::{AccountId20, AccountId20Converter};
use frame_support::traits::Currency;
use frame_support::{assert_err, assert_ok};
use hex_literal::hex;
use sp_domains::storage::RawGenesis;
use sp_domains::RuntimeObject;
use sp_runtime::traits::Convert;
use sp_std::vec;
use sp_version::RuntimeVersion;
Expand Down
26 changes: 13 additions & 13 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern crate alloc;
use crate::block_tree::verify_execution_receipt;
use crate::bundle_storage_fund::storage_fund_account;
use crate::domain_registry::Error as DomainRegistryError;
use crate::runtime_registry::into_complete_raw_genesis;
use crate::staking::OperatorStatus;
use crate::staking_epoch::EpochTransitionResult;
use crate::weights::WeightInfo;
Expand Down Expand Up @@ -169,8 +170,7 @@ mod pallet {
};
use crate::runtime_registry::{
do_register_runtime, do_schedule_runtime_upgrade, do_upgrade_runtimes,
register_runtime_at_genesis, Error as RuntimeRegistryError, RuntimeObject,
ScheduledRuntimeUpgrade,
register_runtime_at_genesis, Error as RuntimeRegistryError, ScheduledRuntimeUpgrade,
};
#[cfg(not(feature = "runtime-benchmarks"))]
use crate::staking::do_reward_operators;
Expand Down Expand Up @@ -208,7 +208,7 @@ mod pallet {
use sp_domains::{
BundleDigest, ConfirmedDomainBlock, DomainBundleSubmitted, DomainId,
DomainsTransfersTracker, EpochIndex, GenesisDomain, OperatorAllowList, OperatorId,
OperatorPublicKey, RuntimeId, RuntimeType,
OperatorPublicKey, RuntimeId, RuntimeObject, RuntimeType,
};
use sp_domains_fraud_proof::fraud_proof::FraudProof;
use sp_domains_fraud_proof::InvalidTransactionCode;
Expand Down Expand Up @@ -398,7 +398,7 @@ mod pallet {

/// Bundles submitted successfully in current block.
#[pallet::storage]
pub(super) type SuccessfulBundles<T> = StorageMap<_, Identity, DomainId, Vec<H256>, ValueQuery>;
pub type SuccessfulBundles<T> = StorageMap<_, Identity, DomainId, Vec<H256>, ValueQuery>;

/// Fraud proofs submitted successfully in current block.
#[pallet::storage]
Expand All @@ -425,7 +425,7 @@ mod pallet {
pub(super) type NextEVMChainId<T> = StorageValue<_, EVMChainId, ValueQuery, StartingEVMChainId>;

#[pallet::storage]
pub(super) type RuntimeRegistry<T: Config> =
pub type RuntimeRegistry<T: Config> =
StorageMap<_, Identity, RuntimeId, RuntimeObject<BlockNumberFor<T>, T::Hash>, OptionQuery>;

#[pallet::storage]
Expand Down Expand Up @@ -1661,14 +1661,14 @@ impl<T: Config> Pallet<T> {
let runtime_object = RuntimeRegistry::<T>::get(domain_obj.domain_config.runtime_id)?;
let runtime_type = runtime_object.runtime_type.clone();
let total_issuance = domain_obj.domain_config.total_issuance()?;
let raw_genesis = runtime_object
.into_complete_raw_genesis::<T>(
domain_id,
domain_obj.domain_runtime_info,
total_issuance,
domain_obj.domain_config.initial_balances,
)
.ok()?;
let raw_genesis = into_complete_raw_genesis::<T>(
runtime_object,
domain_id,
domain_obj.domain_runtime_info,
total_issuance,
domain_obj.domain_config.initial_balances,
)
.ok()?;
Some((
DomainInstanceData {
runtime_type,
Expand Down
122 changes: 52 additions & 70 deletions crates/pallet-domains/src/runtime_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use scale_info::TypeInfo;
use sp_core::crypto::AccountId32;
use sp_core::Hasher;
use sp_domains::storage::{RawGenesis, StorageData, StorageKey};
use sp_domains::{DomainId, DomainsDigestItem, RuntimeId, RuntimeType};
use sp_domains::{DomainId, DomainsDigestItem, RuntimeId, RuntimeObject, RuntimeType};
use sp_runtime::traits::{CheckedAdd, Get, Zero};
use sp_runtime::DigestItem;
use sp_std::vec;
Expand All @@ -39,20 +39,6 @@ pub enum Error {
InvalidAccountIdType,
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
pub struct RuntimeObject<Number, Hash> {
pub runtime_name: String,
pub runtime_type: RuntimeType,
pub runtime_upgrades: u32,
pub hash: Hash,
// The raw gensis storage that contains the runtime code.
// NOTE: don't use this field directly but `into_complete_raw_genesis` instead
pub raw_genesis: RawGenesis,
pub version: RuntimeVersion,
pub created_at: Number,
pub updated_at: Number,
}

/// Domain runtime specific information to create domain raw genesis.
#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq, Copy)]
pub enum DomainRuntimeInfo {
Expand Down Expand Up @@ -91,63 +77,59 @@ fn derive_initial_balances_storages<T: Config, AccountId: Encode>(
initial_storages
}

impl<Number, Hash> RuntimeObject<Number, Hash> {
// Return a complete raw genesis with runtime code and domain id set properly
pub fn into_complete_raw_genesis<T: Config>(
self,
domain_id: DomainId,
domain_runtime_info: DomainRuntimeInfo,
total_issuance: BalanceOf<T>,
initial_balances: Vec<(MultiAccountId, BalanceOf<T>)>,
) -> Result<RawGenesis, Error> {
let RuntimeObject {
mut raw_genesis, ..
} = self;
raw_genesis.set_domain_id(domain_id);
match domain_runtime_info {
DomainRuntimeInfo::EVM { chain_id } => {
raw_genesis.set_evm_chain_id(chain_id);
let initial_balances = initial_balances.into_iter().try_fold(
Vec::<(AccountId20, BalanceOf<T>)>::new(),
|mut balances, (account_id, balance)| {
let account_id =
domain_runtime_primitives::AccountId20Converter::try_convert_back(
account_id,
)
.ok_or(Error::InvalidAccountIdType)?;

balances.push((account_id, balance));
Ok(balances)
},
)?;
raw_genesis.set_top_storages(derive_initial_balances_storages::<T, _>(
total_issuance,
initial_balances,
));
}
DomainRuntimeInfo::AutoId => {
let initial_balances = initial_balances.into_iter().try_fold(
Vec::<(AccountId32, BalanceOf<T>)>::new(),
|mut balances, (account_id, balance)| {
let account_id =
domain_runtime_primitives::AccountIdConverter::try_convert_back(
account_id,
)
// Return a complete raw genesis with runtime code and domain id set properly
pub fn into_complete_raw_genesis<T: Config>(
runtime_obj: RuntimeObject<BlockNumberFor<T>, T::Hash>,
domain_id: DomainId,
domain_runtime_info: DomainRuntimeInfo,
total_issuance: BalanceOf<T>,
initial_balances: Vec<(MultiAccountId, BalanceOf<T>)>,
) -> Result<RawGenesis, Error> {
let RuntimeObject {
mut raw_genesis, ..
} = runtime_obj;
raw_genesis.set_domain_id(domain_id);
match domain_runtime_info {
DomainRuntimeInfo::EVM { chain_id } => {
raw_genesis.set_evm_chain_id(chain_id);
let initial_balances = initial_balances.into_iter().try_fold(
Vec::<(AccountId20, BalanceOf<T>)>::new(),
|mut balances, (account_id, balance)| {
let account_id =
domain_runtime_primitives::AccountId20Converter::try_convert_back(
account_id,
)
.ok_or(Error::InvalidAccountIdType)?;

balances.push((account_id, balance));
Ok(balances)
},
)?;
raw_genesis.set_top_storages(derive_initial_balances_storages::<T, _>(
total_issuance,
initial_balances,
));
}
DomainRuntimeInfo::AutoId => {
let initial_balances = initial_balances.into_iter().try_fold(
Vec::<(AccountId32, BalanceOf<T>)>::new(),
|mut balances, (account_id, balance)| {
let account_id =
domain_runtime_primitives::AccountIdConverter::try_convert_back(account_id)
.ok_or(Error::InvalidAccountIdType)?;

balances.push((account_id, balance));
Ok(balances)
},
)?;
raw_genesis.set_top_storages(derive_initial_balances_storages::<T, _>(
total_issuance,
initial_balances,
));
}
balances.push((account_id, balance));
Ok(balances)
},
)?;
raw_genesis.set_top_storages(derive_initial_balances_storages::<T, _>(
total_issuance,
initial_balances,
));
}

Ok(raw_genesis)
}

Ok(raw_genesis)
}

#[derive(TypeInfo, Debug, Encode, Decode, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -318,7 +300,7 @@ pub(crate) fn do_upgrade_runtimes<T: Config>(at: BlockNumberFor<T>) {
#[cfg(test)]
mod tests {
use crate::pallet::{NextRuntimeId, RuntimeRegistry, ScheduledRuntimeUpgrades};
use crate::runtime_registry::{Error as RuntimeRegistryError, RuntimeObject};
use crate::runtime_registry::Error as RuntimeRegistryError;
use crate::tests::{
new_test_ext, DomainRuntimeUpgradeDelay, Domains, ReadRuntimeVersion, System, Test,
};
Expand All @@ -328,7 +310,7 @@ mod tests {
use frame_support::dispatch::RawOrigin;
use frame_support::traits::OnInitialize;
use sp_domains::storage::RawGenesis;
use sp_domains::{DomainsDigestItem, RuntimeId, RuntimeType};
use sp_domains::{DomainsDigestItem, RuntimeId, RuntimeObject, RuntimeType};
use sp_runtime::traits::BlockNumberProvider;
use sp_runtime::{Digest, DispatchError};
use sp_version::RuntimeVersion;
Expand Down
2 changes: 1 addition & 1 deletion crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,7 +1280,7 @@ fn test_invalid_domain_block_hash_fraud_proof() {
fn generate_invalid_domain_block_hash_fraud_proof<T: Config>(
digest: Digest,
) -> (T::Hash, StorageProof) {
let digest_storage_key = sp_domains_fraud_proof::fraud_proof::system_digest_final_key();
let digest_storage_key = sp_domains::system_digest_final_key();
let mut root = T::Hash::default();
let mut mdb = PrefixedMemoryDB::<T::Hashing>::default();
{
Expand Down
32 changes: 12 additions & 20 deletions crates/pallet-transaction-fees/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@

mod default_weights;

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use codec::{Codec, Decode, Encode};
use frame_support::sp_runtime::traits::Zero;
use frame_support::sp_runtime::SaturatedConversion;
use frame_support::traits::{tokens, Currency, Get};
use frame_support::traits::{Currency, Get};
use frame_support::weights::Weight;
use frame_system::pallet_prelude::*;
pub use pallet::*;
Expand All @@ -45,30 +50,13 @@ struct CollectedFees<Balance: Codec> {
tips: Balance,
}

#[derive(Encode, Decode, TypeInfo)]
struct BlockTransactionByteFee<Balance: Codec> {
// The value of `transaction_byte_fee` for the current block
current: Balance,
// The value of `transaction_byte_fee` for the next block
next: Balance,
}

impl<Balance: Codec + tokens::Balance> Default for BlockTransactionByteFee<Balance> {
fn default() -> Self {
BlockTransactionByteFee {
current: Balance::max_value(),
next: Balance::max_value(),
}
}
}

#[frame_support::pallet]
mod pallet {
use super::{BalanceOf, BlockTransactionByteFee, CollectedFees, WeightInfo};
use super::{BalanceOf, CollectedFees, WeightInfo};
use frame_support::pallet_prelude::*;
use frame_support::traits::Currency;
use frame_system::pallet_prelude::*;
use subspace_runtime_primitives::FindBlockRewardAddress;
Comment on lines -48 to -71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this refactoring necessary? It was an implementation detail of pallet-transaction-fees, you shouldn't need to access it from the outside. I also don't understand how it helps you if the rest of APIs that worked with this type remained private.

use subspace_runtime_primitives::{BlockTransactionByteFee, FindBlockRewardAddress};

#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down Expand Up @@ -254,6 +242,10 @@ where
}
}

pub fn transaction_byte_fee_storage_key() -> Vec<u8> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly I really don't like that this abstraction is now leaking to the outside. Can you explain why it is needed and why can't it be achieved without making more implemenation details public?

TransactionByteFee::<T>::hashed_key().to_vec()
}

pub fn calculate_transaction_byte_fee() -> BalanceOf<T> {
let credit_supply = T::CreditSupply::get();

Expand Down
2 changes: 1 addition & 1 deletion crates/sp-domains-fraud-proof/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ where
DomainHeader: HeaderT,
{
let state_root = bad_receipt.final_state_root;
let digest_storage_key = StorageKey(crate::fraud_proof::system_digest_final_key());
let digest_storage_key = StorageKey(sp_domains::system_digest_final_key());

let digest = StorageProofVerifier::<DomainHeader::Hashing>::get_decoded_value::<Digest>(
&state_root,
Expand Down
Loading