zebra_chain/transparent/
serialize.rs

1//! Serializes and deserializes transparent data.
2
3use std::io;
4
5use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
6
7use crate::{
8    block::{self, Height},
9    serialization::{
10        zcash_serialize_bytes, FakeWriter, ReadZcashExt, SerializationError, ZcashDeserialize,
11        ZcashDeserializeInto, ZcashSerialize,
12    },
13    transaction,
14};
15
16use super::{CoinbaseData, Input, OutPoint, Output, Script};
17
18/// The maximum length of the coinbase data.
19///
20/// Includes the encoded coinbase height, if any.
21///
22/// # Consensus
23///
24/// > A coinbase transaction script MUST have length in {2 .. 100} bytes.
25///
26/// <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
27pub const MAX_COINBASE_DATA_LEN: usize = 100;
28
29/// The maximum length of the encoded coinbase height.
30///
31/// # Consensus
32///
33/// > The length of heightBytes MUST be in the range {1 .. 5}. Then the encoding is the length
34/// > of heightBytes encoded as one byte, followed by heightBytes itself.
35///
36/// <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
37pub const MAX_COINBASE_HEIGHT_DATA_LEN: usize = 6;
38
39/// The minimum length of the coinbase data.
40///
41/// Includes the encoded coinbase height, if any.
42///
43/// # Consensus
44///
45/// > A coinbase transaction script MUST have length in {2 .. 100} bytes.
46///
47/// <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
48pub const MIN_COINBASE_DATA_LEN: usize = 2;
49
50/// The coinbase data for a genesis block.
51///
52/// Zcash uses the same coinbase data for the Mainnet, Testnet, and Regtest
53/// genesis blocks.
54pub const GENESIS_COINBASE_DATA: [u8; 77] = [
55    4, 255, 255, 7, 31, 1, 4, 69, 90, 99, 97, 115, 104, 48, 98, 57, 99, 52, 101, 101, 102, 56, 98,
56    55, 99, 99, 52, 49, 55, 101, 101, 53, 48, 48, 49, 101, 51, 53, 48, 48, 57, 56, 52, 98, 54, 102,
57    101, 97, 51, 53, 54, 56, 51, 97, 55, 99, 97, 99, 49, 52, 49, 97, 48, 52, 51, 99, 52, 50, 48,
58    54, 52, 56, 51, 53, 100, 51, 52,
59];
60
61impl ZcashSerialize for OutPoint {
62    fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
63        writer.write_all(&self.hash.0[..])?;
64        writer.write_u32::<LittleEndian>(self.index)?;
65        Ok(())
66    }
67}
68
69impl ZcashDeserialize for OutPoint {
70    fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
71        Ok(OutPoint {
72            hash: transaction::Hash(reader.read_32_bytes()?),
73            index: reader.read_u32::<LittleEndian>()?,
74        })
75    }
76}
77
78// Coinbase inputs include block heights (BIP34). These are not encoded
79// directly, but as a Bitcoin script that pushes the block height to the stack
80// when executed. The script data is otherwise unused. Because we want to
81// *parse* transactions into an internal representation where illegal states are
82// unrepresentable, we need just enough parsing of Bitcoin scripts to parse the
83// coinbase height and split off the rest of the (inert) coinbase data.
84
85// Starting at Network Upgrade 5, coinbase transactions also encode the block
86// height in the expiry height field. But Zebra does not use this field to
87// determine the coinbase height, because it is not present in older network
88// upgrades.
89
90/// Split `data` into a block height and remaining miner-controlled coinbase data.
91///
92/// The height may consume `0..=5` bytes at the stat of the coinbase data.
93/// The genesis block does not include an encoded coinbase height.
94///
95/// # Consensus
96///
97/// > A coinbase transaction for a *block* at *block height* greater than 0 MUST have
98/// > a script that, as its first item, encodes the *block height* `height` as follows.
99/// > For `height` in the range {1..16}, the encoding is a single byte of value
100/// > `0x50` + `height`. Otherwise, let `heightBytes` be the signed little-endian
101/// > representation of `height`, using the minimum nonzero number of bytes such that
102/// > the most significant byte is < `0x80`.
103/// > The length of `heightBytes` MUST be in the range {1..5}.
104/// > Then the encoding is the length of `heightBytes` encoded as one byte,
105/// > followed by `heightBytes` itself. This matches the encoding used by Bitcoin in the
106/// > implementation of [BIP-34] (but the description here is to be considered normative).
107///
108/// <https://zips.z.cash/protocol/protocol.pdf#txnconsensus>
109/// <https://github.com/bitcoin/bips/blob/master/bip-0034.mediawiki>
110pub(crate) fn parse_coinbase_height(
111    mut data: Vec<u8>,
112) -> Result<(block::Height, CoinbaseData), SerializationError> {
113    match (data.first(), data.len()) {
114        // Blocks 1 through 16 inclusive encode block height with OP_N opcodes.
115        (Some(op_n @ 0x51..=0x60), len) if len >= 1 => Ok((
116            Height((op_n - 0x50) as u32),
117            CoinbaseData(data.split_off(1)),
118        )),
119        // Blocks 17 through 128 exclusive encode block height with the `0x01` opcode.
120        // The Bitcoin encoding requires that the most significant byte is below 0x80.
121        (Some(0x01), len) if len >= 2 && data[1] < 0x80 => {
122            let h = data[1] as u32;
123            if (17..128).contains(&h) {
124                Ok((Height(h), CoinbaseData(data.split_off(2))))
125            } else {
126                Err(SerializationError::Parse("Invalid block height"))
127            }
128        }
129        // Blocks 128 through 32768 exclusive encode block height with the `0x02` opcode.
130        // The Bitcoin encoding requires that the most significant byte is below 0x80.
131        (Some(0x02), len) if len >= 3 && data[2] < 0x80 => {
132            let h = data[1] as u32 + ((data[2] as u32) << 8);
133            if (128..32_768).contains(&h) {
134                Ok((Height(h), CoinbaseData(data.split_off(3))))
135            } else {
136                Err(SerializationError::Parse("Invalid block height"))
137            }
138        }
139        // Blocks 32768 through 2**23 exclusive encode block height with the `0x03` opcode.
140        // The Bitcoin encoding requires that the most significant byte is below 0x80.
141        (Some(0x03), len) if len >= 4 && data[3] < 0x80 => {
142            let h = data[1] as u32 + ((data[2] as u32) << 8) + ((data[3] as u32) << 16);
143            if (32_768..8_388_608).contains(&h) {
144                Ok((Height(h), CoinbaseData(data.split_off(4))))
145            } else {
146                Err(SerializationError::Parse("Invalid block height"))
147            }
148        }
149        // The genesis block does not encode the block height by mistake; special case it.
150        // The first five bytes are [4, 255, 255, 7, 31], the little-endian encoding of
151        // 520_617_983.
152        //
153        // In the far future, Zcash might reach this height, and the miner might use the
154        // same coinbase data as the genesis block. So we need an updated consensus rule
155        // to handle this edge case.
156        //
157        // TODO: update this check based on the consensus rule changes in
158        //       https://github.com/zcash/zips/issues/540
159        (Some(0x04), _) if data[..] == GENESIS_COINBASE_DATA[..] => {
160            Ok((Height(0), CoinbaseData(data)))
161        }
162        // As noted above, this is included for completeness.
163        // The Bitcoin encoding requires that the most significant byte is below 0x80.
164        (Some(0x04), len) if len >= 5 && data[4] < 0x80 => {
165            let h = data[1] as u32
166                + ((data[2] as u32) << 8)
167                + ((data[3] as u32) << 16)
168                + ((data[4] as u32) << 24);
169            if (8_388_608..=Height::MAX.0).contains(&h) {
170                Ok((Height(h), CoinbaseData(data.split_off(5))))
171            } else {
172                Err(SerializationError::Parse("Invalid block height"))
173            }
174        }
175        _ => Err(SerializationError::Parse(
176            "Could not parse BIP34 height in coinbase data",
177        )),
178    }
179}
180
181/// Encode `height` into a block height, as a prefix of the coinbase data.
182/// Does not write `coinbase_data`.
183///
184/// The height may produce `0..=5` initial bytes of coinbase data.
185///
186/// # Errors
187///
188/// Returns an error if the coinbase height is zero,
189/// and the `coinbase_data` does not match the Zcash mainnet and testnet genesis coinbase data.
190/// (They are identical.)
191///
192/// This check is required, because the genesis block does not include an encoded
193/// coinbase height,
194pub(crate) fn write_coinbase_height<W: io::Write>(
195    height: block::Height,
196    coinbase_data: &CoinbaseData,
197    mut w: W,
198) -> Result<(), io::Error> {
199    // We can't write this as a match statement on stable until exclusive range
200    // guards are stabilized.
201    // The Bitcoin encoding requires that the most significant byte is below 0x80,
202    // so the ranges run up to 2^{n-1} rather than 2^n.
203    if let 0 = height.0 {
204        // The genesis block's coinbase data does not have a height prefix.
205        // So we return an error if the entire coinbase data doesn't match genesis.
206        // (If we don't do this check, then deserialization will fail.)
207        //
208        // TODO: update this check based on the consensus rule changes in
209        //       https://github.com/zcash/zips/issues/540
210        if coinbase_data.0 != GENESIS_COINBASE_DATA {
211            return Err(io::Error::other("invalid genesis coinbase data"));
212        }
213    } else if let h @ 1..=16 = height.0 {
214        w.write_u8(0x50 + (h as u8))?;
215    } else if let h @ 17..=127 = height.0 {
216        w.write_u8(0x01)?;
217        w.write_u8(h as u8)?;
218    } else if let h @ 128..=32_767 = height.0 {
219        w.write_u8(0x02)?;
220        w.write_u16::<LittleEndian>(h as u16)?;
221    } else if let h @ 32_768..=8_388_607 = height.0 {
222        w.write_u8(0x03)?;
223        w.write_u8(h as u8)?;
224        w.write_u8((h >> 8) as u8)?;
225        w.write_u8((h >> 16) as u8)?;
226    } else if let h @ 8_388_608..=block::Height::MAX_AS_U32 = height.0 {
227        w.write_u8(0x04)?;
228        w.write_u32::<LittleEndian>(h)?;
229    } else {
230        panic!("Invalid coinbase height");
231    }
232    Ok(())
233}
234
235impl Height {
236    /// Get the size of `Height` when serialized into a coinbase input script.
237    pub fn coinbase_zcash_serialized_size(&self) -> usize {
238        let mut writer = FakeWriter(0);
239        let empty_data = CoinbaseData(Vec::new());
240
241        write_coinbase_height(*self, &empty_data, &mut writer).expect("writer should never fail");
242        writer.0
243    }
244}
245
246impl ZcashSerialize for Input {
247    /// Serialize this transparent input.
248    ///
249    /// # Errors
250    ///
251    /// Returns an error if the coinbase height is zero,
252    /// and the coinbase data does not match the Zcash mainnet and testnet genesis coinbase data.
253    /// (They are identical.)
254    ///
255    /// This check is required, because the genesis block does not include an encoded
256    /// coinbase height,
257    fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
258        match self {
259            Input::PrevOut {
260                outpoint,
261                unlock_script,
262                sequence,
263            } => {
264                outpoint.zcash_serialize(&mut writer)?;
265                unlock_script.zcash_serialize(&mut writer)?;
266                writer.write_u32::<LittleEndian>(*sequence)?;
267            }
268            Input::Coinbase {
269                height,
270                data,
271                sequence,
272            } => {
273                writer.write_all(&[0; 32][..])?;
274                writer.write_u32::<LittleEndian>(0xffff_ffff)?;
275
276                let mut height_and_data = Vec::new();
277                write_coinbase_height(*height, data, &mut height_and_data)?;
278                height_and_data.extend(&data.0);
279                zcash_serialize_bytes(&height_and_data, &mut writer)?;
280
281                writer.write_u32::<LittleEndian>(*sequence)?;
282            }
283        }
284        Ok(())
285    }
286}
287
288impl ZcashDeserialize for Input {
289    fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
290        // This inlines the OutPoint deserialization to peek at the hash value
291        // and detect whether we have a coinbase input.
292        let bytes = reader.read_32_bytes()?;
293        if bytes == [0; 32] {
294            if reader.read_u32::<LittleEndian>()? != 0xffff_ffff {
295                return Err(SerializationError::Parse("wrong index in coinbase"));
296            }
297
298            let data: Vec<u8> = (&mut reader).zcash_deserialize_into()?;
299
300            // Check the coinbase data length.
301            if data.len() > MAX_COINBASE_DATA_LEN {
302                return Err(SerializationError::Parse("coinbase data is too long"));
303            } else if data.len() < MIN_COINBASE_DATA_LEN {
304                return Err(SerializationError::Parse("coinbase data is too short"));
305            }
306
307            let (height, data) = parse_coinbase_height(data)?;
308
309            let sequence = reader.read_u32::<LittleEndian>()?;
310
311            Ok(Input::Coinbase {
312                height,
313                data,
314                sequence,
315            })
316        } else {
317            Ok(Input::PrevOut {
318                outpoint: OutPoint {
319                    hash: transaction::Hash(bytes),
320                    index: reader.read_u32::<LittleEndian>()?,
321                },
322                unlock_script: Script::zcash_deserialize(&mut reader)?,
323                sequence: reader.read_u32::<LittleEndian>()?,
324            })
325        }
326    }
327}
328
329impl ZcashSerialize for Output {
330    fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
331        self.value.zcash_serialize(&mut writer)?;
332        self.lock_script.zcash_serialize(&mut writer)?;
333        Ok(())
334    }
335}
336
337impl ZcashDeserialize for Output {
338    fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
339        let reader = &mut reader;
340
341        Ok(Output {
342            value: reader.zcash_deserialize_into()?,
343            lock_script: Script::zcash_deserialize(reader)?,
344        })
345    }
346}