Skip to content
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
10 changes: 5 additions & 5 deletions core-primitives/enclave-api/src/sidechain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ use codec::Encode;
use frame_support::ensure;
use itp_enclave_api_ffi as ffi;
use sgx_types::sgx_status_t;
use sp_runtime::{generic::SignedBlock, traits::Block};
use sp_runtime::{generic::SignedBlock, traits::Block as ParentchainBlockTrait};

/// trait for handling blocks on the side chain
pub trait Sidechain: Send + Sync + 'static {
/// Sync parentchain blocks and execute pending tops in the enclave
fn sync_parentchain<PB: Block>(
fn sync_parentchain<ParentchainBlock: ParentchainBlockTrait>(
&self,
blocks: &[SignedBlock<PB>],
blocks: &[SignedBlock<ParentchainBlock>],
nonce: u32,
) -> EnclaveResult<()>;

Expand All @@ -38,9 +38,9 @@ pub trait Sidechain: Send + Sync + 'static {
}

impl Sidechain for Enclave {
fn sync_parentchain<PB: Block>(
fn sync_parentchain<ParentchainBlock: ParentchainBlockTrait>(
&self,
blocks: &[SignedBlock<PB>],
blocks: &[SignedBlock<ParentchainBlock>],
nonce: u32,
) -> EnclaveResult<()> {
let mut retval = sgx_status_t::SGX_SUCCESS;
Expand Down
10 changes: 8 additions & 2 deletions core-primitives/ocall-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,14 @@ pub trait EnclaveOnChainOCallApi: Clone + Send + Sync {
}

pub trait EnclaveSidechainOCallApi: Clone + Send + Sync {
fn propose_sidechain_blocks<SB: Encode>(&self, signed_blocks: Vec<SB>) -> SgxResult<()>;
fn store_sidechain_blocks<SB: Encode>(&self, signed_blocks: Vec<SB>) -> SgxResult<()>;
fn propose_sidechain_blocks<SignedSidechainBlock: Encode>(
&self,
signed_blocks: Vec<SignedSidechainBlock>,
) -> SgxResult<()>;
fn store_sidechain_blocks<SignedSidechainBlock: Encode>(
&self,
signed_blocks: Vec<SignedSidechainBlock>,
) -> SgxResult<()>;
}

/// Newtype for IPFS CID
Expand Down
10 changes: 8 additions & 2 deletions core-primitives/test/src/mock/onchain_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,17 @@ impl EnclaveAttestationOCallApi for OnchainMock {
}

impl EnclaveSidechainOCallApi for OnchainMock {
fn propose_sidechain_blocks<SB: Encode>(&self, _signed_blocks: Vec<SB>) -> SgxResult<()> {
fn propose_sidechain_blocks<SignedSidechainBlock: Encode>(
&self,
_signed_blocks: Vec<SignedSidechainBlock>,
) -> SgxResult<()> {
Ok(())
}

fn store_sidechain_blocks<SB: Encode>(&self, _signed_blocks: Vec<SB>) -> SgxResult<()> {
fn store_sidechain_blocks<SignedSidechainBlock: Encode>(
&self,
_signed_blocks: Vec<SignedSidechainBlock>,
) -> SgxResult<()> {
Ok(())
}
}
Expand Down
21 changes: 9 additions & 12 deletions core-primitives/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ pub type Amount = u128;
pub type Header = HeaderG<BlockNumber, BlakeTwo256>;
pub type Block = BlockG<Header, OpaqueExtrinsic>;
pub type SignedBlock = SignedBlockG<Block>;
pub type BlockHash = H256;

pub type IpfsHash = [u8; 46];
pub type MrEnclave = [u8; 32];

pub type ConfirmCallFn = ([u8; 2], ShardIdentifier, H256, Vec<u8>);
pub type ShieldFundsFn = ([u8; 2], Vec<u8>, Amount, ShardIdentifier);
pub type CallWorkerFn = ([u8; 2], Request);

pub type Enclave = EnclaveGen<AccountId>;
/// Simple blob to hold an encoded call
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct OpaqueCall(pub Vec<u8>);
Expand All @@ -58,16 +67,6 @@ pub struct Request {
pub cyphertext: Vec<u8>,
}

pub type IpfsHash = [u8; 46];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seemed strange to me that some types were defined below struct implementations, so I moved them up.


pub type MrEnclave = [u8; 32];

pub type BlockHash = H256;

pub type ConfirmCallFn = ([u8; 2], ShardIdentifier, H256, Vec<u8>);
pub type ShieldFundsFn = ([u8; 2], Vec<u8>, Amount, ShardIdentifier);
pub type CallWorkerFn = ([u8; 2], Request);

// Todo: move this improved enclave definition into a primitives crate in the pallet_teerex repo.
#[derive(Encode, Decode, Default, Clone, PartialEq, sp_core::RuntimeDebug)]
pub struct EnclaveGen<AccountId> {
Expand All @@ -85,8 +84,6 @@ impl<AccountId> EnclaveGen<AccountId> {
}
}

pub type Enclave = EnclaveGen<AccountId>;

#[derive(Debug, Clone, PartialEq, Encode, Decode)]
pub enum DirectRequestStatus {
/// Direct request was successfully executed
Expand Down
49 changes: 31 additions & 18 deletions core/parentchain/block-importer/src/block_importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ use itp_types::{OpaqueCall, H256};
use log::*;
use sp_runtime::{
generic::SignedBlock as SignedBlockG,
traits::{Block as BlockT, NumberFor},
traits::{Block as ParentchainBlockTrait, NumberFor},
};
use std::{marker::PhantomData, sync::Arc, vec::Vec};

/// Parentchain block import implementation.
pub struct ParentchainBlockImporter<
PB,
ParentchainBlock,
ValidatorAccessor,
OCallApi,
StfExecutor,
ExtrinsicsFactory,
IndirectCallsExecutor,
> where
PB: BlockT<Hash = H256>,
NumberFor<PB>: BlockNumberOps,
ValidatorAccessor: ValidatorAccess<PB>,
ParentchainBlock: ParentchainBlockTrait<Hash = H256>,
NumberFor<ParentchainBlock>: BlockNumberOps,
ValidatorAccessor: ValidatorAccess<ParentchainBlock>,
OCallApi: EnclaveOnChainOCallApi + EnclaveAttestationOCallApi,
StfExecutor: StfUpdateState + StfExecuteTrustedCall + StfExecuteShieldFunds,
ExtrinsicsFactory: CreateExtrinsics,
Expand All @@ -61,21 +61,28 @@ pub struct ParentchainBlockImporter<
stf_executor: Arc<StfExecutor>,
extrinsics_factory: Arc<ExtrinsicsFactory>,
indirect_calls_executor: Arc<IndirectCallsExecutor>,
_phantom: PhantomData<PB>,
_phantom: PhantomData<ParentchainBlock>,
}

impl<PB, ValidatorAccessor, OCallApi, StfExecutor, ExtrinsicsFactory, IndirectCallsExecutor>
impl<
ParentchainBlock,
ValidatorAccessor,
OCallApi,
StfExecutor,
ExtrinsicsFactory,
IndirectCallsExecutor,
>
ParentchainBlockImporter<
PB,
ParentchainBlock,
ValidatorAccessor,
OCallApi,
StfExecutor,
ExtrinsicsFactory,
IndirectCallsExecutor,
> where
PB: BlockT<Hash = H256, Header = ParentchainHeader>,
NumberFor<PB>: BlockNumberOps,
ValidatorAccessor: ValidatorAccess<PB>,
ParentchainBlock: ParentchainBlockTrait<Hash = H256, Header = ParentchainHeader>,
NumberFor<ParentchainBlock>: BlockNumberOps,
ValidatorAccessor: ValidatorAccess<ParentchainBlock>,
OCallApi: EnclaveOnChainOCallApi + EnclaveAttestationOCallApi,
StfExecutor: StfUpdateState + StfExecuteTrustedCall + StfExecuteShieldFunds,
ExtrinsicsFactory: CreateExtrinsics,
Expand All @@ -99,25 +106,31 @@ impl<PB, ValidatorAccessor, OCallApi, StfExecutor, ExtrinsicsFactory, IndirectCa
}
}

impl<PB, ValidatorAccessor, OCallApi, StfExecutor, ExtrinsicsFactory, IndirectCallsExecutor>
ImportParentchainBlocks
impl<
ParentchainBlock,
ValidatorAccessor,
OCallApi,
StfExecutor,
ExtrinsicsFactory,
IndirectCallsExecutor,
> ImportParentchainBlocks
for ParentchainBlockImporter<
PB,
ParentchainBlock,
ValidatorAccessor,
OCallApi,
StfExecutor,
ExtrinsicsFactory,
IndirectCallsExecutor,
> where
PB: BlockT<Hash = H256, Header = ParentchainHeader>,
NumberFor<PB>: BlockNumberOps,
ValidatorAccessor: ValidatorAccess<PB>,
ParentchainBlock: ParentchainBlockTrait<Hash = H256, Header = ParentchainHeader>,
NumberFor<ParentchainBlock>: BlockNumberOps,
ValidatorAccessor: ValidatorAccess<ParentchainBlock>,
OCallApi: EnclaveOnChainOCallApi + EnclaveAttestationOCallApi,
StfExecutor: StfUpdateState + StfExecuteTrustedCall + StfExecuteShieldFunds,
ExtrinsicsFactory: CreateExtrinsics,
IndirectCallsExecutor: ExecuteIndirectCalls,
{
type SignedBlockType = SignedBlockG<PB>;
type SignedBlockType = SignedBlockG<ParentchainBlock>;

fn import_parentchain_blocks(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use itp_stf_executor::traits::{StatePostProcessing, StfExecuteShieldFunds, StfEx
use itp_types::{CallWorkerFn, OpaqueCall, ShardIdentifier, ShieldFundsFn, H256};
use log::*;
use sp_core::blake2_256;
use sp_runtime::traits::{Block as BlockT, Header};
use sp_runtime::traits::{Block as ParentchainBlockTrait, Header};
use std::{sync::Arc, vec::Vec};
use substrate_api_client::UncheckedExtrinsicV4;

Expand All @@ -35,12 +35,12 @@ pub trait ExecuteIndirectCalls {
/// Scans blocks for extrinsics that ask the enclave to execute some actions.
/// Executes indirect invocation calls, including shielding and unshielding calls.
/// Returns all unshielding call confirmations as opaque calls and the hashes of executed shielding calls.
fn execute_indirect_calls_in_extrinsics<PB>(
fn execute_indirect_calls_in_extrinsics<ParentchainBlock>(
&self,
block: &PB,
block: &ParentchainBlock,
) -> Result<(Vec<OpaqueCall>, Vec<H256>)>
where
PB: BlockT<Hash = H256>;
ParentchainBlock: ParentchainBlockTrait<Hash = H256>;
}

pub struct IndirectCallsExecutor<ShieldingKey, StfExecutor> {
Expand Down Expand Up @@ -95,12 +95,12 @@ where
ShieldingKey: ShieldingCrypto<Error = itp_sgx_crypto::Error>,
StfExecutor: StfExecuteTrustedCall + StfExecuteShieldFunds,
{
fn execute_indirect_calls_in_extrinsics<PB>(
fn execute_indirect_calls_in_extrinsics<ParentchainBlock>(
&self,
block: &PB,
block: &ParentchainBlock,
) -> Result<(Vec<OpaqueCall>, Vec<H256>)>
where
PB: BlockT<Hash = H256>,
ParentchainBlock: ParentchainBlockTrait<Hash = H256>,
{
debug!("Scanning block {:?} for relevant xt", block.header().number());
let mut opaque_calls = Vec::<OpaqueCall>::new();
Expand Down
58 changes: 30 additions & 28 deletions core/parentchain/light-client/src/concurrent_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ use std::sync::RwLock;

use crate::{
error::{Error, Result},
LightClientState, Validator,
LightClientState, Validator as ValidatorTrait,
};
use finality_grandpa::BlockNumberOps;
use itp_sgx_io::SealedIO;
use lazy_static::lazy_static;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use sp_runtime::traits::{Block as ParentchainBlockTrait, NumberFor};
use std::marker::PhantomData;

lazy_static! {
Expand All @@ -46,12 +46,12 @@ lazy_static! {
/// either a mutating, or a non-mutating function on the validator.
/// The reason we have this additional wrapper around `SealedIO`, is that we need
/// to guard against concurrent access by using RWLocks (which `SealedIO` does not do).
pub trait ValidatorAccess<PB>
pub trait ValidatorAccess<ParentchainBlock>
where
PB: BlockT,
NumberFor<PB>: BlockNumberOps,
ParentchainBlock: ParentchainBlockTrait,
NumberFor<ParentchainBlock>: BlockNumberOps,
{
type ValidatorType: Validator<PB> + LightClientState<PB>;
type ValidatorType: ValidatorTrait<ParentchainBlock> + LightClientState<ParentchainBlock>;

/// Execute a non-mutating function on the validator.
fn execute_on_validator<F, R>(&self, getter_function: F) -> Result<R>
Expand All @@ -66,48 +66,50 @@ where

/// Implementation of a validator access based on a global lock and corresponding file.
#[derive(Clone, Debug)]
pub struct GlobalValidatorAccessor<ValidatorT, PB, Seal>
pub struct GlobalValidatorAccessor<Validator, ParentchainBlock, Seal>
where
ValidatorT: Validator<PB> + LightClientState<PB>,
Seal: SealedIO<Error = Error, Unsealed = ValidatorT>,
PB: BlockT,
NumberFor<PB>: BlockNumberOps,
Validator: ValidatorTrait<ParentchainBlock> + LightClientState<ParentchainBlock>,
Seal: SealedIO<Error = Error, Unsealed = Validator>,
ParentchainBlock: ParentchainBlockTrait,
NumberFor<ParentchainBlock>: BlockNumberOps,
{
_phantom: PhantomData<(Seal, ValidatorT, PB)>,
_phantom: PhantomData<(Seal, Validator, ParentchainBlock)>,
}

impl<ValidatorT, PB, Seal> Default for GlobalValidatorAccessor<ValidatorT, PB, Seal>
impl<Validator, ParentchainBlock, Seal> Default
for GlobalValidatorAccessor<Validator, ParentchainBlock, Seal>
where
ValidatorT: Validator<PB> + LightClientState<PB>,
Seal: SealedIO<Error = Error, Unsealed = ValidatorT>,
PB: BlockT,
NumberFor<PB>: BlockNumberOps,
Validator: ValidatorTrait<ParentchainBlock> + LightClientState<ParentchainBlock>,
Seal: SealedIO<Error = Error, Unsealed = Validator>,
ParentchainBlock: ParentchainBlockTrait,
NumberFor<ParentchainBlock>: BlockNumberOps,
{
fn default() -> Self {
GlobalValidatorAccessor { _phantom: Default::default() }
}
}

impl<ValidatorT, PB, Seal> GlobalValidatorAccessor<ValidatorT, PB, Seal>
impl<Validator, ParentchainBlock, Seal> GlobalValidatorAccessor<Validator, ParentchainBlock, Seal>
where
ValidatorT: Validator<PB> + LightClientState<PB>,
Seal: SealedIO<Error = Error, Unsealed = ValidatorT>,
PB: BlockT,
NumberFor<PB>: BlockNumberOps,
Validator: ValidatorTrait<ParentchainBlock> + LightClientState<ParentchainBlock>,
Seal: SealedIO<Error = Error, Unsealed = Validator>,
ParentchainBlock: ParentchainBlockTrait,
NumberFor<ParentchainBlock>: BlockNumberOps,
{
pub fn new() -> Self {
GlobalValidatorAccessor { _phantom: Default::default() }
}
}

impl<ValidatorT, PB, Seal> ValidatorAccess<PB> for GlobalValidatorAccessor<ValidatorT, PB, Seal>
impl<Validator, ParentchainBlock, Seal> ValidatorAccess<ParentchainBlock>
for GlobalValidatorAccessor<Validator, ParentchainBlock, Seal>
where
ValidatorT: Validator<PB> + LightClientState<PB>,
Seal: SealedIO<Error = Error, Unsealed = ValidatorT>,
PB: BlockT,
NumberFor<PB>: BlockNumberOps,
Validator: ValidatorTrait<ParentchainBlock> + LightClientState<ParentchainBlock>,
Seal: SealedIO<Error = Error, Unsealed = Validator>,
ParentchainBlock: ParentchainBlockTrait,
NumberFor<ParentchainBlock>: BlockNumberOps,
{
type ValidatorType = ValidatorT;
type ValidatorType = Validator;

fn execute_on_validator<F, R>(&self, getter_function: F) -> Result<R>
where
Expand Down
Loading