-
Notifications
You must be signed in to change notification settings - Fork 63
cuprated: relay rules
#424
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| use std::cmp::max; | ||
|
|
||
| use monero_serai::transaction::Timelock; | ||
| use thiserror::Error; | ||
|
|
||
| use cuprate_consensus_context::BlockchainContext; | ||
| use cuprate_consensus_rules::miner_tx::calculate_block_reward; | ||
| use cuprate_helper::cast::usize_to_u64; | ||
| use cuprate_types::TransactionVerificationData; | ||
|
|
||
| /// The maximum size of the tx extra field. | ||
| /// | ||
| /// <https://github.com/monero-project/monero/blob/3b01c490953fe92f3c6628fa31d280a4f0490d28/src/cryptonote_config.h#L217> | ||
| const MAX_TX_EXTRA_SIZE: usize = 1060; | ||
|
|
||
| /// <https://github.com/monero-project/monero/blob/3b01c490953fe92f3c6628fa31d280a4f0490d28/src/cryptonote_config.h#L75> | ||
| const DYNAMIC_FEE_REFERENCE_TRANSACTION_WEIGHT: u128 = 3_000; | ||
|
|
||
| /// <https://github.com/monero-project/monero/blob/3b01c490953fe92f3c6628fa31d280a4f0490d28/src/cryptonote_core/blockchain.h#L646> | ||
| const FEE_MASK: u64 = 10_u64.pow(4); | ||
|
Boog900 marked this conversation as resolved.
|
||
|
|
||
| #[derive(Debug, Error)] | ||
| pub enum RelayRuleError { | ||
| #[error("Tx has non-zero timelock.")] | ||
| NonZeroTimelock, | ||
| #[error("Tx extra field is too large.")] | ||
| ExtraFieldTooLarge, | ||
| #[error("Tx fee too low.")] | ||
| FeeBelowMinimum, | ||
| } | ||
|
|
||
| /// Checks the transaction passes the relay rules. | ||
| /// | ||
| /// Relay rules are rules that govern the txs we accept to our tx-pool and propagate around the network. | ||
| pub fn check_tx_relay_rules( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some questions:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That one isn't really a relay rule IMO, it just prevents a previously timed out tx from getting added to the pool again. All relay rules should be included here.
Probably, I don't know how we would do that without generating txs though so that will have to come later
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is other code there: https://github.com/monero-project/monero/blob/3b01c490953fe92f3c6628fa31d280a4f0490d28/src/cryptonote_core/tx_pool.cpp#L204-L344. Should that be replicated here?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. monerod's tx-pool also deals with some consensus checks, only the checks that have |
||
| tx: &TransactionVerificationData, | ||
| context: &BlockchainContext, | ||
| ) -> Result<(), RelayRuleError> { | ||
| if tx.tx.prefix().additional_timelock != Timelock::None { | ||
| return Err(RelayRuleError::NonZeroTimelock); | ||
| } | ||
|
|
||
| if tx.tx.prefix().extra.len() > MAX_TX_EXTRA_SIZE { | ||
| return Err(RelayRuleError::ExtraFieldTooLarge); | ||
| } | ||
|
|
||
| check_fee(tx.tx_weight, tx.fee, context) | ||
| } | ||
|
|
||
| /// Checks the fee is enough for the tx weight and current blockchain state. | ||
| fn check_fee( | ||
| tx_weight: usize, | ||
| fee: u64, | ||
| context: &BlockchainContext, | ||
| ) -> Result<(), RelayRuleError> { | ||
| let base_reward = calculate_block_reward( | ||
| 1, | ||
| context.effective_median_weight, | ||
| context.already_generated_coins, | ||
| context.current_hf, | ||
| ); | ||
|
|
||
| let fee_per_byte = dynamic_base_fee(base_reward, context.effective_median_weight); | ||
| let needed_fee = usize_to_u64(tx_weight) * fee_per_byte; | ||
|
|
||
| let needed_fee = needed_fee.div_ceil(FEE_MASK) * FEE_MASK; | ||
|
|
||
| if fee < (needed_fee - needed_fee / 50) { | ||
| tracing::debug!(fee, needed_fee, "Tx fee is below minimum."); | ||
| return Err(RelayRuleError::FeeBelowMinimum); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Calculates the base fee per byte for tx relay. | ||
| fn dynamic_base_fee(base_reward: u64, effective_media_block_weight: usize) -> u64 { | ||
| let median_block_weight = effective_media_block_weight as u128; | ||
|
|
||
| let fee_per_byte_100 = u128::from(base_reward) * DYNAMIC_FEE_REFERENCE_TRANSACTION_WEIGHT | ||
| / median_block_weight | ||
| / median_block_weight; | ||
| let fee_per_byte = fee_per_byte_100 - fee_per_byte_100 / 20; | ||
|
|
||
| #[expect(clippy::cast_possible_truncation)] | ||
| max(fee_per_byte as u64, 1) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.