Skip to content

Commit

Permalink
Merge pull request #361 from 0xPolygonMiden/bobbin-tx-script
Browse files Browse the repository at this point in the history
Add `from_parts()` constructor to `TransactionScript`
  • Loading branch information
bobbinth authored Dec 14, 2023
2 parents 53b29de + 858eb9e commit 3a91399
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions objects/src/transaction/script.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::{Felt, Vec, Word};
use super::{Digest, Felt, Word};
use crate::{
advice::{AdviceInputsBuilder, ToAdviceInputs},
assembly::ProgramAst,
assembly::{Assembler, AssemblyContext, CodeBlock, ProgramAst},
errors::TransactionScriptError,
utils::collections::BTreeMap,
Digest,
utils::collections::{BTreeMap, Vec},
};
use assembly::{Assembler, AssemblyContext};
use vm_core::code_blocks::CodeBlock;

// TRANSACTION SCRIPT
// ================================================================================================

/// A struct that represents a transaction script.
///
Expand All @@ -18,7 +18,7 @@ use vm_core::code_blocks::CodeBlock;
/// - [code](TransactionScript::code): the transaction script source code.
/// - [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 map such that the transaction script can access them.
/// advice map such that the transaction script can access them.
#[derive(Clone, Debug)]
pub struct TransactionScript {
code: ProgramAst,
Expand All @@ -27,19 +27,19 @@ pub struct TransactionScript {
}

impl TransactionScript {
// CONSTRUCTOR
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------

/// Returns a new instance of a [TransactionScript] with the provided script and inputs and the
/// compiled script code block.
pub fn new<T>(
///
/// # Errors
/// Returns an error if script compilation fails.
pub fn new<T: IntoIterator<Item = (Word, Vec<Felt>)>>(
code: ProgramAst,
inputs: T,
assembler: &mut Assembler,
) -> Result<(Self, CodeBlock), TransactionScriptError>
where
T: IntoIterator<Item = (Word, Vec<Felt>)>,
{
) -> Result<(Self, CodeBlock), TransactionScriptError> {
let code_block = assembler
.compile_in_context(&code, &mut AssemblyContext::for_program(Some(&code)))
.map_err(TransactionScriptError::ScriptCompilationError)?;
Expand All @@ -53,6 +53,22 @@ impl TransactionScript {
))
}

/// Returns a new instance of a [TransactionScript] instantiated from the provided components.
///
/// Note: this constructor does not verify that a compiled code in fact results in the provided
/// hash.
pub fn from_parts<T: IntoIterator<Item = (Word, Vec<Felt>)>>(
code: ProgramAst,
hash: Digest,
inputs: T,
) -> Result<Self, TransactionScriptError> {
Ok(Self {
code,
hash,
inputs: inputs.into_iter().map(|(k, v)| (k.into(), v)).collect(),
})
}

// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------

Expand Down

0 comments on commit 3a91399

Please sign in to comment.