Skip to content

Commit

Permalink
feat: make TransactionResult serializable (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd authored Jan 28, 2025
1 parent 42a23b5 commit b8be277
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 35 deletions.
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
94 changes: 62 additions & 32 deletions Cargo.lock

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

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);
}
}

0 comments on commit b8be277

Please sign in to comment.