Skip to content

Commit 405487e

Browse files
committed
ffi: move Raw* structs out of FFI crate
These structures are private data types used ephemerally by src/jet/elements/c_env.rs. They should not be public and certainly should not be exposed in simplicity-sys.
1 parent c42a375 commit 405487e

File tree

2 files changed

+43
-53
lines changed

2 files changed

+43
-53
lines changed

simplicity-sys/src/c_jets/c_env/elements.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,13 @@ use hashes::{sha256, Hash};
55
use crate::ffi::sha256::CSha256Midstate;
66
use crate::ffi::{c_size_t, c_uchar, c_uint, c_uint_fast32_t};
77

8-
/// Documentation of RawInputData/RawOutputData/RawTapData/Raw.
9-
///
10-
/// Data structure for holding data that CTransaction points to.
11-
///
12-
/// Why do we this special data structure?
13-
/// 1. We need to keep the data in memory until the CTransaction is dropped.
14-
/// 2. The memory is Transaction is not saved in the same format as required by FFI.
15-
/// We use more ergonomics in rust to allow better UX which interfere with the FFI. For example,
16-
/// the Value is stored as Tagged Union, but we require it to be a slice of bytes in elements format.
17-
/// 3. Allocating inside FFI functions does not work because the memory is freed after the function returns.
18-
/// 4. We only create allocations for data fields that are stored differently from the
19-
/// consensus serialization format.
20-
#[derive(Debug)]
21-
pub struct RawOutputData {
22-
pub asset: Option<[c_uchar; 33]>,
23-
pub value: Vec<c_uchar>,
24-
pub nonce: Option<[c_uchar; 33]>,
25-
pub surjection_proof: Vec<c_uchar>,
26-
pub range_proof: Vec<c_uchar>,
27-
}
28-
298
#[derive(Debug)]
309
#[repr(C)]
3110
pub struct CRawBuffer {
3211
pub ptr: *const c_uchar,
3312
pub len: u32,
3413
}
3514

36-
/// Similar to [`RawOutputData`], for inputs.
37-
#[derive(Debug)]
38-
pub struct RawInputData {
39-
pub annex: Option<Vec<c_uchar>>,
40-
// pegin
41-
pub genesis_hash: Option<[c_uchar; 32]>,
42-
// issuance
43-
pub issuance_amount: Vec<c_uchar>,
44-
pub issuance_inflation_keys: Vec<c_uchar>,
45-
pub amount_range_proof: Vec<c_uchar>,
46-
pub inflation_keys_range_proof: Vec<c_uchar>,
47-
// spent txo
48-
pub asset: Option<[c_uchar; 33]>,
49-
pub value: Vec<c_uchar>,
50-
}
51-
52-
/// Similar to [`RawOutputData`], but for transaction
53-
#[derive(Debug)]
54-
pub struct RawTransactionData {
55-
pub inputs: Vec<RawInputData>,
56-
pub outputs: Vec<RawOutputData>,
57-
}
58-
5915
#[derive(Debug)]
6016
#[repr(C)]
6117
pub struct CRawOutput<'raw> {

src/jet/elements/c_env.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,46 @@ use crate::merkle::cmr::Cmr;
1818

1919
use super::ElementsUtxo;
2020

21+
/// Holds transaction output data which needs to be re-serialized before being
22+
/// passed to the C FFI.
23+
#[derive(Debug)]
24+
struct RawOutputData {
25+
pub asset: Option<[c_uchar; 33]>,
26+
pub value: Vec<c_uchar>,
27+
pub nonce: Option<[c_uchar; 33]>,
28+
pub surjection_proof: Vec<c_uchar>,
29+
pub range_proof: Vec<c_uchar>,
30+
}
31+
32+
/// Holds transaction input data which needs to be re-serialized before being
33+
/// passed to the C FFI.
34+
#[derive(Debug)]
35+
struct RawInputData {
36+
#[allow(dead_code)] // see FIXME below
37+
pub annex: Option<Vec<c_uchar>>,
38+
// pegin
39+
pub genesis_hash: Option<[c_uchar; 32]>,
40+
// issuance
41+
pub issuance_amount: Vec<c_uchar>,
42+
pub issuance_inflation_keys: Vec<c_uchar>,
43+
pub amount_range_proof: Vec<c_uchar>,
44+
pub inflation_keys_range_proof: Vec<c_uchar>,
45+
// spent txo
46+
pub asset: Option<[c_uchar; 33]>,
47+
pub value: Vec<c_uchar>,
48+
}
49+
50+
/// Holds transaction data which needs to be re-serialized before being
51+
/// passed to the C FFI.
52+
#[derive(Debug)]
53+
struct RawTransactionData {
54+
pub inputs: Vec<RawInputData>,
55+
pub outputs: Vec<RawOutputData>,
56+
}
57+
2158
fn new_raw_output<'raw>(
2259
out: &elements::TxOut,
23-
out_data: &'raw c_elements::RawOutputData,
60+
out_data: &'raw RawOutputData,
2461
) -> c_elements::CRawOutput<'raw> {
2562
c_elements::CRawOutput {
2663
asset: out_data.asset.as_ref(),
@@ -35,7 +72,7 @@ fn new_raw_output<'raw>(
3572
fn new_raw_input<'raw>(
3673
inp: &'raw elements::TxIn,
3774
in_utxo: &'raw ElementsUtxo,
38-
inp_data: &'raw c_elements::RawInputData,
75+
inp_data: &'raw RawInputData,
3976
) -> c_elements::CRawInput<'raw> {
4077
c_elements::CRawInput {
4178
// FIXME actually pass the annex in; see https://github.com/BlockstreamResearch/simplicity/issues/311 for some difficulty here.
@@ -70,16 +107,13 @@ fn new_raw_input<'raw>(
70107
}
71108
}
72109

73-
fn new_tx_data(
74-
tx: &elements::Transaction,
75-
in_utxos: &[ElementsUtxo],
76-
) -> c_elements::RawTransactionData {
77-
let mut tx_data = c_elements::RawTransactionData {
110+
fn new_tx_data(tx: &elements::Transaction, in_utxos: &[ElementsUtxo]) -> RawTransactionData {
111+
let mut tx_data = RawTransactionData {
78112
inputs: Vec::with_capacity(tx.input.len()),
79113
outputs: Vec::with_capacity(tx.output.len()),
80114
};
81115
for (inp, in_utxo) in tx.input.iter().zip(in_utxos.iter()) {
82-
let inp_data = c_elements::RawInputData {
116+
let inp_data = RawInputData {
83117
annex: None, // Actually store annex
84118
genesis_hash: inp
85119
.pegin_data()
@@ -96,7 +130,7 @@ fn new_tx_data(
96130
tx_data.inputs.push(inp_data);
97131
}
98132
for out in &tx.output {
99-
let out_data = c_elements::RawOutputData {
133+
let out_data = RawOutputData {
100134
asset: asset_array(&out.asset),
101135
value: serialize(&out.value),
102136
nonce: nonce_array(&out.nonce),

0 commit comments

Comments
 (0)