Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document the consensus rules for Section 3.6 #3338

Merged
merged 9 commits into from
Jan 24, 2022
8 changes: 8 additions & 0 deletions zebra-chain/src/sapling/shielded_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,14 @@ where
/// descriptions of the transaction, and the balancing value.
///
/// <https://zips.z.cash/protocol/protocol.pdf#saplingbalance>
///
/// # Consensus
///
/// > The Spend transfers and Action transfers of a transaction MUST be
/// > consistent with its vbalanceSapling value as specified in § 4.13
/// > ‘Balance and Binding Signature (Sapling)’ on p. 49.
///
/// <https://zips.z.cash/protocol/protocol.pdf#spendsandoutputs>
upbqdn marked this conversation as resolved.
Show resolved Hide resolved
pub fn binding_verification_key(&self) -> redjubjub::VerificationKeyBytes<Binding> {
let cv_old: ValueCommitment = self.spends().map(|spend| spend.cv.into()).sum();
let cv_new: ValueCommitment = self.outputs().map(|output| output.cv.into()).sum();
Expand Down
14 changes: 14 additions & 0 deletions zebra-chain/src/sapling/spend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,20 @@ impl ZcashSerialize for Spend<PerSpendAnchor> {
}

impl ZcashDeserialize for Spend<PerSpendAnchor> {
/// # Consensus
///
/// > The anchor of each Spend description MUST refer to some earlier
/// > block’s final Sapling treestate. The anchor is encoded separately in
/// > each Spend description for v4 transactions, or encoded once and shared
/// > between all Spend descriptions in a v5 transaction.
///
/// <https://zips.z.cash/protocol/protocol.pdf#spendsandoutputs>
///
/// This rule is also implemented in
/// [`zebra_state::service::check::anchor`] and
/// [`zebra_chain::transaction::serialize`].
///
/// The "anchor encoding for v4 transactions" is implemented here.
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Spend {
cv: commitment::NotSmallOrderValueCommitment::zcash_deserialize(&mut reader)?,
Expand Down
15 changes: 15 additions & 0 deletions zebra-chain/src/transaction/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,21 @@ impl ZcashDeserialize for Option<sapling::ShieldedData<SharedAnchor>> {
.collect();

// Create transfers
//
// # Consensus
//
// > The anchor of each Spend description MUST refer to some earlier
// > block’s final Sapling treestate. The anchor is encoded separately
// > in each Spend description for v4 transactions, or encoded once and
// > shared between all Spend descriptions in a v5 transaction.
//
// <https://zips.z.cash/protocol/protocol.pdf#spendsandoutputs>
//
// This rule is also implemented in
// [`zebra_state::service::check::anchor`] and
// [`zebra_chain::sapling::spend`].
//
// The "anchor encoding for v5 transactions" is implemented here.
let transfers = match shared_anchor {
Some(shared_anchor) => sapling::TransferData::SpendsAndMaybeOutputs {
shared_anchor,
Expand Down