From a910f3fd8cdef9b744145951b17c1d65bfbf6fc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Kami=C5=84ski?= Date: Fri, 21 Feb 2025 14:43:09 +0200 Subject: [PATCH] Fix (some) unit tests --- linera-base/src/unit_tests.rs | 6 +- linera-chain/src/chain.rs | 2 +- linera-chain/src/unit_tests/chain_tests.rs | 23 +- .../src/unit_tests/data_types_tests.rs | 3 + .../src/unit_tests/wasm_worker_tests.rs | 48 ++-- linera-core/src/unit_tests/worker_tests.rs | 21 +- linera-execution/src/execution.rs | 7 +- linera-execution/src/test_utils/mod.rs | 56 ++--- .../src/test_utils/system_execution_state.rs | 18 +- .../src/unit_tests/runtime_tests.rs | 22 +- .../src/unit_tests/system_tests.rs | 40 +-- .../tests/contract_runtime_apis.rs | 53 ++-- linera-execution/tests/fee_consumption.rs | 4 +- .../tests/service_runtime_apis.rs | 10 +- linera-execution/tests/test_execution.rs | 227 +++++------------- .../tests/test_system_execution.rs | 4 +- linera-execution/tests/wasm.rs | 8 +- linera-sdk/src/test/block.rs | 11 - linera-sdk/src/test/chain.rs | 91 ++----- 19 files changed, 223 insertions(+), 431 deletions(-) diff --git a/linera-base/src/unit_tests.rs b/linera-base/src/unit_tests.rs index b49052274b40..075947aa97db 100644 --- a/linera-base/src/unit_tests.rs +++ b/linera-base/src/unit_tests.rs @@ -104,11 +104,7 @@ fn application_id_test_case() -> ApplicationId { CryptoHash::test_hash("contract bytecode"), CryptoHash::test_hash("service bytecode"), ), - creation: MessageId { - chain_id: ChainId::root(0), - height: BlockHeight(0), - index: 0, - }, + application_description_hash: CryptoHash::test_hash("application description"), } } diff --git a/linera-chain/src/chain.rs b/linera-chain/src/chain.rs index a3efa59c2c91..fab90b26b4be 100644 --- a/linera-chain/src/chain.rs +++ b/linera-chain/src/chain.rs @@ -152,7 +152,7 @@ static STATE_HASH_COMPUTATION_LATENCY: LazyLock = LazyLock::new(|| }); /// The BCS-serialized size of an empty [`Block`]. -const EMPTY_BLOCK_SIZE: usize = 91; +const EMPTY_BLOCK_SIZE: usize = 92; /// An origin, cursor and timestamp of a unskippable bundle in our inbox. #[derive(Debug, Clone, Serialize, Deserialize, async_graphql::SimpleObject)] diff --git a/linera-chain/src/unit_tests/chain_tests.rs b/linera-chain/src/unit_tests/chain_tests.rs index 2a9b68c075cb..dafda5e0a8a0 100644 --- a/linera-chain/src/unit_tests/chain_tests.rs +++ b/linera-chain/src/unit_tests/chain_tests.rs @@ -71,7 +71,9 @@ fn make_app_description() -> (UserApplicationDescription, Blob, Blob) { ( UserApplicationDescription { bytecode_id, - creation: make_admin_message_id(BlockHeight(2)), + creator_chain_id: admin_id(), + block_height: BlockHeight(2), + application_index: 0, required_application_ids: vec![], parameters: vec![], }, @@ -112,7 +114,7 @@ async fn test_block_size_limit() { let mut chain = ChainStateView::new(chain_id).await; // The size of the executed valid block below. - let maximum_executed_block_size = 685; + let maximum_executed_block_size = 687; // Initialize the chain. let mut config = make_open_chain_config(); @@ -198,7 +200,13 @@ async fn test_application_permissions() -> anyhow::Result<()> { extra .user_contracts() .insert(application_id, application.clone().into()); - extra.add_blobs([contract_blob, service_blob]).await?; + extra + .add_blobs([ + contract_blob, + service_blob, + Blob::new_application_description(&app_description), + ]) + .await?; // Initialize the chain, with a chain application. let config = OpenChainConfig { @@ -210,10 +218,6 @@ async fn test_application_permissions() -> anyhow::Result<()> { .await?; let open_chain_message = Message::System(SystemMessage::OpenChain(config)); - let register_app_message = SystemMessage::RegisterApplications { - applications: vec![app_description], - }; - // The OpenChain message must be included in the first block. Also register the app. let bundle = IncomingBundle { origin: Origin::chain(admin_id()), @@ -222,10 +226,7 @@ async fn test_application_permissions() -> anyhow::Result<()> { height: BlockHeight(1), transaction_index: 0, timestamp: Timestamp::from(0), - messages: vec![ - open_chain_message.to_posted(0, MessageKind::Protected), - register_app_message.to_posted(1, MessageKind::Simple), - ], + messages: vec![open_chain_message.to_posted(0, MessageKind::Protected)], }, action: MessageAction::Accept, }; diff --git a/linera-chain/src/unit_tests/data_types_tests.rs b/linera-chain/src/unit_tests/data_types_tests.rs index a99bec38d8cd..b4ee7652bb84 100644 --- a/linera-chain/src/unit_tests/data_types_tests.rs +++ b/linera-chain/src/unit_tests/data_types_tests.rs @@ -23,6 +23,7 @@ fn test_signed_values() { state_hash: CryptoHash::test_hash("state"), oracle_responses: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], } .with(block); let confirmed_value = Hashed::new(ConfirmedBlock::new(executed_block.clone())); @@ -71,6 +72,7 @@ fn test_hashes() { state_hash: CryptoHash::test_hash("state"), oracle_responses: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], } .with(block); let confirmed_hashed = Hashed::new(ConfirmedBlock::new(executed_block.clone())); @@ -94,6 +96,7 @@ fn test_certificates() { state_hash: CryptoHash::test_hash("state"), oracle_responses: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], } .with(block); let value = Hashed::new(ConfirmedBlock::new(executed_block)); diff --git a/linera-core/src/unit_tests/wasm_worker_tests.rs b/linera-core/src/unit_tests/wasm_worker_tests.rs index 5ad39aafaacb..c8f4e00bc223 100644 --- a/linera-core/src/unit_tests/wasm_worker_tests.rs +++ b/linera-core/src/unit_tests/wasm_worker_tests.rs @@ -19,22 +19,17 @@ use linera_base::{ Amount, Blob, BlockHeight, Bytecode, OracleResponse, Timestamp, UserApplicationDescription, }, hashed::Hashed, - identifiers::{ - BytecodeId, ChainDescription, ChainId, Destination, MessageId, UserApplicationId, - }, + identifiers::{BytecodeId, ChainDescription, ChainId}, ownership::ChainOwnership, }; use linera_chain::{ - data_types::{BlockExecutionOutcome, OutgoingMessage}, + data_types::BlockExecutionOutcome, test::{make_child_block, make_first_block, BlockTestExt}, types::ConfirmedBlock, }; use linera_execution::{ - committee::Epoch, - system::{SystemMessage, SystemOperation}, - test_utils::SystemExecutionState, - Message, MessageKind, Operation, OperationContext, ResourceController, TransactionTracker, - WasmContractModule, WasmRuntime, + committee::Epoch, system::SystemOperation, test_utils::SystemExecutionState, Operation, + OperationContext, ResourceController, TransactionTracker, WasmContractModule, WasmRuntime, }; use linera_storage::{DbStorage, Storage}; #[cfg(feature = "dynamodb")] @@ -148,6 +143,7 @@ where BlockExecutionOutcome { messages: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: publisher_state_hash, oracle_responses: vec![vec![]], } @@ -193,34 +189,25 @@ where instantiation_argument: initial_value_bytes.clone(), required_application_ids: vec![], }; - let application_id = UserApplicationId { - bytecode_id, - creation: MessageId { - chain_id: creator_chain.into(), - height: BlockHeight::from(0), - index: 0, - }, - }; let application_description = UserApplicationDescription { bytecode_id, - creation: application_id.creation, + creator_chain_id: creator_chain.into(), + block_height: BlockHeight::from(0), + application_index: 0, required_application_ids: vec![], parameters: parameters_bytes, }; + let application_id = From::from(&application_description); let create_block = make_first_block(creator_chain.into()) .with_timestamp(2) .with_operation(create_operation); - creator_system_state - .registry - .known_applications - .insert(application_id, application_description.clone()); creator_system_state.timestamp = Timestamp::from(2); let mut creator_state = creator_system_state.into_view().await; creator_state .simulate_instantiation( contract.into(), Timestamp::from(2), - application_description, + application_description.clone(), initial_value_bytes.clone(), contract_blob, service_blob, @@ -228,20 +215,16 @@ where .await?; let create_block_proposal = Hashed::new(ConfirmedBlock::new( BlockExecutionOutcome { - messages: vec![vec![OutgoingMessage { - destination: Destination::Recipient(creator_chain.into()), - authenticated_signer: None, - grant: Amount::ZERO, - refund_grant_to: None, - kind: MessageKind::Protected, - message: Message::System(SystemMessage::ApplicationCreated), - }]], + messages: vec![vec![]], events: vec![Vec::new()], state_hash: creator_state.crypto_hash().await?, oracle_responses: vec![vec![ OracleResponse::Blob(contract_blob_id), OracleResponse::Blob(service_blob_id), ]], + blobs: vec![vec![Blob::new_application_description( + &application_description, + )]], } .with(create_block), )); @@ -285,7 +268,7 @@ where application_id, bytes: user_operation, }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await?; @@ -294,6 +277,7 @@ where BlockExecutionOutcome { messages: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: creator_state.crypto_hash().await?, oracle_responses: vec![Vec::new()], } diff --git a/linera-core/src/unit_tests/worker_tests.rs b/linera-core/src/unit_tests/worker_tests.rs index eeab1f0c2340..88e3693be723 100644 --- a/linera-core/src/unit_tests/worker_tests.rs +++ b/linera-core/src/unit_tests/worker_tests.rs @@ -327,11 +327,13 @@ where let tx_count = block.operations.len() + block.incoming_bundles.len(); let oracle_responses = iter::repeat_with(Vec::new).take(tx_count).collect(); let events = iter::repeat_with(Vec::new).take(tx_count).collect(); + let blobs = iter::repeat_with(Vec::new).take(tx_count).collect(); let state_hash = system_state.into_hash().await; let value = Hashed::new(ConfirmedBlock::new( BlockExecutionOutcome { messages, events, + blobs, state_hash, oracle_responses, } @@ -800,6 +802,7 @@ where )], ], events: vec![Vec::new(); 2], + blobs: vec![Vec::new(); 2], state_hash: SystemExecutionState { committees: [(epoch, committee.clone())].into_iter().collect(), ownership: ChainOwnership::single(sender_key_pair.public().into()), @@ -829,6 +832,7 @@ where Amount::from_tokens(3), )]], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: SystemExecutionState { committees: [(epoch, committee.clone())].into_iter().collect(), ownership: ChainOwnership::single(sender_key_pair.public().into()), @@ -1065,6 +1069,7 @@ where vec![direct_credit_message(ChainId::root(3), Amount::ONE)], ], events: vec![Vec::new(); 2], + blobs: vec![Vec::new(); 2], state_hash: SystemExecutionState { committees: [(epoch, committee.clone())].into_iter().collect(), ownership: ChainOwnership::single(recipient_key_pair.public().into()), @@ -1359,6 +1364,7 @@ where BlockExecutionOutcome { messages: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: state.into_hash().await, oracle_responses: vec![Vec::new()], } @@ -2393,6 +2399,7 @@ where ), ]], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: SystemExecutionState { committees: committees.clone(), ownership: ChainOwnership::single(key_pair.public().into()), @@ -2459,6 +2466,7 @@ where vec![direct_credit_message(user_id, Amount::from_tokens(2))], ], events: vec![Vec::new(); 2], + blobs: vec![Vec::new(); 2], state_hash: SystemExecutionState { // The root chain knows both committees at the end. committees: committees2.clone(), @@ -2561,6 +2569,7 @@ where BlockExecutionOutcome { messages: vec![Vec::new(); 3], events: vec![Vec::new(); 3], + blobs: vec![Vec::new(); 3], state_hash: SystemExecutionState { subscriptions: [ChannelSubscription { chain_id: admin_id, @@ -2713,6 +2722,7 @@ where BlockExecutionOutcome { messages: vec![vec![direct_credit_message(admin_id, Amount::ONE)]], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: SystemExecutionState { committees: committees.clone(), ownership: ChainOwnership::single(owner1), @@ -2747,6 +2757,7 @@ where }, )]], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: SystemExecutionState { committees: committees2.clone(), ownership: ChainOwnership::single(owner0), @@ -2844,6 +2855,7 @@ where BlockExecutionOutcome { messages: vec![vec![direct_credit_message(admin_id, Amount::ONE)]], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: SystemExecutionState { committees: committees.clone(), ownership: ChainOwnership::single(owner1), @@ -2878,6 +2890,7 @@ where })], ], events: vec![Vec::new(); 2], + blobs: vec![Vec::new(); 2], state_hash: SystemExecutionState { committees: committees3.clone(), ownership: ChainOwnership::single(owner0), @@ -2939,6 +2952,7 @@ where BlockExecutionOutcome { messages: vec![Vec::new()], events: vec![Vec::new()], + blobs: vec![Vec::new()], state_hash: SystemExecutionState { committees: committees3.clone(), ownership: ChainOwnership::single(owner0), @@ -3792,7 +3806,7 @@ where let (application_id, application); { let mut chain = storage.load_chain(chain_id).await?; - (application_id, application) = chain.execution_state.register_mock_application().await?; + (application_id, application) = chain.execution_state.register_mock_application(0).await?; chain.save().await?; } @@ -3881,7 +3895,7 @@ where let (application_id, application); { let mut chain = storage.load_chain(chain_id).await?; - (application_id, application) = chain.execution_state.register_mock_application().await?; + (application_id, application) = chain.execution_state.register_mock_application(0).await?; chain.save().await?; } @@ -3968,12 +3982,13 @@ where } .into_view() .await; - let _ = state.register_mock_application().await?; + let _ = state.register_mock_application(0).await?; let value = Hashed::new(ConfirmedBlock::new( BlockExecutionOutcome { messages: vec![], events: vec![], + blobs: vec![], state_hash: state.crypto_hash_mut().await?, oracle_responses: vec![], } diff --git a/linera-execution/src/execution.rs b/linera-execution/src/execution.rs index eb8f6a05e9b0..92655ce9c09a 100644 --- a/linera-execution/src/execution.rs +++ b/linera-execution/src/execution.rs @@ -79,10 +79,7 @@ where let action = UserAction::Instantiate(context, instantiation_argument); let next_message_index = application_description.application_index + 1; - let application_id = self - .system - .register_application(application_description) - .await?; + let application_id = From::from(&application_description); self.system.used_blobs.insert(&contract_blob.id())?; self.system.used_blobs.insert(&service_blob.id())?; @@ -104,8 +101,8 @@ where tracker, account: None, }; - // TODO: can we just reuse the same index for both counters here? let mut txn_tracker = TransactionTracker::new(next_message_index, next_message_index, None); + txn_tracker.add_created_blob(Blob::new_application_description(&application_description)); self.run_user_action( application_id, chain_id, diff --git a/linera-execution/src/test_utils/mod.rs b/linera-execution/src/test_utils/mod.rs index 9a48e5a1b936..212f94985955 100644 --- a/linera-execution/src/test_utils/mod.rs +++ b/linera-execution/src/test_utils/mod.rs @@ -29,15 +29,15 @@ pub use self::{ system_execution_state::SystemExecutionState, }; use crate::{ - ApplicationRegistryView, ExecutionRequest, ExecutionRuntimeContext, ExecutionStateView, - MessageContext, OperationContext, QueryContext, ServiceRuntimeEndpoint, ServiceRuntimeRequest, + ExecutionRequest, ExecutionRuntimeContext, ExecutionStateView, MessageContext, + OperationContext, QueryContext, ServiceRuntimeEndpoint, ServiceRuntimeRequest, ServiceSyncRuntime, SystemExecutionStateView, TestExecutionRuntimeContext, UserApplicationDescription, UserApplicationId, }; /// Creates a dummy [`UserApplicationDescription`] for use in tests. pub fn create_dummy_user_application_description( - index: u64, + index: u32, ) -> (UserApplicationDescription, Blob, Blob) { let chain_id = ChainId::root(1); let contract_blob = Blob::new_contract_bytecode(CompressedBytecode { @@ -50,11 +50,9 @@ pub fn create_dummy_user_application_description( ( UserApplicationDescription { bytecode_id: BytecodeId::new(contract_blob.id().hash, service_blob.id().hash), - creation: MessageId { - chain_id, - height: BlockHeight(index), - index: 1, - }, + creator_chain_id: chain_id, + block_height: 0.into(), + application_index: index, required_application_ids: vec![], parameters: vec![], }, @@ -110,19 +108,13 @@ pub trait RegisterMockApplication { /// This is included in the mocked [`ApplicationId`]. fn creator_chain_id(&self) -> ChainId; - /// Returns the amount of known registered applications. - /// - /// Used to avoid duplicate registrations. - async fn registered_application_count(&self) -> anyhow::Result; - /// Registers a new [`MockApplication`] and returns it with the [`UserApplicationId`] that was /// used for it. async fn register_mock_application( &mut self, + index: u32, ) -> anyhow::Result<(UserApplicationId, MockApplication)> { - let (description, contract, service) = create_dummy_user_application_description( - self.registered_application_count().await? as u64, - ); + let (description, contract, service) = create_dummy_user_application_description(index); self.register_mock_application_with(description, contract, service) .await @@ -147,10 +139,6 @@ where self.system.creator_chain_id() } - async fn registered_application_count(&self) -> anyhow::Result { - self.system.registered_application_count().await - } - async fn register_mock_application_with( &mut self, description: UserApplicationDescription, @@ -174,17 +162,13 @@ where ).into() } - async fn registered_application_count(&self) -> anyhow::Result { - Ok(self.registry.known_applications.count().await?) - } - async fn register_mock_application_with( &mut self, description: UserApplicationDescription, contract: Blob, service: Blob, ) -> anyhow::Result<(UserApplicationId, MockApplication)> { - let id = self.registry.register_application(description).await?; + let id = From::from(&description); let extra = self.context().extra(); let mock_application = MockApplication::default(); @@ -194,27 +178,27 @@ where extra .user_services() .insert(id, mock_application.clone().into()); - extra.add_blobs([contract, service]).await?; + extra + .add_blobs([ + contract, + service, + Blob::new_application_description(&description), + ]) + .await?; Ok((id, mock_application)) } } -pub async fn create_dummy_user_application_registrations( - registry: &mut ApplicationRegistryView, - count: u64, -) -> anyhow::Result> -where - C: Context + Clone + Send + Sync + 'static, -{ +pub async fn create_dummy_user_application_registrations( + count: u32, +) -> anyhow::Result> { let mut ids = Vec::with_capacity(count as usize); for index in 0..count { let (description, contract_blob, service_blob) = create_dummy_user_application_description(index); - let id = registry.register_application(description.clone()).await?; - - assert_eq!(registry.describe_application(id).await?, description); + let id = From::from(&description); ids.push((id, description, contract_blob, service_blob)); } diff --git a/linera-execution/src/test_utils/system_execution_state.rs b/linera-execution/src/test_utils/system_execution_state.rs index 48b67d435803..a0bd404fcb04 100644 --- a/linera-execution/src/test_utils/system_execution_state.rs +++ b/linera-execution/src/test_utils/system_execution_state.rs @@ -23,7 +23,6 @@ use linera_views::{ use super::{MockApplication, RegisterMockApplication}; use crate::{ - applications::ApplicationRegistry, committee::{Committee, Epoch}, execution::UserAction, system::SystemChannel, @@ -45,7 +44,6 @@ pub struct SystemExecutionState { #[debug(skip_if = BTreeMap::is_empty)] pub balances: BTreeMap, pub timestamp: Timestamp, - pub registry: ApplicationRegistry, pub used_blobs: BTreeSet, #[debug(skip_if = Not::not)] pub closed: bool, @@ -108,7 +106,6 @@ impl SystemExecutionState { balance, balances, timestamp, - registry, used_blobs, closed, application_permissions, @@ -158,10 +155,6 @@ impl SystemExecutionState { .expect("insertion of balances should not fail"); } view.system.timestamp.set(timestamp); - view.system - .registry - .import(registry) - .expect("serialization of registry components should not fail"); for blob_id in used_blobs { view.system .used_blobs @@ -183,10 +176,6 @@ impl RegisterMockApplication for SystemExecutionState { ).into() } - async fn registered_application_count(&self) -> anyhow::Result { - Ok(self.registry.known_applications.len()) - } - async fn register_mock_application_with( &mut self, description: UserApplicationDescription, @@ -196,8 +185,11 @@ impl RegisterMockApplication for SystemExecutionState { let id = ApplicationId::from(&description); let application = MockApplication::default(); - self.registry.known_applications.insert(id, description); - self.extra_blobs.extend([contract, service]); + self.extra_blobs.extend([ + contract, + service, + Blob::new_application_description(&description), + ]); self.mock_applications.insert(id, application.clone()); Ok((id, application)) diff --git a/linera-execution/src/unit_tests/runtime_tests.rs b/linera-execution/src/unit_tests/runtime_tests.rs index a675e105bfd1..946e4c908296 100644 --- a/linera-execution/src/unit_tests/runtime_tests.rs +++ b/linera-execution/src/unit_tests/runtime_tests.rs @@ -14,7 +14,7 @@ use futures::{channel::mpsc, StreamExt}; use linera_base::{ crypto::CryptoHash, data_types::{BlockHeight, Timestamp}, - identifiers::{ApplicationId, BytecodeId, ChainDescription, MessageId}, + identifiers::{ApplicationId, BytecodeId, ChainDescription}, }; use linera_views::batch::Batch; @@ -22,6 +22,7 @@ use super::{ApplicationStatus, SyncRuntimeHandle, SyncRuntimeInternal}; use crate::{ execution_state_actor::ExecutionRequest, runtime::{LoadedApplication, ResourceController, SyncRuntime}, + test_utils::create_dummy_user_application_description, ContractRuntime, RawExecutionOutcome, TransactionTracker, UserContractInstance, }; @@ -185,7 +186,7 @@ fn create_runtime() -> ( execution_state_sender, None, resource_controller, - TransactionTracker::new(0, Some(Vec::new())), + TransactionTracker::new(0, 0, Some(Vec::new())), ); (runtime, execution_state_receiver) @@ -193,10 +194,12 @@ fn create_runtime() -> ( /// Creates an [`ApplicationStatus`] for a dummy application. fn create_dummy_application() -> ApplicationStatus { + let (description, _, _) = create_dummy_user_application_description(0); + let id = From::from(&description); ApplicationStatus { caller_id: None, - id: create_dummy_application_id(), - parameters: vec![], + id, + description, signer: None, outcome: RawExecutionOutcome::default(), } @@ -204,18 +207,12 @@ fn create_dummy_application() -> ApplicationStatus { /// Creates a dummy [`ApplicationId`]. fn create_dummy_application_id() -> ApplicationId { - let chain_id = ChainDescription::Root(1).into(); - ApplicationId { bytecode_id: BytecodeId::new( CryptoHash::test_hash("contract"), CryptoHash::test_hash("service"), ), - creation: MessageId { - chain_id, - height: BlockHeight(1), - index: 1, - }, + application_description_hash: CryptoHash::test_hash("application description"), } } @@ -224,9 +221,10 @@ fn create_fake_application_with_runtime( runtime: &SyncRuntimeHandle>, ) -> LoadedApplication> { let fake_instance: Arc = runtime.0.clone(); + let (description, _, _) = create_dummy_user_application_description(0); LoadedApplication { instance: Arc::new(Mutex::new(fake_instance)), - parameters: vec![], + description, } } diff --git a/linera-execution/src/unit_tests/system_tests.rs b/linera-execution/src/unit_tests/system_tests.rs index a7c4304e5908..e23509cfcaf5 100644 --- a/linera-execution/src/unit_tests/system_tests.rs +++ b/linera-execution/src/unit_tests/system_tests.rs @@ -1,10 +1,7 @@ // Copyright (c) Zefchain Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -use linera_base::{ - data_types::{Blob, BlockHeight, Bytecode}, - identifiers::ApplicationId, -}; +use linera_base::data_types::{Blob, BlockHeight, Bytecode}; use linera_views::context::MemoryContext; use super::*; @@ -36,6 +33,24 @@ async fn new_view_and_context() -> ( (view, context) } +fn expected_application_id( + context: &OperationContext, + bytecode_id: &BytecodeId, + parameters: Vec, + required_application_ids: Vec, + application_index: u32, +) -> UserApplicationId { + let description = UserApplicationDescription { + bytecode_id: *bytecode_id, + creator_chain_id: context.chain_id, + block_height: context.height, + application_index, + parameters, + required_application_ids, + }; + From::from(&description) +} + #[tokio::test] async fn application_message_index() -> anyhow::Result<()> { let (mut view, context) = new_view_and_context().await; @@ -60,23 +75,10 @@ async fn application_message_index() -> anyhow::Result<()> { .system .execute_operation(context, operation, &mut txn_tracker) .await?; - let [ExecutionOutcome::System(result)] = &txn_tracker.into_outcome().unwrap().outcomes[..] - else { + let [ExecutionOutcome::System(_)] = &txn_tracker.into_outcome().unwrap().outcomes[..] else { panic!("Unexpected outcome"); }; - assert_eq!( - result.messages[CREATE_APPLICATION_MESSAGE_INDEX as usize].message, - SystemMessage::ApplicationCreated - ); - let creation = MessageId { - chain_id: context.chain_id, - height: context.height, - index: CREATE_APPLICATION_MESSAGE_INDEX, - }; - let id = ApplicationId { - bytecode_id, - creation, - }; + let id = expected_application_id(&context, &bytecode_id, vec![], vec![], 0); assert_eq!(new_application, Some((id, vec![]))); Ok(()) diff --git a/linera-execution/tests/contract_runtime_apis.rs b/linera-execution/tests/contract_runtime_apis.rs index 47f206f700c7..c28bba32fb2b 100644 --- a/linera-execution/tests/contract_runtime_apis.rs +++ b/linera-execution/tests/contract_runtime_apis.rs @@ -16,8 +16,7 @@ use linera_base::{ Amount, Blob, BlockHeight, CompressedBytecode, Timestamp, UserApplicationDescription, }, identifiers::{ - Account, AccountOwner, ApplicationId, BytecodeId, ChainDescription, ChainId, MessageId, - Owner, + Account, AccountOwner, ApplicationId, BytecodeId, ChainDescription, ChainId, Owner, }, ownership::ChainOwnership, }; @@ -80,7 +79,7 @@ async fn test_transfer_system_api( application_id, bytes: vec![], }; - let mut tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -111,7 +110,7 @@ async fn test_transfer_system_api( Timestamp::from(0), Message::System(outcome.messages[0].message.clone()), None, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await?; @@ -172,7 +171,7 @@ async fn test_unauthorized_transfer_system_api( context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -253,7 +252,7 @@ async fn test_claim_system_api( application_id, bytes: vec![], }; - let mut tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut tracker = TransactionTracker::new(0, 0, Some(Vec::new())); claimer_view .execute_operation( context, @@ -280,7 +279,7 @@ async fn test_claim_system_api( assert_eq!(outcome.messages.len(), 1); - let mut tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut tracker = TransactionTracker::new(0, 0, Some(Vec::new())); source_view .execute_message( create_dummy_message_context(None), @@ -320,7 +319,7 @@ async fn test_claim_system_api( assert_eq!(outcome.messages.len(), 1); - let mut tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut tracker = TransactionTracker::new(0, 0, Some(Vec::new())); let context = MessageContext { chain_id: claimer_chain_id, ..create_dummy_message_context(None) @@ -403,7 +402,7 @@ async fn test_unauthorized_claims( application_id, bytes: vec![], }; - let mut tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut tracker = TransactionTracker::new(0, 0, Some(Vec::new())); let result = claimer_view .execute_operation( context, @@ -435,7 +434,7 @@ async fn test_read_chain_balance_system_api(chain_balance: Amount) { .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -456,7 +455,7 @@ async fn test_read_chain_balance_system_api(chain_balance: Amount) { context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await @@ -476,7 +475,7 @@ async fn test_read_owner_balance_system_api( .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -499,7 +498,7 @@ async fn test_read_owner_balance_system_api( context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await @@ -516,7 +515,7 @@ async fn test_read_owner_balance_returns_zero_for_missing_accounts(missing_accou .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -540,7 +539,7 @@ async fn test_read_owner_balance_returns_zero_for_missing_accounts(missing_accou context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await @@ -560,7 +559,7 @@ async fn test_read_owner_balances_system_api( .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -584,7 +583,7 @@ async fn test_read_owner_balances_system_api( context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await @@ -604,7 +603,7 @@ async fn test_read_balance_owners_system_api( .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -628,7 +627,7 @@ async fn test_read_balance_owners_system_api( context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await @@ -661,11 +660,9 @@ impl TransferTestEndpoint { UserApplicationDescription { bytecode_id: BytecodeId::new(contract_id, service_id), - creation: MessageId { - chain_id: ChainId::root(1000), - height: BlockHeight(0), - index: 0, - }, + creator_chain_id: ChainId::root(1000), + block_height: BlockHeight(0), + application_index: 0, parameters: vec![], required_application_ids: vec![], } @@ -699,11 +696,9 @@ impl TransferTestEndpoint { CryptoHash::test_hash("recipient contract bytecode"), CryptoHash::test_hash("recipient service bytecode"), ), - creation: MessageId { - chain_id: ChainId::root(2000), - height: BlockHeight(0), - index: 0, - }, + application_description_hash: CryptoHash::test_hash( + "recipient application description", + ), } } diff --git a/linera-execution/tests/fee_consumption.rs b/linera-execution/tests/fee_consumption.rs index fdc4b31b51f7..fab88e7d4467 100644 --- a/linera-execution/tests/fee_consumption.rs +++ b/linera-execution/tests/fee_consumption.rs @@ -121,7 +121,7 @@ async fn test_fee_consumption( description: Some(ChainDescription::Root(0)), ..SystemExecutionState::default() }; - let (application_id, application) = state.register_mock_application().await?; + let (application_id, application) = state.register_mock_application(0).await?; let mut view = state.into_view().await; let signer = Owner::from(AccountPublicKey::test_key(0)); @@ -196,7 +196,7 @@ async fn test_fee_consumption( message_id: MessageId::default(), }; let mut grant = initial_grant.unwrap_or_default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_message( context, Timestamp::from(0), diff --git a/linera-execution/tests/service_runtime_apis.rs b/linera-execution/tests/service_runtime_apis.rs index 0df08ffb40b0..7d3b52f7e87c 100644 --- a/linera-execution/tests/service_runtime_apis.rs +++ b/linera-execution/tests/service_runtime_apis.rs @@ -29,7 +29,7 @@ async fn test_read_chain_balance_system_api(chain_balance: Amount) { .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::handle_query( move |runtime, _context, _query| { @@ -61,7 +61,7 @@ async fn test_read_owner_balance_system_api( .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::handle_query( move |runtime, _context, _query| { @@ -92,7 +92,7 @@ async fn test_read_owner_balance_returns_zero_for_missing_accounts(missing_accou .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::handle_query( move |runtime, _context, _query| { @@ -127,7 +127,7 @@ async fn test_read_owner_balances_system_api( .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::handle_query( move |runtime, _context, _query| { @@ -162,7 +162,7 @@ async fn test_read_balance_owners_system_api( .into_view() .await; - let (application_id, application) = view.register_mock_application().await.unwrap(); + let (application_id, application) = view.register_mock_application(0).await.unwrap(); application.expect_call(ExpectedCall::handle_query( move |runtime, _context, _query| { diff --git a/linera-execution/tests/test_execution.rs b/linera-execution/tests/test_execution.rs index 320cab872575..44ea6dc0aeb2 100644 --- a/linera-execution/tests/test_execution.rs +++ b/linera-execution/tests/test_execution.rs @@ -7,7 +7,6 @@ use std::{collections::BTreeMap, vec}; use anyhow::Context as _; use assert_matches::assert_matches; -use futures::{stream, StreamExt, TryStreamExt}; use linera_base::{ crypto::{AccountPublicKey, ValidatorPublicKey}, data_types::{ @@ -27,9 +26,9 @@ use linera_execution::{ SystemExecutionState, }, BaseRuntime, ContractRuntime, ExecutionError, ExecutionOutcome, ExecutionRuntimeContext, - Message, MessageKind, Operation, OperationContext, Query, QueryContext, QueryOutcome, - QueryResponse, RawExecutionOutcome, RawOutgoingMessage, ResourceControlPolicy, - ResourceController, SystemOperation, TransactionTracker, + Message, Operation, OperationContext, Query, QueryContext, QueryOutcome, QueryResponse, + RawExecutionOutcome, RawOutgoingMessage, ResourceControlPolicy, ResourceController, + SystemOperation, TransactionTracker, }; use linera_views::{batch::Batch, context::Context, views::View}; use test_case::test_case; @@ -41,7 +40,7 @@ async fn test_missing_bytecode_for_user_application() -> anyhow::Result<()> { let mut view = state.into_view().await; let (app_id, app_desc, contract_blob, service_blob) = - &create_dummy_user_application_registrations(&mut view.system.registry, 1).await?[0]; + &create_dummy_user_application_registrations(1).await?[0]; view.context() .extra() .add_blobs([contract_blob.clone(), service_blob.clone()]) @@ -57,7 +56,7 @@ async fn test_missing_bytecode_for_user_application() -> anyhow::Result<()> { application_id: *app_id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -76,8 +75,8 @@ async fn test_simple_user_operation() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; let owner = Owner::from(AccountPublicKey::test_key(0)); let state_key = vec![]; @@ -140,7 +139,7 @@ async fn test_simple_user_operation() -> anyhow::Result<()> { ..create_dummy_operation_context() }; let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -264,8 +263,8 @@ async fn test_simulated_session() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -320,7 +319,7 @@ async fn test_simulated_session() -> anyhow::Result<()> { let context = create_dummy_operation_context(); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -372,8 +371,8 @@ async fn test_simulated_session_leak() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -425,7 +424,7 @@ async fn test_simulated_session_leak() -> anyhow::Result<()> { application_id: caller_id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -441,7 +440,7 @@ async fn test_rejecting_block_from_finalize() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (id, application) = view.register_mock_application().await?; + let (id, application) = view.register_mock_application(0).await?; application.expect_call(ExpectedCall::execute_operation( move |_runtime, _context, _operation| Ok(vec![]), @@ -463,7 +462,7 @@ async fn test_rejecting_block_from_finalize() -> anyhow::Result<()> { application_id: id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -479,10 +478,10 @@ async fn test_rejecting_block_from_called_applications_finalize() -> anyhow::Res state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (first_id, first_application) = view.register_mock_application().await?; - let (second_id, second_application) = view.register_mock_application().await?; - let (third_id, third_application) = view.register_mock_application().await?; - let (fourth_id, fourth_application) = view.register_mock_application().await?; + let (first_id, first_application) = view.register_mock_application(0).await?; + let (second_id, second_application) = view.register_mock_application(1).await?; + let (third_id, third_application) = view.register_mock_application(2).await?; + let (fourth_id, fourth_application) = view.register_mock_application(3).await?; first_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -525,7 +524,7 @@ async fn test_rejecting_block_from_called_applications_finalize() -> anyhow::Res application_id: first_id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -541,10 +540,10 @@ async fn test_sending_message_from_finalize() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (first_id, first_application) = view.register_mock_application().await?; - let (second_id, second_application) = view.register_mock_application().await?; - let (third_id, third_application) = view.register_mock_application().await?; - let (fourth_id, fourth_application) = view.register_mock_application().await?; + let (first_id, first_application) = view.register_mock_application(0).await?; + let (second_id, second_application) = view.register_mock_application(1).await?; + let (third_id, third_application) = view.register_mock_application(2).await?; + let (fourth_id, fourth_application) = view.register_mock_application(3).await?; let destination_chain = ChainId::from(ChainDescription::Root(1)); let first_message = SendMessageRequest { @@ -625,7 +624,7 @@ async fn test_sending_message_from_finalize() -> anyhow::Result<()> { let context = create_dummy_operation_context(); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -637,20 +636,7 @@ async fn test_sending_message_from_finalize() -> anyhow::Result<()> { &mut controller, ) .await?; - view.update_execution_outcomes_with_app_registrations(&mut txn_tracker) - .await?; - let applications = stream::iter([third_id, first_id]) - .then(|id| view.system.registry.describe_application(id)) - .try_collect() - .await?; - let registration_message = RawOutgoingMessage { - destination: Destination::from(destination_chain), - authenticated: false, - grant: Amount::ZERO, - kind: MessageKind::Simple, - message: SystemMessage::RegisterApplications { applications }, - }; let account = Account { chain_id: ChainId::root(0), owner: None, @@ -660,9 +646,7 @@ async fn test_sending_message_from_finalize() -> anyhow::Result<()> { assert_eq!( txn_outcome.outcomes, vec![ - ExecutionOutcome::System( - RawExecutionOutcome::default().with_message(registration_message) - ), + ExecutionOutcome::System(RawExecutionOutcome::default()), ExecutionOutcome::User( fourth_id, RawExecutionOutcome::default().with_refund_grant_to(Some(account)) @@ -714,8 +698,8 @@ async fn test_cross_application_call_from_finalize() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, _target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, _target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |_runtime, _context, _operation| Ok(vec![]), @@ -738,7 +722,7 @@ async fn test_cross_application_call_from_finalize() -> anyhow::Result<()> { application_id: caller_id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -762,8 +746,8 @@ async fn test_cross_application_call_from_finalize_of_called_application() -> an state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -793,7 +777,7 @@ async fn test_cross_application_call_from_finalize_of_called_application() -> an application_id: caller_id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -816,8 +800,8 @@ async fn test_calling_application_again_from_finalize() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -847,7 +831,7 @@ async fn test_calling_application_again_from_finalize() -> anyhow::Result<()> { application_id: caller_id, bytes: vec![], }, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await; @@ -873,8 +857,8 @@ async fn test_cross_application_error() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -900,7 +884,7 @@ async fn test_cross_application_error() -> anyhow::Result<()> { bytes: vec![], }, &mut TransactionTracker::new( - 0, + 0, 0, Some(Vec::new())), &mut controller, ) @@ -919,7 +903,7 @@ async fn test_simple_message() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (application_id, application) = view.register_mock_application().await?; + let (application_id, application) = view.register_mock_application(0).await?; let destination_chain = ChainId::from(ChainDescription::Root(1)); let dummy_message = SendMessageRequest { @@ -944,7 +928,7 @@ async fn test_simple_message() -> anyhow::Result<()> { let context = create_dummy_operation_context(); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -956,23 +940,7 @@ async fn test_simple_message() -> anyhow::Result<()> { &mut controller, ) .await?; - view.update_execution_outcomes_with_app_registrations(&mut txn_tracker) - .await?; - let application_description = view - .system - .registry - .describe_application(application_id) - .await?; - let registration_message = RawOutgoingMessage { - destination: Destination::from(destination_chain), - authenticated: false, - grant: Amount::ZERO, - kind: MessageKind::Simple, - message: SystemMessage::RegisterApplications { - applications: vec![application_description], - }, - }; let account = Account { chain_id: ChainId::root(0), owner: None, @@ -982,9 +950,7 @@ async fn test_simple_message() -> anyhow::Result<()> { assert_eq!( txn_outcome.outcomes, &[ - ExecutionOutcome::System( - RawExecutionOutcome::default().with_message(registration_message) - ), + ExecutionOutcome::System(RawExecutionOutcome::default()), ExecutionOutcome::User( application_id, RawExecutionOutcome::default() @@ -1009,8 +975,8 @@ async fn test_message_from_cross_application_call() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (target_id, target_application) = view.register_mock_application(1).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -1044,7 +1010,7 @@ async fn test_message_from_cross_application_call() -> anyhow::Result<()> { let context = create_dummy_operation_context(); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -1056,19 +1022,7 @@ async fn test_message_from_cross_application_call() -> anyhow::Result<()> { &mut controller, ) .await?; - view.update_execution_outcomes_with_app_registrations(&mut txn_tracker) - .await?; - let target_description = view.system.registry.describe_application(target_id).await?; - let registration_message = RawOutgoingMessage { - destination: Destination::from(destination_chain), - authenticated: false, - grant: Amount::ZERO, - kind: MessageKind::Simple, - message: SystemMessage::RegisterApplications { - applications: vec![target_description], - }, - }; let account = Account { chain_id: ChainId::root(0), owner: None, @@ -1078,9 +1032,7 @@ async fn test_message_from_cross_application_call() -> anyhow::Result<()> { assert_eq!( txn_outcome.outcomes, &[ - ExecutionOutcome::System( - RawExecutionOutcome::default().with_message(registration_message) - ), + ExecutionOutcome::System(RawExecutionOutcome::default()), ExecutionOutcome::User( target_id, RawExecutionOutcome::default() @@ -1112,9 +1064,9 @@ async fn test_message_from_deeper_call() -> anyhow::Result<()> { state.description = Some(ChainDescription::Root(0)); let mut view = state.into_view().await; - let (caller_id, caller_application) = view.register_mock_application().await?; - let (middle_id, middle_application) = view.register_mock_application().await?; - let (target_id, target_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; + let (middle_id, middle_application) = view.register_mock_application(1).await?; + let (target_id, target_application) = view.register_mock_application(2).await?; caller_application.expect_call(ExpectedCall::execute_operation( move |runtime, _context, _operation| { @@ -1156,7 +1108,7 @@ async fn test_message_from_deeper_call() -> anyhow::Result<()> { let context = create_dummy_operation_context(); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -1169,18 +1121,6 @@ async fn test_message_from_deeper_call() -> anyhow::Result<()> { ) .await?; - let target_description = view.system.registry.describe_application(target_id).await?; - let registration_message = RawOutgoingMessage { - destination: Destination::from(destination_chain), - authenticated: false, - grant: Amount::ZERO, - kind: MessageKind::Simple, - message: SystemMessage::RegisterApplications { - applications: vec![target_description], - }, - }; - view.update_execution_outcomes_with_app_registrations(&mut txn_tracker) - .await?; let account = Account { chain_id: ChainId::root(0), owner: None, @@ -1189,9 +1129,7 @@ async fn test_message_from_deeper_call() -> anyhow::Result<()> { assert_eq!( txn_outcome.outcomes, &[ - ExecutionOutcome::System( - RawExecutionOutcome::default().with_message(registration_message) - ), + ExecutionOutcome::System(RawExecutionOutcome::default()), ExecutionOutcome::User( target_id, RawExecutionOutcome::default() @@ -1236,11 +1174,11 @@ async fn test_multiple_messages_from_different_applications() -> anyhow::Result< let mut view = state.into_view().await; // The entrypoint application, which sends a message and calls other applications - let (caller_id, caller_application) = view.register_mock_application().await?; + let (caller_id, caller_application) = view.register_mock_application(0).await?; // An application that does not send any messages - let (silent_target_id, silent_target_application) = view.register_mock_application().await?; + let (silent_target_id, silent_target_application) = view.register_mock_application(1).await?; // An application that sends a message when handling a cross-application call - let (sending_target_id, sending_target_application) = view.register_mock_application().await?; + let (sending_target_id, sending_target_application) = view.register_mock_application(2).await?; // The first destination chain receives messages from the caller and the sending applications let first_destination_chain = ChainId::from(ChainDescription::Root(1)); @@ -1313,7 +1251,7 @@ async fn test_multiple_messages_from_different_applications() -> anyhow::Result< // Execute the operation, starting the test scenario let context = create_dummy_operation_context(); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -1325,39 +1263,6 @@ async fn test_multiple_messages_from_different_applications() -> anyhow::Result< &mut controller, ) .await?; - view.update_execution_outcomes_with_app_registrations(&mut txn_tracker) - .await?; - - // Describe the two applications that sent messages, and will therefore handle them in the - // other chains - let caller_description = view.system.registry.describe_application(caller_id).await?; - let sending_target_description = view - .system - .registry - .describe_application(sending_target_id) - .await?; - - // The registration message for the first destination chain - let first_registration_message = RawOutgoingMessage { - destination: Destination::from(first_destination_chain), - authenticated: false, - grant: Amount::ZERO, - kind: MessageKind::Simple, - message: SystemMessage::RegisterApplications { - applications: vec![sending_target_description.clone(), caller_description], - }, - }; - - // The registration message for the second destination chain - let second_registration_message = RawOutgoingMessage { - destination: Destination::from(second_destination_chain), - authenticated: false, - grant: Amount::ZERO, - kind: MessageKind::Simple, - message: SystemMessage::RegisterApplications { - applications: vec![sending_target_description], - }, - }; let account = Account { chain_id: ChainId::root(0), @@ -1369,11 +1274,7 @@ async fn test_multiple_messages_from_different_applications() -> anyhow::Result< assert_eq!( txn_outcome.outcomes, &[ - ExecutionOutcome::System( - RawExecutionOutcome::default() - .with_message(second_registration_message) - .with_message(first_registration_message) - ), + ExecutionOutcome::System(RawExecutionOutcome::default()), ExecutionOutcome::User( silent_target_id, RawExecutionOutcome::default().with_refund_grant_to(Some(account)), @@ -1424,7 +1325,7 @@ async fn test_open_chain() -> anyhow::Result<()> { ..SystemExecutionState::new(Epoch::ZERO, ChainDescription::Root(0), ChainId::root(0)) }; let mut view = state.into_view().await; - let (application_id, application) = view.register_mock_application().await?; + let (application_id, application) = view.register_mock_application(0).await?; let context = OperationContext { height: BlockHeight(1), @@ -1462,7 +1363,7 @@ async fn test_open_chain() -> anyhow::Result<()> { application_id, bytes: vec![], }; - let mut txn_tracker = TransactionTracker::new(first_message_index, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(first_message_index, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -1527,7 +1428,7 @@ async fn test_close_chain() -> anyhow::Result<()> { ..SystemExecutionState::new(Epoch::ZERO, ChainDescription::Root(0), ChainId::root(0)) }; let mut view = state.into_view().await; - let (application_id, application) = view.register_mock_application().await?; + let (application_id, application) = view.register_mock_application(0).await?; // The application is not authorized to close the chain. let context = create_dummy_operation_context(); @@ -1551,7 +1452,7 @@ async fn test_close_chain() -> anyhow::Result<()> { context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await?; @@ -1564,7 +1465,7 @@ async fn test_close_chain() -> anyhow::Result<()> { context, Timestamp::from(0), operation.into(), - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await?; @@ -1585,7 +1486,7 @@ async fn test_close_chain() -> anyhow::Result<()> { context, Timestamp::from(0), operation, - &mut TransactionTracker::new(0, Some(Vec::new())), + &mut TransactionTracker::new(0, 0, Some(Vec::new())), &mut controller, ) .await?; @@ -1649,7 +1550,7 @@ async fn test_message_receipt_spending_chain_balance( .into_view() .await; - let (application_id, application) = view.register_mock_application().await?; + let (application_id, application) = view.register_mock_application(0).await?; let receiver_chain_account = None; let sender_chain_id = ChainId::root(2); @@ -1668,7 +1569,7 @@ async fn test_message_receipt_spending_chain_balance( let context = create_dummy_message_context(authenticated_signer); let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); let execution_result = view .execute_message( diff --git a/linera-execution/tests/test_system_execution.rs b/linera-execution/tests/test_system_execution.rs index e7801f152591..22e706cc1c18 100644 --- a/linera-execution/tests/test_system_execution.rs +++ b/linera-execution/tests/test_system_execution.rs @@ -44,7 +44,7 @@ async fn test_simple_system_operation() -> anyhow::Result<()> { authenticated_caller_id: None, }; let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), @@ -96,7 +96,7 @@ async fn test_simple_system_message() -> anyhow::Result<()> { refund_grant_to: None, }; let mut controller = ResourceController::default(); - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_message( context, Timestamp::from(0), diff --git a/linera-execution/tests/wasm.rs b/linera-execution/tests/wasm.rs index b85b8a8383c7..322e7953d0b1 100644 --- a/linera-execution/tests/wasm.rs +++ b/linera-execution/tests/wasm.rs @@ -41,11 +41,7 @@ async fn test_fuel_for_counter_wasm_application( .into_view_with(ChainId::root(0), ExecutionRuntimeConfig::default()) .await; let (app_desc, contract_blob, service_blob) = create_dummy_user_application_description(1); - let app_id = view - .system - .registry - .register_application(app_desc.clone()) - .await?; + let app_id = From::from(&app_desc); let contract = WasmContractModule::from_file("tests/fixtures/counter_contract.wasm", wasm_runtime).await?; @@ -92,7 +88,7 @@ async fn test_fuel_for_counter_wasm_application( chain_id: ChainId::root(0), owner: None, }; - let mut txn_tracker = TransactionTracker::new(0, Some(Vec::new())); + let mut txn_tracker = TransactionTracker::new(0, 0, Some(Vec::new())); view.execute_operation( context, Timestamp::from(0), diff --git a/linera-sdk/src/test/block.rs b/linera-sdk/src/test/block.rs index a932a4ddac0d..541742e3595e 100644 --- a/linera-sdk/src/test/block.rs +++ b/linera-sdk/src/test/block.rs @@ -103,17 +103,6 @@ impl BlockBuilder { self } - /// Adds a request to register an application on this chain. - pub fn with_request_for_application( - &mut self, - application: ApplicationId, - ) -> &mut Self { - self.with_system_operation(SystemOperation::RequestApplication { - chain_id: application.creation.chain_id, - application_id: application.forget_abi(), - }) - } - /// Adds an operation to change this chain's ownership. pub fn with_owner_change( &mut self, diff --git a/linera-sdk/src/test/chain.rs b/linera-sdk/src/test/chain.rs index 7a81ac03f085..f676b44d408b 100644 --- a/linera-sdk/src/test/chain.rs +++ b/linera-sdk/src/test/chain.rs @@ -14,15 +14,12 @@ use std::{ use cargo_toml::Manifest; use linera_base::{ crypto::{AccountPublicKey, AccountSecretKey}, - data_types::{Blob, BlockHeight, Bytecode, CompressedBytecode}, - identifiers::{ApplicationId, BytecodeId, ChainDescription, ChainId, MessageId}, + data_types::{Blob, BlockHeight, Bytecode, CompressedBytecode, UserApplicationDescription}, + identifiers::{ApplicationId, BytecodeId, ChainDescription, ChainId}, }; -use linera_chain::{types::ConfirmedBlockCertificate, ChainError, ChainExecutionContext}; +use linera_chain::types::ConfirmedBlockCertificate; use linera_core::{data_types::ChainInfoQuery, worker::WorkerError}; -use linera_execution::{ - system::{SystemExecutionError, SystemOperation, CREATE_APPLICATION_MESSAGE_INDEX}, - ExecutionError, Query, QueryOutcome, QueryResponse, -}; +use linera_execution::{system::SystemOperation, Query, QueryOutcome, QueryResponse}; use linera_storage::Storage as _; use serde::Serialize; use tokio::{fs, sync::Mutex}; @@ -365,33 +362,30 @@ impl ActiveChain { let parameters = serde_json::to_vec(¶meters).unwrap(); let instantiation_argument = serde_json::to_vec(&instantiation_argument).unwrap(); - for &dependency in &required_application_ids { - self.register_application(dependency).await; - } - let creation_certificate = self .add_block(|block| { block.with_system_operation(SystemOperation::CreateApplication { bytecode_id: bytecode_id.forget_abi(), - parameters, + parameters: parameters.clone(), instantiation_argument, - required_application_ids, + required_application_ids: required_application_ids.clone(), }); }) .await; let block = creation_certificate.inner().block(); assert_eq!(block.messages().len(), 1); - let creation = MessageId { - chain_id: block.header.chain_id, - height: block.header.height, - index: CREATE_APPLICATION_MESSAGE_INDEX, + + let description = UserApplicationDescription { + bytecode_id: bytecode_id.forget_abi(), + creator_chain_id: block.header.chain_id, + block_height: block.header.height, + application_index: 0, + parameters, + required_application_ids, }; - ApplicationId { - bytecode_id: bytecode_id.just_abi(), - creation, - } + ApplicationId::<()>::from(&description).with_abi() } /// Returns whether this chain has been closed. @@ -404,61 +398,6 @@ impl ActiveChain { .is_closed() } - /// Registers on this chain an application created on another chain. - pub async fn register_application(&self, application_id: ApplicationId) { - if self.needs_application_description(application_id).await { - let source_chain = self.validator.get_chain(&application_id.creation.chain_id); - - let request_certificate = self - .add_block(|block| { - block.with_request_for_application(application_id); - }) - .await; - - let register_certificate = source_chain - .add_block(|block| { - block.with_messages_from(&request_certificate); - }) - .await; - - let final_certificate = self - .add_block(|block| { - block.with_messages_from(®ister_certificate); - }) - .await; - - assert_eq!(final_certificate.outgoing_message_count(), 0); - } - } - - /// Checks if the `application_id` is missing from this microchain. - async fn needs_application_description(&self, application_id: ApplicationId) -> bool { - let description_result = self - .validator - .worker() - .describe_application(self.id(), application_id.forget_abi()) - .await; - - match description_result { - Ok(_) => false, - Err(WorkerError::ChainError(boxed_chain_error)) - if matches!( - &*boxed_chain_error, - ChainError::ExecutionError( - execution_error, - ChainExecutionContext::DescribeApplication, - ) if matches!( - **execution_error, - ExecutionError::SystemError(SystemExecutionError::UnknownApplicationId(_)) - ) - ) => - { - true - } - Err(_) => panic!("Failed to check known bytecode locations"), - } - } - /// Executes a `query` on an `application`'s state on this microchain. /// /// Returns the deserialized response from the `application`.