diff --git a/cli/demo_direct_call.sh b/cli/demo_direct_call.sh index b2e828129a..4d066d197b 100755 --- a/cli/demo_direct_call.sh +++ b/cli/demo_direct_call.sh @@ -84,10 +84,6 @@ echo "* Send ${AMOUNTTRANSFER} funds from Alice's incognito account to Bob's inc $CLIENT trusted transfer ${ICGACCOUNTALICE} ${ICGACCOUNTBOB} ${AMOUNTTRANSFER} --mrenclave ${MRENCLAVE} --direct echo "" -echo "* Waiting 3 seconds" -sleep 3 -echo "" - echo "* Get balance of Alice's incognito account" RESULT=$(${CLIENT} trusted balance ${ICGACCOUNTALICE} --mrenclave ${MRENCLAVE} | xargs) echo $RESULT diff --git a/cli/demo_sidechain.sh b/cli/demo_sidechain.sh index bd0e7e585d..62968896e9 100755 --- a/cli/demo_sidechain.sh +++ b/cli/demo_sidechain.sh @@ -81,10 +81,6 @@ echo "* Issue ${INITIALFUNDS} tokens to Alice's incognito account (on worker 1)" ${CLIENTWORKER1} trusted set-balance ${ICGACCOUNTALICE} ${INITIALFUNDS} --mrenclave ${MRENCLAVE} --direct echo "" -echo "* Waiting 2 seconds" -sleep 2 -echo "" - echo "Get balance of Alice's incognito account (on worker 1)" ${CLIENTWORKER1} trusted balance ${ICGACCOUNTALICE} --mrenclave ${MRENCLAVE} echo "" @@ -94,19 +90,11 @@ echo "* First transfer: Send ${AMOUNTTRANSFER} funds from Alice's incognito acco $CLIENTWORKER1 trusted transfer ${ICGACCOUNTALICE} ${ICGACCOUNTBOB} ${AMOUNTTRANSFER} --mrenclave ${MRENCLAVE} --direct echo "" -echo "* Waiting 2 seconds" -sleep 2 -echo "" - # Send funds from Alice to Bobs account, on worker 2 echo "* Second transfer: Send ${AMOUNTTRANSFER} funds from Alice's incognito account to Bob's incognito account (on worker 2)" $CLIENTWORKER2 trusted transfer ${ICGACCOUNTALICE} ${ICGACCOUNTBOB} ${AMOUNTTRANSFER} --mrenclave ${MRENCLAVE} --direct echo "" -echo "* Waiting 2 seconds" -sleep 2 -echo "" - echo "* Get balance of Alice's incognito account (on worker 2)" ALICE_BALANCE=$(${CLIENTWORKER2} trusted balance ${ICGACCOUNTALICE} --mrenclave ${MRENCLAVE} | xargs) echo "$ALICE_BALANCE" @@ -139,5 +127,3 @@ fi echo "" exit 0 - - diff --git a/core-primitives/test/src/mock/trusted_operation_pool_mock.rs b/core-primitives/test/src/mock/trusted_operation_pool_mock.rs index 446351718a..2d644bc1f1 100644 --- a/core-primitives/test/src/mock/trusted_operation_pool_mock.rs +++ b/core-primitives/test/src/mock/trusted_operation_pool_mock.rs @@ -211,7 +211,7 @@ impl TrustedOperationPool for TrustedOperationPoolMock { unimplemented!() } - fn on_block_created(&self, _hashes: &[Self::Hash], _block_hash: SidechainBlockHash) {} + fn on_block_imported(&self, _hashes: &[Self::Hash], _block_hash: SidechainBlockHash) {} fn rpc_send_state(&self, _hash: Self::Hash, _state_encoded: Vec) -> Result<(), Error> { Ok(()) diff --git a/enclave-runtime/src/test/mocks/types.rs b/enclave-runtime/src/test/mocks/types.rs index 5f825247af..991689f6cb 100644 --- a/enclave-runtime/src/test/mocks/types.rs +++ b/enclave-runtime/src/test/mocks/types.rs @@ -73,7 +73,7 @@ pub type TestTopPoolExecutor = TopPoolOperationHandler; pub type TestBlockComposer = - BlockComposer; + BlockComposer; pub type TestBlockImporter = BlockImporter< TestSigner, diff --git a/enclave-runtime/src/test/sidechain_aura_tests.rs b/enclave-runtime/src/test/sidechain_aura_tests.rs index 0598a3fa44..407ac841f0 100644 --- a/enclave-runtime/src/test/sidechain_aura_tests.rs +++ b/enclave-runtime/src/test/sidechain_aura_tests.rs @@ -96,8 +96,7 @@ pub fn produce_sidechain_block_and_import_it() { parentchain_block_import_trigger.clone(), ocall_api.clone(), )); - let block_composer = - Arc::new(TestBlockComposer::new(signer.clone(), state_key, rpc_author.clone())); + let block_composer = Arc::new(TestBlockComposer::new(signer.clone(), state_key)); let proposer_environment = ProposerFactory::new(top_pool_operation_handler, stf_executor.clone(), block_composer); let extrinsics_factory = ExtrinsicsFactoryMock::default(); diff --git a/enclave-runtime/src/tests.rs b/enclave-runtime/src/tests.rs index 8fe3288ce9..defc3ef30a 100644 --- a/enclave-runtime/src/tests.rs +++ b/enclave-runtime/src/tests.rs @@ -154,12 +154,9 @@ pub extern "C" fn test_main_entrance() -> size_t { fn test_compose_block_and_confirmation() { // given - let (rpc_author, _, shard, _, _, state_handler) = test_setup(); - let block_composer = BlockComposer::::new( - test_account(), - state_key(), - rpc_author.clone(), - ); + let (_, _, shard, _, _, state_handler) = test_setup(); + let block_composer = + BlockComposer::::new(test_account(), state_key()); let signed_top_hashes: Vec = vec![[94; 32].into(), [1; 32].into()].to_vec(); @@ -285,11 +282,8 @@ fn test_create_block_and_confirmation_works() { rpc_author.clone(), stf_executor.clone(), ); - let block_composer = BlockComposer::::new( - test_account(), - state_key(), - rpc_author.clone(), - ); + let block_composer = + BlockComposer::::new(test_account(), state_key()); let sender = funded_pair(); let receiver = unfunded_public(); @@ -356,11 +350,8 @@ fn test_create_state_diff() { rpc_author.clone(), stf_executor.clone(), ); - let block_composer = BlockComposer::::new( - test_account(), - state_key(), - rpc_author.clone(), - ); + let block_composer = + BlockComposer::::new(test_account(), state_key()); let sender = funded_pair(); let receiver = unfunded_public(); diff --git a/enclave-runtime/src/top_pool_execution.rs b/enclave-runtime/src/top_pool_execution.rs index 5780098619..bccaf83ffb 100644 --- a/enclave-runtime/src/top_pool_execution.rs +++ b/enclave-runtime/src/top_pool_execution.rs @@ -18,7 +18,7 @@ use crate::{ error::{Error, Result}, global_components::{ - GLOBAL_PARENTCHAIN_IMPORT_DISPATCHER_COMPONENT, GLOBAL_RPC_AUTHOR_COMPONENT, + GLOBAL_PARENTCHAIN_IMPORT_DISPATCHER_COMPONENT, GLOBAL_SIDECHAIN_IMPORT_QUEUE_WORKER_COMPONENT, }, ocall::OcallApi, @@ -181,12 +181,7 @@ fn execute_top_pool_trusted_calls_internal() -> Result<()> { Error::ComponentNotInitialized })?; - let rpc_author = GLOBAL_RPC_AUTHOR_COMPONENT.get().ok_or_else(|| { - error!("Failed to retrieve rpc author component. Maybe it's not initialized?"); - Error::ComponentNotInitialized - })?; - - let block_composer = Arc::new(BlockComposer::new(authority.clone(), state_key, rpc_author)); + let block_composer = Arc::new(BlockComposer::new(authority.clone(), state_key)); match yield_next_slot( slot_beginning_timestamp, diff --git a/sidechain/block-composer/src/block_composer.rs b/sidechain/block-composer/src/block_composer.rs index f4b47e05df..dd1bb9b006 100644 --- a/sidechain/block-composer/src/block_composer.rs +++ b/sidechain/block-composer/src/block_composer.rs @@ -26,7 +26,6 @@ use its_primitives::traits::{ Block as SidechainBlockTrait, SignBlock, SignedBlock as SignedSidechainBlockTrait, }; use its_state::{LastBlockExt, SidechainDB, SidechainState, SidechainSystemExt, StateHash}; -use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockCreated, SendState}; use log::*; use sgx_externalities::SgxExternalitiesTrait; use sp_core::Pair; @@ -34,7 +33,7 @@ use sp_runtime::{ traits::{Block as ParentchainBlockTrait, Header}, MultiSignature, }; -use std::{format, marker::PhantomData, sync::Arc, vec::Vec}; +use std::{format, marker::PhantomData, vec::Vec}; /// Compose a sidechain block and corresponding confirmation extrinsic for the parentchain /// @@ -52,15 +51,14 @@ pub trait ComposeBlockAndConfirmation { +pub struct BlockComposer { signer: Signer, state_key: StateKey, - rpc_author: Arc, _phantom: PhantomData<(ParentchainBlock, SignedSidechainBlock)>, } -impl - BlockComposer +impl + BlockComposer where ParentchainBlock: ParentchainBlockTrait, SignedSidechainBlock: @@ -68,21 +66,18 @@ where SignedSidechainBlock::Block: SidechainBlockTrait, SignedSidechainBlock::Signature: From, - RpcAuthor: AuthorApi - + OnBlockCreated - + SendState, Signer: Pair, Signer::Public: Encode, StateKey: StateCrypto, { - pub fn new(signer: Signer, state_key: StateKey, rpc_author: Arc) -> Self { - BlockComposer { signer, state_key, rpc_author, _phantom: Default::default() } + pub fn new(signer: Signer, state_key: StateKey) -> Self { + BlockComposer { signer, state_key, _phantom: Default::default() } } } -impl +impl ComposeBlockAndConfirmation - for BlockComposer + for BlockComposer where ParentchainBlock: ParentchainBlockTrait, SignedSidechainBlock: @@ -90,9 +85,6 @@ where SignedSidechainBlock::Block: SidechainBlockTrait, SignedSidechainBlock::Signature: From, - RpcAuthor: AuthorApi - + OnBlockCreated - + SendState, Externalities: SgxExternalitiesTrait + SidechainState + SidechainSystemExt + StateHash + Encode, Signer: Pair, Signer::Public: Encode, @@ -150,7 +142,6 @@ where let opaque_call = create_proposed_sidechain_block_call(shard, block_hash); - self.rpc_author.on_block_created(block.signed_top_hashes(), block.hash()); let signed_block = block.sign_block(&self.signer); Ok((opaque_call, signed_block)) diff --git a/sidechain/consensus/aura/src/block_importer.rs b/sidechain/consensus/aura/src/block_importer.rs index 1815570ee4..749a5a6ac1 100644 --- a/sidechain/consensus/aura/src/block_importer.rs +++ b/sidechain/consensus/aura/src/block_importer.rs @@ -132,21 +132,25 @@ impl< } } - pub(crate) fn remove_calls_from_top_pool( - &self, - signed_top_hashes: &[H256], - shard: &ShardIdentifierFor, - ) { - let executed_operations = signed_top_hashes + pub(crate) fn update_top_pool(&self, sidechain_block: &SignedSidechainBlock::Block) { + // FIXME: we should take the rpc author here directly #547. + + // Notify pool about imported block for status updates of the calls. + self.top_pool_executor.on_block_imported(sidechain_block); + + // Remove calls from pool. + let executed_operations = sidechain_block + .signed_top_hashes() .iter() .map(|hash| { // Only successfully executed operations are included in a block. ExecutedOperation::success(*hash, TrustedOperationOrHash::Hash(*hash), Vec::new()) }) .collect(); - // FIXME: we should take the rpc author here directly #547 - let unremoved_calls = - self.top_pool_executor.remove_calls_from_pool(shard, executed_operations); + + let unremoved_calls = self + .top_pool_executor + .remove_calls_from_pool(&sidechain_block.shard_id(), executed_operations); for unremoved_call in unremoved_calls { error!( @@ -321,10 +325,7 @@ impl< // If the block has been proposed by this enclave, remove all successfully applied // trusted calls from the top pool. if self.block_author_is_self(sidechain_block.block_author()) { - self.remove_calls_from_top_pool( - sidechain_block.signed_top_hashes(), - &sidechain_block.shard_id(), - ) + self.update_top_pool(sidechain_block) } // Send metric about sidechain block height (i.e. block number) diff --git a/sidechain/top-pool-executor/src/call_operator.rs b/sidechain/top-pool-executor/src/call_operator.rs index 165843654b..fc7467deec 100644 --- a/sidechain/top-pool-executor/src/call_operator.rs +++ b/sidechain/top-pool-executor/src/call_operator.rs @@ -18,12 +18,12 @@ use crate::{error::Result, TopPoolOperationHandler}; use ita_stf::TrustedCallSigned; use itp_stf_executor::traits::{StateUpdateProposer, StfExecuteTimedGettersBatch}; -use itp_types::{ShardIdentifier, H256}; +use itp_types::H256; use its_primitives::traits::{ Block as SidechainBlockTrait, ShardIdentifierFor, SignedBlock as SignedSidechainBlockTrait, }; use its_state::{SidechainState, SidechainSystemExt, StateHash}; -use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockCreated, SendState}; +use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockImported, SendState}; use log::*; use sgx_externalities::SgxExternalitiesTrait; use sp_runtime::{traits::Block as ParentchainBlockTrait, MultiSignature}; @@ -49,8 +49,11 @@ pub trait TopPoolCallOperator< fn remove_calls_from_pool( &self, shard: &ShardIdentifierFor, - calls: Vec, + executed_calls: Vec, ) -> Vec; + + // Notify pool about block import for status updates + fn on_block_imported(&self, block: &SignedSidechainBlock::Block); } impl @@ -63,19 +66,22 @@ where SignedSidechainBlock::Block: SidechainBlockTrait, RpcAuthor: AuthorApi - + OnBlockCreated + + OnBlockImported + SendState, StfExecutor: StateUpdateProposer + StfExecuteTimedGettersBatch, ::Externalities: SgxExternalitiesTrait + SidechainState + SidechainSystemExt + StateHash, { - fn get_trusted_calls(&self, shard: &ShardIdentifier) -> Result> { + fn get_trusted_calls( + &self, + shard: &ShardIdentifierFor, + ) -> Result> { Ok(self.rpc_author.get_pending_tops_separated(*shard)?.0) } fn remove_calls_from_pool( &self, - shard: &ShardIdentifier, + shard: &ShardIdentifierFor, executed_calls: Vec, ) -> Vec { let mut failed_to_remove = Vec::new(); @@ -93,4 +99,8 @@ where } failed_to_remove } + + fn on_block_imported(&self, block: &SignedSidechainBlock::Block) { + self.rpc_author.on_block_imported(block.signed_top_hashes(), block.hash()); + } } diff --git a/sidechain/top-pool-executor/src/call_operator_mock.rs b/sidechain/top-pool-executor/src/call_operator_mock.rs index 8f29974b19..fb476cd0b7 100644 --- a/sidechain/top-pool-executor/src/call_operator_mock.rs +++ b/sidechain/top-pool-executor/src/call_operator_mock.rs @@ -97,4 +97,9 @@ where remove_call_invoked_lock.push((*shard, calls)); Default::default() } + + fn on_block_imported(&self, _block: &SignedSidechainBlock::Block) { + // Do nothing for now + // FIXME: We should include unit tests to see if pool is notified about block import + } } diff --git a/sidechain/top-pool-executor/src/getter_operator.rs b/sidechain/top-pool-executor/src/getter_operator.rs index 9d1719b580..5910c6f6fc 100644 --- a/sidechain/top-pool-executor/src/getter_operator.rs +++ b/sidechain/top-pool-executor/src/getter_operator.rs @@ -27,7 +27,7 @@ use its_primitives::traits::{ Block as SidechainBlockTrait, SignedBlock as SignedSidechainBlockTrait, }; use its_state::{SidechainState, SidechainSystemExt, StateHash}; -use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockCreated, SendState}; +use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockImported, SendState}; use log::*; use sgx_externalities::SgxExternalitiesTrait; use sp_runtime::{traits::Block as ParentchainBlockTrait, MultiSignature}; @@ -64,7 +64,7 @@ where SignedSidechainBlock::Block: SidechainBlockTrait, RpcAuthor: AuthorApi - + OnBlockCreated + + OnBlockImported + SendState, StfExecutor: StateUpdateProposer + StfExecuteTimedGettersBatch, ::Externalities: diff --git a/sidechain/top-pool-executor/src/lib.rs b/sidechain/top-pool-executor/src/lib.rs index 03266822a0..bf427b3888 100644 --- a/sidechain/top-pool-executor/src/lib.rs +++ b/sidechain/top-pool-executor/src/lib.rs @@ -54,7 +54,7 @@ use its_primitives::traits::{ Block as SidechainBlockTrait, SignedBlock as SignedSidechainBlockTrait, }; use its_state::{SidechainState, SidechainSystemExt, StateHash}; -use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockCreated, SendState}; +use its_top_pool_rpc_author::traits::{AuthorApi, OnBlockImported, SendState}; use sgx_externalities::SgxExternalitiesTrait; use sp_runtime::{traits::Block as ParentchainBlockTrait, MultiSignature}; use std::{marker::PhantomData, sync::Arc}; @@ -77,7 +77,7 @@ where SignedSidechainBlock::Block: SidechainBlockTrait, RpcAuthor: AuthorApi - + OnBlockCreated + + OnBlockImported + SendState, StfExecutor: StateUpdateProposer + StfExecuteTimedGettersBatch, ::Externalities: diff --git a/sidechain/top-pool-rpc-author/src/author.rs b/sidechain/top-pool-rpc-author/src/author.rs index 406ec76610..9462fd16ba 100644 --- a/sidechain/top-pool-rpc-author/src/author.rs +++ b/sidechain/top-pool-rpc-author/src/author.rs @@ -22,7 +22,7 @@ use crate::{ client_error::Error as ClientError, error::{Error as StateRpcError, Result}, top_filter::{AllowAllTopsFilter, Filter}, - traits::{AuthorApi, OnBlockCreated, SendState}, + traits::{AuthorApi, OnBlockImported, SendState}, }; use codec::{Decode, Encode}; use ita_stf::{hash, Getter, TrustedCallSigned, TrustedGetterSigned, TrustedOperation}; @@ -277,7 +277,7 @@ where } } -impl OnBlockCreated +impl OnBlockImported for Author where TopPool: TrustedOperationPool + Sync + Send + 'static, @@ -288,8 +288,8 @@ where { type Hash = ::Hash; - fn on_block_created(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash) { - self.top_pool.on_block_created(hashes, block_hash) + fn on_block_imported(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash) { + self.top_pool.on_block_imported(hashes, block_hash) } } diff --git a/sidechain/top-pool-rpc-author/src/traits.rs b/sidechain/top-pool-rpc-author/src/traits.rs index 84099b707c..2a977b3b92 100644 --- a/sidechain/top-pool-rpc-author/src/traits.rs +++ b/sidechain/top-pool-rpc-author/src/traits.rs @@ -28,7 +28,7 @@ use std::vec::Vec; /// Trait alias for a full STF author API pub trait FullAuthor = AuthorApi + SendState - + OnBlockCreated + + OnBlockImported + Send + Sync + 'static; @@ -75,8 +75,8 @@ pub trait SendState { } /// Trait to notify listeners/observer of a newly created block -pub trait OnBlockCreated { +pub trait OnBlockImported { type Hash; - fn on_block_created(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash); + fn on_block_imported(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash); } diff --git a/sidechain/top-pool/src/basic_pool.rs b/sidechain/top-pool/src/basic_pool.rs index 51e27fcfe8..e1af0eaf92 100644 --- a/sidechain/top-pool/src/basic_pool.rs +++ b/sidechain/top-pool/src/basic_pool.rs @@ -230,8 +230,8 @@ where self.pool.validated_pool().ready_by_hash(hash, shard) } - fn on_block_created(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash) { - self.pool.validated_pool().on_block_created(hashes, block_hash); + fn on_block_imported(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash) { + self.pool.validated_pool().on_block_imported(hashes, block_hash); } fn rpc_send_state(&self, hash: Self::Hash, state_encoded: Vec) -> Result<(), Error> { diff --git a/sidechain/top-pool/src/primitives.rs b/sidechain/top-pool/src/primitives.rs index ff3e75cc95..f14129d13d 100644 --- a/sidechain/top-pool/src/primitives.rs +++ b/sidechain/top-pool/src/primitives.rs @@ -258,7 +258,7 @@ pub trait TrustedOperationPool: Send + Sync { ) -> Option>; /// Notify the listener of top inclusion in sidechain block - fn on_block_created(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash); + fn on_block_imported(&self, hashes: &[Self::Hash], block_hash: SidechainBlockHash); /// Notify the RPC client of a state update fn rpc_send_state(&self, hash: Self::Hash, state_encoded: Vec) -> Result<(), error::Error>; diff --git a/sidechain/top-pool/src/validated_pool.rs b/sidechain/top-pool/src/validated_pool.rs index 289de3cefb..3b51d83a7b 100644 --- a/sidechain/top-pool/src/validated_pool.rs +++ b/sidechain/top-pool/src/validated_pool.rs @@ -688,7 +688,7 @@ where } /// Notify the listener of top inclusion in sidechain block - pub fn on_block_created(&self, hashes: &[ExtrinsicHash], block_hash: SidechainBlockHash) { + pub fn on_block_imported(&self, hashes: &[ExtrinsicHash], block_hash: SidechainBlockHash) { for top_hash in hashes.iter() { self.listener.write().unwrap().in_block(top_hash, block_hash); }