Skip to content

Commit

Permalink
feat: implement serialization and Eq for TransactionScript (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyrd authored Aug 9, 2024
1 parent 77f0f74 commit 19c750f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- [BREAKING] Changed the `NoteFile::NoteDetails` type to struct and added a `after_block_num` field (#823).
- Implemented `cteate_note` and `move_asset_into_note` basic wallet procedures (#808).
- [BREAKING] Interface of the `miden::tx::add_asset_to_note` procedure was changed (#808).
- Added serialization and equality comparison for `TransactionScript` (#824).

## 0.4.0 (2024-07-03)

Expand Down
29 changes: 27 additions & 2 deletions objects/src/transaction/tx_args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use alloc::{collections::BTreeMap, vec::Vec};
use core::ops::Deref;

use assembly::ast::AstSerdeOptions;
use miden_crypto::merkle::InnerNodeInfo;
use vm_processor::{AdviceInputs, AdviceMap};
use vm_core::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::{AdviceInputs, AdviceMap, DeserializationError};

use super::{Digest, Felt, Word};
use crate::{
Expand Down Expand Up @@ -154,7 +156,7 @@ impl TransactionArgs {
/// - [hash](TransactionScript::hash): the hash of the compiled transaction script.
/// - [inputs](TransactionScript::inputs): a map of key, value inputs that are loaded into the
/// advice inputs' map such that the transaction script can access them.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TransactionScript {
code: ProgramAst,
hash: Digest,
Expand Down Expand Up @@ -222,3 +224,26 @@ impl TransactionScript {
&self.inputs
}
}

// SERIALIZATION
// ================================================================================================

impl Serializable for TransactionScript {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.code.write_into(target, AstSerdeOptions { serialize_imports: true });
self.code.write_source_locations(target);
self.hash.write_into(target);
self.inputs.write_into(target);
}
}

impl Deserializable for TransactionScript {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let mut code = ProgramAst::read_from(source)?;
code.load_source_locations(source)?;
let hash = Digest::read_from(source)?;
let inputs = BTreeMap::<Digest, Vec<Felt>>::read_from(source)?;

Ok(Self { code, hash, inputs })
}
}

0 comments on commit 19c750f

Please sign in to comment.