Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the DataStore conditionally async #720

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
341 changes: 304 additions & 37 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ doc-serve: ## Serves documentation site

.PHONY: test-default
test-default: ## Run default tests excluding `prove`
$(DEBUG_ASSERTIONS) cargo nextest run --profile default --cargo-profile test-release --features concurrent,testing --filter-expr "not test(prove)"
$(DEBUG_ASSERTIONS) cargo nextest run --profile default --cargo-profile test-release --features concurrent,testing,sync --filter-expr "not test(prove)"


.PHONY: test-prove
test-prove: ## Run `prove` tests (tests which use the Miden prover)
$(DEBUG_ASSERTIONS) cargo nextest run --profile prove --cargo-profile test-release --features concurrent,testing --filter-expr "test(prove)"
$(DEBUG_ASSERTIONS) cargo nextest run --profile prove --cargo-profile test-release --features concurrent,testing,sync --filter-expr "test(prove)"


.PHONY: test
Expand Down
12 changes: 10 additions & 2 deletions bench-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ exclude.workspace = true
name = "bench-tx"
path = "src/main.rs"

[features]
sync = ["maybe-async/is_sync", "miden-tx/sync"]

[dependencies]
async-trait = "0.1.80"
maybe-async = "0.2.10"
miden-lib = { path = "../miden-lib", version = "0.4" }
miden-objects = { path = "../objects", version = "0.4" }
miden-tx = { path = "../miden-tx", version = "0.4" }
mock = { package = "miden-mock", path = "../mock" }
mock = { package = "miden-mock", path = "../mock" }
rand = { workspace = true }
serde = { package = "serde", version = "1.0" }
serde_json = { package = "serde_json", version = "1.0", features = ["preserve_order"] }
serde_json = { package = "serde_json", version = "1.0", features = [
"preserve_order",
] }
tokio = { version = "1", features = ["full"] }
vm-processor = { workspace = true }
34 changes: 30 additions & 4 deletions bench-tx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{
rc::Rc,
};

use maybe_async::{async_impl, maybe_async, sync_impl};
use miden_lib::{
notes::create_p2id_note, transaction::ToTransactionKernelInputs, utils::Serializable,
};
Expand Down Expand Up @@ -44,6 +45,27 @@ impl fmt::Display for Benchmark {
}
}

#[tokio::main]
#[async_impl]
async fn main() -> Result<(), String> {
// create a template file for benchmark results
let path = Path::new("bench-tx/bench-tx.json");
let mut file = File::create(path).map_err(|e| e.to_string())?;
file.write_all(b"{}").map_err(|e| e.to_string())?;

// run all available benchmarks
let benchmark_results = vec![
(Benchmark::Simple, benchmark_default_tx().await?),
(Benchmark::P2ID, benchmark_p2id().await?),
];

// store benchmark results in the JSON file
write_bench_results_to_json(path, benchmark_results)?;

Ok(())
}

#[sync_impl]
fn main() -> Result<(), String> {
// create a template file for benchmark results
let path = Path::new("bench-tx/bench-tx.json");
Expand All @@ -66,19 +88,21 @@ fn main() -> Result<(), String> {
// ================================================================================================

/// Runs the default transaction with empty transaction script and two default notes.
pub fn benchmark_default_tx() -> Result<TransactionProgress, String> {
#[maybe_async]
pub async fn benchmark_default_tx() -> Result<TransactionProgress, String> {
let data_store = MockDataStore::default();
let mut executor: TransactionExecutor<_, ()> =
TransactionExecutor::new(data_store.clone(), None).with_tracing();

let account_id = data_store.account.id();
executor.load_account(account_id).map_err(|e| e.to_string())?;
executor.load_account(account_id).await.map_err(|e| e.to_string())?;

let block_ref = data_store.block_header.block_num();
let note_ids = data_store.notes.iter().map(|note| note.id()).collect::<Vec<_>>();

let transaction = executor
.prepare_transaction(account_id, block_ref, &note_ids, data_store.tx_args().clone())
.await
.map_err(|e| e.to_string())?;

let (stack_inputs, advice_inputs) = transaction.get_kernel_inputs();
Expand All @@ -98,7 +122,8 @@ pub fn benchmark_default_tx() -> Result<TransactionProgress, String> {
}

/// Runs the transaction which consumes a P2ID note into a basic wallet.
pub fn benchmark_p2id() -> Result<TransactionProgress, String> {
#[maybe_async]
pub async fn benchmark_p2id() -> Result<TransactionProgress, String> {
// Create assets
let faucet_id = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN).unwrap();
let fungible_asset: Asset = FungibleAsset::new(faucet_id, 100).unwrap().into();
Expand Down Expand Up @@ -132,7 +157,7 @@ pub fn benchmark_p2id() -> Result<TransactionProgress, String> {

let mut executor: TransactionExecutor<_, ()> =
TransactionExecutor::new(data_store.clone(), None).with_tracing();
executor.load_account(target_account_id).unwrap();
executor.load_account(target_account_id).await.unwrap();

let block_ref = data_store.block_header.block_num();
let note_ids = data_store.notes.iter().map(|note| note.id()).collect::<Vec<_>>();
Expand All @@ -151,6 +176,7 @@ pub fn benchmark_p2id() -> Result<TransactionProgress, String> {
// execute transaction
let transaction = executor
.prepare_transaction(target_account_id, block_ref, &note_ids, tx_args_target)
.await
.map_err(|e| e.to_string())?;

let (stack_inputs, advice_inputs) = transaction.get_kernel_inputs();
Expand Down
6 changes: 4 additions & 2 deletions bench-tx/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate alloc;
pub use alloc::collections::BTreeMap;

use maybe_async::maybe_async;
use miden_lib::transaction::TransactionKernel;
use miden_objects::{
accounts::{Account, AccountCode, AccountId, AccountStorage, SlotItem, StorageSlot},
Expand Down Expand Up @@ -114,8 +115,9 @@ impl Default for MockDataStore {
}
}

#[maybe_async]
impl DataStore for MockDataStore {
fn get_transaction_inputs(
async fn get_transaction_inputs(
&self,
account_id: AccountId,
block_num: u32,
Expand All @@ -142,7 +144,7 @@ impl DataStore for MockDataStore {
.unwrap())
}

fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError> {
async fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError> {
assert_eq!(account_id, self.account.id());
Ok(self.account.code().module().clone())
}
Expand Down
18 changes: 16 additions & 2 deletions miden-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,25 @@ name = "miden-tx"
path = "tests/integration/main.rs"

[features]
concurrent = ["miden-lib/concurrent", "miden-objects/concurrent", "miden-prover/concurrent", "std"]
concurrent = [
"miden-lib/concurrent",
"miden-objects/concurrent",
"miden-prover/concurrent",
"std",
]
default = ["std"]
std = ["miden-lib/std", "miden-objects/std", "miden-prover/std", "miden-verifier/std", "vm-processor/std"]
std = [
"miden-lib/std",
"miden-objects/std",
"miden-prover/std",
"miden-verifier/std",
"vm-processor/std",
]
sync = ["maybe-async/is_sync"]

[dependencies]
async-trait = "0.1.80"
maybe-async = "0.2.10"
miden-lib = { path = "../miden-lib", version = "0.4", default-features = false }
miden-objects = { path = "../objects", version = "0.4", default-features = false }
miden-prover = { workspace = true }
Expand Down
9 changes: 7 additions & 2 deletions miden-tx/src/executor/data_store.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(not(feature = "sync"))]
use alloc::boxed::Box;

use maybe_async::maybe_async;
use miden_objects::{
accounts::AccountId, assembly::ModuleAst, notes::NoteId, transaction::TransactionInputs,
};
Expand All @@ -9,6 +13,7 @@ use crate::DataStoreError;

/// The [DataStore] trait defines the interface that transaction objects use to fetch data
/// required for transaction execution.
#[maybe_async]
pub trait DataStore {
/// Returns account, chain, and input note data required to execute a transaction against
/// the account with the specified ID and consuming the set of specified input notes.
Expand All @@ -25,13 +30,13 @@ pub trait DataStore {
/// - Any of the notes with the specified IDs were already consumed.
/// - The combination of specified inputs resulted in a transaction input error.
/// - The data store encountered some internal error
fn get_transaction_inputs(
async fn get_transaction_inputs(
&self,
account_id: AccountId,
block_ref: u32,
notes: &[NoteId],
) -> Result<TransactionInputs, DataStoreError>;

/// Returns the account code [ModuleAst] associated with the specified [AccountId].
fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError>;
async fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError>;
}
12 changes: 8 additions & 4 deletions miden-tx/src/executor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::{rc::Rc, vec::Vec};

use maybe_async::maybe_async;
use miden_lib::transaction::{ToTransactionKernelInputs, TransactionKernel};
use miden_objects::{
assembly::ProgramAst,
Expand Down Expand Up @@ -42,6 +43,7 @@ pub struct TransactionExecutor<D, A> {
exec_options: ExecutionOptions,
}

#[maybe_async]
impl<D: DataStore, A: TransactionAuthenticator> TransactionExecutor<D, A> {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -102,13 +104,14 @@ impl<D: DataStore, A: TransactionAuthenticator> TransactionExecutor<D, A> {
/// Returns an error if:
/// - If the account code cannot be fetched from the [DataStore].
/// - If the account code fails to be loaded into the compiler.
pub fn load_account(
pub async fn load_account(
&mut self,
account_id: AccountId,
) -> Result<AccountCode, TransactionExecutorError> {
let account_code = self
.data_store
.get_account_code(account_id)
.await
.map_err(TransactionExecutorError::FetchAccountCodeFailed)?;
self.compiler
.load_account(account_id, account_code)
Expand Down Expand Up @@ -174,14 +177,14 @@ impl<D: DataStore, A: TransactionAuthenticator> TransactionExecutor<D, A> {
/// - If required data can not be fetched from the [DataStore].
/// - If the transaction program can not be compiled.
/// - If the transaction program can not be executed.
pub fn execute_transaction(
pub async fn execute_transaction(
&self,
account_id: AccountId,
block_ref: u32,
notes: &[NoteId],
tx_args: TransactionArgs,
) -> Result<ExecutedTransaction, TransactionExecutorError> {
let transaction = self.prepare_transaction(account_id, block_ref, notes, tx_args)?;
let transaction = self.prepare_transaction(account_id, block_ref, notes, tx_args).await?;

let (stack_inputs, advice_inputs) = transaction.get_kernel_inputs();
let advice_recorder: RecAdviceProvider = advice_inputs.into();
Expand Down Expand Up @@ -221,7 +224,7 @@ impl<D: DataStore, A: TransactionAuthenticator> TransactionExecutor<D, A> {
/// Returns an error if:
/// - If required data can not be fetched from the [DataStore].
/// - If the transaction can not be compiled.
pub fn prepare_transaction(
pub async fn prepare_transaction(
&self,
account_id: AccountId,
block_ref: u32,
Expand All @@ -231,6 +234,7 @@ impl<D: DataStore, A: TransactionAuthenticator> TransactionExecutor<D, A> {
let tx_inputs = self
.data_store
.get_transaction_inputs(account_id, block_ref, notes)
.await
.map_err(TransactionExecutorError::FetchTransactionInputsFailed)?;

let tx_program = self
Expand Down
18 changes: 11 additions & 7 deletions miden-tx/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#[cfg(not(feature = "sync"))]
use alloc::boxed::Box;
use alloc::vec::Vec;

use maybe_async::maybe_async;
use miden_lib::transaction::{ToTransactionKernelInputs, TransactionKernel};
use miden_objects::{
accounts::{
Expand Down Expand Up @@ -50,7 +53,7 @@ use super::{
// TESTS
// ================================================================================================

#[test]
#[maybe_async::test(feature = "sync")]
fn transaction_executor_witness() {
let data_store = MockDataStore::default();
let mut executor: TransactionExecutor<_, ()> =
Expand Down Expand Up @@ -90,7 +93,7 @@ fn transaction_executor_witness() {
assert_eq!(executed_transaction.output_notes(), &tx_outputs.output_notes);
}

#[test]
#[maybe_async::test(feature = "sync")]
fn executed_transaction_account_delta() {
let data_store = MockDataStore::new(AssetPreservationStatus::PreservedWithAccountVaultDelta);
let mut executor: TransactionExecutor<_, ()> =
Expand Down Expand Up @@ -347,7 +350,7 @@ fn executed_transaction_account_delta() {
);
}

#[test]
#[maybe_async::test(feature = "sync")]
fn executed_transaction_output_notes() {
let data_store = MockDataStore::new(AssetPreservationStatus::PreservedWithAccountVaultDelta);
let mut executor: TransactionExecutor<_, ()> =
Expand Down Expand Up @@ -536,7 +539,7 @@ fn executed_transaction_output_notes() {
assert_eq!(NoteHeader::from(created_note), NoteHeader::new(note_id, *note_metadata));
}

#[test]
#[maybe_async::test(feature = "sync")]
fn prove_witness_and_verify() {
let data_store = MockDataStore::default();
let mut executor: TransactionExecutor<_, ()> =
Expand Down Expand Up @@ -569,7 +572,7 @@ fn prove_witness_and_verify() {
// TEST TRANSACTION SCRIPT
// ================================================================================================

#[test]
#[maybe_async::test(feature = "sync")]
fn test_tx_script() {
let data_store = MockDataStore::default();
let mut executor: TransactionExecutor<_, ()> =
Expand Down Expand Up @@ -658,8 +661,9 @@ impl Default for MockDataStore {
}
}

#[maybe_async]
impl DataStore for MockDataStore {
fn get_transaction_inputs(
async fn get_transaction_inputs(
&self,
account_id: AccountId,
block_num: u32,
Expand All @@ -686,7 +690,7 @@ impl DataStore for MockDataStore {
.unwrap())
}

fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError> {
async fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError> {
assert_eq!(account_id, self.account.id());
Ok(self.account.code().module().clone())
}
Expand Down
6 changes: 4 additions & 2 deletions miden-tx/tests/integration/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod scripts;
mod wallet;

use maybe_async::maybe_async;
use miden_lib::transaction::TransactionKernel;
use miden_objects::{
accounts::{
Expand Down Expand Up @@ -98,8 +99,9 @@ impl Default for MockDataStore {
}
}

#[maybe_async]
impl DataStore for MockDataStore {
fn get_transaction_inputs(
async fn get_transaction_inputs(
&self,
account_id: AccountId,
block_num: u32,
Expand All @@ -126,7 +128,7 @@ impl DataStore for MockDataStore {
.unwrap())
}

fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError> {
async fn get_account_code(&self, account_id: AccountId) -> Result<ModuleAst, DataStoreError> {
assert_eq!(account_id, self.account.id());
Ok(self.account.code().module().clone())
}
Expand Down
Loading
Loading