Skip to content

Commit

Permalink
Fix (some) unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bart-linera committed Feb 21, 2025
1 parent 018529b commit a910f3f
Show file tree
Hide file tree
Showing 19 changed files with 223 additions and 431 deletions.
6 changes: 1 addition & 5 deletions linera-base/src/unit_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand Down
2 changes: 1 addition & 1 deletion linera-chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ static STATE_HASH_COMPUTATION_LATENCY: LazyLock<HistogramVec> = 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)]
Expand Down
23 changes: 12 additions & 11 deletions linera-chain/src/unit_tests/chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
},
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand All @@ -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()),
Expand All @@ -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,
};
Expand Down
3 changes: 3 additions & 0 deletions linera-chain/src/unit_tests/data_types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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()));
Expand All @@ -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));
Expand Down
48 changes: 16 additions & 32 deletions linera-core/src/unit_tests/wasm_worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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![]],
}
Expand Down Expand Up @@ -193,55 +189,42 @@ 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,
)
.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),
));
Expand Down Expand Up @@ -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?;
Expand All @@ -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()],
}
Expand Down
21 changes: 18 additions & 3 deletions linera-core/src/unit_tests/worker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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()],
}
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -2747,6 +2757,7 @@ where
},
)]],
events: vec![Vec::new()],
blobs: vec![Vec::new()],
state_hash: SystemExecutionState {
committees: committees2.clone(),
ownership: ChainOwnership::single(owner0),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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?;
}

Expand Down Expand Up @@ -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?;
}

Expand Down Expand Up @@ -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![],
}
Expand Down
7 changes: 2 additions & 5 deletions linera-execution/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand All @@ -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,
Expand Down
Loading

0 comments on commit a910f3f

Please sign in to comment.