Skip to content

Commit

Permalink
add shielded_coinbase()
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Oct 20, 2020
1 parent 8def574 commit da6e96f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
23 changes: 23 additions & 0 deletions zebra-chain/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,27 @@ impl Transaction {
) -> blake2b_simd::Hash {
sighash::SigHasher::new(self, hash_type, network_upgrade, input).sighash()
}

/// Access the joinsplits of this transaction, regardless of version.
// Todo: signature haves to be like:
// pub fn joinsplits<P: ZkSnarkProof>(&self) -> Option<JoinSplitData<P>> {
// but i was not able to make that work yet
pub fn joinsplits(&self) -> bool {
match self {
Transaction::V1 { .. } => false,
Transaction::V2 { joinsplit_data, .. } => joinsplit_data.is_some(),
Transaction::V3 { joinsplit_data, .. } => joinsplit_data.is_some(),
Transaction::V4 { joinsplit_data, .. } => joinsplit_data.is_some(),
}
}
/// Access the shielded data of this transaction.
/// Only V4 transactions will return Some() if any shielded data is present.
pub fn shields(&self) -> Option<ShieldedData> {
match self {
Transaction::V1 { .. } => None,
Transaction::V2 { .. } => None,
Transaction::V3 { .. } => None,
Transaction::V4 { shielded_data, .. } => shielded_data.clone(),
}
}
}
6 changes: 6 additions & 0 deletions zebra-consensus/src/block/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ pub fn subsidy_is_valid(block: &Block, network: Network) -> Result<(), BlockErro
.activation_height(network)
.expect("Canopy activation height is known");

// Validate shielded coinbase
let shielded_validation = subsidy::general::shielded_coinbase(height, network, coinbase);
if !shielded_validation {
panic!("Failed validating shielded coinbase");
}

// TODO: the sum of the coinbase transaction outputs must be less than or equal to the block subsidy plus transaction fees

// Check founders reward and funding streams
Expand Down
20 changes: 20 additions & 0 deletions zebra-consensus/src/block/subsidy/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ pub fn find_output_with_amount(
.collect()
}

/// Validate shielded consensus rules as described in [ZIP-213][ZIP-213]
///
/// [ZIP-213]: https://zips.z.cash/zip-0213#specification
pub fn shielded_coinbase(height: Height, network: Network, transaction: &Transaction) -> bool {
let heartwood_height = Heartwood
.activation_height(network)
.expect("heartwood activation height should be available");

if height < heartwood_height {
if !transaction.joinsplits() && transaction.shields().is_none() {
return true;
}
} else {
if !transaction.joinsplits() {
return true;
}
}
return false;
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit da6e96f

Please sign in to comment.