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
//! Generate large transparent blocks and transactions for testing.

use chrono::DateTime;
use std::sync::Arc;

use crate::{
    block::{serialize::MAX_BLOCK_BYTES, Block, Header},
    serialization::{ZcashDeserialize, ZcashSerialize},
    transaction::{LockTime, Transaction},
    transparent,
};

/// The minimum size of the blocks produced by this module.
pub const MIN_LARGE_BLOCK_BYTES: u64 = MAX_BLOCK_BYTES - 100;

/// The maximum number of bytes used to serialize a CompactSize,
/// for the transaction, input, and output counts generated by this module.
pub const MAX_COMPACT_SIZE_BYTES: usize = 4;

/// The number of bytes used to serialize a version 1 transaction header.
pub const TX_V1_HEADER_BYTES: usize = 4;

/// Returns a generated block header, and its canonical serialized bytes.
pub fn block_header() -> (Header, Vec<u8>) {
    // Some of the test vectors are in a non-canonical format,
    // so we have to round-trip serialize them.

    let block_header = Header::zcash_deserialize(&zebra_test::vectors::DUMMY_HEADER[..]).unwrap();
    let block_header_bytes = block_header.zcash_serialize_to_vec().unwrap();

    (block_header, block_header_bytes)
}

/// Returns a generated transparent transaction, and its canonical serialized bytes.
pub fn transaction() -> (Transaction, Vec<u8>) {
    // Some of the test vectors are in a non-canonical format,
    // so we have to round-trip serialize them.

    let transaction = Transaction::zcash_deserialize(&zebra_test::vectors::DUMMY_TX1[..]).unwrap();
    let transaction_bytes = transaction.zcash_serialize_to_vec().unwrap();

    (transaction, transaction_bytes)
}

/// Returns a generated transparent input, and its canonical serialized bytes.
pub fn input() -> (transparent::Input, Vec<u8>) {
    // Some of the test vectors are in a non-canonical format,
    // so we have to round-trip serialize them.

    let input =
        transparent::Input::zcash_deserialize(&zebra_test::vectors::DUMMY_INPUT1[..]).unwrap();
    let input_bytes = input.zcash_serialize_to_vec().unwrap();

    (input, input_bytes)
}

/// Returns a generated transparent output, and its canonical serialized bytes.
pub fn output() -> (transparent::Output, Vec<u8>) {
    // Some of the test vectors are in a non-canonical format,
    // so we have to round-trip serialize them.

    let output =
        transparent::Output::zcash_deserialize(&zebra_test::vectors::DUMMY_OUTPUT1[..]).unwrap();
    let output_bytes = output.zcash_serialize_to_vec().unwrap();

    (output, output_bytes)
}

/// Generate a block with multiple transparent transactions just below limit
///
/// TODO: add a coinbase height to the returned block
pub fn large_multi_transaction_block() -> Block {
    multi_transaction_block(false)
}

/// Generate a block with one transaction and multiple transparent inputs just below limit
///
/// TODO: add a coinbase height to the returned block
///       make the returned block stable under round-trip serialization
pub fn large_single_transaction_block_many_inputs() -> Block {
    single_transaction_block_many_inputs(false)
}

/// Generate a block with one transaction and multiple transparent outputs just below limit
///
/// TODO: add a coinbase height to the returned block
///       make the returned block stable under round-trip serialization
pub fn large_single_transaction_block_many_outputs() -> Block {
    single_transaction_block_many_outputs(false)
}

/// Generate a block with multiple transparent transactions just above limit
///
/// TODO: add a coinbase height to the returned block
pub fn oversized_multi_transaction_block() -> Block {
    multi_transaction_block(true)
}

/// Generate a block with one transaction and multiple transparent inputs just above limit
///
/// TODO: add a coinbase height to the returned block
///       make the returned block stable under round-trip serialization
pub fn oversized_single_transaction_block_many_inputs() -> Block {
    single_transaction_block_many_inputs(true)
}

/// Generate a block with one transaction and multiple transparent outputs just above limit
///
/// TODO: add a coinbase height to the returned block
///       make the returned block stable under round-trip serialization
pub fn oversized_single_transaction_block_many_outputs() -> Block {
    single_transaction_block_many_outputs(true)
}

/// Implementation of block generation with multiple transparent transactions
fn multi_transaction_block(oversized: bool) -> Block {
    // A dummy transaction
    let (transaction, transaction_bytes) = transaction();

    // A block header
    let (block_header, block_header_bytes) = block_header();

    // Calculate the number of transactions we need,
    // subtracting the bytes used to serialize the expected transaction count.
    let mut max_transactions_in_block = (usize::try_from(MAX_BLOCK_BYTES).unwrap()
        - block_header_bytes.len()
        - MAX_COMPACT_SIZE_BYTES)
        / transaction_bytes.len();
    if oversized {
        max_transactions_in_block += 1;
    }

    // Create transactions to be just below or just above the limit
    let transactions = std::iter::repeat(Arc::new(transaction))
        .take(max_transactions_in_block)
        .collect::<Vec<_>>();

    // Add the transactions into a block
    let block = Block {
        header: block_header.into(),
        transactions,
    };

    let serialized_len = block.zcash_serialize_to_vec().unwrap().len();
    assert_eq!(
        oversized,
        serialized_len > MAX_BLOCK_BYTES.try_into().unwrap(),
        "block is over-sized if requested:\n\
         oversized: {oversized},\n\
         serialized_len: {serialized_len},\n\
         MAX_BLOCK_BYTES: {MAX_BLOCK_BYTES},",
    );
    assert!(
        serialized_len > MIN_LARGE_BLOCK_BYTES.try_into().unwrap(),
        "block is large\n\
         oversized: {oversized},\n\
         serialized_len: {serialized_len},\n\
         MIN_LARGE_BLOCK_BYTES: {MIN_LARGE_BLOCK_BYTES},",
    );

    block
}

/// Implementation of block generation with one transaction and multiple transparent inputs
fn single_transaction_block_many_inputs(oversized: bool) -> Block {
    // Dummy input and output
    let (input, input_bytes) = input();
    let (output, output_bytes) = output();

    // A block header
    let (block_header, block_header_bytes) = block_header();

    let lock_time = LockTime::Time(DateTime::from_timestamp(61, 0).unwrap());
    let lock_time_bytes = lock_time.zcash_serialize_to_vec().unwrap();

    // Calculate the number of inputs we need,
    // subtracting the bytes used to serialize the expected input count,
    // transaction count, and output count.
    let mut max_inputs_in_tx = (usize::try_from(MAX_BLOCK_BYTES).unwrap()
        - block_header_bytes.len()
        - 1
        - TX_V1_HEADER_BYTES
        - lock_time_bytes.len()
        - MAX_COMPACT_SIZE_BYTES
        - 1
        - output_bytes.len())
        / input_bytes.len();

    if oversized {
        max_inputs_in_tx += 1;
    }

    let mut outputs = Vec::new();

    // Create inputs to be just below the limit
    let inputs = std::iter::repeat(input)
        .take(max_inputs_in_tx)
        .collect::<Vec<_>>();

    // 1 single output
    outputs.push(output);

    // Create a big transaction
    let big_transaction = Transaction::V1 {
        inputs,
        outputs,
        lock_time,
    };

    // Put the big transaction into a block
    let transactions = vec![Arc::new(big_transaction)];

    let block = Block {
        header: block_header.into(),
        transactions,
    };

    let serialized_len = block.zcash_serialize_to_vec().unwrap().len();
    assert_eq!(
        oversized,
        serialized_len > MAX_BLOCK_BYTES.try_into().unwrap(),
        "block is over-sized if requested:\n\
         oversized: {oversized},\n\
         serialized_len: {serialized_len},\n\
         MAX_BLOCK_BYTES: {MAX_BLOCK_BYTES},",
    );
    assert!(
        serialized_len > MIN_LARGE_BLOCK_BYTES.try_into().unwrap(),
        "block is large\n\
         oversized: {oversized},\n\
         serialized_len: {serialized_len},\n\
         MIN_LARGE_BLOCK_BYTES: {MIN_LARGE_BLOCK_BYTES},",
    );

    block
}

/// Implementation of block generation with one transaction and multiple transparent outputs
fn single_transaction_block_many_outputs(oversized: bool) -> Block {
    // Dummy input and output
    let (input, input_bytes) = input();
    let (output, output_bytes) = output();

    // A block header
    let (block_header, block_header_bytes) = block_header();

    let lock_time = LockTime::Time(DateTime::from_timestamp(61, 0).unwrap());
    let lock_time_bytes = lock_time.zcash_serialize_to_vec().unwrap();

    // Calculate the number of outputs we need,
    // subtracting the bytes used to serialize the expected output count,
    // transaction count, and input count.
    let mut max_outputs_in_tx = (usize::try_from(MAX_BLOCK_BYTES).unwrap()
        - block_header_bytes.len()
        - 1
        - TX_V1_HEADER_BYTES
        - lock_time_bytes.len()
        - 1
        - input_bytes.len()
        - MAX_COMPACT_SIZE_BYTES)
        / output_bytes.len();

    if oversized {
        max_outputs_in_tx += 1;
    }

    // 1 single input
    let inputs = vec![input];

    // Create outputs to be just below the limit
    let outputs = std::iter::repeat(output)
        .take(max_outputs_in_tx)
        .collect::<Vec<_>>();

    // Create a big transaction
    let big_transaction = Transaction::V1 {
        inputs,
        outputs,
        lock_time,
    };

    // Put the big transaction into a block
    let transactions = vec![Arc::new(big_transaction)];

    let block = Block {
        header: block_header.into(),
        transactions,
    };

    let serialized_len = block.zcash_serialize_to_vec().unwrap().len();
    assert_eq!(
        oversized,
        serialized_len > MAX_BLOCK_BYTES.try_into().unwrap(),
        "block is over-sized if requested:\n\
         oversized: {oversized},\n\
         serialized_len: {serialized_len},\n\
         MAX_BLOCK_BYTES: {MAX_BLOCK_BYTES},",
    );
    assert!(
        serialized_len > MIN_LARGE_BLOCK_BYTES.try_into().unwrap(),
        "block is large\n\
         oversized: {oversized},\n\
         serialized_len: {serialized_len},\n\
         MIN_LARGE_BLOCK_BYTES: {MIN_LARGE_BLOCK_BYTES},",
    );

    block
}