Skip to content

Commit

Permalink
Support different Parentchain configs for Integritee, TargetA, Target…
Browse files Browse the repository at this point in the history
…B and default to AssetTip for TargetB (aka support Asset Hub) (#1652)

* trying to use a supertrait to make ParentchainApi generic

* introduce ParentchainRuntimeConfig for simpler generic parentchain support

* [onchain_bridge] worker_request compiles

* [onchain_bridge] send_to_parentchain compiles

* [onchain_bridge] stuck at prometheus metrics

* prometheus metrics wip

* finish metrics

* worker builds

* fix unused warnings

* wip splitting up extrinsic factory

* cleanup

* remove unnecessary alias

* remove core

* some cleanup

* cleanup and remove naming ambiguities

* fix test build

* clippy

* fix teeracle build

* fix attesteer build

* Fix typo in `AdditionalParamsOf`

* remove parentchainapi trait

* fmt

* re-activate check for rarenewal period to derive account funding

* log error for init target chain

* add trace! logs for signed extra and additional signed

* [enclave-runtime] try_runtime now takes the parentchain as arg. Sending xt to asset hub works

* [enclave-runtime] implement parentchain event handler

* more verbose logs and wait for proxy creation inclusion

* some log pimp

* use per-parentchain block type

* WIP: trait jungle untangle

* Revert "WIP: trait jungle untangle"

This reverts commit f9bc9df.

* Revert "use per-parentchain block type"

This reverts commit 344da8e.

* [aura] improve logging around block import

* [aura] implement debug on SlotInfo to get Header info

* [stf-executor] fix updating the target_b block

* fix clippy

* fix log

* minor logging improvement

---------

Co-authored-by: Alain Brenzikofer <[email protected]>
  • Loading branch information
clangenb and brenzi authored Dec 20, 2024
1 parent 0d56e12 commit 3e9ebb0
Show file tree
Hide file tree
Showing 55 changed files with 1,562 additions and 581 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,7 @@ dependencies = [
"sgx_tstd",
"sp-core",
"sp-runtime",
"sp-version",
"substrate-api-client",
]

Expand Down Expand Up @@ -3267,6 +3268,7 @@ dependencies = [
"itp-node-api",
"itp-nonce-cache",
"itp-types",
"log 0.4.22",
"parity-scale-codec",
"sgx_tstd",
"sgx_types",
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
18 changes: 14 additions & 4 deletions app-libs/parentchain-interface/src/event_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@
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, BlockNumber, ExtrinsicFailed, Hash, ParentchainId,
};
use log::{debug, warn};
use sp_core::crypto::AccountId32;
use sp_runtime::DispatchError;
use substrate_api_client::{ac_primitives::Header, GetChainInfo, SubscribeEvents};
use substrate_api_client::{
ac_primitives::{BlakeTwo256, Header, SubstrateHeader},
GetChainInfo, SubscribeEvents,
};

pub fn subscribe_to_parentchain_events(
pub fn subscribe_to_parentchain_events<
ParentchainApi: AccountApi<AccountId = AccountId32>
+ SubscribeEvents<Hash = Hash>
+ GetChainInfo<Header = SubstrateHeader<BlockNumber, BlakeTwo256>>,
>(
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::{fmt::Debug, 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 + Debug,
{
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

0 comments on commit 3e9ebb0

Please sign in to comment.