-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update resource counters with write operations (#1541)
* Simplify `Batch` size calculation Use a more functional paradigm. * Count the number of bytes written per block Update the `RuntimeCounts` on every write call from applications. * Add a `Batch::num_operations` method Returns the number of write operations contained in the batch. * Keep track of the number of writes per block Update the `RuntimeCounts` on every write request from an application. * Add write operation count to `RuntimeCounts` Prepare to keep track of the number of write operations executed. * Derive `Debug` for `Batch` Make it simple to print out the batch operations. * Impl. `Debug` for `execution_state_actor::Request` Make it easy to print unexpected requests in tests. * Derive `Clone` and `Eq` for `Batch` Make it more ergonomic to use `Batch` in tests. * Test if resources are updated by `write_batch` Request to write a dummy batch and check that the resource consumption counters are updated appropriately.
- Loading branch information
Showing
5 changed files
with
220 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright (c) Zefchain Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//! Resource consumption unit tests. | ||
use super::{ApplicationStatus, SyncRuntimeInternal}; | ||
use crate::{ | ||
execution_state_actor::Request, resources::RuntimeLimits, BaseRuntime, UserContractInstance, | ||
}; | ||
use futures::{channel::mpsc, StreamExt}; | ||
use linera_base::{ | ||
data_types::BlockHeight, | ||
identifiers::{ApplicationId, BytecodeId, ChainDescription, MessageId}, | ||
}; | ||
use linera_views::batch::Batch; | ||
use std::thread; | ||
use tokio::runtime::Runtime; | ||
|
||
/// Test writing a batch of changes. | ||
/// | ||
/// Ensure that resource consumption counts are updated correctly. | ||
#[test] | ||
fn test_write_batch() { | ||
let (mut runtime, mut execution_state_receiver) = create_contract_runtime(); | ||
let mut batch = Batch::new(); | ||
|
||
let write_key = vec![1, 2, 3, 4, 5]; | ||
let write_data = vec![6, 7, 8, 9]; | ||
let delete_key = vec![10, 11, 12]; | ||
let delete_key_prefix = vec![13, 14, 15, 16, 17, 18]; | ||
|
||
let expected_bytes_count = | ||
write_key.len() + write_data.len() + delete_key.len() + delete_key_prefix.len(); | ||
|
||
batch.put_key_value_bytes(write_key, write_data); | ||
batch.delete_key(delete_key); | ||
batch.delete_key_prefix(delete_key_prefix); | ||
|
||
let expected_write_count = batch.operations.len(); | ||
let expected_application_id = runtime.current_application().id; | ||
let expected_batch = batch.clone(); | ||
|
||
thread::spawn(move || { | ||
Runtime::new() | ||
.expect("Failed to create Tokio runtime") | ||
.block_on(async move { | ||
let request = execution_state_receiver | ||
.next() | ||
.await | ||
.expect("Missing expected request to write a batch"); | ||
|
||
let Request::WriteBatch { | ||
id, | ||
batch, | ||
callback, | ||
} = request | ||
else { | ||
panic!("Expected a `Request::WriteBatch` but got {request:?} instead"); | ||
}; | ||
|
||
assert_eq!(id, expected_application_id); | ||
assert_eq!(batch, expected_batch); | ||
|
||
callback | ||
.send(()) | ||
.expect("Failed to notify that writing the batch finished"); | ||
}) | ||
}); | ||
|
||
runtime | ||
.write_batch(batch) | ||
.expect("Failed to write test batch"); | ||
|
||
assert_eq!( | ||
runtime.runtime_counts.num_writes, | ||
expected_write_count as u64 | ||
); | ||
assert_eq!( | ||
runtime.runtime_counts.bytes_written, | ||
expected_bytes_count as u64 | ||
); | ||
} | ||
|
||
/// Creates a [`SyncRuntimeInternal`] instance for contracts, and returns it and the receiver | ||
/// endpoint for the requests the runtime sends to the [`ExecutionStateView`] actor. | ||
fn create_contract_runtime() -> ( | ||
SyncRuntimeInternal<UserContractInstance>, | ||
mpsc::UnboundedReceiver<Request>, | ||
) { | ||
let chain_id = ChainDescription::Root(0).into(); | ||
let (execution_state_sender, execution_state_receiver) = mpsc::unbounded(); | ||
let limits = RuntimeLimits::default(); | ||
|
||
let mut runtime = SyncRuntimeInternal::new(chain_id, execution_state_sender, limits, 0); | ||
|
||
runtime.push_application(create_dummy_application()); | ||
|
||
(runtime, execution_state_receiver) | ||
} | ||
|
||
/// Create an [`ApplicationStatus`] for a dummy application. | ||
fn create_dummy_application() -> ApplicationStatus { | ||
let chain_id = ChainDescription::Root(1).into(); | ||
let id = ApplicationId { | ||
bytecode_id: BytecodeId::new(MessageId { | ||
chain_id, | ||
height: BlockHeight(1), | ||
index: 0, | ||
}), | ||
creation: MessageId { | ||
chain_id, | ||
height: BlockHeight(1), | ||
index: 1, | ||
}, | ||
}; | ||
|
||
ApplicationStatus { | ||
id, | ||
parameters: vec![], | ||
signer: None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters