Skip to content

Commit

Permalink
move DomainCoreApi under sp_domains to avoid cyclic deps
Browse files Browse the repository at this point in the history
  • Loading branch information
vedhavyas committed Feb 15, 2024
1 parent 9023434 commit 93957e1
Show file tree
Hide file tree
Showing 26 changed files with 184 additions and 168 deletions.
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.

4 changes: 2 additions & 2 deletions crates/pallet-domains/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -909,7 +909,7 @@ type FraudProofFor<T> =
fn generate_invalid_block_fees_fraud_proof<T: Config>(
domain_id: DomainId,
bad_receipt_hash: ReceiptHashFor<T>,
block_fees: domain_runtime_primitives::BlockFees<BalanceOf<T>>,
block_fees: sp_domains::BlockFees<BalanceOf<T>>,
) -> (FraudProofFor<T>, T::Hash) {
let storage_key = sp_domains_fraud_proof::fraud_proof::operator_block_fees_final_key();
let mut root = T::Hash::default();
Expand Down
13 changes: 7 additions & 6 deletions crates/sp-domains-fraud-proof/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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
)
),
})
);

Expand All @@ -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
)
),
})
);

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/sp-domains-fraud-proof/src/verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down
82 changes: 82 additions & 0 deletions crates/sp-domains/src/core_api.rs
Original file line number Diff line number Diff line change
@@ -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<<Block as BlockT>::Extrinsic>,
) -> Vec<(Option<opaque::AccountId>, <Block as BlockT>::Extrinsic)>;

fn is_within_tx_range(
extrinsic: &<Block as BlockT>::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: &<Block as BlockT>::Header) -> Vec<u8>;

/// Returns the storage root after applying the extrinsic.
fn apply_extrinsic_with_post_state_root(extrinsic: <Block as BlockT>::Extrinsic) -> Vec<u8>;

/// Returns an encoded extrinsic aiming to upgrade the runtime using given code.
fn construct_set_code_extrinsic(code: Vec<u8>) -> Vec<u8>;

/// 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: &<Block as BlockT>::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<<Block as BlockT>::Extrinsic>, block_number: NumberFor<Block>,
block_hash: <Block as BlockT>::Hash) -> Result<(), CheckExtrinsicsValidityError>;

/// Decodes the domain specific extrinsic from the opaque extrinsic.
fn decode_extrinsic(
opaque_extrinsic: sp_runtime::OpaqueExtrinsic,
) -> Result<<Block as BlockT>::Extrinsic, DecodeExtrinsicError>;

/// Returns extrinsic Era if present.
fn extrinsic_era(
extrinsic: &<Block as BlockT>::Extrinsic
) -> Option<Era>;

/// 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<Balance>;

/// 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<Balance>;

/// Returns the storage key for the Transfers on Domain.
fn transfers_storage_key() -> Vec<u8>;
}
}
39 changes: 38 additions & 1 deletion crates/sp-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -248,6 +249,42 @@ impl From<DomainId> for ChainId {
}
}

#[derive(Clone, Debug, Decode, Default, Encode, Eq, PartialEq, TypeInfo)]
pub struct BlockFees<Balance> {
/// 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<Balance> BlockFees<Balance>
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<Balance> {
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<Balance> {
/// 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<Number, Hash, DomainHeader: HeaderT, Balance> {
/// Proof of bundle producer election.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion domains/client/block-preprocessor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions domains/client/block-preprocessor/src/stateless_runtime.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
2 changes: 1 addition & 1 deletion domains/client/domain-operator/src/bundle_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion domains/client/domain-operator/src/domain_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;
Expand Down
Loading

0 comments on commit 93957e1

Please sign in to comment.