diff --git a/Cargo.lock b/Cargo.lock index fb3c78adf2..01c0aac10d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7136,6 +7136,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-block-fees", + "sp-domains", "sp-runtime", "sp-std", ] diff --git a/crates/pallet-domains/src/tests.rs b/crates/pallet-domains/src/tests.rs index 1ce914cc8b..f4a2a9b217 100644 --- a/crates/pallet-domains/src/tests.rs +++ b/crates/pallet-domains/src/tests.rs @@ -884,7 +884,7 @@ fn test_invalid_block_fees_fraud_proof() { domain_id, bad_receipt_hash, // set different reward in the storage and generate proof for that value - domain_runtime_primitives::BlockFees::new( + sp_domains::BlockFees::new( domain_block .execution_receipt .block_fees @@ -909,7 +909,7 @@ type FraudProofFor = fn generate_invalid_block_fees_fraud_proof( domain_id: DomainId, bad_receipt_hash: ReceiptHashFor, - block_fees: domain_runtime_primitives::BlockFees>, + block_fees: sp_domains::BlockFees>, ) -> (FraudProofFor, T::Hash) { let storage_key = sp_domains_fraud_proof::fraud_proof::operator_block_fees_final_key(); let mut root = T::Hash::default(); diff --git a/crates/sp-domains-fraud-proof/src/tests.rs b/crates/sp-domains-fraud-proof/src/tests.rs index 32550adc34..4a067b0e4d 100644 --- a/crates/sp-domains-fraud-proof/src/tests.rs +++ b/crates/sp-domains-fraud-proof/src/tests.rs @@ -2,7 +2,7 @@ use crate::test_ethereum_tx::{ EIP1559UnsignedTransaction, EIP2930UnsignedTransaction, LegacyUnsignedTransaction, }; use codec::Encode; -use domain_runtime_primitives::{Balance, CheckExtrinsicsValidityError, DomainCoreApi}; +use domain_runtime_primitives::{Balance, CheckExtrinsicsValidityError}; use domain_test_service::evm_domain_test_runtime::{ Runtime as TestRuntime, RuntimeCall, UncheckedExtrinsic as RuntimeUncheckedExtrinsic, }; @@ -23,6 +23,7 @@ use sp_api::{ApiExt, ProvideRuntimeApi, TransactionOutcome}; use sp_core::crypto::AccountId32; use sp_core::ecdsa::Pair; use sp_core::{keccak_256, Pair as _, H160, H256, U256}; +use sp_domains::core_api::DomainCoreApi; use sp_runtime::traits::{Extrinsic, Zero}; use sp_runtime::transaction_validity::{InvalidTransaction, TransactionValidityError}; use sp_runtime::OpaqueExtrinsic; @@ -379,7 +380,7 @@ async fn storage_change_of_the_same_runtime_instance_should_perserved_cross_runt best_hash, vec![ transfer_to_charlie_with_big_tip_1.clone().into(), - transfer_to_charlie_with_big_tip_2.clone().into() + transfer_to_charlie_with_big_tip_2.clone().into(), ], best_number, best_hash, @@ -389,7 +390,7 @@ async fn storage_change_of_the_same_runtime_instance_should_perserved_cross_runt extrinsic_index: 1, transaction_validity_error: TransactionValidityError::Invalid( InvalidTransaction::Payment - ) + ), }) ); @@ -411,7 +412,7 @@ async fn storage_change_of_the_same_runtime_instance_should_perserved_cross_runt extrinsic_index: 0, transaction_validity_error: TransactionValidityError::Invalid( InvalidTransaction::Future - ) + ), }) ); @@ -460,7 +461,7 @@ async fn storage_change_of_the_same_runtime_instance_should_perserved_cross_runt best_hash, vec![transfer_with_big_tip_1.clone().into()], best_number, - best_hash + best_hash, ) .unwrap() .is_ok()); @@ -491,7 +492,7 @@ async fn storage_change_of_the_same_runtime_instance_should_perserved_cross_runt best_hash, vec![transfer_with_big_tip_3.clone().into()], best_number, - best_hash + best_hash, ) .unwrap(), if commit_mode { diff --git a/crates/sp-domains-fraud-proof/src/verification.rs b/crates/sp-domains-fraud-proof/src/verification.rs index 78cb38ceb8..9a61f144b4 100644 --- a/crates/sp-domains-fraud-proof/src/verification.rs +++ b/crates/sp-domains-fraud-proof/src/verification.rs @@ -8,7 +8,6 @@ use crate::{ FraudProofVerificationInfoResponse, SetCodeExtrinsic, StorageKeyRequest, }; use codec::{Decode, Encode}; -use domain_runtime_primitives::{BlockFees, Transfers}; use hash_db::Hasher; use sp_core::storage::StorageKey; use sp_core::H256; @@ -17,8 +16,9 @@ use sp_domains::extrinsics::{deduplicate_and_shuffle_extrinsics, extrinsics_shuf use sp_domains::proof_provider_and_verifier::StorageProofVerifier; use sp_domains::valued_trie::valued_ordered_trie_root; use sp_domains::{ - BundleValidity, ExecutionReceipt, ExtrinsicDigest, HeaderHashFor, HeaderHashingFor, + BlockFees, BundleValidity, ExecutionReceipt, ExtrinsicDigest, HeaderHashFor, HeaderHashingFor, HeaderNumberFor, InboxedBundle, InvalidBundleType, OperatorPublicKey, SealedBundleHeader, + Transfers, }; use sp_runtime::generic::Digest; use sp_runtime::traits::{Block as BlockT, Hash, Header as HeaderT, NumberFor}; diff --git a/crates/sp-domains/src/core_api.rs b/crates/sp-domains/src/core_api.rs new file mode 100644 index 0000000000..f363eb6b52 --- /dev/null +++ b/crates/sp-domains/src/core_api.rs @@ -0,0 +1,82 @@ +use crate::{BlockFees, Transfers}; +use domain_runtime_primitives::{ + opaque, Balance, CheckExtrinsicsValidityError, DecodeExtrinsicError, +}; +use sp_runtime::generic::Era; +use sp_runtime::traits::{Block as BlockT, NumberFor}; +use sp_runtime::Digest; +use sp_std::vec::Vec; +use sp_weights::Weight; +use subspace_core_primitives::U256; +use subspace_runtime_primitives::Moment; + +sp_api::decl_runtime_apis! { + /// Base API that every domain runtime must implement. + pub trait DomainCoreApi { + /// Extracts the optional signer per extrinsic. + fn extract_signer( + extrinsics: Vec<::Extrinsic>, + ) -> Vec<(Option, ::Extrinsic)>; + + fn is_within_tx_range( + extrinsic: &::Extrinsic, + bundle_vrf_hash: &U256, + tx_range: &U256, + ) -> bool; + + /// Returns the intermediate storage roots in an encoded form. + fn intermediate_roots() -> Vec<[u8; 32]>; + + /// Returns the storage root after initializing the block. + fn initialize_block_with_post_state_root(header: &::Header) -> Vec; + + /// Returns the storage root after applying the extrinsic. + fn apply_extrinsic_with_post_state_root(extrinsic: ::Extrinsic) -> Vec; + + /// Returns an encoded extrinsic aiming to upgrade the runtime using given code. + fn construct_set_code_extrinsic(code: Vec) -> Vec; + + /// Returns an encoded extrinsic to set timestamp. + fn construct_timestamp_extrinsic(moment: Moment) -> Block::Extrinsic; + + /// Returns an encoded extrinsic to set domain transaction byte fee. + fn construct_consensus_chain_byte_fee_extrinsic(consensus_chain_byte_fee: Balance) -> Block::Extrinsic; + + /// Returns true if the extrinsic is an inherent extrinsic. + fn is_inherent_extrinsic(extrinsic: &::Extrinsic) -> bool; + + /// Checks the validity of array of extrinsics + pre_dispatch + /// returning failure on first extrinsic that fails runtime call. + /// IMPORTANT: Change `CHECK_EXTRINSICS_AND_DO_PRE_DISPATCH_METHOD_NAME` constant when this method name is changed + fn check_extrinsics_and_do_pre_dispatch(uxts: Vec<::Extrinsic>, block_number: NumberFor, + block_hash: ::Hash) -> Result<(), CheckExtrinsicsValidityError>; + + /// Decodes the domain specific extrinsic from the opaque extrinsic. + fn decode_extrinsic( + opaque_extrinsic: sp_runtime::OpaqueExtrinsic, + ) -> Result<::Extrinsic, DecodeExtrinsicError>; + + /// Returns extrinsic Era if present. + fn extrinsic_era( + extrinsic: &::Extrinsic + ) -> Option; + + /// Returns the extrinsic weight. + fn extrinsic_weight(ext: &Block::Extrinsic) -> Weight; + + /// The accumulated transaction fee of all transactions included in the block. + fn block_fees() -> BlockFees; + + /// Returns the block digest. + fn block_digest() -> Digest; + + /// Returns the consumed weight of the block. + fn block_weight() -> Weight; + + /// Returns the transfers for this domain in the block. + fn transfers() -> Transfers; + + /// Returns the storage key for the Transfers on Domain. + fn transfers_storage_key() -> Vec; + } +} diff --git a/crates/sp-domains/src/lib.rs b/crates/sp-domains/src/lib.rs index 5a305935fa..c7289d4b5e 100644 --- a/crates/sp-domains/src/lib.rs +++ b/crates/sp-domains/src/lib.rs @@ -18,6 +18,7 @@ #![cfg_attr(not(feature = "std"), no_std)] pub mod bundle_producer_election; +pub mod core_api; pub mod extrinsics; pub mod merkle_tree; pub mod proof_provider_and_verifier; @@ -34,7 +35,7 @@ use bundle_producer_election::{BundleProducerElectionParams, ProofOfElectionErro use core::num::ParseIntError; use core::ops::{Add, Sub}; use core::str::FromStr; -use domain_runtime_primitives::{BlockFees, MultiAccountId, Transfers}; +use domain_runtime_primitives::MultiAccountId; use frame_support::storage::storage_prefix; use frame_support::{Blake2_128Concat, StorageHasher}; use hexlit::hex; @@ -248,6 +249,42 @@ impl From for ChainId { } } +#[derive(Clone, Debug, Decode, Default, Encode, Eq, PartialEq, TypeInfo)] +pub struct BlockFees { + /// The consensus chain storage fee + pub consensus_storage_fee: Balance, + /// The domain execution fee including the storage and compute fee on domain chain, + /// tip, and the XDM reward. + pub domain_execution_fee: Balance, +} + +impl BlockFees +where + Balance: CheckedAdd, +{ + pub fn new(domain_execution_fee: Balance, consensus_storage_fee: Balance) -> Self { + BlockFees { + consensus_storage_fee, + domain_execution_fee, + } + } + + /// Returns the total fees that was collected and burned on the Domain. + pub fn total_fees(&self) -> Option { + self.consensus_storage_fee + .checked_add(&self.domain_execution_fee) + } +} + +/// Type that holds the transfers(in/out) for a given chain. +#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone, Default)] +pub struct Transfers { + /// Total transfers that came into the domain. + pub transfers_in: Balance, + /// Total transfers that went out of the domain. + pub transfers_out: Balance, +} + #[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone)] pub struct BundleHeader { /// Proof of bundle producer election. diff --git a/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs b/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs index bc197707a2..8722012be5 100644 --- a/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs +++ b/crates/subspace-malicious-operator/src/malicious_bundle_producer.rs @@ -3,7 +3,6 @@ use domain_client_operator::domain_bundle_producer::DomainBundleProducer; use domain_client_operator::domain_bundle_proposer::DomainBundleProposer; use domain_client_operator::{OpaqueBundleFor, OperatorSlotInfo}; use domain_runtime_primitives::opaque::Block as DomainBlock; -use domain_runtime_primitives::DomainCoreApi; use frame_system_rpc_runtime_api::AccountNonceApi; use futures::{Stream, StreamExt, TryFutureExt}; use pallet_domains::OperatorConfig; @@ -20,6 +19,7 @@ use sp_consensus_slots::Slot; use sp_consensus_subspace::FarmerPublicKey; use sp_core::crypto::UncheckedFrom; use sp_core::Get; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{BundleProducerElectionApi, DomainId, DomainsApi, OperatorId, OperatorPublicKey}; use sp_keyring::Sr25519Keyring; use sp_keystore::KeystorePtr; diff --git a/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs b/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs index 2d2690b65b..81cb025460 100644 --- a/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs +++ b/crates/subspace-malicious-operator/src/malicious_bundle_tamper.rs @@ -1,12 +1,13 @@ use domain_client_operator::{ExecutionReceiptFor, OpaqueBundleFor}; -use domain_runtime_primitives::{BlockFees, DomainCoreApi}; use parity_scale_codec::{Decode, Encode}; use sc_client_api::HeaderBackend; use sp_api::ProvideRuntimeApi; use sp_domain_digests::AsPredigest; +use sp_domains::core_api::DomainCoreApi; use sp_domains::merkle_tree::MerkleTree; use sp_domains::{ - BundleValidity, HeaderHashingFor, InvalidBundleType, OperatorPublicKey, OperatorSignature, + BlockFees, BundleValidity, HeaderHashingFor, InvalidBundleType, OperatorPublicKey, + OperatorSignature, }; use sp_keystore::KeystorePtr; use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT, NumberFor, One, Zero}; diff --git a/domains/client/block-preprocessor/src/lib.rs b/domains/client/block-preprocessor/src/lib.rs index 7dcea37fb5..6421906111 100644 --- a/domains/client/block-preprocessor/src/lib.rs +++ b/domains/client/block-preprocessor/src/lib.rs @@ -18,11 +18,11 @@ use crate::inherents::is_runtime_upgraded; use crate::xdm_verifier::is_valid_xdm; use codec::Encode; use domain_runtime_primitives::opaque::AccountId; -use domain_runtime_primitives::DomainCoreApi; use sc_client_api::BlockBackend; use sp_api::ProvideRuntimeApi; use sp_blockchain::HeaderBackend; use sp_core::H256; +use sp_domains::core_api::DomainCoreApi; use sp_domains::extrinsics::deduplicate_and_shuffle_extrinsics; use sp_domains::{ DomainId, DomainsApi, ExecutionReceipt, ExtrinsicDigest, HeaderHashingFor, InboxedBundle, diff --git a/domains/client/block-preprocessor/src/stateless_runtime.rs b/domains/client/block-preprocessor/src/stateless_runtime.rs index c66409d5e5..17da78aa79 100644 --- a/domains/client/block-preprocessor/src/stateless_runtime.rs +++ b/domains/client/block-preprocessor/src/stateless_runtime.rs @@ -1,12 +1,11 @@ use codec::{Codec, Encode}; use domain_runtime_primitives::opaque::AccountId; -use domain_runtime_primitives::{ - Balance, CheckExtrinsicsValidityError, DecodeExtrinsicError, DomainCoreApi, -}; +use domain_runtime_primitives::{Balance, CheckExtrinsicsValidityError, DecodeExtrinsicError}; use sc_executor::RuntimeVersionOf; use sp_api::{ApiError, Core}; use sp_core::traits::{CallContext, CodeExecutor, FetchRuntimeCode, RuntimeCode}; use sp_core::Hasher; +use sp_domains::core_api::DomainCoreApi; use sp_messenger::messages::ExtractedStateRootsFromProof; use sp_messenger::MessengerApi; use sp_runtime::traits::{Block as BlockT, NumberFor}; diff --git a/domains/client/domain-operator/src/bundle_processor.rs b/domains/client/domain-operator/src/bundle_processor.rs index f9cb4d6c65..3c36eb5d82 100644 --- a/domains/client/domain-operator/src/bundle_processor.rs +++ b/domains/client/domain-operator/src/bundle_processor.rs @@ -3,7 +3,6 @@ use crate::domain_block_processor::{ }; use crate::ExecutionReceiptFor; use domain_block_preprocessor::DomainBlockPreprocessor; -use domain_runtime_primitives::DomainCoreApi; use sc_client_api::{AuxStore, BlockBackend, Finalizer, ProofProvider}; use sc_consensus::{BlockImportParams, ForkChoiceStrategy, StateAction}; use sp_api::ProvideRuntimeApi; @@ -12,6 +11,7 @@ use sp_consensus::BlockOrigin; use sp_core::traits::CodeExecutor; use sp_core::H256; use sp_domain_digests::AsPredigest; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{DomainId, DomainsApi, ReceiptValidity}; use sp_domains_fraud_proof::FraudProofApi; use sp_messenger::MessengerApi; diff --git a/domains/client/domain-operator/src/domain_block_processor.rs b/domains/client/domain-operator/src/domain_block_processor.rs index 83b7f144be..c03a273128 100644 --- a/domains/client/domain-operator/src/domain_block_processor.rs +++ b/domains/client/domain-operator/src/domain_block_processor.rs @@ -6,7 +6,6 @@ use codec::{Decode, Encode}; use domain_block_builder::{BlockBuilder, BuiltBlock, RecordProof}; use domain_block_preprocessor::inherents::get_inherent_data; use domain_block_preprocessor::PreprocessResult; -use domain_runtime_primitives::DomainCoreApi; use sc_client_api::{AuxStore, BlockBackend, Finalizer, ProofProvider}; use sc_consensus::{ BlockImportParams, ForkChoiceStrategy, ImportResult, SharedBlockImport, StateAction, @@ -18,6 +17,7 @@ use sp_blockchain::{HashAndNumber, HeaderBackend, HeaderMetadata}; use sp_consensus::{BlockOrigin, SyncOracle}; use sp_core::traits::CodeExecutor; use sp_core::H256; +use sp_domains::core_api::DomainCoreApi; use sp_domains::merkle_tree::MerkleTree; use sp_domains::{BundleValidity, DomainId, DomainsApi, ExecutionReceipt, HeaderHashingFor}; use sp_domains_fraud_proof::fraud_proof::{FraudProof, ValidBundleProof}; diff --git a/domains/client/domain-operator/src/domain_bundle_producer.rs b/domains/client/domain-operator/src/domain_bundle_producer.rs index 81334b6f32..5014b64ec9 100644 --- a/domains/client/domain-operator/src/domain_bundle_producer.rs +++ b/domains/client/domain-operator/src/domain_bundle_producer.rs @@ -3,11 +3,11 @@ use crate::domain_bundle_proposer::DomainBundleProposer; use crate::utils::OperatorSlotInfo; use crate::BundleSender; use codec::Decode; -use domain_runtime_primitives::DomainCoreApi; use sc_client_api::{AuxStore, BlockBackend}; use sp_api::ProvideRuntimeApi; use sp_block_builder::BlockBuilder; use sp_blockchain::HeaderBackend; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{ Bundle, BundleProducerElectionApi, DomainId, DomainsApi, OperatorId, OperatorPublicKey, OperatorSignature, SealedBundleHeader, diff --git a/domains/client/domain-operator/src/domain_bundle_proposer.rs b/domains/client/domain-operator/src/domain_bundle_proposer.rs index ffd1f35925..927e23b0f4 100644 --- a/domains/client/domain-operator/src/domain_bundle_proposer.rs +++ b/domains/client/domain-operator/src/domain_bundle_proposer.rs @@ -1,12 +1,12 @@ use crate::ExecutionReceiptFor; use codec::Encode; -use domain_runtime_primitives::DomainCoreApi; use futures::{select, FutureExt}; use sc_client_api::{AuxStore, BlockBackend}; use sc_transaction_pool_api::InPoolTransaction; use sp_api::{ApiExt, ProvideRuntimeApi}; use sp_block_builder::BlockBuilder; use sp_blockchain::HeaderBackend; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{ BundleHeader, DomainId, DomainsApi, ExecutionReceipt, HeaderHashingFor, ProofOfElection, }; diff --git a/domains/client/domain-operator/src/domain_worker.rs b/domains/client/domain-operator/src/domain_worker.rs index d18942c988..154483a4e1 100644 --- a/domains/client/domain-operator/src/domain_worker.rs +++ b/domains/client/domain-operator/src/domain_worker.rs @@ -18,7 +18,6 @@ use crate::bundle_processor::BundleProcessor; use crate::domain_bundle_producer::DomainBundleProducer; use crate::utils::{BlockInfo, OperatorSlotInfo}; use crate::{NewSlotNotification, OperatorStreams}; -use domain_runtime_primitives::DomainCoreApi; use futures::channel::mpsc; use futures::{SinkExt, Stream, StreamExt}; use sc_client_api::{ @@ -30,6 +29,7 @@ use sp_block_builder::BlockBuilder; use sp_blockchain::{HeaderBackend, HeaderMetadata}; use sp_core::traits::{CodeExecutor, SpawnEssentialNamed}; use sp_core::H256; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{BundleProducerElectionApi, DomainsApi, OpaqueBundle, OperatorId}; use sp_domains_fraud_proof::FraudProofApi; use sp_messenger::MessengerApi; diff --git a/domains/client/domain-operator/src/fraud_proof.rs b/domains/client/domain-operator/src/fraud_proof.rs index 35ae92b83e..ed07f9c5b4 100644 --- a/domains/client/domain-operator/src/fraud_proof.rs +++ b/domains/client/domain-operator/src/fraud_proof.rs @@ -3,13 +3,14 @@ use crate::ExecutionReceiptFor; use codec::{Decode, Encode}; use domain_block_builder::{BlockBuilder, RecordProof}; use domain_runtime_primitives::opaque::AccountId; -use domain_runtime_primitives::{CheckExtrinsicsValidityError, DomainCoreApi}; +use domain_runtime_primitives::CheckExtrinsicsValidityError; use sc_client_api::{AuxStore, BlockBackend, ProofProvider}; use sp_api::{ApiExt, ProvideRuntimeApi}; use sp_blockchain::HeaderBackend; use sp_core::traits::CodeExecutor; use sp_core::H256; use sp_domain_digests::AsPredigest; +use sp_domains::core_api::DomainCoreApi; use sp_domains::proof_provider_and_verifier::StorageProofProvider; use sp_domains::{DomainId, DomainsApi, ExtrinsicDigest, HeaderHashingFor, InvalidBundleType}; use sp_domains_fraud_proof::execution_prover::ExecutionProver; diff --git a/domains/client/domain-operator/src/operator.rs b/domains/client/domain-operator/src/operator.rs index da8c9521e3..9e8abfb5ff 100644 --- a/domains/client/domain-operator/src/operator.rs +++ b/domains/client/domain-operator/src/operator.rs @@ -4,7 +4,6 @@ use crate::domain_bundle_producer::DomainBundleProducer; use crate::domain_bundle_proposer::DomainBundleProposer; use crate::fraud_proof::FraudProofGenerator; use crate::{DomainImportNotifications, NewSlotNotification, OperatorParams}; -use domain_runtime_primitives::DomainCoreApi; use futures::channel::mpsc; use futures::{FutureExt, Stream}; use sc_client_api::{ @@ -15,6 +14,7 @@ use sp_api::ProvideRuntimeApi; use sp_blockchain::{HeaderBackend, HeaderMetadata}; use sp_core::traits::{CodeExecutor, SpawnEssentialNamed}; use sp_core::H256; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{BundleProducerElectionApi, DomainsApi}; use sp_domains_fraud_proof::FraudProofApi; use sp_keystore::KeystorePtr; diff --git a/domains/client/domain-operator/src/tests.rs b/domains/client/domain-operator/src/tests.rs index 7c3c633035..d37b1e8858 100644 --- a/domains/client/domain-operator/src/tests.rs +++ b/domains/client/domain-operator/src/tests.rs @@ -5,7 +5,7 @@ use crate::fraud_proof::{FraudProofGenerator, TraceDiffType}; use crate::tests::TxPoolError::InvalidTransaction as TxPoolInvalidTransaction; use crate::OperatorSlotInfo; use codec::{Decode, Encode}; -use domain_runtime_primitives::{DomainCoreApi, Hash, Transfers}; +use domain_runtime_primitives::Hash; use domain_test_primitives::{OnchainStateApi, TimestampApi}; use domain_test_service::evm_domain_test_runtime::{Header, UncheckedExtrinsic}; use domain_test_service::EcdsaKeyring::{Alice, Bob, Charlie, Eve}; @@ -24,9 +24,11 @@ use sp_core::storage::StateVersion; use sp_core::traits::FetchRuntimeCode; use sp_core::{Pair, H256}; use sp_domain_digests::AsPredigest; +use sp_domains::core_api::DomainCoreApi; use sp_domains::merkle_tree::MerkleTree; use sp_domains::{ Bundle, BundleValidity, DomainsApi, HeaderHashingFor, InboxedBundle, InvalidBundleType, + Transfers, }; use sp_domains_fraud_proof::fraud_proof::{ ApplyExtrinsicMismatch, ExecutionPhase, FinalizeBlockMismatch, FraudProof, diff --git a/domains/pallets/block-fees/Cargo.toml b/domains/pallets/block-fees/Cargo.toml index 90372266db..4009c325e0 100644 --- a/domains/pallets/block-fees/Cargo.toml +++ b/domains/pallets/block-fees/Cargo.toml @@ -8,8 +8,8 @@ homepage = "https://subspace.network" repository = "https://github.com/subspace/subspace" description = "Subspace node pallet for charging and re-distributing domain transaction fees" include = [ - "/src", - "/Cargo.toml", + "/src", + "/Cargo.toml", ] [dependencies] @@ -20,18 +20,20 @@ frame-system = { version = "4.0.0-dev", default-features = false, git = "https:/ pallet-transaction-payment = { version = "4.0.0-dev", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } scale-info = { version = "2.7.0", default-features = false, features = ["derive"] } sp-block-fees = { version = "0.1.0", path = "../../primitives/block-fees", default-features = false } +sp-domains = { version = "0.1.0", path = "../../../crates/sp-domains", default-features = false } sp-runtime = { version = "24.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } sp-std = { version = "8.0.0", default-features = false, git = "https://github.com/subspace/polkadot-sdk", rev = "d6b500960579d73c43fc4ef550b703acfa61c4c8" } [features] default = ["std"] std = [ - "codec/std", - "domain-runtime-primitives/std", - "frame-support/std", - "frame-system/std", - "scale-info/std", - "sp-block-fees/std", - "sp-runtime/std", - "sp-std/std", + "codec/std", + "domain-runtime-primitives/std", + "frame-support/std", + "frame-system/std", + "scale-info/std", + "sp-block-fees/std", + "sp-domains/std", + "sp-runtime/std", + "sp-std/std", ] diff --git a/domains/pallets/block-fees/src/lib.rs b/domains/pallets/block-fees/src/lib.rs index b26a2c0ba4..99370eb1d2 100644 --- a/domains/pallets/block-fees/src/lib.rs +++ b/domains/pallets/block-fees/src/lib.rs @@ -24,11 +24,11 @@ pub use pallet::*; #[frame_support::pallet] mod pallet { use codec::{Codec, MaxEncodedLen}; - use domain_runtime_primitives::BlockFees; use frame_support::pallet_prelude::*; use frame_system::pallet_prelude::*; use scale_info::TypeInfo; use sp_block_fees::{InherentError, InherentType, INHERENT_IDENTIFIER}; + use sp_domains::BlockFees; use sp_runtime::traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize, Saturating}; use sp_runtime::{FixedPointOperand, SaturatedConversion}; use sp_std::fmt::Debug; @@ -98,10 +98,10 @@ mod pallet { impl Pallet { #[pallet::call_index(0)] #[pallet::weight(( - // TODO: proper weight - Weight::from_all(10_000), - DispatchClass::Mandatory - ))] + // TODO: proper weight + Weight::from_all(10_000), + DispatchClass::Mandatory + ))] pub fn set_next_consensus_chain_byte_fee( origin: OriginFor, #[pallet::compact] transaction_byte_fee: T::Balance, diff --git a/domains/pallets/transporter/src/lib.rs b/domains/pallets/transporter/src/lib.rs index 8bcba75d08..9794019e30 100644 --- a/domains/pallets/transporter/src/lib.rs +++ b/domains/pallets/transporter/src/lib.rs @@ -72,12 +72,11 @@ mod pallet { use crate::weights::WeightInfo; use crate::{BalanceOf, Location, MessageIdOf, MultiAccountId, Transfer, TryConvertBack}; use codec::{Decode, Encode}; - use domain_runtime_primitives::Transfers; use frame_support::pallet_prelude::*; use frame_support::traits::{Currency, ExistenceRequirement, WithdrawReasons}; use frame_support::weights::Weight; use frame_system::pallet_prelude::*; - use sp_domains::DomainId; + use sp_domains::{DomainId, Transfers}; use sp_messenger::endpoint::{ Endpoint, EndpointHandler as EndpointHandlerT, EndpointId, EndpointRequest, EndpointResponse, Sender, diff --git a/domains/primitives/runtime/src/lib.rs b/domains/primitives/runtime/src/lib.rs index 2219aae437..9b3d6db4fc 100644 --- a/domains/primitives/runtime/src/lib.rs +++ b/domains/primitives/runtime/src/lib.rs @@ -27,16 +27,13 @@ use frame_system::limits::{BlockLength, BlockWeights}; use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use serde::{Deserialize, Serialize}; -use sp_runtime::generic::{Era, UncheckedExtrinsic}; -use sp_runtime::traits::{ - Block as BlockT, CheckedAdd, Convert, IdentifyAccount, NumberFor, Verify, -}; +use sp_runtime::generic::UncheckedExtrinsic; +use sp_runtime::traits::{Convert, IdentifyAccount, Verify}; use sp_runtime::transaction_validity::TransactionValidityError; -use sp_runtime::{Digest, MultiAddress, MultiSignature, Perbill}; +use sp_runtime::{MultiAddress, MultiSignature, Perbill}; use sp_std::vec::Vec; use sp_weights::Weight; -use subspace_core_primitives::U256; -use subspace_runtime_primitives::{Moment, SHANNON}; +use subspace_runtime_primitives::SHANNON; /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. pub type Signature = MultiSignature; @@ -202,42 +199,6 @@ pub struct DecodeExtrinsicError(pub String); pub const CHECK_EXTRINSICS_AND_DO_PRE_DISPATCH_METHOD_NAME: &str = "DomainCoreApi_check_extrinsics_and_do_pre_dispatch"; -#[derive(Clone, Debug, Decode, Default, Encode, Eq, PartialEq, TypeInfo)] -pub struct BlockFees { - /// The consensus chain storage fee - pub consensus_storage_fee: Balance, - /// The domain execution fee including the storage and compute fee on domain chain, - /// tip, and the XDM reward. - pub domain_execution_fee: Balance, -} - -/// Type that holds the transfers(in/out) for a given chain. -#[derive(Debug, Decode, Encode, TypeInfo, PartialEq, Eq, Clone, Default)] -pub struct Transfers { - /// Total transfers that came into the domain. - pub transfers_in: Balance, - /// Total transfers that went out of the domain. - pub transfers_out: Balance, -} - -impl BlockFees -where - Balance: CheckedAdd, -{ - pub fn new(domain_execution_fee: Balance, consensus_storage_fee: Balance) -> Self { - BlockFees { - consensus_storage_fee, - domain_execution_fee, - } - } - - /// Returns the total fees that was collected and burned on the Domain. - pub fn total_fees(&self) -> Option { - self.consensus_storage_fee - .checked_add(&self.domain_execution_fee) - } -} - /// Opaque types. These are used by the CLI to instantiate machinery that don't need to know /// the specifics of the runtime. They can then be made to be agnostic over specific formats /// of data like extrinsics, allowing for them to continue syncing the network through upgrades @@ -259,77 +220,6 @@ pub mod opaque { pub type AccountId = Vec; } -sp_api::decl_runtime_apis! { - /// Base API that every domain runtime must implement. - pub trait DomainCoreApi { - /// Extracts the optional signer per extrinsic. - fn extract_signer( - extrinsics: Vec<::Extrinsic>, - ) -> Vec<(Option, ::Extrinsic)>; - - fn is_within_tx_range( - extrinsic: &::Extrinsic, - bundle_vrf_hash: &U256, - tx_range: &U256, - ) -> bool; - - /// Returns the intermediate storage roots in an encoded form. - fn intermediate_roots() -> Vec<[u8; 32]>; - - /// Returns the storage root after initializing the block. - fn initialize_block_with_post_state_root(header: &::Header) -> Vec; - - /// Returns the storage root after applying the extrinsic. - fn apply_extrinsic_with_post_state_root(extrinsic: ::Extrinsic) -> Vec; - - /// Returns an encoded extrinsic aiming to upgrade the runtime using given code. - fn construct_set_code_extrinsic(code: Vec) -> Vec; - - /// Returns an encoded extrinsic to set timestamp. - fn construct_timestamp_extrinsic(moment: Moment) -> Block::Extrinsic; - - /// Returns an encoded extrinsic to set domain transaction byte fee. - fn construct_consensus_chain_byte_fee_extrinsic(consensus_chain_byte_fee: Balance) -> Block::Extrinsic; - - /// Returns true if the extrinsic is an inherent extrinsic. - fn is_inherent_extrinsic(extrinsic: &::Extrinsic) -> bool; - - /// Checks the validity of array of extrinsics + pre_dispatch - /// returning failure on first extrinsic that fails runtime call. - /// IMPORTANT: Change `CHECK_EXTRINSICS_AND_DO_PRE_DISPATCH_METHOD_NAME` constant when this method name is changed - fn check_extrinsics_and_do_pre_dispatch(uxts: Vec<::Extrinsic>, block_number: NumberFor, - block_hash: ::Hash) -> Result<(), CheckExtrinsicsValidityError>; - - /// Decodes the domain specific extrinsic from the opaque extrinsic. - fn decode_extrinsic( - opaque_extrinsic: sp_runtime::OpaqueExtrinsic, - ) -> Result<::Extrinsic, DecodeExtrinsicError>; - - /// Returns extrinsic Era if present. - fn extrinsic_era( - extrinsic: &::Extrinsic - ) -> Option; - - /// Returns the extrinsic weight. - fn extrinsic_weight(ext: &Block::Extrinsic) -> Weight; - - /// The accumulated transaction fee of all transactions included in the block. - fn block_fees() -> BlockFees; - - /// Returns the block digest. - fn block_digest() -> Digest; - - /// Returns the consumed weight of the block. - fn block_weight() -> Weight; - - /// Returns the transfers for this domain in the block. - fn transfers() -> Transfers; - - /// Returns the storage key for the Transfers on Domain. - fn transfers_storage_key() -> Vec; - } -} - #[cfg(test)] mod test { use super::block_weights; diff --git a/domains/runtime/evm/src/lib.rs b/domains/runtime/evm/src/lib.rs index 06801ff780..f22796d1a6 100644 --- a/domains/runtime/evm/src/lib.rs +++ b/domains/runtime/evm/src/lib.rs @@ -17,7 +17,7 @@ pub use domain_runtime_primitives::{ EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT, }; use domain_runtime_primitives::{ - CheckExtrinsicsValidityError, DecodeExtrinsicError, Transfers, SLOT_DURATION, + CheckExtrinsicsValidityError, DecodeExtrinsicError, SLOT_DURATION, }; use fp_account::EthereumSignature; use fp_self_contained::{CheckedSignature, SelfContainedCall}; @@ -41,7 +41,7 @@ use pallet_transporter::EndpointHandler; use sp_api::impl_runtime_apis; use sp_core::crypto::KeyTypeId; use sp_core::{Get, OpaqueMetadata, H160, H256, U256}; -use sp_domains::DomainId; +use sp_domains::{DomainId, Transfers}; use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId}; use sp_messenger::messages::{ BlockInfo, BlockMessagesWithStorageKey, ChainId, CrossDomainMessage, @@ -856,7 +856,7 @@ impl_runtime_apis! { } } - impl domain_runtime_primitives::DomainCoreApi for Runtime { + impl sp_domains::core_api::DomainCoreApi for Runtime { fn extract_signer( extrinsics: Vec<::Extrinsic>, ) -> Vec<(Option, ::Extrinsic)> { @@ -959,7 +959,7 @@ impl_runtime_apis! { ext.get_dispatch_info().weight } - fn block_fees() -> domain_runtime_primitives::BlockFees { + fn block_fees() -> sp_domains::BlockFees { BlockFees::collected_block_fees() } diff --git a/domains/service/src/domain.rs b/domains/service/src/domain.rs index 53bacd5390..7c622e4ec4 100644 --- a/domains/service/src/domain.rs +++ b/domains/service/src/domain.rs @@ -6,7 +6,7 @@ use domain_client_block_preprocessor::inherents::CreateInherentDataProvider; use domain_client_message_relayer::GossipMessageSink; use domain_client_operator::{Operator, OperatorParams, OperatorStreams}; use domain_runtime_primitives::opaque::{Block, Header}; -use domain_runtime_primitives::{Balance, DomainCoreApi, Hash}; +use domain_runtime_primitives::{Balance, Hash}; use futures::channel::mpsc; use futures::Stream; use pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi; @@ -29,6 +29,7 @@ use sp_consensus::SyncOracle; use sp_consensus_slots::Slot; use sp_core::traits::SpawnEssentialNamed; use sp_core::{Decode, Encode}; +use sp_domains::core_api::DomainCoreApi; use sp_domains::{BundleProducerElectionApi, DomainId, DomainsApi, OperatorId}; use sp_domains_fraud_proof::FraudProofApi; use sp_messenger::messages::ChainId; diff --git a/domains/test/runtime/evm/src/lib.rs b/domains/test/runtime/evm/src/lib.rs index 43e34e672e..d867212901 100644 --- a/domains/test/runtime/evm/src/lib.rs +++ b/domains/test/runtime/evm/src/lib.rs @@ -13,8 +13,7 @@ extern crate alloc; use codec::{Decode, Encode}; pub use domain_runtime_primitives::opaque::Header; use domain_runtime_primitives::{ - block_weights, maximum_block_length, Transfers, EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT, - SLOT_DURATION, + block_weights, maximum_block_length, EXISTENTIAL_DEPOSIT, MAXIMUM_BLOCK_WEIGHT, SLOT_DURATION, }; pub use domain_runtime_primitives::{ opaque, Balance, BlockNumber, CheckExtrinsicsValidityError, DecodeExtrinsicError, Hash, Nonce, @@ -41,7 +40,7 @@ use pallet_transporter::EndpointHandler; use sp_api::impl_runtime_apis; use sp_core::crypto::KeyTypeId; use sp_core::{Get, OpaqueMetadata, H160, H256, U256}; -use sp_domains::DomainId; +use sp_domains::{DomainId, Transfers}; use sp_messenger::endpoint::{Endpoint, EndpointHandler as EndpointHandlerT, EndpointId}; use sp_messenger::messages::{ BlockInfo, BlockMessagesWithStorageKey, ChainId, ChannelId, CrossDomainMessage, @@ -841,7 +840,7 @@ impl_runtime_apis! { } } - impl domain_runtime_primitives::DomainCoreApi for Runtime { + impl sp_domains::core_api::DomainCoreApi for Runtime { fn extract_signer( extrinsics: Vec<::Extrinsic>, ) -> Vec<(Option, ::Extrinsic)> { @@ -944,7 +943,7 @@ impl_runtime_apis! { ext.get_dispatch_info().weight } - fn block_fees() -> domain_runtime_primitives::BlockFees { + fn block_fees() -> sp_domains::BlockFees { BlockFees::collected_block_fees() } diff --git a/domains/test/service/src/domain.rs b/domains/test/service/src/domain.rs index 4d37fc5fef..bb20baccb7 100644 --- a/domains/test/service/src/domain.rs +++ b/domains/test/service/src/domain.rs @@ -8,7 +8,7 @@ use crate::{ use cross_domain_message_gossip::ChainTxPoolMsg; use domain_client_operator::{fetch_domain_bootstrap_info, BootstrapResult, OperatorStreams}; use domain_runtime_primitives::opaque::Block; -use domain_runtime_primitives::{Balance, DomainCoreApi}; +use domain_runtime_primitives::Balance; use domain_service::providers::DefaultProvider; use domain_service::{FullClient, RuntimeExecutor}; use domain_test_primitives::OnchainStateApi; @@ -29,6 +29,7 @@ use serde::de::DeserializeOwned; use sp_api::{ApiExt, ConstructRuntimeApi, Metadata, ProvideRuntimeApi}; use sp_block_builder::BlockBuilder; use sp_core::{Decode, Encode, H256}; +use sp_domains::core_api::DomainCoreApi; use sp_domains::DomainId; use sp_messenger::messages::ChainId; use sp_messenger::{MessengerApi, RelayerApi};