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

Support different Parentchain configs for Integritee, TargetA, TargetB and default to AssetTip for TargetB (aka support Asset Hub) #1652

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
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
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2793,6 +2793,7 @@ dependencies = [
"sgx_tstd",
"sp-core",
"sp-runtime",
"sp-version",
"substrate-api-client",
]

Expand Down
6 changes: 4 additions & 2 deletions app-libs/parentchain-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ codec = { package = "parity-scale-codec", version = "3.0.0", default-features =
log = { version = "0.4", default-features = false }
regex = { optional = true, version = "1.9.5" }

substrate-api-client = { optional = true, default-features = false, features = ["std", "sync-api"], git = "https://github.com/encointer/substrate-api-client.git", branch = "v0.9.42-tag-v0.14.0-retracted-check-metadata-hash" }
substrate-api-client = { default-features = false, git = "https://github.com/encointer/substrate-api-client.git", branch = "v0.9.42-tag-v0.14.0-retracted-check-metadata-hash" }

# substrate dep
sp-core = { default-features = false, features = ["full_crypto"], git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-version = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

[dev-dependencies]
env_logger = "0.9.0"
Expand Down Expand Up @@ -62,7 +63,8 @@ std = [
"regex",
"sp-core/std",
"sp-runtime/std",
"substrate-api-client",
"substrate-api-client/std",
"substrate-api-client/sync-api",
]
sgx = [
"sgx_tstd",
Expand Down
11 changes: 8 additions & 3 deletions app-libs/parentchain-interface/src/event_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
extern crate alloc;
use alloc::sync::Arc;
use core::sync::atomic::{AtomicBool, Ordering};
use itp_api_client_types::ParentchainApi;
use itp_types::parentchain::{AddedSgxEnclave, BalanceTransfer, ExtrinsicFailed, ParentchainId};
use itp_node_api::api_client::AccountApi;
use itp_types::parentchain::{
AddedSgxEnclave, BalanceTransfer, ExtrinsicFailed, Hash, ParentchainId,
};
use log::warn;
use sp_core::crypto::AccountId32;
use sp_runtime::DispatchError;
use substrate_api_client::SubscribeEvents;

pub fn subscribe_to_parentchain_events(
pub fn subscribe_to_parentchain_events<
ParentchainApi: AccountApi<AccountId = AccountId32> + SubscribeEvents<Hash = Hash>,
>(
api: &ParentchainApi,
parentchain_id: ParentchainId,
shutdown_flag: Arc<AtomicBool>,
Expand Down
66 changes: 66 additions & 0 deletions app-libs/parentchain-interface/src/integritee/api_client_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

//! Contains semi-generic type definitions to talk to the node without depending on an implementation of Runtime.
//!
//! You need to update this if you have a signed extension in your node that
//! is different from the integritee-node, e.g., if you use the `pallet_asset_tx_payment`.

use crate::{
GenericAdditionalParams, GenericExtrinsicParams, GenericSignedExtra, ParentchainRuntimeConfig,
PlainTip, UncheckedExtrinsicV4,
};
use itp_types::parentchain::Header;
pub use itp_types::parentchain::{
AccountData, AccountId, AccountInfo, Address, Balance, Hash, Index, Signature as PairSignature,
};
use sp_runtime::generic;

pub type IntegriteeRuntimeConfig = ParentchainRuntimeConfig<IntegriteeTip>;

// Configuration for the ExtrinsicParams.
pub type IntegriteeTip = PlainTip<Balance>;
pub type IntegriteeExtrinsicParams = GenericExtrinsicParams<IntegriteeRuntimeConfig, IntegriteeTip>;
pub type IntegriteeAdditionalParams = GenericAdditionalParams<IntegriteeRuntimeConfig, Hash>;

pub type IntegriteeSignedExtra = GenericSignedExtra<IntegriteeTip, Index>;
pub type IntegriteeSignature = Signature<IntegriteeSignedExtra>;

pub type IntegriteeUncheckedExtrinsic<Call> =
UncheckedExtrinsicV4<Address, Call, PairSignature, IntegriteeSignedExtra>;

/// Signature type of the [UncheckedExtrinsicV4].
pub type Signature<SignedExtra> = Option<(Address, PairSignature, SignedExtra)>;

pub type Block = generic::Block<Header, IntegriteeUncheckedExtrinsic<([u8; 2])>>;

#[cfg(feature = "std")]
pub use api::*;

#[cfg(feature = "std")]
mod api {
use crate::ParentchainRuntimeConfig;
use itp_api_client_types::PlainTip;
use itp_types::parentchain::Balance;
pub use substrate_api_client::{
api::Error as ApiClientError,
rpc::{tungstenite_client::TungsteniteRpcClient, Error as RpcClientError},
Api,
};

pub type IntegriteeApi = Api<ParentchainRuntimeConfig<PlainTip<Balance>>, TungsteniteRpcClient>;
}
49 changes: 49 additions & 0 deletions app-libs/parentchain-interface/src/integritee/api_factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

use super::api_client_types::IntegriteeTip;
use crate::ParentchainRuntimeConfig;
use itp_api_client_types::{Api, TungsteniteRpcClient};
use itp_node_api::node_api_factory::{CreateNodeApi, NodeApiFactoryError, Result};
use sp_core::sr25519;

/// Node API factory implementation.
pub struct IntegriteeNodeApiFactory {
node_url: String,
signer: sr25519::Pair,
}

impl IntegriteeNodeApiFactory {
pub fn new(url: String, signer: sr25519::Pair) -> Self {
Self { node_url: url, signer }
}
}

impl CreateNodeApi<ParentchainRuntimeConfig<IntegriteeTip>, TungsteniteRpcClient>
for IntegriteeNodeApiFactory
{
fn create_api(
&self,
) -> Result<Api<ParentchainRuntimeConfig<IntegriteeTip>, TungsteniteRpcClient>> {
let rpc_client = TungsteniteRpcClient::new(self.node_url.as_str(), 5)
.map_err(NodeApiFactoryError::FailedToCreateRpcClient)?;
let mut api = Api::new(rpc_client).map_err(NodeApiFactoryError::FailedToCreateNodeApi)?;
api.set_signer(self.signer.clone().into());
Ok(api)
}
}
3 changes: 3 additions & 0 deletions app-libs/parentchain-interface/src/integritee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

*/

pub mod api_client_types;
#[cfg(feature = "std")]
pub mod api_factory;
mod event_filter;
mod event_handler;

Expand Down
56 changes: 56 additions & 0 deletions app-libs/parentchain-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@
extern crate sgx_tstd as std;

use codec::{Decode, Encode};
use core::marker::PhantomData;
use itp_types::parentchain::Hash;
use sp_core::{crypto::AccountId32, sr25519};
use sp_runtime::{MultiAddress, MultiSignature};
use substrate_api_client::ac_primitives::{
BlakeTwo256, ExtrinsicSigner, SubstrateBlock, SubstrateHeader, SubstrateOpaqueExtrinsic,
};

pub use substrate_api_client::{
ac_node_api::{
metadata::{InvalidMetadataError, Metadata, MetadataError},
EventDetails, Events, StaticEvent,
},
ac_primitives::{
config::Config,
extrinsics::{
AssetTip, CallIndex, ExtrinsicParams, GenericAdditionalParams, GenericAdditionalSigned,
GenericExtrinsicParams, GenericSignedExtra, PlainTip, UncheckedExtrinsicV4,
},
serde_impls::StorageKey,
signer::{SignExtrinsic, StaticExtrinsicSigner},
},
rpc::Request,
storage_key, Api,
};

#[cfg(feature = "std")]
pub mod event_subscriber;
Expand Down Expand Up @@ -54,3 +79,34 @@ pub fn decode_and_log_error<V: Decode>(encoded: &mut &[u8]) -> Option<V> {
},
}
}

/// Config matching the specs of the typical polkadot chains.
/// We can define some more if we realize that we need more
/// granular control than the tip.
#[derive(Decode, Encode, Clone, Eq, PartialEq, Debug)]
pub struct ParentchainRuntimeConfig<Tip: Sized> {
_phantom: PhantomData<Tip>,
}

impl<Tip> Config for ParentchainRuntimeConfig<Tip>
where
u128: From<Tip>,
Tip: Copy + Default + Encode,
{
type Index = u32;
type BlockNumber = u32;
type Hash = Hash;
type AccountId = AccountId32;
type Address = MultiAddress<Self::AccountId, u32>;
type Signature = MultiSignature;
type Hasher = BlakeTwo256;
type Header = SubstrateHeader<Self::BlockNumber, BlakeTwo256>;
type AccountData = itp_types::AccountData;
type ExtrinsicParams = GenericExtrinsicParams<Self, Tip>;
type CryptoKey = sr25519::Pair;
type ExtrinsicSigner = ExtrinsicSigner<Self>;
type Block = SubstrateBlock<Self::Header, SubstrateOpaqueExtrinsic>;
type Balance = itp_types::Balance;
type ContractCurrency = u128;
type StakingBalance = u128;
}
64 changes: 64 additions & 0 deletions app-libs/parentchain-interface/src/target_a/api_client_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

//! Contains semi-generic type definitions to talk to the node without depending on an implementation of Runtime.
//!
//! You need to update this if you have a signed extension in your node that
//! is different from the integritee-node, e.g., if you use the `pallet_asset_tx_payment`.

use crate::{
GenericAdditionalParams, GenericExtrinsicParams, GenericSignedExtra, ParentchainRuntimeConfig,
PlainTip, UncheckedExtrinsicV4,
};
pub use itp_types::parentchain::{
AccountData, AccountId, AccountInfo, Address, Balance, Hash, Index, Signature as PairSignature,
};

pub type TargetARuntimeConfig = ParentchainRuntimeConfig<TargetATip>;

// Configuration for the ExtrinsicParams.

pub type TargetATip = PlainTip<Balance>;
pub type TargetAExtrinsicParams = GenericExtrinsicParams<TargetARuntimeConfig, TargetATip>;
pub type TargetAAdditionalParams = GenericAdditionalParams<TargetARuntimeConfig, Hash>;

pub type TargetASignedExtra = GenericSignedExtra<TargetATip, Index>;
pub type TargetASignature = Signature<TargetASignedExtra>;

pub type TargetAUncheckedExtrinsic<Call> =
UncheckedExtrinsicV4<Address, Call, PairSignature, TargetASignedExtra>;

/// Signature type of the [UncheckedExtrinsicV4].
pub type Signature<SignedExtra> = Option<(Address, PairSignature, SignedExtra)>;

#[cfg(feature = "std")]
pub use api::*;

#[cfg(feature = "std")]
mod api {
use crate::ParentchainRuntimeConfig;
use itp_api_client_types::PlainTip;
use itp_types::parentchain::Balance;
pub use substrate_api_client::{
api::Error as ApiClientError,
rpc::{tungstenite_client::TungsteniteRpcClient, Error as RpcClientError},
Api,
};

pub type TargetANodeConfig = ParentchainRuntimeConfig<PlainTip<Balance>>;
pub type TargetAApi = Api<TargetANodeConfig, TungsteniteRpcClient>;
}
49 changes: 49 additions & 0 deletions app-libs/parentchain-interface/src/target_a/api_factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Copyright (C) 2017-2019 Baidu, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*/

use super::api_client_types::TargetATip;
use crate::ParentchainRuntimeConfig;
use itp_api_client_types::{Api, TungsteniteRpcClient};
use itp_node_api::node_api_factory::{CreateNodeApi, NodeApiFactoryError, Result};
use sp_core::sr25519;

/// Node API factory implementation.
pub struct TargetANodeApiFactory {
node_url: String,
signer: sr25519::Pair,
}

impl TargetANodeApiFactory {
pub fn new(url: String, signer: sr25519::Pair) -> Self {
Self { node_url: url, signer }
}
}

impl CreateNodeApi<ParentchainRuntimeConfig<TargetATip>, TungsteniteRpcClient>
for TargetANodeApiFactory
{
fn create_api(
&self,
) -> Result<Api<ParentchainRuntimeConfig<TargetATip>, TungsteniteRpcClient>> {
let rpc_client = TungsteniteRpcClient::new(self.node_url.as_str(), 5)
.map_err(NodeApiFactoryError::FailedToCreateRpcClient)?;
let mut api = Api::new(rpc_client).map_err(NodeApiFactoryError::FailedToCreateNodeApi)?;
api.set_signer(self.signer.clone().into());
Ok(api)
}
}
Loading
Loading