Skip to content

Commit 0b6deef

Browse files
committed
zebra-test: refactor orchard_zsa_workflow_blocks.rs to use a u8 static ref for bytes OrchardWorkflowBlock instead of a vec, matching the approach used in block.rs there
1 parent e33aed7 commit 0b6deef

File tree

4 files changed

+71
-51
lines changed

4 files changed

+71
-51
lines changed

zebra-consensus/src/orchard_zsa/tests.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use orchard::{
3131
};
3232

3333
use zebra_chain::{
34-
block::{genesis::regtest_genesis_block, Block, Hash},
34+
block::{Block, Hash},
3535
parameters::{testnet::ConfiguredActivationHeights, Network},
3636
serialization::ZcashDeserialize,
3737
};
@@ -179,30 +179,24 @@ fn build_asset_records<'a, I: IntoIterator<Item = &'a TranscriptItem>>(
179179
fn create_transcript_data<'a, I: IntoIterator<Item = &'a OrchardWorkflowBlock>>(
180180
serialized_blocks: I,
181181
) -> impl Iterator<Item = TranscriptItem> + use<'a, I> {
182-
let workflow_blocks =
183-
serialized_blocks
184-
.into_iter()
185-
.map(|OrchardWorkflowBlock { bytes, is_valid }| {
186-
(
187-
Arc::new(
188-
Block::zcash_deserialize(&bytes[..]).expect("block should deserialize"),
189-
),
190-
*is_valid,
191-
)
192-
});
193-
194-
std::iter::once((regtest_genesis_block(), true))
195-
.chain(workflow_blocks)
196-
.map(|(block, is_valid)| {
182+
serialized_blocks.into_iter().map(
183+
|OrchardWorkflowBlock {
184+
height: _,
185+
bytes,
186+
is_valid,
187+
}| {
188+
let block =
189+
Arc::new(Block::zcash_deserialize(&bytes[..]).expect("block should deserialize"));
197190
(
198191
Request::Commit(block.clone()),
199-
if is_valid {
192+
if *is_valid {
200193
Ok(block.hash())
201194
} else {
202195
Err(ExpectedTranscriptError::Any)
203196
},
204197
)
205-
})
198+
},
199+
)
206200
}
207201

208202
/// Queries the state service for the asset state of the given asset.

zebra-state/src/service/check/tests/issuance.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::sync::Arc;
22

33
use zebra_chain::{
4-
block::{self, genesis::regtest_genesis_block, Block},
4+
block::{self, Block},
55
orchard_zsa::{AssetBase, IssuedAssetChanges},
66
parameters::Network,
77
serialization::ZcashDeserialize,
@@ -15,15 +15,6 @@ use crate::{
1515
CheckpointVerifiedBlock, Config, NonFinalizedState,
1616
};
1717

18-
fn valid_issuance_blocks() -> Vec<Arc<Block>> {
19-
ORCHARD_ZSA_WORKFLOW_BLOCKS
20-
.iter()
21-
.map(|OrchardWorkflowBlock { bytes, .. }| {
22-
Arc::new(Block::zcash_deserialize(&bytes[..]).expect("block should deserialize"))
23-
})
24-
.collect()
25-
}
26-
2718
#[test]
2819
fn check_burns_and_issuance() {
2920
let _init_guard = zebra_test::init();
@@ -41,23 +32,26 @@ fn check_burns_and_issuance() {
4132

4233
let mut non_finalized_state = NonFinalizedState::new(&network);
4334

44-
let regtest_genesis_block = regtest_genesis_block();
45-
let regtest_genesis_hash = regtest_genesis_block.hash();
35+
let mut block_iter =
36+
ORCHARD_ZSA_WORKFLOW_BLOCKS
37+
.iter()
38+
.map(|OrchardWorkflowBlock { bytes, .. }| {
39+
Arc::new(Block::zcash_deserialize(&bytes[..]).expect("block should deserialize"))
40+
});
41+
42+
let genesis_block = block_iter.next().expect("genesis block must exist").clone();
4643

4744
finalized_state
48-
.commit_finalized_direct(regtest_genesis_block.into(), None, "test")
45+
.commit_finalized_direct(genesis_block.into(), None, "test")
4946
.expect("unexpected invalid genesis block test vector");
5047

51-
let block = valid_issuance_blocks().first().unwrap().clone();
52-
let mut header = Arc::<block::Header>::unwrap_or_clone(block.header.clone());
53-
header.previous_block_hash = regtest_genesis_hash;
54-
header.commitment_bytes = [0; 32].into();
55-
let block = Arc::new(Block {
56-
header: Arc::new(header),
57-
transactions: block.transactions.clone(),
58-
});
48+
let block_1 = {
49+
let mut block = (*block_iter.next().expect("block 1 must exist")).clone();
50+
Arc::make_mut(&mut block.header).commitment_bytes = [0; 32].into();
51+
Arc::new(block)
52+
};
5953

60-
let CheckpointVerifiedBlock(block) = CheckpointVerifiedBlock::new(block, None, None);
54+
let CheckpointVerifiedBlock(block_1) = CheckpointVerifiedBlock::new(block_1, None, None);
6155

6256
let empty_chain = Chain::new(
6357
&network,
@@ -73,7 +67,7 @@ fn check_burns_and_issuance() {
7367
);
7468

7569
let block_1_issued_assets = IssuedAssetChanges::validate_and_get_changes(
76-
&block.block.transactions,
70+
&block_1.block.transactions,
7771
None,
7872
|asset_base: &AssetBase| {
7973
read::asset_state(
@@ -85,7 +79,7 @@ fn check_burns_and_issuance() {
8579
)
8680
.expect("test transactions should be valid");
8781

88-
validate_and_commit_non_finalized(&finalized_state.db, &mut non_finalized_state, block)
82+
validate_and_commit_non_finalized(&finalized_state.db, &mut non_finalized_state, block_1)
8983
.expect("validation should succeed");
9084

9185
let best_chain = non_finalized_state
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
040000000000000000000000000000000000000000000000000000000000000000000000db4d7a85b768123f1dff1d4c4cece70083b2d27e117b4ac2e31d087988a5eac40000000000000000000000000000000000000000000000000000000000000000dae5494d0f0f0f2009000000000000000000000000000000000000000000000000000000000000002401936b7db1eb4ac39f151b8704642d0a8bda13ec547d54cd5e43ba142fc6d8877cab07b30101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff071f0104455a6361736830623963346565663862376363343137656535303031653335303039383462366665613335363833613763616331343161303433633432303634383335643334ffffffff010000000000000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000

zebra-test/src/vectors/orchard_zsa_workflow_blocks.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,79 @@ use lazy_static::lazy_static;
77

88
/// Represents a serialized block and its validity status.
99
pub struct OrchardWorkflowBlock {
10+
/// Block height.
11+
pub height: u32,
1012
/// Serialized byte data of the block.
11-
pub bytes: Vec<u8>,
13+
pub bytes: &'static [u8],
1214
/// Indicates whether the block is valid.
1315
pub is_valid: bool,
1416
}
1517

1618
fn decode_bytes(hex: &str) -> Vec<u8> {
17-
<Vec<u8>>::from_hex((hex).trim()).expect("Block bytes are in valid hex representation")
19+
<Vec<u8>>::from_hex(hex.trim()).expect("Block bytes are in valid hex representation")
1820
}
1921

2022
lazy_static! {
23+
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCK_0_BYTES: Vec<u8> =
24+
decode_bytes(include_str!("orchard-zsa-workflow-block-0-genesis.txt"));
25+
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCK_1_BYTES: Vec<u8> =
26+
decode_bytes(include_str!("orchard-zsa-workflow-block-1.txt"));
27+
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCK_2_BYTES: Vec<u8> =
28+
decode_bytes(include_str!("orchard-zsa-workflow-block-2.txt"));
29+
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCK_3_BYTES: Vec<u8> =
30+
decode_bytes(include_str!("orchard-zsa-workflow-block-3.txt"));
31+
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCK_4_BYTES: Vec<u8> =
32+
decode_bytes(include_str!("orchard-zsa-workflow-block-4.txt"));
33+
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCK_5_BYTES: Vec<u8> =
34+
decode_bytes(include_str!("orchard-zsa-workflow-block-5.txt"));
35+
2136
/// Test blocks for a Zcash Shielded Assets (ZSA) workflow.
22-
/// The sequence demonstrates issuing, transferring and burning a custom
23-
/// asset, then finalising the issuance and attempting an extra issue.
37+
/// The sequence demonstrates issuing, transferring, and burning a custom
38+
/// asset, then finalizing the issuance and attempting an extra issue.
39+
///
40+
/// Block 0 is the Regtest genesis block, copied from
41+
/// `zebra-chain/src/block/genesis/block-regtest-0-000-000.txt`.
42+
/// The remaining workflow blocks were generated using `zcash_tx_tool`
2443
pub static ref ORCHARD_ZSA_WORKFLOW_BLOCKS: Vec<OrchardWorkflowBlock> = vec![
44+
// Genesis
45+
OrchardWorkflowBlock {
46+
height: 0,
47+
bytes: ORCHARD_ZSA_WORKFLOW_BLOCK_0_BYTES.as_slice(),
48+
is_valid: true
49+
},
50+
2551
// Issue: 1000
2652
OrchardWorkflowBlock {
27-
bytes: decode_bytes(include_str!("orchard-zsa-workflow-block-1.txt")),
53+
height: 1,
54+
bytes: ORCHARD_ZSA_WORKFLOW_BLOCK_1_BYTES.as_slice(),
2855
is_valid: true
2956
},
3057

3158
// Transfer
3259
OrchardWorkflowBlock {
33-
bytes: decode_bytes(include_str!("orchard-zsa-workflow-block-2.txt")),
60+
height: 2,
61+
bytes: ORCHARD_ZSA_WORKFLOW_BLOCK_2_BYTES.as_slice(),
3462
is_valid: true
3563
},
3664

3765
// Burn: 7, Burn: 2
3866
OrchardWorkflowBlock {
39-
bytes: decode_bytes(include_str!("orchard-zsa-workflow-block-3.txt")),
67+
height: 3,
68+
bytes: ORCHARD_ZSA_WORKFLOW_BLOCK_3_BYTES.as_slice(),
4069
is_valid: true
4170
},
4271

4372
// Issue: finalize
4473
OrchardWorkflowBlock {
45-
bytes: decode_bytes(include_str!("orchard-zsa-workflow-block-4.txt")),
74+
height: 4,
75+
bytes: ORCHARD_ZSA_WORKFLOW_BLOCK_4_BYTES.as_slice(),
4676
is_valid: true
4777
},
4878

4979
// Try to issue: 2000
5080
OrchardWorkflowBlock {
51-
bytes: decode_bytes(include_str!("orchard-zsa-workflow-block-5.txt")),
81+
height: 5,
82+
bytes: ORCHARD_ZSA_WORKFLOW_BLOCK_5_BYTES.as_slice(),
5283
is_valid: false
5384
},
5485
];

0 commit comments

Comments
 (0)