Skip to content

Commit

Permalink
Merge of #7019
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Jul 18, 2023
2 parents 33a3483 + 5f41852 commit 573695f
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 4 deletions.
50 changes: 49 additions & 1 deletion zebra-chain/src/transaction/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use crate::{

use itertools::Itertools;

use super::{FieldNotPresent, JoinSplitData, LockTime, Memo, Transaction, UnminedTx};
use super::{
FieldNotPresent, JoinSplitData, LockTime, Memo, Transaction, UnminedTx, VerifiedUnminedTx,
};

/// The maximum number of arbitrary transactions, inputs, or outputs.
///
Expand Down Expand Up @@ -783,6 +785,52 @@ impl Arbitrary for UnminedTx {
type Strategy = BoxedStrategy<Self>;
}

impl Arbitrary for VerifiedUnminedTx {
type Parameters = ();

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
(
any::<UnminedTx>(),
any::<Amount<NonNegative>>(),
any::<u64>(),
any::<(u16, u16)>().prop_map(|(unpaid_actions, conventional_actions)| {
(
unpaid_actions % conventional_actions.saturating_add(1),
conventional_actions,
)
}),
any::<f32>(),
)
.prop_map(
|(
transaction,
miner_fee,
legacy_sigop_count,
(conventional_actions, mut unpaid_actions),
fee_weight_ratio,
)| {
if unpaid_actions > conventional_actions {
unpaid_actions = conventional_actions;
}

let conventional_actions = conventional_actions as u32;
let unpaid_actions = unpaid_actions as u32;

Self {
transaction,
miner_fee,
legacy_sigop_count,
conventional_actions,
unpaid_actions,
fee_weight_ratio,
}
},
)
.boxed()
}
type Strategy = BoxedStrategy<Self>;
}

// Utility functions

/// Convert `trans` into a fake v5 transaction,
Expand Down
10 changes: 9 additions & 1 deletion zebra-chain/src/transaction/unmined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ impl From<&Arc<Transaction>> for UnminedTx {
//
// This struct can't be `Eq`, because it contains a `f32`.
#[derive(Clone, PartialEq)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct VerifiedUnminedTx {
/// The unmined transaction.
pub transaction: UnminedTx,
Expand All @@ -337,6 +336,13 @@ pub struct VerifiedUnminedTx {
/// transparent inputs and outputs.
pub legacy_sigop_count: u64,

/// The number of conventional actions for `transaction`, as defined by [ZIP-317].
///
/// The number of actions is limited by [`MAX_BLOCK_BYTES`], so it fits in a u32.
///
/// [ZIP-317]: https://zips.z.cash/zip-0317#block-production
pub conventional_actions: u32,

/// The number of unpaid actions for `transaction`,
/// as defined by [ZIP-317] for block production.
///
Expand Down Expand Up @@ -381,6 +387,7 @@ impl VerifiedUnminedTx {
legacy_sigop_count: u64,
) -> Result<Self, zip317::Error> {
let fee_weight_ratio = zip317::conventional_fee_weight_ratio(&transaction, miner_fee);
let conventional_actions = zip317::conventional_actions(&transaction.transaction);
let unpaid_actions = zip317::unpaid_actions(&transaction, miner_fee);

zip317::mempool_checks(unpaid_actions, miner_fee, transaction.size)?;
Expand All @@ -390,6 +397,7 @@ impl VerifiedUnminedTx {
miner_fee,
legacy_sigop_count,
fee_weight_ratio,
conventional_actions,
unpaid_actions,
})
}
Expand Down
2 changes: 1 addition & 1 deletion zebra-chain/src/transaction/unmined/zip317.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub fn conventional_fee_weight_ratio(
/// as defined by [ZIP-317].
///
/// [ZIP-317]: https://zips.z.cash/zip-0317#fee-calculation
fn conventional_actions(transaction: &Transaction) -> u32 {
pub fn conventional_actions(transaction: &Transaction) -> u32 {
let tx_in_total_size: usize = transaction
.inputs()
.iter()
Expand Down
5 changes: 4 additions & 1 deletion zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
block::{Hash, MAX_BLOCK_BYTES, ZCASH_BLOCK_VERSION},
chain_sync_status::MockSyncStatus,
serialization::DateTime32,
transaction::VerifiedUnminedTx,
transaction::{zip317, VerifiedUnminedTx},
work::difficulty::{CompactDifficulty, ExpandedDifficulty, U256},
};
use zebra_consensus::MAX_BLOCK_SIGOPS;
Expand Down Expand Up @@ -1441,10 +1441,13 @@ async fn rpc_getblocktemplate_mining_address(use_p2pkh: bool) {
conventional_fee: 0.try_into().unwrap(),
};

let conventional_actions = zip317::conventional_actions(&unmined_tx.transaction);

let verified_unmined_tx = VerifiedUnminedTx {
transaction: unmined_tx,
miner_fee: 0.try_into().unwrap(),
legacy_sigop_count: 0,
conventional_actions,
unpaid_actions: 0,
fee_weight_ratio: 1.0,
};
Expand Down
100 changes: 100 additions & 0 deletions zebrad/src/components/mempool/storage/verified_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,110 @@ impl VerifiedSet {
}

fn update_metrics(&mut self) {
// Track the sum of unpaid actions within each transaction (as they are subject to the
// unpaid action limit). Transactions that have weight >= 1 have no unpaid actions by
// definition.
let mut unpaid_actions_with_weight_lt20pct = 0;
let mut unpaid_actions_with_weight_lt40pct = 0;
let mut unpaid_actions_with_weight_lt60pct = 0;
let mut unpaid_actions_with_weight_lt80pct = 0;
let mut unpaid_actions_with_weight_lt1 = 0;

// Track the total number of paid actions across all transactions in the mempool. This
// added to the bucketed unpaid actions above is equal to the total number of conventional
// actions in the mempool.
let mut paid_actions = 0;

// Track the sum of transaction sizes (the metric by which they are mainly limited) across
// several buckets.
let mut size_with_weight_lt1 = 0;
let mut size_with_weight_eq1 = 0;
let mut size_with_weight_gt1 = 0;
let mut size_with_weight_gt2 = 0;
let mut size_with_weight_gt3 = 0;

for entry in self.full_transactions() {
paid_actions += entry.conventional_actions - entry.unpaid_actions;

if entry.fee_weight_ratio > 3.0 {
size_with_weight_gt3 += entry.transaction.size;
} else if entry.fee_weight_ratio > 2.0 {
size_with_weight_gt2 += entry.transaction.size;
} else if entry.fee_weight_ratio > 1.0 {
size_with_weight_gt1 += entry.transaction.size;
} else if entry.fee_weight_ratio == 1.0 {
size_with_weight_eq1 += entry.transaction.size;
} else {
size_with_weight_lt1 += entry.transaction.size;
if entry.fee_weight_ratio < 0.2 {
unpaid_actions_with_weight_lt20pct += entry.unpaid_actions;
} else if entry.fee_weight_ratio < 0.4 {
unpaid_actions_with_weight_lt40pct += entry.unpaid_actions;
} else if entry.fee_weight_ratio < 0.6 {
unpaid_actions_with_weight_lt60pct += entry.unpaid_actions;
} else if entry.fee_weight_ratio < 0.8 {
unpaid_actions_with_weight_lt80pct += entry.unpaid_actions;
} else {
unpaid_actions_with_weight_lt1 += entry.unpaid_actions;
}
}
}

metrics::gauge!(
"zcash.mempool.actions.unpaid",
unpaid_actions_with_weight_lt20pct as f64,
"bk" => "< 0.2",
);
metrics::gauge!(
"zcash.mempool.actions.unpaid",
unpaid_actions_with_weight_lt40pct as f64,
"bk" => "< 0.4",
);
metrics::gauge!(
"zcash.mempool.actions.unpaid",
unpaid_actions_with_weight_lt60pct as f64,
"bk" => "< 0.6",
);
metrics::gauge!(
"zcash.mempool.actions.unpaid",
unpaid_actions_with_weight_lt80pct as f64,
"bk" => "< 0.8",
);
metrics::gauge!(
"zcash.mempool.actions.unpaid",
unpaid_actions_with_weight_lt1 as f64,
"bk" => "< 1",
);
metrics::gauge!("zcash.mempool.actions.paid", paid_actions as f64);
metrics::gauge!(
"zcash.mempool.size.transactions",
self.transaction_count() as f64,
);
metrics::gauge!(
"zcash.mempool.size.weighted",
size_with_weight_lt1 as f64,
"bk" => "< 1",
);
metrics::gauge!(
"zcash.mempool.size.weighted",
size_with_weight_eq1 as f64,
"bk" => "1",
);
metrics::gauge!(
"zcash.mempool.size.weighted",
size_with_weight_gt1 as f64,
"bk" => "> 1",
);
metrics::gauge!(
"zcash.mempool.size.weighted",
size_with_weight_gt2 as f64,
"bk" => "> 2",
);
metrics::gauge!(
"zcash.mempool.size.weighted",
size_with_weight_gt3 as f64,
"bk" => "> 3",
);
metrics::gauge!(
"zcash.mempool.size.bytes",
self.transactions_serialized_size as f64,
Expand Down

0 comments on commit 573695f

Please sign in to comment.