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: make TransactionResult serializable #704

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* [BREAKING] Renamed APIs for retrieving account information to use the `try_get_*` naming convention, and added/improved module documentation (#683).
* Enabled TLS on tonic client (#697).
* Added account creation from component templates (#680).
* Added serialization for `TransactionResult` (#704).

### Fixes

Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ repository = "https://github.com/0xPolygonMiden/miden-client"
[workspace.dependencies]
async-trait = "0.1"
miden-lib = { version = "0.7", default-features = false }
miden-objects = { version = "0.7", default-features = false }
miden-tx = { version = "0.7", default-features = false, features = ["async"] }
miden-objects = { version = "0.7.2", default-features = false }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can drop the patch version from this and just cargo update miden-base dependencies

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also nit (for miden-tx as well), the extra space can be dropped as well:

Suggested change
miden-objects = { version = "0.7.2", default-features = false }
miden-objects = { version = "0.7", default-features = false }

miden-tx = { version = "0.7", default-features = false, features = ["async"] }
miden-proving-service-client = { version = "0.7", default-features = false, features = ["tx-prover"] }
miette = { version = "7.2", features = ["fancy"] }
rand = { version = "0.8" }
Expand Down
35 changes: 32 additions & 3 deletions crates/rust-client/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ use miden_objects::{
transaction::{InputNotes, TransactionArgs},
AssetError, Digest, Felt, Word, ZERO,
};
use miden_tx::TransactionExecutor;
use miden_tx::{
utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
TransactionExecutor,
};
pub use miden_tx::{
LocalTransactionProver, ProvingOptions, TransactionProver, TransactionProverError,
};
Expand Down Expand Up @@ -124,7 +127,7 @@ pub use script_builder::TransactionScriptBuilderError;
/// `output_notes` that the client has to store as input notes, based on the NoteScreener
/// output from filtering the transaction's output notes or some partial note we expect to receive
/// in the future (you can check at swap notes for an example of this).
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub struct TransactionResult {
transaction: ExecutedTransaction,
relevant_notes: Vec<InputNoteRecord>,
Expand Down Expand Up @@ -221,6 +224,22 @@ impl From<TransactionResult> for ExecutedTransaction {
}
}

impl Serializable for TransactionResult {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.transaction.write_into(target);
self.relevant_notes.write_into(target);
}
}

impl Deserializable for TransactionResult {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let transaction = ExecutedTransaction::read_from(source)?;
let relevant_notes = Vec::<InputNoteRecord>::read_from(source)?;

Ok(Self { transaction, relevant_notes })
}
}

// TRANSACTION RECORD
// --------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -1014,9 +1033,13 @@ mod test {
},
Word,
};
use miden_tx::utils::{Deserializable, Serializable};

use super::PaymentTransactionData;
use crate::{mock::create_test_client, transaction::TransactionRequestBuilder};
use crate::{
mock::create_test_client,
transaction::{TransactionRequestBuilder, TransactionResult},
};

#[tokio::test]
async fn test_transaction_creates_two_notes() {
Expand Down Expand Up @@ -1081,5 +1104,11 @@ mod test {
.is_some_and(|assets| assets.num_assets() == 2));
// Prove and apply transaction
client.testing_apply_transaction(tx_result.clone()).await.unwrap();

// Test serialization
let bytes: std::vec::Vec<u8> = tx_result.to_bytes();
let decoded = TransactionResult::read_from_bytes(&bytes).unwrap();

assert_eq!(tx_result, decoded);
}
}