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

Improve metadata handling #1148

Merged
merged 8 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions app-libs/stf/src/stf_sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::test_genesis::test_genesis_setup;
use crate::{helpers::enclave_signer_account, Stf, StfError, ENCLAVE_ACCOUNT_KEY};
use codec::Encode;
use frame_support::traits::{OriginTrait, UnfilteredDispatchable};
use itp_node_api::metadata::{pallet_teerex::TeerexCallIndexes, provider::AccessNodeMetadata};
use itp_node_api::metadata::{provider::AccessNodeMetadata, NodeMetadataTrait};
use itp_sgx_externalities::SgxExternalitiesTrait;
use itp_stf_interface::{
parentchain_pallet::ParentchainPalletInterface,
Expand Down Expand Up @@ -126,7 +126,7 @@ where
Call: ExecuteCall<NodeMetadataRepository>,
State: SgxExternalitiesTrait + Debug,
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: TeerexCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
{
type Error = Call::Error;

Expand Down
14 changes: 10 additions & 4 deletions app-libs/stf/src/trusted_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ use codec::{Decode, Encode};
use frame_support::{ensure, traits::UnfilteredDispatchable};
pub use ita_sgx_runtime::{Balance, Index};
use ita_sgx_runtime::{Runtime, System};
use itp_node_api::metadata::{pallet_teerex::TeerexCallIndexes, provider::AccessNodeMetadata};
use itp_node_api::metadata::{provider::AccessNodeMetadata, NodeMetadataTrait};
use itp_node_api_metadata::pallet_teerex::TeerexCallIndexes;
use itp_stf_interface::ExecuteCall;
use itp_stf_primitives::types::{AccountId, KeyPair, ShardIdentifier, Signature};
use itp_types::OpaqueCall;
Expand Down Expand Up @@ -169,7 +170,7 @@ impl TrustedReturnValue
impl<NodeMetadataRepository> ExecuteCall<NodeMetadataRepository> for TrustedCallSigned
where
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: TeerexCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
{
type Error = StfError;

Expand Down Expand Up @@ -202,8 +203,13 @@ where
.map_err(|e| {
Self::Error::Dispatch(format!("Balance Set Balance error: {:?}", e.error))
})?;
// TODO: we need clearly define the types so that the compiler can infer types
// see https://github.com/integritee-network/worker/issues/1145
// This explicit Error type is somehow still needed, otherwise the compiler complains
// multiple `impl`s satisfying `StfError: std::convert::From<_>`
// note: and another `impl` found in the `core` crate: `impl<T> std::convert::From<T> for T;`
// the impl From<..> for StfError conflicts with the standard convert
//
// Alternatively, removing the customised "impl From<..> for StfError" and use map_err directly
// would also work
Ok::<(), Self::Error>(())
},
TrustedCall::balance_transfer(from, to, value) => {
Expand Down
5 changes: 1 addition & 4 deletions core-primitives/node-api/metadata-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ pub struct NodeMetadataRepository<NodeMetadata> {
metadata_lock: RwLock<Option<NodeMetadata>>,
}

impl<NodeMetadata> NodeMetadataRepository<NodeMetadata>
where
NodeMetadata: Default,
{
impl<NodeMetadata> NodeMetadataRepository<NodeMetadata> {
pub fn new(metadata: NodeMetadata) -> Self {
NodeMetadataRepository { metadata_lock: RwLock::new(Some(metadata)) }
}
Expand Down
7 changes: 6 additions & 1 deletion core-primitives/node-api/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

#![cfg_attr(not(feature = "std"), no_std)]

use crate::error::Result;
use crate::{
error::Result, pallet_sidechain::SidechainCallIndexes, pallet_teerex::TeerexCallIndexes,
};
use codec::{Decode, Encode};
use sp_core::storage::StorageKey;
use substrate_api_client::{Metadata, MetadataError};
Expand All @@ -35,6 +37,9 @@ pub mod pallet_teerex;
#[cfg(feature = "mocks")]
pub mod metadata_mocks;

pub trait NodeMetadataTrait: TeerexCallIndexes + SidechainCallIndexes {}
impl<T: TeerexCallIndexes + SidechainCallIndexes> NodeMetadataTrait for T {}
Comment on lines +40 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳


#[derive(Default, Encode, Decode, Debug, Clone)]
pub struct NodeMetadata {
node_metadata: Option<Metadata>,
Expand Down
6 changes: 3 additions & 3 deletions core-primitives/stf-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use ita_stf::{
stf_sgx::{shards_key_hash, storage_hashes_to_update_per_shard},
ParentchainHeader, TrustedCallSigned, TrustedOperation,
};
use itp_node_api::metadata::{pallet_teerex::TeerexCallIndexes, provider::AccessNodeMetadata};
use itp_node_api::metadata::{provider::AccessNodeMetadata, NodeMetadataTrait};
use itp_ocall_api::{EnclaveAttestationOCallApi, EnclaveOnChainOCallApi};
use itp_sgx_externalities::{SgxExternalitiesTrait, StateHash};
use itp_stf_interface::{
Expand Down Expand Up @@ -56,7 +56,7 @@ where
StateHandler: HandleState<HashType = H256>,
StateHandler::StateT: SgxExternalitiesTrait + Encode,
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: TeerexCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
Stf: UpdateState<
StateHandler::StateT,
<StateHandler::StateT as SgxExternalitiesTrait>::SgxExternalitiesDiffType,
Expand Down Expand Up @@ -231,7 +231,7 @@ where
StateHandler::StateT: SgxExternalitiesTrait + Encode + StateHash,
<StateHandler::StateT as SgxExternalitiesTrait>::SgxExternalitiesType: Encode,
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: TeerexCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
Stf: UpdateState<
StateHandler::StateT,
<StateHandler::StateT as SgxExternalitiesTrait>::SgxExternalitiesDiffType,
Expand Down
6 changes: 3 additions & 3 deletions core-primitives/stf-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
extern crate alloc;

use alloc::{sync::Arc, vec::Vec};
use itp_node_api_metadata::pallet_teerex::TeerexCallIndexes;
use itp_node_api_metadata::NodeMetadataTrait;
use itp_node_api_metadata_provider::AccessNodeMetadata;
use itp_types::OpaqueCall;

Expand Down Expand Up @@ -51,7 +51,7 @@ pub trait UpdateState<State, StateDiff> {
pub trait StateCallInterface<Call, State, NodeMetadataRepository>
where
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: TeerexCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
{
type Error;

Expand All @@ -74,7 +74,7 @@ pub trait StateGetterInterface<Getter, State> {
pub trait ExecuteCall<NodeMetadataRepository>
where
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: TeerexCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
{
type Error;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use futures::executor;
use ita_stf::{TrustedCall, TrustedOperation};
use itp_node_api::{
api_client::ParentchainUncheckedExtrinsic,
metadata::{pallet_teerex::TeerexCallIndexes, provider::AccessNodeMetadata},
metadata::{pallet_teerex::TeerexCallIndexes, provider::AccessNodeMetadata, NodeMetadataTrait},
};
use itp_sgx_crypto::{key_repository::AccessKey, ShieldingCryptoDecrypt, ShieldingCryptoEncrypt};
use itp_stf_executor::traits::StfEnclaveSigning;
Expand Down Expand Up @@ -73,7 +73,7 @@ where
StfEnclaveSigner: StfEnclaveSigning,
TopPoolAuthor: AuthorApi<H256, H256> + Send + Sync + 'static,
NodeMetadataProvider: AccessNodeMetadata,
NodeMetadataProvider::MetadataType: TeerexCallIndexes,
NodeMetadataProvider::MetadataType: NodeMetadataTrait,
{
pub fn new(
shielding_key_repo: Arc<ShieldingKeyRepository>,
Expand Down Expand Up @@ -188,7 +188,7 @@ impl<ShieldingKeyRepository, StfEnclaveSigner, TopPoolAuthor, NodeMetadataProvid
StfEnclaveSigner: StfEnclaveSigning,
TopPoolAuthor: AuthorApi<H256, H256> + Send + Sync + 'static,
NodeMetadataProvider: AccessNodeMetadata,
NodeMetadataProvider::MetadataType: TeerexCallIndexes,
NodeMetadataProvider::MetadataType: NodeMetadataTrait,
{
fn execute_indirect_calls_in_extrinsics<ParentchainBlock>(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use itc_parentchain_light_client::{
concurrent_access::ValidatorAccess, BlockNumberOps, ExtrinsicSender, NumberFor,
};
use itp_extrinsics_factory::CreateExtrinsics;
use itp_node_api_metadata::pallet_sidechain::SidechainCallIndexes;
use itp_node_api_metadata::{pallet_sidechain::SidechainCallIndexes, NodeMetadataTrait};
use itp_node_api_metadata_provider::AccessNodeMetadata;
use itp_settings::worker::BLOCK_NUMBER_FINALIZATION_DIFF;
use itp_types::{OpaqueCall, ShardIdentifier};
Expand Down Expand Up @@ -95,7 +95,7 @@ impl<
NumberFor<ParentchainBlock>: BlockNumberOps,
SidechainHeader: HeaderTrait,
NodeMetadataRepository: AccessNodeMetadata,
NodeMetadataRepository::MetadataType: SidechainCallIndexes,
NodeMetadataRepository::MetadataType: NodeMetadataTrait,
ExtrinsicsFactory: CreateExtrinsics,
ValidatorAccessor: ValidatorAccess<ParentchainBlock> + Send + Sync + 'static,
{
Expand Down