Skip to content

Commit 67dfb83

Browse files
committed
elements: Environment with generic transaction ref
A generic transaction reference is more flexible and will prevent us from cloning transactions in the upcoming sighash. Because we changed jet/init/elements.rs, we need to update the Haskell code that generated the original file.
1 parent 9182897 commit 67dfb83

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

jets-bench/benches/elements/env.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ pub enum EnvSampling {
2828

2929
impl EnvSampling {
3030
/// Obtain a random environment without any annex
31-
pub fn env(&self) -> ElementsEnv {
31+
pub fn env(&self) -> ElementsEnv<Arc<Transaction>> {
3232
self.env_with_annex(None)
3333
}
3434

3535
/// Obtains a env with annex
36-
pub fn env_with_annex(&self, annex: Option<Vec<u8>>) -> ElementsEnv {
36+
pub fn env_with_annex(&self, annex: Option<Vec<u8>>) -> ElementsEnv<Arc<Transaction>> {
3737
let ((txin, spent_utxo), n_in, n_out) = match self {
3838
EnvSampling::Null => return null_env(),
3939
EnvSampling::Issuance(n_in, n_out) => (txin_utils::issuance(), n_in, n_out),
@@ -81,7 +81,7 @@ fn env_with_spent_utxos(
8181
tx: Transaction,
8282
utxos: Vec<ElementsUtxo>,
8383
annex: Option<Vec<u8>>,
84-
) -> ElementsEnv {
84+
) -> ElementsEnv<Arc<Transaction>> {
8585
let ctrl_blk: [u8; 33] = [
8686
0xc0, 0xeb, 0x04, 0xb6, 0x8e, 0x9a, 0x26, 0xd1, 0x16, 0x04, 0x6c, 0x76, 0xe8, 0xff, 0x47,
8787
0x33, 0x2f, 0xb7, 0x1d, 0xda, 0x90, 0xff, 0x4b, 0xef, 0x53, 0x70, 0xf2, 0x52, 0x26, 0xd3,
@@ -98,7 +98,7 @@ fn env_with_spent_utxos(
9898
)
9999
}
100100

101-
fn null_env() -> ElementsEnv {
101+
fn null_env() -> ElementsEnv<Arc<Transaction>> {
102102
let tx = Transaction {
103103
version: u32::default(),
104104
lock_time: LockTime::ZERO,

jets-bench/benches/elements/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ enum ElementsBenchEnvType {
5959
}
6060

6161
impl ElementsBenchEnvType {
62-
fn env(&self) -> ElementsEnv {
62+
fn env(&self) -> ElementsEnv<Arc<elements::Transaction>> {
6363
let n_in = NUM_TX_INPUTS;
6464
let n_out = NUM_TX_OUTPUTS;
6565
let env_sampler = match self {

src/jet/elements/environment.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::merkle::cmr::Cmr;
1616
use elements::confidential;
1717
use elements::taproot::ControlBlock;
1818
use simplicity_sys::c_jets::c_env::CElementsTxEnv;
19-
use std::sync::Arc;
19+
use std::ops::Deref;
2020

2121
use super::c_env;
2222

@@ -58,11 +58,11 @@ impl From<elements::TxOut> for ElementsUtxo {
5858
// Similar story if we tried to use a &'a elements::Transaction rather than
5959
// an Arc: we'd have a lifetime parameter <'a> that would cause us trouble.
6060
#[allow(dead_code)]
61-
pub struct ElementsEnv {
61+
pub struct ElementsEnv<T: Deref<Target = elements::Transaction>> {
6262
/// The CTxEnv struct
6363
c_tx_env: CElementsTxEnv,
6464
/// The elements transaction
65-
tx: Arc<elements::Transaction>,
65+
tx: T,
6666
/// the current index of the input
6767
ix: u32,
6868
/// Control block used to spend this leaf script
@@ -73,9 +73,12 @@ pub struct ElementsEnv {
7373
genesis_hash: elements::BlockHash,
7474
}
7575

76-
impl ElementsEnv {
76+
impl<T> ElementsEnv<T>
77+
where
78+
T: Deref<Target = elements::Transaction>,
79+
{
7780
pub fn new(
78-
tx: Arc<elements::Transaction>,
81+
tx: T,
7982
utxos: Vec<ElementsUtxo>,
8083
ix: u32,
8184
script_cmr: Cmr,
@@ -128,7 +131,7 @@ impl ElementsEnv {
128131
}
129132

130133
#[cfg(test)]
131-
impl ElementsEnv {
134+
impl ElementsEnv<std::sync::Arc<elements::Transaction>> {
132135
/// Return a dummy Elements environment
133136
pub fn dummy() -> Self {
134137
Self::dummy_with(elements::LockTime::ZERO, elements::Sequence::MAX)
@@ -146,7 +149,7 @@ impl ElementsEnv {
146149
];
147150

148151
ElementsEnv::new(
149-
Arc::new(elements::Transaction {
152+
std::sync::Arc::new(elements::Transaction {
150153
version: 2,
151154
lock_time,
152155
// Enable locktime in dummy txin

src/jet/init/elements.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::io::Write;
1212
use std::{fmt, str};
1313
use crate::jet::elements::ElementsEnv;
1414
use simplicity_sys::CElementsTxEnv;
15+
use std::sync::Arc;
1516

1617
/// Elements jet family
1718
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
@@ -313,7 +314,7 @@ pub enum Elements {
313314

314315
impl Jet for Elements {
315316

316-
type Environment = ElementsEnv;
317+
type Environment = ElementsEnv<Arc<elements::Transaction>>;
317318
type CJetEnvironment = CElementsTxEnv;
318319

319320
fn c_jet_env<'env>(&self, env: &'env Self::Environment) -> &'env Self::CJetEnvironment {

src/policy/satisfy.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,9 @@ mod tests {
249249
}
250250
}
251251

252-
fn get_satisfier(env: &ElementsEnv) -> PolicySatisfier<XOnlyPublicKey> {
252+
fn get_satisfier(
253+
env: &ElementsEnv<Arc<elements::Transaction>>,
254+
) -> PolicySatisfier<XOnlyPublicKey> {
253255
let mut preimages = HashMap::new();
254256

255257
for i in 0..3 {
@@ -283,7 +285,10 @@ mod tests {
283285
}
284286
}
285287

286-
fn execute_successful(program: Arc<RedeemNode<Elements>>, env: &ElementsEnv) {
288+
fn execute_successful(
289+
program: Arc<RedeemNode<Elements>>,
290+
env: &ElementsEnv<Arc<elements::Transaction>>,
291+
) {
287292
let mut mac = BitMachine::for_program(&program);
288293
assert!(mac.exec(&program, env).is_ok());
289294
}

src/policy/serialize.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,12 @@ mod tests {
255255
use hashes::{sha256, Hash};
256256
use std::sync::Arc;
257257

258-
fn compile(policy: Policy<XOnlyPublicKey>) -> (Arc<ConstructNode<Elements>>, ElementsEnv) {
258+
fn compile(
259+
policy: Policy<XOnlyPublicKey>,
260+
) -> (
261+
Arc<ConstructNode<Elements>>,
262+
ElementsEnv<Arc<elements::Transaction>>,
263+
) {
259264
let commit = policy.serialize_no_witness();
260265
let env = ElementsEnv::dummy();
261266

@@ -265,7 +270,7 @@ mod tests {
265270
fn execute_successful(
266271
commit: &ConstructNode<Elements>,
267272
witness: Vec<Value>,
268-
env: &ElementsEnv,
273+
env: &ElementsEnv<Arc<elements::Transaction>>,
269274
) -> bool {
270275
let finalized = commit
271276
.finalize_types()

0 commit comments

Comments
 (0)