-
Notifications
You must be signed in to change notification settings - Fork 105
/
vectors.rs
491 lines (398 loc) · 15.7 KB
/
vectors.rs
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
use std::{
collections::HashSet,
io::{Cursor, Write},
};
use chrono::{DateTime, Duration, LocalResult, TimeZone, Utc};
use crate::{
block::{
serialize::MAX_BLOCK_BYTES, Block, BlockTimeError, Commitment::*, Hash, Header, Height,
},
parameters::{
Network::{self, *},
NetworkUpgrade::*,
},
serialization::{
sha256d, SerializationError, ZcashDeserialize, ZcashDeserializeInto, ZcashSerialize,
},
transaction::LockTime,
};
use super::generate; // TODO: this should be rewritten as strategies
#[test]
fn blockheaderhash_debug() {
let _init_guard = zebra_test::init();
let preimage = b"foo bar baz";
let mut sha_writer = sha256d::Writer::default();
let _ = sha_writer.write_all(preimage);
let hash = Hash(sha_writer.finish());
assert_eq!(
format!("{hash:?}"),
"block::Hash(\"3166411bd5343e0b284a108f39a929fbbb62619784f8c6dafe520703b5b446bf\")"
);
}
#[test]
fn blockheaderhash_from_blockheader() {
let _init_guard = zebra_test::init();
let (blockheader, _blockheader_bytes) = generate::block_header();
let hash = Hash::from(&blockheader);
assert_eq!(
format!("{hash:?}"),
"block::Hash(\"d1d6974bbe1d4d127c889119b2fc05724c67588dc72708839727586b8c2bc939\")"
);
let mut bytes = Cursor::new(Vec::new());
blockheader
.zcash_serialize(&mut bytes)
.expect("these bytes to serialize from a blockheader without issue");
bytes.set_position(0);
let other_header = bytes
.zcash_deserialize_into()
.expect("these bytes to deserialize into a blockheader without issue");
assert_eq!(blockheader, other_header);
}
#[test]
fn blockheader_serialization() {
let _init_guard = zebra_test::init();
// Includes the 32-byte nonce and 3-byte equihash length field.
const BLOCK_HEADER_LENGTH: usize = crate::work::equihash::Solution::INPUT_LENGTH
+ 32
+ 3
+ crate::work::equihash::SOLUTION_SIZE;
for block in zebra_test::vectors::BLOCKS.iter() {
// successful deserialization
let header_bytes = &block[..BLOCK_HEADER_LENGTH];
let mut header = header_bytes
.zcash_deserialize_into::<Header>()
.expect("blockheader test vector should deserialize");
// successful serialization
let _serialized_header = header
.zcash_serialize_to_vec()
.expect("blockheader test vector should serialize");
// deserialiation errors
let header_bytes = [&[255; 4], &header_bytes[4..]].concat();
let deserialization_err = header_bytes
.zcash_deserialize_into::<Header>()
.expect_err("blockheader test vector should fail to deserialize");
let SerializationError::Parse(err_msg) = deserialization_err else {
panic!("SerializationError variant should be Parse")
};
assert_eq!(err_msg, "high bit was set in version field");
let header_bytes = [&[0; 4], &header_bytes[4..]].concat();
let deserialization_err = header_bytes
.zcash_deserialize_into::<Header>()
.expect_err("blockheader test vector should fail to deserialize");
let SerializationError::Parse(err_msg) = deserialization_err else {
panic!("SerializationError variant should be Parse")
};
assert_eq!(err_msg, "version must be at least 4");
// serialiation errors
header.version = u32::MAX;
let serialization_err = header
.zcash_serialize_to_vec()
.expect_err("blockheader test vector with modified version should fail to serialize");
assert_eq!(
serialization_err.kind(),
std::io::ErrorKind::Other,
"error kind should be Other"
);
let err_msg = serialization_err
.into_inner()
.expect("there should be an inner error");
assert_eq!(err_msg.to_string(), "high bit was set in version field");
}
}
#[test]
fn round_trip_blocks() {
let _init_guard = zebra_test::init();
// this one has a bad version field, but it is still valid
zebra_test::vectors::BLOCK_MAINNET_434873_BYTES
.zcash_deserialize_into::<Block>()
.expect("bad version block test vector should deserialize");
// now do a round-trip test on all the block test vectors
for block_bytes in zebra_test::vectors::BLOCKS.iter() {
let block = block_bytes
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid");
let round_trip_bytes = block
.zcash_serialize_to_vec()
.expect("vec serialization is infallible");
assert_eq!(&round_trip_bytes[..], *block_bytes);
}
}
#[test]
fn coinbase_parsing_rejects_above_0x80() {
let _init_guard = zebra_test::init();
zebra_test::vectors::BAD_BLOCK_MAINNET_202_BYTES
.zcash_deserialize_into::<Block>()
.expect_err("parsing fails");
}
#[test]
fn block_test_vectors_unique() {
let _init_guard = zebra_test::init();
let block_count = zebra_test::vectors::BLOCKS.len();
let block_hashes: HashSet<_> = zebra_test::vectors::BLOCKS
.iter()
.map(|b| {
b.zcash_deserialize_into::<Block>()
.expect("block is structurally valid")
.hash()
})
.collect();
// putting the same block in two files is an easy mistake to make
assert_eq!(
block_count,
block_hashes.len(),
"block test vectors must be unique"
);
}
#[test]
fn block_test_vectors_height_mainnet() {
let _init_guard = zebra_test::init();
block_test_vectors_height(Mainnet);
}
#[test]
fn block_test_vectors_height_testnet() {
let _init_guard = zebra_test::init();
block_test_vectors_height(Testnet);
}
/// Test that the block test vector indexes match the heights in the block data,
/// and that each post-sapling block has a corresponding final sapling root.
fn block_test_vectors_height(network: Network) {
let (block_iter, sapling_roots) = match network {
Mainnet => (
zebra_test::vectors::MAINNET_BLOCKS.iter(),
zebra_test::vectors::MAINNET_FINAL_SAPLING_ROOTS.clone(),
),
Testnet => (
zebra_test::vectors::TESTNET_BLOCKS.iter(),
zebra_test::vectors::TESTNET_FINAL_SAPLING_ROOTS.clone(),
),
};
for (&height, block) in block_iter {
let block = block
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid");
assert_eq!(
block.coinbase_height().expect("block height is valid").0,
height,
"deserialized height must match BTreeMap key height"
);
if height
>= Sapling
.activation_height(network)
.expect("sapling activation height is set")
.0
{
assert!(
sapling_roots.contains_key(&height),
"post-sapling block test vectors must have matching sapling root test vectors: missing {network} {height}"
);
}
}
}
#[test]
fn block_commitment_mainnet() {
let _init_guard = zebra_test::init();
block_commitment(Mainnet);
}
#[test]
fn block_commitment_testnet() {
let _init_guard = zebra_test::init();
block_commitment(Testnet);
}
/// Check that the block commitment field parses without errors.
/// For sapling and blossom blocks, also check the final sapling root value.
///
/// TODO: add chain history test vectors?
fn block_commitment(network: Network) {
let (block_iter, sapling_roots) = match network {
Mainnet => (
zebra_test::vectors::MAINNET_BLOCKS.iter(),
zebra_test::vectors::MAINNET_FINAL_SAPLING_ROOTS.clone(),
),
Testnet => (
zebra_test::vectors::TESTNET_BLOCKS.iter(),
zebra_test::vectors::TESTNET_FINAL_SAPLING_ROOTS.clone(),
),
};
for (height, block) in block_iter {
let block = block
.zcash_deserialize_into::<Block>()
.expect("block is structurally valid");
let commitment = block.commitment(network).unwrap_or_else(|_| {
panic!("unexpected structurally invalid block commitment at {network} {height}")
});
if let FinalSaplingRoot(final_sapling_root) = commitment {
let expected_final_sapling_root = *sapling_roots
.get(height)
.expect("unexpected missing final sapling root test vector");
assert_eq!(
final_sapling_root,
crate::sapling::tree::Root::try_from(*expected_final_sapling_root).unwrap(),
"unexpected invalid final sapling root commitment at {network} {height}"
);
}
}
}
#[test]
fn block_limits_multi_tx() {
let _init_guard = zebra_test::init();
// Test multiple small transactions to fill a block max size
// Create a block just below the limit
let mut block = generate::large_multi_transaction_block();
// Serialize the block
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() <= MAX_BLOCK_BYTES as usize);
// Deserialize by now is ok as we are lower than the limit
let block2 = Block::zcash_deserialize(&data[..])
.expect("block should deserialize as we are just below limit");
assert_eq!(block, block2);
// Add 1 more transaction to the block, limit will be reached
block = generate::oversized_multi_transaction_block();
// Serialize will still be fine
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() > MAX_BLOCK_BYTES as usize);
// Deserialize will now fail
Block::zcash_deserialize(&data[..]).expect_err("block should not deserialize");
}
#[test]
fn block_limits_single_tx() {
let _init_guard = zebra_test::init();
// Test block limit with a big single transaction
// Create a block just below the limit
let mut block = generate::large_single_transaction_block_many_inputs();
// Serialize the block
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() <= MAX_BLOCK_BYTES as usize);
// Deserialize by now is ok as we are lower than the limit
Block::zcash_deserialize(&data[..])
.expect("block should deserialize as we are just below limit");
// Add 1 more input to the transaction, limit will be reached
block = generate::oversized_single_transaction_block_many_inputs();
let mut data = Vec::new();
block
.zcash_serialize(&mut data)
.expect("block should serialize as we are not limiting generation yet");
assert!(data.len() > MAX_BLOCK_BYTES as usize);
// Will fail as block overall size is above limit
Block::zcash_deserialize(&data[..]).expect_err("block should not deserialize");
}
/// Test wrapper for `BlockHeader.time_is_valid_at`.
///
/// Generates a block header, sets its `time` to `block_header_time`, then
/// calls `time_is_valid_at`.
fn node_time_check(
block_header_time: DateTime<Utc>,
now: DateTime<Utc>,
) -> Result<(), BlockTimeError> {
let (mut header, _header_bytes) = generate::block_header();
header.time = block_header_time;
// pass a zero height and hash - they are only used in the returned error
header.time_is_valid_at(now, &Height(0), &Hash([0; 32]))
}
#[test]
fn time_check_now() {
let _init_guard = zebra_test::init();
// These checks are deteministic, because all the times are offset
// from the current time.
let now = Utc::now();
let three_hours_in_the_past = now - Duration::hours(3);
let two_hours_in_the_future = now + Duration::hours(2);
let two_hours_and_one_second_in_the_future = now + Duration::hours(2) + Duration::seconds(1);
node_time_check(now, now).expect("the current time should be valid as a block header time");
node_time_check(three_hours_in_the_past, now)
.expect("a past time should be valid as a block header time");
node_time_check(two_hours_in_the_future, now)
.expect("2 hours in the future should be valid as a block header time");
node_time_check(two_hours_and_one_second_in_the_future, now)
.expect_err("2 hours and 1 second in the future should be invalid as a block header time");
// Now invert the tests
// 3 hours in the future should fail
node_time_check(now, three_hours_in_the_past)
.expect_err("3 hours in the future should be invalid as a block header time");
// The past should succeed
node_time_check(now, two_hours_in_the_future)
.expect("2 hours in the past should be valid as a block header time");
node_time_check(now, two_hours_and_one_second_in_the_future)
.expect("2 hours and 1 second in the past should be valid as a block header time");
}
/// Valid unix epoch timestamps for blocks, in seconds
static BLOCK_HEADER_VALID_TIMESTAMPS: &[i64] = &[
// These times are currently invalid DateTimes, but they could
// become valid in future chrono versions
i64::MIN,
i64::MIN + 1,
// These times are valid DateTimes
(i32::MIN as i64) - 1,
(i32::MIN as i64),
(i32::MIN as i64) + 1,
-1,
0,
1,
LockTime::MIN_TIMESTAMP - 1,
LockTime::MIN_TIMESTAMP,
LockTime::MIN_TIMESTAMP + 1,
];
/// Invalid unix epoch timestamps for blocks, in seconds
static BLOCK_HEADER_INVALID_TIMESTAMPS: &[i64] = &[
(i32::MAX as i64) - 1,
(i32::MAX as i64),
(i32::MAX as i64) + 1,
LockTime::MAX_TIMESTAMP - 1,
LockTime::MAX_TIMESTAMP,
LockTime::MAX_TIMESTAMP + 1,
// These times are currently invalid DateTimes, but they could
// become valid in future chrono versions
i64::MAX - 1,
i64::MAX,
];
#[test]
fn time_check_fixed() {
let _init_guard = zebra_test::init();
// These checks are non-deterministic, but the times are all in the
// distant past or far future. So it's unlikely that the test
// machine will have a clock that makes these tests fail.
let now = Utc::now();
for valid_timestamp in BLOCK_HEADER_VALID_TIMESTAMPS {
let block_header_time = match Utc.timestamp_opt(*valid_timestamp, 0) {
LocalResult::Single(time) => time,
LocalResult::None => {
// Skip the test if the timestamp is invalid
continue;
}
LocalResult::Ambiguous(_, _) => {
// Utc doesn't have ambiguous times
unreachable!();
}
};
node_time_check(block_header_time, now)
.expect("the time should be valid as a block header time");
// Invert the check, leading to an invalid time
node_time_check(now, block_header_time)
.expect_err("the inverse comparison should be invalid");
}
for invalid_timestamp in BLOCK_HEADER_INVALID_TIMESTAMPS {
let block_header_time = match Utc.timestamp_opt(*invalid_timestamp, 0) {
LocalResult::Single(time) => time,
LocalResult::None => {
// Skip the test if the timestamp is invalid
continue;
}
LocalResult::Ambiguous(_, _) => {
// Utc doesn't have ambiguous times
unreachable!();
}
};
node_time_check(block_header_time, now)
.expect_err("the time should be invalid as a block header time");
// Invert the check, leading to a valid time
node_time_check(now, block_header_time).expect("the inverse comparison should be valid");
}
}