blockle-core 0.1.12

Core data structures for the Blockle blockchain: blocks, transactions, keys, addresses
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Blockle's transaction model: a note/output system (UTXO-style) with a
//! reserved shielded bundle so the privacy milestone extends the same wire
//! format instead of forking it.

use serde::{Deserialize, Serialize};

use crate::encode::{put_bytes, put_fixed, put_u32, put_u64};
use crate::hash::{blake2b_256, Hash32};
use crate::keys::Address;

/// Reference to a transaction output being spent.
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct OutPoint {
    pub txid: Hash32,
    pub vout: u32,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct TxInput {
    pub prev: OutPoint,
    /// ML-DSA-44 public key whose hash must match the spent output's address.
    pub pubkey: Vec<u8>,
    /// ML-DSA-44 signature over the transaction sighash. Empty while unsigned.
    pub signature: Vec<u8>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct TxOutput {
    pub recipient: Address,
    pub amount: u64,
}

/// A shielded spend: consumes a note from the commitment tree.
///
/// Reveals the nullifier (double-spend tag) and the note's value; the STARK
/// proof shows the note exists under `anchor` without revealing which one.
/// The proof is excluded from the sighash (it *binds* the sighash via the
/// proof transcript instead).
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ShieldedSpend {
    /// A historical note-tree root this spend proves membership against.
    pub anchor: Hash32,
    pub nullifier: Hash32,
    pub value: u64,
    pub proof: Vec<u8>,
}

/// A shielded output: appends a note commitment to the tree.
/// The value is public in protocol v1 (ownership privacy, not amount
/// privacy); the note secrets travel to the recipient out of band.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ShieldedOutput {
    pub commitment: Hash32,
    pub value: u64,
}

/// A fully-shielded transfer: spends one note into two new notes (payment +
/// change) with **hidden values** — only the fee is public. The STARK proves
/// value conservation. `memo` carries the recipient's encrypted note secrets
/// (opaque to consensus, bound by the sighash).
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ShieldedTransfer {
    pub anchor: Hash32,
    pub nullifier: Hash32,
    pub commitment1: Hash32,
    pub commitment2: Hash32,
    pub fee: u64,
    pub memo: Vec<u8>,
    pub proof: Vec<u8>,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct ShieldedBundle {
    pub spends: Vec<ShieldedSpend>,
    pub outputs: Vec<ShieldedOutput>,
    /// Hidden-amount z→z transfers.
    #[serde(default)]
    pub transfers: Vec<ShieldedTransfer>,
}

/// A Blockle VM action carried by a transaction. Gas is prepaid: consensus
/// requires `fee >= gas_limit * gas_price`, whether or not execution
/// succeeds or uses less.
/// Blockle Intelligence actions (height-gated at `ChainParams::intel_activation_height`;
/// see docs/BLOCKLE-INTELLIGENCE.md §3). Wrapped in `ContractAction::Intel` so they ride
/// the existing contract-action rails (fees, gas, refunds) without touching base-tx rules.
/// Additive: old nodes decode the unknown tag as malformed, so these are inherently a
/// height-activated fork — rejected below H_INTEL, accepted at/above it.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum IntelAction {
    /// Register a subnet (a task market). `tier` = verifiability tier (0 checkable,
    /// 1 redundant/reference, 2 subjective). `schema` describes the task.
    SubnetRegister { schema: Vec<u8>, tier: u8, gas_limit: u64 },
    /// Register a neuron (miner `role`=0 / validator `role`=1) in a subnet, locking a
    /// `bond` of BLOCK (from inputs; recoverable later via Unbond). `hotkey` is the
    /// serving/identity key.
    NeuronRegister { subnet_id: u32, role: u8, hotkey: Vec<u8>, bond: u64, gas_limit: u64 },
    /// Validator commits a hash of its weight vector for the epoch (commit-reveal §5.4).
    SetWeightsCommit { subnet_id: u32, commit: [u8; 32], gas_limit: u64 },
    /// Validator reveals the weights committed last epoch. `uids` are miner UIDs and
    /// `weights` the matching fixed-point weights (0..1_000_000). The reveal is accepted
    /// only if sha256(canonical(uids,weights) ‖ nonce) equals the stored commit.
    SetWeightsReveal { subnet_id: u32, uids: Vec<u32>, weights: Vec<u32>, nonce: [u8; 32], gas_limit: u64 },
    /// Pay out the sender's accrued intelligence emission to a new BLOCK output.
    NeuronClaim { gas_limit: u64 },
    /// Deregister (unbond) a neuron the sender owns, identified by `(subnet_id, uid)`.
    /// The remaining (post-slash) bond is refunded to the owner and the neuron is
    /// retired. Height-gated separately (not active until the unbond height) and
    /// subject to a one-epoch cooldown after registration so a validator cannot
    /// register, vote, and flee within a single epoch.
    NeuronDeregister { subnet_id: u32, uid: u32, gas_limit: u64 },
    /// Proof-of-validation (liveness): a validator acknowledges the current epoch's
    /// in-node beacon (`beacon_ack` must equal it), proving it is live against fresh,
    /// unpredictable randomness. Validators that reveal weights but skip this each epoch
    /// are excluded from emission and have their bond slashed.
    ValidationProof { subnet_id: u32, beacon_ack: [u8; 32], gas_limit: u64 },
}

impl IntelAction {
    pub fn gas_limit(&self) -> u64 {
        match self {
            IntelAction::SubnetRegister { gas_limit, .. } => *gas_limit,
            IntelAction::NeuronRegister { gas_limit, .. } => *gas_limit,
            IntelAction::SetWeightsCommit { gas_limit, .. } => *gas_limit,
            IntelAction::SetWeightsReveal { gas_limit, .. } => *gas_limit,
            IntelAction::NeuronClaim { gas_limit, .. } => *gas_limit,
            IntelAction::ValidationProof { gas_limit, .. } => *gas_limit,
            IntelAction::NeuronDeregister { gas_limit, .. } => *gas_limit,
        }
    }
    /// BLOCK moved from inputs into locked state (the neuron bond). Weight/claim actions
    /// move no BLOCK in (claim pays OUT via a payout).
    pub fn value(&self) -> u64 {
        match self {
            IntelAction::NeuronRegister { bond, .. } => *bond,
            _ => 0,
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum ContractAction {
    /// Store `code` as a new contract. The contract id is derived from the
    /// deploying txid.
    Deploy { code: Vec<u8>, gas_limit: u64 },
    /// Execute a deployed contract. `value` moves from this transaction's
    /// inputs into the contract's balance before execution (refunded to the
    /// sender if the call fails).
    Call { contract: Hash32, input: Vec<u8>, value: u64, gas_limit: u64 },
    /// Native AMM — create a BLOCK/`token` pool seeded with `block_amt` BLOCK
    /// (from inputs) and `token_amt` of the BLOCK-20 token (from the sender's
    /// token balance). LP shares lock for one week.
    PoolCreate { token: Hash32, block_amt: u64, token_amt: u64, gas_limit: u64 },
    /// Add liquidity at the current ratio: `block_amt` BLOCK plus up to
    /// `token_max` of the token. Re-locks the position for one week.
    PoolAdd { token: Hash32, block_amt: u64, token_max: u64, gas_limit: u64 },
    /// Withdraw `shares` of LP (BLOCK paid out, token credited). Fails if the
    /// position is still within its one-week lock.
    PoolRemove { token: Hash32, shares: u64, gas_limit: u64 },
    /// Swap `block_in` BLOCK (from inputs) for the token; reverts (refund) if
    /// the output would be below `min_token_out`.
    PoolSwapBuy { token: Hash32, block_in: u64, min_token_out: u64, gas_limit: u64 },
    /// Swap `token_in` of the token for BLOCK (paid out); no-op if the output
    /// would be below `min_block_out`.
    PoolSwapSell { token: Hash32, token_in: u64, min_block_out: u64, gas_limit: u64 },
    /// Atomic token launch — deploy `code`, run its `init()` (minting the baked
    /// supply to the sender), and seed a BLOCK/token AMM pool with `block_amt`
    /// BLOCK (from inputs) and `token_amt` of the freshly minted supply, all in
    /// ONE transaction (one block). Removes the deploy→init→pool race entirely:
    /// the pool can't fire before the mint exists. If any step fails the whole
    /// action reverts and `block_amt` is refunded to the sender.
    LaunchToken { code: Vec<u8>, block_amt: u64, token_amt: u64, gas_limit: u64 },
    /// Atomic NFT-collection launch — deploy `code` (a BLOCK-721 collection) and
    /// run its `init()` (selector 0, which fixes the deployer as the minter) in
    /// ONE transaction. Removes the deploy→init race for collections; subsequent
    /// `mint`s are independent single calls once this confirms.
    LaunchNft { code: Vec<u8>, gas_limit: u64 },
    /// Blockle Intelligence action (height-gated; see `IntelAction`).
    Intel(IntelAction),
}

impl ContractAction {
    pub fn gas_limit(&self) -> u64 {
        match self {
            ContractAction::Deploy { gas_limit, .. } => *gas_limit,
            ContractAction::Call { gas_limit, .. } => *gas_limit,
            ContractAction::PoolCreate { gas_limit, .. } => *gas_limit,
            ContractAction::PoolAdd { gas_limit, .. } => *gas_limit,
            ContractAction::PoolRemove { gas_limit, .. } => *gas_limit,
            ContractAction::PoolSwapBuy { gas_limit, .. } => *gas_limit,
            ContractAction::PoolSwapSell { gas_limit, .. } => *gas_limit,
            ContractAction::LaunchToken { gas_limit, .. } => *gas_limit,
            ContractAction::LaunchNft { gas_limit, .. } => *gas_limit,
            ContractAction::Intel(a) => a.gas_limit(),
        }
    }

    /// BLOCK that moves from this tx's inputs into a pool/contract.
    pub fn value(&self) -> u64 {
        match self {
            ContractAction::Deploy { .. } => 0,
            ContractAction::Call { value, .. } => *value,
            ContractAction::PoolCreate { block_amt, .. } => *block_amt,
            ContractAction::PoolAdd { block_amt, .. } => *block_amt,
            ContractAction::PoolRemove { .. } => 0,
            ContractAction::PoolSwapBuy { block_in, .. } => *block_in,
            ContractAction::PoolSwapSell { .. } => 0,
            ContractAction::LaunchToken { block_amt, .. } => *block_amt,
            ContractAction::LaunchNft { .. } => 0,
            ContractAction::Intel(a) => a.value(),
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Transaction {
    pub version: u32,
    /// Empty for coinbase transactions.
    pub inputs: Vec<TxInput>,
    pub outputs: Vec<TxOutput>,
    /// Coinbase-only payload (block height + arbitrary tag); makes coinbase
    /// txids unique. Must be empty for regular transactions.
    pub coinbase_data: Vec<u8>,
    pub shielded: Option<ShieldedBundle>,
    /// Optional Blockle VM deploy/call.
    #[serde(default)]
    pub contract: Option<ContractAction>,
}

impl Transaction {
    /// Coinbase: no transparent inputs, no shielded bundle, and no
    /// (the other input-less transaction kind).
    pub fn is_coinbase(&self) -> bool {
        self.inputs.is_empty() && self.shielded.is_none()
    }

    /// Total value entering from the shielded pool.
    pub fn shielded_in(&self) -> Option<u64> {
        self.shielded.as_ref().map_or(Some(0), |b| {
            b.spends.iter().try_fold(0u64, |acc, s| acc.checked_add(s.value))
        })
    }

    /// Total value leaving into the shielded pool.
    pub fn shielded_out(&self) -> Option<u64> {
        self.shielded.as_ref().map_or(Some(0), |b| {
            b.outputs.iter().try_fold(0u64, |acc, o| acc.checked_add(o.value))
        })
    }

    /// Fees paid by hidden-amount transfers (their only public value flow).
    pub fn transfer_fees(&self) -> Option<u64> {
        self.shielded.as_ref().map_or(Some(0), |b| {
            b.transfers.iter().try_fold(0u64, |acc, t| acc.checked_add(t.fee))
        })
    }

    /// All nullifiers this transaction reveals.
    pub fn nullifiers(&self) -> Vec<Hash32> {
        self.shielded
            .as_ref()
            .map(|b| {
                b.spends
                    .iter()
                    .map(|s| s.nullifier)
                    .chain(b.transfers.iter().map(|t| t.nullifier))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// Canonical consensus encoding. With `for_sighash` set, signatures are
    /// replaced by empty strings so inputs can be signed.
    pub fn encode(&self, for_sighash: bool) -> Vec<u8> {
        let mut out = Vec::new();
        put_u32(&mut out, self.version);
        put_u32(&mut out, self.inputs.len() as u32);
        for input in &self.inputs {
            put_fixed(&mut out, &input.prev.txid);
            put_u32(&mut out, input.prev.vout);
            put_bytes(&mut out, &input.pubkey);
            if for_sighash {
                put_bytes(&mut out, &[]);
            } else {
                put_bytes(&mut out, &input.signature);
            }
        }
        put_u32(&mut out, self.outputs.len() as u32);
        for output in &self.outputs {
            put_fixed(&mut out, &output.recipient);
            put_u64(&mut out, output.amount);
        }
        put_bytes(&mut out, &self.coinbase_data);
        match &self.shielded {
            None => put_u32(&mut out, 0),
            Some(s) => {
                put_u32(&mut out, 1);
                put_u32(&mut out, s.spends.len() as u32);
                for spend in &s.spends {
                    put_fixed(&mut out, &spend.anchor);
                    put_fixed(&mut out, &spend.nullifier);
                    put_u64(&mut out, spend.value);
                    // Like signatures, proofs are excluded from the sighash:
                    // each proof binds the sighash through its transcript.
                    if for_sighash {
                        put_bytes(&mut out, &[]);
                    } else {
                        put_bytes(&mut out, &spend.proof);
                    }
                }
                put_u32(&mut out, s.outputs.len() as u32);
                for output in &s.outputs {
                    put_fixed(&mut out, &output.commitment);
                    put_u64(&mut out, output.value);
                }
                put_u32(&mut out, s.transfers.len() as u32);
                for t in &s.transfers {
                    put_fixed(&mut out, &t.anchor);
                    put_fixed(&mut out, &t.nullifier);
                    put_fixed(&mut out, &t.commitment1);
                    put_fixed(&mut out, &t.commitment2);
                    put_u64(&mut out, t.fee);
                    put_bytes(&mut out, &t.memo);
                    if for_sighash {
                        put_bytes(&mut out, &[]);
                    } else {
                        put_bytes(&mut out, &t.proof);
                    }
                }
            }
        }
        match &self.contract {
            None => put_u32(&mut out, 0),
            Some(ContractAction::Deploy { code, gas_limit }) => {
                put_u32(&mut out, 1);
                put_bytes(&mut out, code);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Call { contract, input, value, gas_limit }) => {
                put_u32(&mut out, 2);
                put_fixed(&mut out, contract);
                put_bytes(&mut out, input);
                put_u64(&mut out, *value);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::PoolCreate { token, block_amt, token_amt, gas_limit }) => {
                put_u32(&mut out, 3);
                put_fixed(&mut out, token);
                put_u64(&mut out, *block_amt);
                put_u64(&mut out, *token_amt);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::PoolAdd { token, block_amt, token_max, gas_limit }) => {
                put_u32(&mut out, 4);
                put_fixed(&mut out, token);
                put_u64(&mut out, *block_amt);
                put_u64(&mut out, *token_max);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::PoolRemove { token, shares, gas_limit }) => {
                put_u32(&mut out, 5);
                put_fixed(&mut out, token);
                put_u64(&mut out, *shares);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::PoolSwapBuy { token, block_in, min_token_out, gas_limit }) => {
                put_u32(&mut out, 6);
                put_fixed(&mut out, token);
                put_u64(&mut out, *block_in);
                put_u64(&mut out, *min_token_out);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::PoolSwapSell { token, token_in, min_block_out, gas_limit }) => {
                put_u32(&mut out, 7);
                put_fixed(&mut out, token);
                put_u64(&mut out, *token_in);
                put_u64(&mut out, *min_block_out);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::LaunchToken { code, block_amt, token_amt, gas_limit }) => {
                put_u32(&mut out, 8);
                put_bytes(&mut out, code);
                put_u64(&mut out, *block_amt);
                put_u64(&mut out, *token_amt);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::LaunchNft { code, gas_limit }) => {
                put_u32(&mut out, 9);
                put_bytes(&mut out, code);
                put_u64(&mut out, *gas_limit);
            }
            // Intelligence actions: tag 10, then an inner sub-tag + fields.
            Some(ContractAction::Intel(IntelAction::SubnetRegister { schema, tier, gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 0); // inner: SubnetRegister
                put_bytes(&mut out, schema);
                out.push(*tier);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Intel(IntelAction::NeuronRegister { subnet_id, role, hotkey, bond, gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 1); // inner: NeuronRegister
                put_u32(&mut out, *subnet_id);
                out.push(*role);
                put_bytes(&mut out, hotkey);
                put_u64(&mut out, *bond);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Intel(IntelAction::SetWeightsCommit { subnet_id, commit, gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 2); // inner: SetWeightsCommit
                put_u32(&mut out, *subnet_id);
                put_fixed(&mut out, commit);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Intel(IntelAction::SetWeightsReveal { subnet_id, uids, weights, nonce, gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 3); // inner: SetWeightsReveal
                put_u32(&mut out, *subnet_id);
                put_u32(&mut out, uids.len() as u32);
                for u in uids { put_u32(&mut out, *u); }
                put_u32(&mut out, weights.len() as u32);
                for w in weights { put_u32(&mut out, *w); }
                put_fixed(&mut out, nonce);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Intel(IntelAction::NeuronClaim { gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 4); // inner: NeuronClaim
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Intel(IntelAction::ValidationProof { subnet_id, beacon_ack, gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 5); // inner: ValidationProof
                put_u32(&mut out, *subnet_id);
                put_fixed(&mut out, beacon_ack);
                put_u64(&mut out, *gas_limit);
            }
            Some(ContractAction::Intel(IntelAction::NeuronDeregister { subnet_id, uid, gas_limit })) => {
                put_u32(&mut out, 10);
                put_u32(&mut out, 6); // inner: NeuronDeregister
                put_u32(&mut out, *subnet_id);
                put_u32(&mut out, *uid);
                put_u64(&mut out, *gas_limit);
            }
        }
        out
    }

    pub fn txid(&self) -> Hash32 {
        blake2b_256(&self.encode(false))
    }

    /// The message each input signs: the transaction with signatures blanked.
    pub fn sighash(&self) -> Hash32 {
        blake2b_256(&self.encode(true))
    }

    pub fn total_output(&self) -> Option<u64> {
        self.outputs
            .iter()
            .try_fold(0u64, |acc, o| acc.checked_add(o.amount))
    }

    pub fn serialized_size(&self) -> usize {
        self.encode(false).len()
    }

    /// Build a coinbase transaction paying `recipient` at `height`.
    pub fn coinbase(height: u64, recipient: Address, amount: u64, tag: &[u8]) -> Self {
        let mut coinbase_data = height.to_le_bytes().to_vec();
        coinbase_data.extend_from_slice(tag);
        Transaction {
            version: 1,
            inputs: vec![],
            outputs: vec![TxOutput { recipient, amount }],
            coinbase_data,
            shielded: None,
            contract: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::keys::Keypair;

    #[test]
    fn txid_changes_with_content_sighash_ignores_sigs() {
        let kp = Keypair::generate();
        let mut tx = Transaction {
            version: 1,
            inputs: vec![TxInput {
                prev: OutPoint { txid: [1; 32], vout: 0 },
                pubkey: kp.public_bytes(),
                signature: vec![],
            }],
            outputs: vec![TxOutput { recipient: kp.address(), amount: 42 }],
            coinbase_data: vec![],
            shielded: None,
            contract: None,
        };
        let sighash_before = tx.sighash();
        let txid_before = tx.txid();
        tx.inputs[0].signature = kp.sign(&sighash_before).to_vec();
        assert_eq!(tx.sighash(), sighash_before, "sighash must ignore signatures");
        assert_ne!(tx.txid(), txid_before, "txid commits to signatures");

        tx.outputs[0].amount = 43;
        assert_ne!(tx.sighash(), sighash_before);
    }

    #[test]
    fn coinbase_txids_unique_per_height() {
        let addr = [9u8; 32];
        let a = Transaction::coinbase(1, addr, 50, b"blockle");
        let b = Transaction::coinbase(2, addr, 50, b"blockle");
        assert!(a.is_coinbase());
        assert_ne!(a.txid(), b.txid());
    }
}