@@ -9,6 +9,101 @@ use crate::{
99} ;
1010
1111impl Transaction {
12+ /// Returns a new version 6 coinbase transaction for `network` and `height`,
13+ /// which contains the specified `outputs`.
14+ #[ cfg( feature = "tx_v6" ) ]
15+ pub fn new_v6_coinbase (
16+ network : & Network ,
17+ height : Height ,
18+ outputs : impl IntoIterator < Item = ( Amount < NonNegative > , transparent:: Script ) > ,
19+ miner_data : Vec < u8 > ,
20+ zip233_amount : Option < Amount < NonNegative > > ,
21+ ) -> Transaction {
22+ // # Consensus
23+ //
24+ // These consensus rules apply to v5 coinbase transactions after NU5 activation:
25+ //
26+ // > If effectiveVersion ≥ 5 then this condition MUST hold:
27+ // > tx_in_count > 0 or nSpendsSapling > 0 or
28+ // > (nActionsOrchard > 0 and enableSpendsOrchard = 1).
29+ //
30+ // > A coinbase transaction for a block at block height greater than 0 MUST have
31+ // > a script that, as its first item, encodes the block height as follows. ...
32+ // > let heightBytes be the signed little-endian representation of height,
33+ // > using the minimum nonzero number of bytes such that the most significant byte
34+ // > is < 0x80. The length of heightBytes MUST be in the range {1 .. 5}.
35+ // > Then the encoding is the length of heightBytes encoded as one byte,
36+ // > followed by heightBytes itself. This matches the encoding used by Bitcoin
37+ // > in the implementation of [BIP-34]
38+ // > (but the description here is to be considered normative).
39+ //
40+ // > A coinbase transaction script MUST have length in {2 .. 100} bytes.
41+ //
42+ // Zebra adds extra coinbase data if configured to do so.
43+ //
44+ // Since we're not using a lock time, any sequence number is valid here.
45+ // See `Transaction::lock_time()` for the relevant consensus rules.
46+ //
47+ // <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
48+ let inputs = vec ! [ transparent:: Input :: new_coinbase( height, miner_data, None ) ] ;
49+
50+ // > The block subsidy is composed of a miner subsidy and a series of funding streams.
51+ //
52+ // <https://zips.z.cash/protocol/protocol.pdf#subsidyconcepts>
53+ //
54+ // > The total value in zatoshi of transparent outputs from a coinbase transaction,
55+ // > minus vbalanceSapling, minus vbalanceOrchard, MUST NOT be greater than
56+ // > the value in zatoshi of block subsidy plus the transaction fees
57+ // > paid by transactions in this block.
58+ //
59+ // > If effectiveVersion ≥ 5 then this condition MUST hold:
60+ // > tx_out_count > 0 or nOutputsSapling > 0 or
61+ // > (nActionsOrchard > 0 and enableOutputsOrchard = 1).
62+ //
63+ // <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
64+ let outputs: Vec < _ > = outputs
65+ . into_iter ( )
66+ . map ( |( amount, lock_script) | transparent:: Output :: new_coinbase ( amount, lock_script) )
67+ . collect ( ) ;
68+ assert ! (
69+ !outputs. is_empty( ) ,
70+ "invalid coinbase transaction: must have at least one output"
71+ ) ;
72+
73+ Transaction :: V6 {
74+ // > The transaction version number MUST be 4 or 5. ...
75+ // > If the transaction version number is 5 then the version group ID
76+ // > MUST be 0x26A7270A.
77+ // > If effectiveVersion ≥ 5, the nConsensusBranchId field MUST match the consensus
78+ // > branch ID used for SIGHASH transaction hashes, as specified in [ZIP-244].
79+ network_upgrade : NetworkUpgrade :: current ( network, height) ,
80+
81+ // There is no documented consensus rule for the lock time field in coinbase
82+ // transactions, so we just leave it unlocked. (We could also set it to `height`.)
83+ lock_time : LockTime :: unlocked ( ) ,
84+
85+ // > The nExpiryHeight field of a coinbase transaction MUST be equal to its
86+ // > block height.
87+ expiry_height : height,
88+
89+ // > The NSM zip233_amount field [ZIP-233] must be set. It must be >= 0.
90+ zip233_amount : zip233_amount. unwrap_or ( Amount :: zero ( ) ) ,
91+
92+ inputs,
93+ outputs,
94+
95+ // Zebra does not support shielded coinbase yet.
96+ //
97+ // > In a version 5 coinbase transaction, the enableSpendsOrchard flag MUST be 0.
98+ // > In a version 5 transaction, the reserved bits 2 .. 7 of the flagsOrchard field
99+ // > MUST be zero.
100+ //
101+ // See the Zcash spec for additional shielded coinbase consensus rules.
102+ sapling_shielded_data : None ,
103+ orchard_shielded_data : None ,
104+ }
105+ }
106+
12107 /// Returns a new version 5 coinbase transaction for `network` and `height`,
13108 /// which contains the specified `outputs`.
14109 pub fn new_v5_coinbase (
0 commit comments