zebra_chain/transaction/
builder.rs

1//! Methods for building transactions.
2
3use crate::{
4    amount::{Amount, NonNegative},
5    block::Height,
6    parameters::{Network, NetworkUpgrade},
7    transaction::{LockTime, Transaction},
8    transparent,
9};
10
11impl Transaction {
12    /// Returns a new version 5 coinbase transaction for `network` and `height`,
13    /// which contains the specified `outputs`.
14    pub fn new_v5_coinbase(
15        network: &Network,
16        height: Height,
17        outputs: impl IntoIterator<Item = (Amount<NonNegative>, transparent::Script)>,
18        miner_data: Vec<u8>,
19    ) -> Transaction {
20        // # Consensus
21        //
22        // These consensus rules apply to v5 coinbase transactions after NU5 activation:
23        //
24        // > If effectiveVersion ≥ 5 then this condition MUST hold:
25        // > tx_in_count > 0 or nSpendsSapling > 0 or
26        // > (nActionsOrchard > 0 and enableSpendsOrchard = 1).
27        //
28        // > A coinbase transaction for a block at block height greater than 0 MUST have
29        // > a script that, as its first item, encodes the block height as follows. ...
30        // > let heightBytes be the signed little-endian representation of height,
31        // > using the minimum nonzero number of bytes such that the most significant byte
32        // > is < 0x80. The length of heightBytes MUST be in the range {1 .. 5}.
33        // > Then the encoding is the length of heightBytes encoded as one byte,
34        // > followed by heightBytes itself. This matches the encoding used by Bitcoin
35        // > in the implementation of [BIP-34]
36        // > (but the description here is to be considered normative).
37        //
38        // > A coinbase transaction script MUST have length in {2 .. 100} bytes.
39        //
40        // Zebra adds extra coinbase data if configured to do so.
41        //
42        // Since we're not using a lock time, any sequence number is valid here.
43        // See `Transaction::lock_time()` for the relevant consensus rules.
44        //
45        // <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
46        let inputs = vec![transparent::Input::new_coinbase(height, miner_data, None)];
47
48        // > The block subsidy is composed of a miner subsidy and a series of funding streams.
49        //
50        // <https://zips.z.cash/protocol/protocol.pdf#subsidyconcepts>
51        //
52        // > The total value in zatoshi of transparent outputs from a coinbase transaction,
53        // > minus vbalanceSapling, minus vbalanceOrchard, MUST NOT be greater than
54        // > the value in zatoshi of block subsidy plus the transaction fees
55        // > paid by transactions in this block.
56        //
57        // > If effectiveVersion ≥ 5 then this condition MUST hold:
58        // > tx_out_count > 0 or nOutputsSapling > 0 or
59        // > (nActionsOrchard > 0 and enableOutputsOrchard = 1).
60        //
61        // <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
62        let outputs: Vec<_> = outputs
63            .into_iter()
64            .map(|(amount, lock_script)| transparent::Output::new_coinbase(amount, lock_script))
65            .collect();
66
67        assert!(
68            !outputs.is_empty(),
69            "invalid coinbase transaction: must have at least one output"
70        );
71
72        Transaction::V5 {
73            // > The transaction version number MUST be 4 or 5. ...
74            // > If the transaction version number is 5 then the version group ID
75            // > MUST be 0x26A7270A.
76            // > If effectiveVersion ≥ 5, the nConsensusBranchId field MUST match the consensus
77            // > branch ID used for SIGHASH transaction hashes, as specified in [ZIP-244].
78            network_upgrade: NetworkUpgrade::current(network, height),
79
80            // There is no documented consensus rule for the lock time field in coinbase
81            // transactions, so we just leave it unlocked. (We could also set it to `height`.)
82            lock_time: LockTime::unlocked(),
83
84            // > The nExpiryHeight field of a coinbase transaction MUST be equal to its
85            // > block height.
86            expiry_height: height,
87
88            inputs,
89            outputs,
90
91            // Zebra does not support shielded coinbase yet.
92            //
93            // > In a version 5 coinbase transaction, the enableSpendsOrchard flag MUST be 0.
94            // > In a version 5 transaction, the reserved bits 2 .. 7 of the flagsOrchard field
95            // > MUST be zero.
96            //
97            // See the Zcash spec for additional shielded coinbase consensus rules.
98            sapling_shielded_data: None,
99            orchard_shielded_data: None,
100        }
101    }
102
103    /// Returns a new version 4 coinbase transaction for `network` and `height`,
104    /// which contains the specified `outputs`.
105    ///
106    /// If `like_zcashd` is true, try to match the coinbase transactions generated by `zcashd`
107    /// in the `getblocktemplate` RPC.
108    pub fn new_v4_coinbase(
109        height: Height,
110        outputs: impl IntoIterator<Item = (Amount<NonNegative>, transparent::Script)>,
111        miner_data: Vec<u8>,
112    ) -> Transaction {
113        // # Consensus
114        //
115        // See the other consensus rules above in new_v5_coinbase().
116        //
117        // > If effectiveVersion < 5, then at least one of tx_in_count, nSpendsSapling,
118        // > and nJoinSplit MUST be nonzero.
119        let inputs = vec![transparent::Input::new_coinbase(
120            height,
121            miner_data,
122            // zcashd uses a sequence number of u32::MAX.
123            Some(u32::MAX),
124        )];
125
126        // > If effectiveVersion < 5, then at least one of tx_out_count, nOutputsSapling,
127        // > and nJoinSplit MUST be nonzero.
128        let outputs: Vec<_> = outputs
129            .into_iter()
130            .map(|(amount, lock_script)| transparent::Output::new_coinbase(amount, lock_script))
131            .collect();
132
133        assert!(
134            !outputs.is_empty(),
135            "invalid coinbase transaction: must have at least one output"
136        );
137
138        // > The transaction version number MUST be 4 or 5. ...
139        // > If the transaction version number is 4 then the version group ID MUST be 0x892F2085.
140        Transaction::V4 {
141            lock_time: LockTime::unlocked(),
142            expiry_height: height,
143            inputs,
144            outputs,
145            joinsplit_data: None,
146            sapling_shielded_data: None,
147        }
148    }
149}