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

feat: Separate TransactionRequest and TransactionRequestBuilder #605

Merged
merged 17 commits into from
Dec 2, 2024
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
### Features

* [BREAKING] Added `AccountRecord` with information about the account's status (#600).
* [BREAKING] Added `TransactionRequestBuilder` for building `TransactionRequest` (#605).
* Added caching for foreign account code (#597).
* Added support for unauthenticated notes consumption in the CLI (#609).

### Fixes

* Added Sync Loop to Integration Tests for Small Speedup (#590).

### Changes
Expand Down
37 changes: 21 additions & 16 deletions bin/miden-cli/src/commands/new_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use miden_client::{
crypto::{Digest, FeltRng},
notes::{build_swap_tag, get_input_note_with_id_prefix, NoteType as MidenNoteType},
transactions::{
PaymentTransactionData, SwapTransactionData, TransactionRequest, TransactionResult,
PaymentTransactionData, SwapTransactionData, TransactionRequest, TransactionRequestBuilder,
TransactionResult,
},
Client,
};
Expand Down Expand Up @@ -68,13 +69,14 @@ impl MintCmd {

let target_account_id = parse_account_id(&client, self.target_account_id.as_str()).await?;

let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
fungible_asset,
target_account_id,
(&self.note_type).into(),
client.rng(),
)
.map_err(|err| err.to_string())?;
.map_err(|err| err.to_string())?
.build();

execute_transaction(
&mut client,
Expand Down Expand Up @@ -138,13 +140,14 @@ impl SendCmd {
target_account_id,
);

let transaction_request = TransactionRequest::pay_to_id(
let transaction_request = TransactionRequestBuilder::pay_to_id(
payment_transaction,
self.recall_height,
(&self.note_type).into(),
client.rng(),
)
.map_err(|err| err.to_string())?;
.map_err(|err| err.to_string())?
.build();

execute_transaction(
&mut client,
Expand Down Expand Up @@ -205,12 +208,13 @@ impl SwapCmd {
requested_fungible_asset.into(),
);

let transaction_request = TransactionRequest::swap(
let transaction_request = TransactionRequestBuilder::swap(
swap_transaction.clone(),
(&self.note_type).into(),
client.rng(),
)
.map_err(|err| err.to_string())?;
.map_err(|err| err.to_string())?
.build();

execute_transaction(
&mut client,
Expand Down Expand Up @@ -261,37 +265,38 @@ impl ConsumeNotesCmd {
pub async fn execute(&self, mut client: Client<impl FeltRng>) -> Result<(), String> {
let force = self.force;

let mut list_of_authenticated_notes = Vec::new();
let mut list_of_unauthenticated_notes = Vec::new();
let mut authenticated_notes = Vec::new();
let mut unauthenticated_notes = Vec::new();

for note_id in &self.list_of_notes {
let note_record = get_input_note_with_id_prefix(&client, note_id)
.await
.map_err(|err| err.to_string())?;

if note_record.is_authenticated() {
list_of_authenticated_notes.push(note_record.id());
authenticated_notes.push(note_record.id());
} else {
list_of_unauthenticated_notes.push((note_record.try_into()?, None));
unauthenticated_notes.push((note_record.try_into()?, None));
}
}

let account_id =
get_input_acc_id_by_prefix_or_default(&client, self.account_id.clone()).await?;

if list_of_authenticated_notes.is_empty() {
if authenticated_notes.is_empty() {
info!("No input note IDs provided, getting all notes consumable by {}", account_id);
let consumable_notes = client.get_consumable_notes(Some(account_id)).await?;

list_of_authenticated_notes.extend(consumable_notes.iter().map(|(note, _)| note.id()));
authenticated_notes.extend(consumable_notes.iter().map(|(note, _)| note.id()));
}

if list_of_authenticated_notes.is_empty() && list_of_unauthenticated_notes.is_empty() {
if authenticated_notes.is_empty() && unauthenticated_notes.is_empty() {
return Err(format!("No input notes were provided and the store does not contain any notes consumable by {account_id}"));
}

let transaction_request = TransactionRequest::consume_notes(list_of_authenticated_notes)
.with_unauthenticated_input_notes(list_of_unauthenticated_notes);
let transaction_request = TransactionRequestBuilder::consume_notes(authenticated_notes)
.with_unauthenticated_input_notes(unauthenticated_notes)
.build();

execute_transaction(
&mut client,
Expand Down
30 changes: 18 additions & 12 deletions crates/rust-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
mock::create_test_client,
rpc::NodeRpcClient,
store::{InputNoteRecord, NoteFilter, Store, StoreError},
transactions::TransactionRequest,
transactions::TransactionRequestBuilder,
ClientError,
};

Expand Down Expand Up @@ -403,13 +403,14 @@ async fn test_mint_transaction() {
client.sync_state().await.unwrap();

// Test submitting a mint transaction
let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
AccountId::from_hex("0x168187d729b31a84").unwrap(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

let transaction = client.new_transaction(faucet.id(), transaction_request).await.unwrap();

Expand All @@ -434,13 +435,14 @@ async fn test_get_output_notes() {
.unwrap();

// Test submitting a mint transaction
let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
AccountId::from_hex("0x0123456789abcdef").unwrap(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

//Before executing transaction, there are no output notes
assert!(client.get_output_notes(NoteFilter::All).await.unwrap().is_empty());
Expand Down Expand Up @@ -500,15 +502,16 @@ async fn test_transaction_request_expiration() {
.await
.unwrap();

let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
AccountId::from_hex("0x168187d729b31a84").unwrap(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap()
.with_expiration_delta(5)
.unwrap();
.unwrap()
.build();

let transaction = client.new_transaction(faucet.id(), transaction_request).await.unwrap();

Expand Down Expand Up @@ -543,13 +546,14 @@ async fn test_import_processing_note_returns_error() {
.unwrap();

// Test submitting a mint transaction
let transaction_request = TransactionRequest::mint_fungible_asset(
let transaction_request = TransactionRequestBuilder::mint_fungible_asset(
FungibleAsset::new(faucet.id(), 5u64).unwrap(),
account.id(),
miden_objects::notes::NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

let transaction =
client.new_transaction(faucet.id(), transaction_request.clone()).await.unwrap();
Expand All @@ -558,8 +562,9 @@ async fn test_import_processing_note_returns_error() {
let note_id = transaction_request.expected_output_notes().next().unwrap().id();
let note = client.get_input_note(note_id).await.unwrap();

let input = [(note.try_into().unwrap(), None)].into_iter();
let consume_note_request = TransactionRequest::new().with_unauthenticated_input_notes(input);
let input = [(note.try_into().unwrap(), None)];
let consume_note_request =
TransactionRequestBuilder::new().with_unauthenticated_input_notes(input).build();
let transaction = client
.new_transaction(account.id(), consume_note_request.clone())
.await
Expand Down Expand Up @@ -605,7 +610,8 @@ async fn test_no_nonce_change_transaction_request() {

let tx_script = client.compile_tx_script(vec![], code).unwrap();

let transaction_request = TransactionRequest::new().with_custom_script(tx_script).unwrap();
let transaction_request =
TransactionRequestBuilder::new().with_custom_script(tx_script).unwrap().build();

let transaction_execution_result =
client.new_transaction(regular_account.id(), transaction_request).await.unwrap();
Expand Down
11 changes: 6 additions & 5 deletions crates/rust-client/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
mod request;
pub use request::{
NoteArgs, PaymentTransactionData, SwapTransactionData, TransactionRequest,
TransactionRequestError, TransactionScriptTemplate,
TransactionRequestBuilder, TransactionRequestError, TransactionScriptTemplate,
};

mod script_builder;
Expand Down Expand Up @@ -922,8 +922,8 @@ mod test {
Felt, FieldElement, Word,
};

use super::{PaymentTransactionData, TransactionRequest};
use crate::mock::create_test_client;
use super::PaymentTransactionData;
use crate::{mock::create_test_client, transactions::TransactionRequestBuilder};

#[tokio::test]
async fn test_transaction_creates_two_notes() {
Expand Down Expand Up @@ -965,7 +965,7 @@ mod test {
.await
.unwrap();
client.sync_state().await.unwrap();
let tx_request = TransactionRequest::pay_to_id(
let tx_request = TransactionRequestBuilder::pay_to_id(
PaymentTransactionData::new(
vec![asset_1, asset_2],
account.id(),
Expand All @@ -975,7 +975,8 @@ mod test {
NoteType::Private,
client.rng(),
)
.unwrap();
.unwrap()
.build();

let tx_result = client.new_transaction(account.id(), tx_request).await.unwrap();
assert!(tx_result
Expand Down
Loading