Skip to content

Commit

Permalink
Removes issued_assets_change field from SemanticallyVerifiedBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 authored and dmidem committed Dec 5, 2024
1 parent c278758 commit d144774
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 84 deletions.
11 changes: 11 additions & 0 deletions zebra-chain/src/orchard_zsa/asset_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ impl std::ops::Add for IssuedAssetsChange {
}
}
}

impl From<Arc<[IssuedAssetsChange]>> for IssuedAssetsChange {
fn from(change: Arc<[IssuedAssetsChange]>) -> Self {
change
.iter()
.cloned()
.reduce(|a, b| a + b)
.unwrap_or_default()
}
}

/// Used in snapshot test for `getassetstate` RPC method.
// TODO: Replace with `AssetBase::random()` or a known value.
pub trait RandomAssetBase {
Expand Down
11 changes: 3 additions & 8 deletions zebra-consensus/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
};

use chrono::Utc;
use futures::stream::FuturesOrdered;
use futures::stream::FuturesUnordered;
use futures_util::FutureExt;
use thiserror::Error;
use tower::{Service, ServiceExt};
Expand Down Expand Up @@ -226,7 +226,7 @@ where
tx::check::coinbase_outputs_are_decryptable(&coinbase_tx, &network, height)?;

// Send transactions to the transaction verifier to be checked
let mut async_checks = FuturesOrdered::new();
let mut async_checks = FuturesUnordered::new();

let known_utxos = Arc::new(transparent::new_ordered_outputs(
&block,
Expand All @@ -243,7 +243,7 @@ where
height,
time: block.header.time,
});
async_checks.push_back(rsp);
async_checks.push(rsp);
}
tracing::trace!(len = async_checks.len(), "built async tx checks");

Expand All @@ -252,7 +252,6 @@ where
// Sum up some block totals from the transaction responses.
let mut legacy_sigop_count = 0;
let mut block_miner_fees = Ok(Amount::zero());
let mut issued_assets_changes = Vec::new();

use futures::StreamExt;
while let Some(result) = async_checks.next().await {
Expand All @@ -261,7 +260,6 @@ where
tx_id: _,
miner_fee,
legacy_sigop_count: tx_legacy_sigop_count,
issued_assets_change,
} = result
.map_err(Into::into)
.map_err(VerifyBlockError::Transaction)?
Expand All @@ -276,8 +274,6 @@ where
if let Some(miner_fee) = miner_fee {
block_miner_fees += miner_fee;
}

issued_assets_changes.push(issued_assets_change);
}

// Check the summed block totals
Expand Down Expand Up @@ -327,7 +323,6 @@ where
new_outputs,
transaction_hashes,
deferred_balance: Some(expected_deferred_amount),
issued_assets_changes: issued_assets_changes.into(),
};

// Return early for proposal requests when getblocktemplate-rpcs feature is enabled
Expand Down
5 changes: 2 additions & 3 deletions zebra-consensus/src/checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
Progress::{self, *},
TargetHeight::{self, *},
},
error::{BlockError, SubsidyError, TransactionError},
error::{BlockError, SubsidyError},
funding_stream_values, BoxError, ParameterCheckpoint as _,
};

Expand Down Expand Up @@ -619,8 +619,7 @@ where
};

// don't do precalculation until the block passes basic difficulty checks
let block = CheckpointVerifiedBlock::new(block, Some(hash), expected_deferred_amount)
.ok_or_else(|| VerifyBlockError::from(TransactionError::InvalidAssetIssuanceOrBurn))?;
let block = CheckpointVerifiedBlock::new(block, Some(hash), expected_deferred_amount);

crate::block::check::merkle_root_validity(
&self.network,
Expand Down
3 changes: 0 additions & 3 deletions zebra-consensus/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,6 @@ pub enum TransactionError {
#[error("failed to verify ZIP-317 transaction rules, transaction was not inserted to mempool")]
#[cfg_attr(any(test, feature = "proptest-impl"), proptest(skip))]
Zip317(#[from] zebra_chain::transaction::zip317::Error),

#[error("failed to validate asset issuance and/or burns")]
InvalidAssetIssuanceOrBurn,
}

impl From<ValidateContextError> for TransactionError {
Expand Down
6 changes: 0 additions & 6 deletions zebra-consensus/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use tracing::Instrument;
use zebra_chain::{
amount::{Amount, NonNegative},
block, orchard,
orchard_zsa::IssuedAssetsChange,
parameters::{Network, NetworkUpgrade},
primitives::Groth16Proof,
sapling,
Expand Down Expand Up @@ -144,10 +143,6 @@ pub enum Response {
/// The number of legacy signature operations in this transaction's
/// transparent inputs and outputs.
legacy_sigop_count: u64,

/// The changes to the issued assets map that should be applied for
/// this transaction.
issued_assets_change: IssuedAssetsChange,
},

/// A response to a mempool transaction verification request.
Expand Down Expand Up @@ -485,7 +480,6 @@ where
tx_id,
miner_fee,
legacy_sigop_count,
issued_assets_change: IssuedAssetsChange::from_transaction(&tx).ok_or(TransactionError::InvalidAssetIssuanceOrBurn)?,
},
Request::Mempool { transaction, .. } => {
let transaction = VerifiedUnminedTx::new(
Expand Down
5 changes: 0 additions & 5 deletions zebra-state/src/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::Arc;
use zebra_chain::{
amount::Amount,
block::{self, Block},
orchard_zsa::IssuedAssetsChange,
transaction::Transaction,
transparent,
value_balance::ValueBalance,
Expand All @@ -31,8 +30,6 @@ impl Prepare for Arc<Block> {
let transaction_hashes: Arc<[_]> = block.transactions.iter().map(|tx| tx.hash()).collect();
let new_outputs =
transparent::new_ordered_outputs_with_height(&block, height, &transaction_hashes);
let issued_assets_changes = IssuedAssetsChange::from_transactions(&block.transactions)
.expect("prepared blocks should be semantically valid");

SemanticallyVerifiedBlock {
block,
Expand All @@ -41,7 +38,6 @@ impl Prepare for Arc<Block> {
new_outputs,
transaction_hashes,
deferred_balance: None,
issued_assets_changes,
}
}
}
Expand Down Expand Up @@ -120,7 +116,6 @@ impl ContextuallyVerifiedBlock {
new_outputs,
transaction_hashes,
deferred_balance: _,
issued_assets_changes: _,
} = block.into();

Self {
Expand Down
42 changes: 7 additions & 35 deletions zebra-state/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,6 @@ pub struct SemanticallyVerifiedBlock {
pub transaction_hashes: Arc<[transaction::Hash]>,
/// This block's contribution to the deferred pool.
pub deferred_balance: Option<Amount<NonNegative>>,
/// A precomputed list of the [`IssuedAssetsChange`]s for the transactions in this block,
/// in the same order as `block.transactions`.
pub issued_assets_changes: Arc<[IssuedAssetsChange]>,
}

/// A block ready to be committed directly to the finalized state with
Expand Down Expand Up @@ -301,10 +298,9 @@ pub struct FinalizedBlock {
pub(super) treestate: Treestate,
/// This block's contribution to the deferred pool.
pub(super) deferred_balance: Option<Amount<NonNegative>>,
/// Either changes to be applied to the previous `issued_assets` map for the finalized tip, or
/// updates asset states to be inserted into the finalized state, replacing the previous
/// Updated asset states to be inserted into the finalized state, replacing the previous
/// asset states for those asset bases.
pub issued_assets: IssuedAssetsOrChange,
pub issued_assets: Option<IssuedAssets>,
}

/// Either changes to be applied to the previous `issued_assets` map for the finalized tip, or
Expand All @@ -319,18 +315,6 @@ pub enum IssuedAssetsOrChange {
Change(IssuedAssetsChange),
}

impl From<Arc<[IssuedAssetsChange]>> for IssuedAssetsOrChange {
fn from(change: Arc<[IssuedAssetsChange]>) -> Self {
Self::Change(
change
.iter()
.cloned()
.reduce(|a, b| a + b)
.unwrap_or_default(),
)
}
}

impl From<IssuedAssets> for IssuedAssetsOrChange {
fn from(updated_issued_assets: IssuedAssets) -> Self {
Self::Updated(updated_issued_assets)
Expand All @@ -340,21 +324,15 @@ impl From<IssuedAssets> for IssuedAssetsOrChange {
impl FinalizedBlock {
/// Constructs [`FinalizedBlock`] from [`CheckpointVerifiedBlock`] and its [`Treestate`].
pub fn from_checkpoint_verified(block: CheckpointVerifiedBlock, treestate: Treestate) -> Self {
let issued_assets = block.issued_assets_changes.clone().into();

Self::from_semantically_verified(
SemanticallyVerifiedBlock::from(block),
treestate,
issued_assets,
)
Self::from_semantically_verified(SemanticallyVerifiedBlock::from(block), treestate, None)
}

/// Constructs [`FinalizedBlock`] from [`ContextuallyVerifiedBlock`] and its [`Treestate`].
pub fn from_contextually_verified(
block: ContextuallyVerifiedBlock,
treestate: Treestate,
) -> Self {
let issued_assets = block.issued_assets.clone().into();
let issued_assets = Some(block.issued_assets.clone());
Self::from_semantically_verified(
SemanticallyVerifiedBlock::from(block),
treestate,
Expand All @@ -366,7 +344,7 @@ impl FinalizedBlock {
fn from_semantically_verified(
block: SemanticallyVerifiedBlock,
treestate: Treestate,
issued_assets: IssuedAssetsOrChange,
issued_assets: Option<IssuedAssets>,
) -> Self {
Self {
block: block.block,
Expand Down Expand Up @@ -451,7 +429,6 @@ impl ContextuallyVerifiedBlock {
new_outputs,
transaction_hashes,
deferred_balance,
issued_assets_changes: _,
} = semantically_verified;

// This is redundant for the non-finalized state,
Expand Down Expand Up @@ -483,12 +460,10 @@ impl CheckpointVerifiedBlock {
block: Arc<Block>,
hash: Option<block::Hash>,
deferred_balance: Option<Amount<NonNegative>>,
) -> Option<Self> {
let issued_assets_change = IssuedAssetsChange::from_transactions(&block.transactions)?;
) -> Self {
let mut block = Self::with_hash(block.clone(), hash.unwrap_or(block.hash()));
block.deferred_balance = deferred_balance;
block.issued_assets_changes = issued_assets_change;
Some(block)
block
}

/// Creates a block that's ready to be committed to the finalized state,
Expand Down Expand Up @@ -517,7 +492,6 @@ impl SemanticallyVerifiedBlock {
new_outputs,
transaction_hashes,
deferred_balance: None,
issued_assets_changes: Arc::new([]),
}
}

Expand Down Expand Up @@ -550,7 +524,6 @@ impl From<Arc<Block>> for SemanticallyVerifiedBlock {
new_outputs,
transaction_hashes,
deferred_balance: None,
issued_assets_changes: Arc::new([]),
}
}
}
Expand All @@ -570,7 +543,6 @@ impl From<ContextuallyVerifiedBlock> for SemanticallyVerifiedBlock {
.constrain::<NonNegative>()
.expect("deferred balance in a block must me non-negative"),
),
issued_assets_changes: Arc::new([]),
}
}
}
Expand Down
1 change: 0 additions & 1 deletion zebra-state/src/service/chain_tip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ impl From<SemanticallyVerifiedBlock> for ChainTipBlock {
new_outputs: _,
transaction_hashes,
deferred_balance: _,
issued_assets_changes: _,
} = prepared;

Self {
Expand Down
11 changes: 5 additions & 6 deletions zebra-state/src/service/check/issuance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{collections::HashMap, sync::Arc};

use zebra_chain::orchard_zsa::{AssetBase, AssetState, IssuedAssets};
use zebra_chain::orchard_zsa::{AssetBase, AssetState, IssuedAssets, IssuedAssetsChange};

use crate::{service::read, SemanticallyVerifiedBlock, ValidateContextError, ZebraDb};

Expand All @@ -17,11 +17,10 @@ pub fn valid_burns_and_issuance(

// Burns need to be checked and asset state changes need to be applied per tranaction, in case
// the asset being burned was also issued in an earlier transaction in the same block.
for (issued_assets_change, transaction) in semantically_verified
.issued_assets_changes
.iter()
.zip(&semantically_verified.block.transactions)
{
for transaction in &semantically_verified.block.transactions {
let issued_assets_change = IssuedAssetsChange::from_transaction(transaction)
.ok_or(ValidateContextError::InvalidIssuance)?;

// Check that no burn item attempts to burn more than the issued supply for an asset
for burn in transaction.orchard_burns() {
let asset_base = burn.asset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use zebra_chain::{
},
Block, Height,
},
orchard_zsa::IssuedAssetsChange,
parameters::Network::{self, *},
serialization::{ZcashDeserializeInto, ZcashSerialize},
transparent::new_ordered_outputs_with_height,
Expand Down Expand Up @@ -130,9 +129,6 @@ fn test_block_db_round_trip_with(
.collect();
let new_outputs =
new_ordered_outputs_with_height(&original_block, Height(0), &transaction_hashes);
let issued_assets_changes =
IssuedAssetsChange::from_transactions(&original_block.transactions)
.expect("issued assets should be valid");

CheckpointVerifiedBlock(SemanticallyVerifiedBlock {
block: original_block.clone(),
Expand All @@ -141,7 +137,6 @@ fn test_block_db_round_trip_with(
new_outputs,
transaction_hashes,
deferred_balance: None,
issued_assets_changes,
})
};

Expand Down
Loading

0 comments on commit d144774

Please sign in to comment.