Skip to content

Commit

Permalink
Native token inherent data provider
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolaos Dymitriadis <[email protected]>
  • Loading branch information
AmbientTea committed Aug 26, 2024
1 parent bb58c2c commit 29b5e0b
Show file tree
Hide file tree
Showing 14 changed files with 529 additions and 17 deletions.
22 changes: 22 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ members = [
"primitives/session-validator-management/query",
"primitives/session-manager",
"primitives/sidechain",
"partner-chains-cli"
]
"partner-chains-cli",
"primitives/native-token-management"]
resolver = "2"

[profile.release]
Expand Down Expand Up @@ -188,3 +188,4 @@ authority-selection-inherents = { path = "primitives/authority-selection-inheren
session-manager = { path = "primitives/session-manager", default-features = false }
sp-sidechain = { path = "primitives/sidechain", default-features = false }
chain-params = { path = "primitives/chain-params", default-features = false }
sp-native-token-management = { path = "primitives/native-token-management", default-features = false }
1 change: 1 addition & 0 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ frame-benchmarking-cli = { workspace = true }
# Local Dependencies
sidechain-runtime = { workspace = true }
sidechain-mc-hash = { workspace = true, features = ["mock"] }
sp-native-token-management = { workspace = true }
main-chain-follower-api = { workspace = true }
db-sync-follower = { workspace = true, features = ["block-source", "candidate-source", "native-token"] }
main-chain-follower-mock = { workspace = true, features = ["block-source", "candidate-source", "native-token"] }
Expand Down
31 changes: 27 additions & 4 deletions node/src/inherent_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use sp_consensus_aura::inherents::InherentDataProvider as AuraIDP;
use sp_consensus_aura::{sr25519::AuthorityPair as AuraPair, Slot};
use sp_core::Pair;
use sp_inherents::CreateInherentDataProviders;
use sp_native_token_management::NativeTokenManagementApi;
use sp_native_token_management::NativeTokenManagementInherentDataProvider as NativeTokenIDP;
use sp_runtime::traits::{Block as BlockT, Header, Zero};
use sp_session_validator_management::SessionValidatorManagementApi;
use sp_timestamp::InherentDataProvider as TimestampIDP;
Expand All @@ -38,13 +40,15 @@ pub struct ProposalCIDP<T> {
impl<T> CreateInherentDataProviders<Block, ()> for ProposalCIDP<T>
where
T: ProvideRuntimeApi<Block> + Send + Sync + 'static,
T: HeaderBackend<Block>,
T::Api: SessionValidatorManagementApi<
Block,
SessionKeys,
CrossChainPublic,
AuthoritySelectionInputs,
ScEpochNumber,
>,
T::Api: NativeTokenManagementApi<Block>,
{
#[cfg(feature = "block-beneficiary")]
type InherentDataProviders = (
Expand All @@ -53,9 +57,10 @@ where
McHashIDP,
AriadneIDP,
BlockBeneficiaryInherentProvider<BeneficiaryId>,
NativeTokenIDP,
);
#[cfg(not(feature = "block-beneficiary"))]
type InherentDataProviders = (AuraIDP, TimestampIDP, McHashIDP, AriadneIDP);
type InherentDataProviders = (AuraIDP, TimestampIDP, McHashIDP, AriadneIDP, NativeTokenIDP);

async fn create_inherent_data_providers(
&self,
Expand Down Expand Up @@ -89,13 +94,22 @@ where
"SIDECHAIN_BLOCK_BENEFICIARY",
)?;

let native_token = NativeTokenIDP::new(
client.clone(),
data_sources.native_token.as_ref(),
mc_hash.mc_hash(),
parent_hash.clone(),
)
.await?;

Ok((
slot,
timestamp,
mc_hash,
ariadne_data_provider,
#[cfg(feature = "block-beneficiary")]
block_beneficiary_provider,
native_token,
))
}
}
Expand Down Expand Up @@ -124,8 +138,9 @@ where
AuthoritySelectionInputs,
ScEpochNumber,
>,
T::Api: NativeTokenManagementApi<Block>,
{
type InherentDataProviders = (TimestampIDP, AriadneIDP);
type InherentDataProviders = (TimestampIDP, AriadneIDP, NativeTokenIDP);

async fn create_inherent_data_providers(
&self,
Expand All @@ -142,7 +157,7 @@ where
parent_header,
parent_slot,
verified_block_slot,
mc_hash,
mc_hash.clone(),
config.slot_duration(),
data_sources.block.as_ref(),
)
Expand All @@ -159,7 +174,15 @@ where
)
.await?;

Ok((timestamp, ariadne_data_provider))
let native_token = NativeTokenIDP::new(
client.clone(),
data_sources.native_token.as_ref(),
mc_hash,
parent_hash.clone(),
)
.await?;

Ok((timestamp, ariadne_data_provider, native_token))
}
}

Expand Down
27 changes: 21 additions & 6 deletions node/src/tests/inherent_data_tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::inherent_data::{ProposalCIDP, VerifierCIDP};
use crate::tests::mock::{test_client, test_create_inherent_data_config};
use crate::tests::runtime_api_mock::mock_header;
use crate::tests::runtime_api_mock::{mock_header, TestApi};
use authority_selection_inherents::authority_selection_inputs::AuthoritySelectionInputs;
use main_chain_follower_api::{block::MainchainBlock, mock_services::*};
use sidechain_domain::{McBlockHash, McBlockNumber, McEpochNumber, McSlotNumber};
use sidechain_domain::{
McBlockHash, McBlockNumber, McEpochNumber, McSlotNumber, NativeTokenAmount, ScEpochNumber,
};
use sp_consensus_aura::Slot;
use sp_core::H256;
use sp_inherents::CreateInherentDataProviders;
Expand All @@ -20,24 +22,28 @@ async fn block_proposal_cidp_should_be_created_correctly() {

let inherent_data_providers = ProposalCIDP::new(
test_create_inherent_data_config(),
test_client(),
TestApi::new(ScEpochNumber(2))
.with_headers([(H256::zero(), mock_header())])
.into(),
TestDataSources::new().into(),
)
.create_inherent_data_providers(H256::zero(), ())
.await
.unwrap();

#[cfg(not(feature = "block-beneficiary"))]
let (slot, timestamp, mc_hash, ariadne_data) = inherent_data_providers;
let (slot, timestamp, mc_hash, ariadne_data, native_token) = inherent_data_providers;
#[cfg(feature = "block-beneficiary")]
let (slot, timestamp, mc_hash, ariadne_data, block_beneficiary) = inherent_data_providers;
let (slot, timestamp, mc_hash, ariadne_data, block_beneficiary, native_token) =
inherent_data_providers;
let mut inherent_data = InherentData::new();
slot.provide_inherent_data(&mut inherent_data).await.unwrap();
timestamp.provide_inherent_data(&mut inherent_data).await.unwrap();
mc_hash.provide_inherent_data(&mut inherent_data).await.unwrap();
ariadne_data.provide_inherent_data(&mut inherent_data).await.unwrap();
#[cfg(feature = "block-beneficiary")]
block_beneficiary.provide_inherent_data(&mut inherent_data).await.unwrap();
native_token.provide_inherent_data(&mut inherent_data).await.unwrap();
assert_eq!(
inherent_data
.get_data::<Slot>(&sp_consensus_aura::inherents::INHERENT_IDENTIFIER)
Expand All @@ -63,6 +69,10 @@ async fn block_proposal_cidp_should_be_created_correctly() {
.get_data::<AuthoritySelectionInputs>(&sp_session_validator_management::INHERENT_IDENTIFIER)
.unwrap()
.is_some());
assert!(inherent_data
.get_data::<NativeTokenAmount>(&sp_native_token_management::INHERENT_IDENTIFIER)
.unwrap()
.is_some())
}

#[tokio::test]
Expand All @@ -88,10 +98,11 @@ async fn block_verification_cidp_should_be_created_correctly() {
.create_inherent_data_providers(mock_header().hash(), (30.into(), mc_block_hash))
.await
.unwrap();
let (timestamp, ariadne_data_provider) = inherent_data_providers;
let (timestamp, ariadne_data_provider, native_token_provider) = inherent_data_providers;
let mut inherent_data = InherentData::new();
timestamp.provide_inherent_data(&mut inherent_data).await.unwrap();
ariadne_data_provider.provide_inherent_data(&mut inherent_data).await.unwrap();
native_token_provider.provide_inherent_data(&mut inherent_data).await.unwrap();

assert_eq!(
inherent_data.get_data::<Timestamp>(&sp_timestamp::INHERENT_IDENTIFIER).unwrap(),
Expand All @@ -101,4 +112,8 @@ async fn block_verification_cidp_should_be_created_correctly() {
.get_data::<AuthoritySelectionInputs>(&sp_session_validator_management::INHERENT_IDENTIFIER)
.unwrap()
.is_some());
assert!(inherent_data
.get_data::<NativeTokenAmount>(&sp_native_token_management::INHERENT_IDENTIFIER)
.unwrap()
.is_some())
}
22 changes: 20 additions & 2 deletions node/src/tests/runtime_api_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ use sidechain_runtime::opaque::SessionKeys;
use sidechain_runtime::CrossChainPublic;
use sp_api::{ApiRef, ProvideRuntimeApi};
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, Header, NumberFor, Zero};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor, Zero};
use sp_runtime::Digest;
use sp_sidechain::GetSidechainParams;
use std::collections::HashMap;

type Hash = <Block as BlockT>::Hash;
type Header = <Block as BlockT>::Header;

#[derive(Clone)]
pub struct TestApi {
pub next_unset_epoch_number: ScEpochNumber,
pub headers: HashMap<<Block as BlockT>::Hash, <Block as BlockT>::Header>,
pub headers: HashMap<Hash, Header>,
}

impl TestApi {
Expand All @@ -25,6 +28,10 @@ impl TestApi {
headers.insert(header.hash(), header);
Self { next_unset_epoch_number, headers }
}

pub fn with_headers<Hs: Into<HashMap<Hash, Header>>>(self, headers: Hs) -> Self {
Self { headers: headers.into(), ..self }
}
}

impl Default for TestApi {
Expand Down Expand Up @@ -82,6 +89,17 @@ sp_api::mock_impl_runtime_apis! {
}
}
}

impl sp_native_token_management::NativeTokenManagementApi<Block> for TestApi {
fn get_main_chain_scripts() -> sp_native_token_management::MainChainScripts {
sp_native_token_management::MainChainScripts {
native_token_policy: Default::default(),
native_token_asset_name: Default::default(),
illiquid_supply_address: Default::default(),

}
}
}
}

impl HeaderBackend<Block> for TestApi {
Expand Down
45 changes: 45 additions & 0 deletions primitives/native-token-management/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[package]
name = "sp-native-token-management"
version = "0.1.0"
edition = "2021"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
async-trait = { workspace = true, optional = true }
main-chain-follower-api = { workspace = true, optional = true, features = ["native-token"] }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
sidechain-domain = { workspace = true }
sidechain-mc-hash = { workspace = true, optional = true }
sp-api = { workspace = true }
sp-blockchain = { workspace = true, optional = true }
sp-inherents = { workspace = true }
sp-runtime = { workspace = true }
thiserror = { workspace = true, optional = true }
serde = { workspace = true, optional = true }

[dev-dependencies]
tokio = { workspace = true }

[features]
default = ["std"]
std = [
"async-trait",
"main-chain-follower-api/std",
"parity-scale-codec/std",
"scale-info/std",
"sidechain-domain/std",
"sidechain-mc-hash",
"sp-api/std",
"sp-blockchain",
"sp-inherents/std",
"sp-runtime/std",
"thiserror"
]
serde = [
"dep:serde",
"scale-info/serde",
"sidechain-domain/serde",
]
Loading

0 comments on commit 29b5e0b

Please sign in to comment.