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

implement rpc method that fetches fee per mass based on block template #503

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cli/src/modules/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ impl Rpc {
}
}
}
RpcApiOps::GetFeeInfo => {
let result = rpc.get_fee_info_call(GetFeeInfoRequest {}).await;
self.println(&ctx, result);
}
_ => {
tprintln!(ctx, "rpc method exists but is not supported by the cli: '{op_str}'\r\n");
return Ok(());
Expand Down
13 changes: 12 additions & 1 deletion consensus/core/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub struct BlockTemplate {
pub selected_parent_timestamp: u64,
pub selected_parent_daa_score: u64,
pub selected_parent_hash: Hash,
/// length one less than txs length due to lack of coinbase transaction
pub calculated_fees: Vec<u64>,
}

impl BlockTemplate {
Expand All @@ -115,8 +117,17 @@ impl BlockTemplate {
selected_parent_timestamp: u64,
selected_parent_daa_score: u64,
selected_parent_hash: Hash,
calculated_fees: Vec<u64>,
) -> Self {
Self { block, miner_data, coinbase_has_red_reward, selected_parent_timestamp, selected_parent_daa_score, selected_parent_hash }
Self {
block,
miner_data,
coinbase_has_red_reward,
selected_parent_timestamp,
selected_parent_daa_score,
selected_parent_hash,
calculated_fees,
}
}

pub fn to_virtual_state_approx_id(&self) -> VirtualStateApproxId {
Expand Down
49 changes: 30 additions & 19 deletions consensus/src/pipeline/virtual_processor/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ use kaspa_notify::{events::EventType, notifier::Notify};

use crossbeam_channel::{Receiver as CrossbeamReceiver, Sender as CrossbeamSender};
use itertools::Itertools;
use kaspa_consensus_core::tx::ValidatedTransaction;
use kaspa_utils::binary_heap::BinaryHeapExtensions;
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use rand::{seq::SliceRandom, Rng};
Expand Down Expand Up @@ -821,26 +822,24 @@ impl VirtualStateProcessor {
txs: &[Transaction],
virtual_state: &VirtualState,
utxo_view: &V,
) -> Vec<TxResult<()>> {
self.thread_pool.install(|| {
txs.par_iter()
.map(|tx| self.validate_block_template_transaction(tx, virtual_state, &utxo_view))
.collect::<Vec<TxResult<()>>>()
})
) -> Vec<TxResult<u64>> {
self.thread_pool
.install(|| txs.par_iter().map(|tx| self.validate_block_template_transaction(tx, virtual_state, &utxo_view)).collect())
}

fn validate_block_template_transaction(
&self,
tx: &Transaction,
virtual_state: &VirtualState,
utxo_view: &impl UtxoView,
) -> TxResult<()> {
) -> TxResult<u64> {
// No need to validate the transaction in isolation since we rely on the mining manager to submit transactions
// which were previously validated through `validate_mempool_transaction_and_populate`, hence we only perform
// in-context validations
self.transaction_validator.utxo_free_tx_validation(tx, virtual_state.daa_score, virtual_state.past_median_time)?;
self.validate_transaction_in_utxo_context(tx, utxo_view, virtual_state.daa_score, TxValidationFlags::Full)?;
Ok(())
let ValidatedTransaction { calculated_fee, .. } =
self.validate_transaction_in_utxo_context(tx, utxo_view, virtual_state.daa_score, TxValidationFlags::Full)?;
Ok(calculated_fee)
}

pub fn build_block_template(
Expand All @@ -863,11 +862,17 @@ impl VirtualStateProcessor {
let virtual_utxo_view = &virtual_read.utxo_set;

let mut invalid_transactions = HashMap::new();
let mut calculated_fees = Vec::with_capacity(txs.len());
let results = self.validate_block_template_transactions_in_parallel(&txs, &virtual_state, &virtual_utxo_view);
for (tx, res) in txs.iter().zip(results) {
if let Err(e) = res {
invalid_transactions.insert(tx.id(), e);
tx_selector.reject_selection(tx.id());
match res {
Err(e) => {
invalid_transactions.insert(tx.id(), e);
tx_selector.reject_selection(tx.id());
}
Ok(fee) => {
calculated_fees.push(fee);
}
}
}

Expand All @@ -882,12 +887,16 @@ impl VirtualStateProcessor {
let next_batch_results =
self.validate_block_template_transactions_in_parallel(&next_batch, &virtual_state, &virtual_utxo_view);
for (tx, res) in next_batch.into_iter().zip(next_batch_results) {
if let Err(e) = res {
invalid_transactions.insert(tx.id(), e);
tx_selector.reject_selection(tx.id());
has_rejections = true;
} else {
txs.push(tx);
match res {
Err(e) => {
invalid_transactions.insert(tx.id(), e);
tx_selector.reject_selection(tx.id());
has_rejections = true;
}
Ok(fee) => {
txs.push(tx);
calculated_fees.push(fee);
}
}
}
}
Expand All @@ -904,7 +913,7 @@ impl VirtualStateProcessor {
drop(virtual_read);

// Build the template
self.build_block_template_from_virtual_state(virtual_state, miner_data, txs)
self.build_block_template_from_virtual_state(virtual_state, miner_data, txs, calculated_fees)
}

pub(crate) fn validate_block_template_transactions(
Expand Down Expand Up @@ -932,6 +941,7 @@ impl VirtualStateProcessor {
virtual_state: Arc<VirtualState>,
miner_data: MinerData,
mut txs: Vec<Transaction>,
calculated_fees: Vec<u64>,
) -> Result<BlockTemplate, RuleError> {
// [`calc_block_parents`] can use deep blocks below the pruning point for this calculation, so we
// need to hold the pruning lock.
Expand Down Expand Up @@ -985,6 +995,7 @@ impl VirtualStateProcessor {
selected_parent_timestamp,
selected_parent_daa_score,
selected_parent_hash,
calculated_fees,
))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ impl TestBlockBuilder {
let pov_virtual_utxo_view = (&virtual_read.utxo_set).compose(accumulated_diff);
self.validate_block_template_transactions(&txs, &pov_virtual_state, &pov_virtual_utxo_view)?;
drop(virtual_read);
self.build_block_template_from_virtual_state(pov_virtual_state, miner_data, txs)
self.build_block_template_from_virtual_state(pov_virtual_state, miner_data, txs, vec![])
}
}
6 changes: 6 additions & 0 deletions mining/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ pub struct MiningCounters {
pub txs_sample: AtomicU64,
pub orphans_sample: AtomicU64,
pub accepted_sample: AtomicU64,

pub total_mass: AtomicU64,
}

impl Default for MiningCounters {
Expand All @@ -49,6 +51,7 @@ impl Default for MiningCounters {
txs_sample: Default::default(),
orphans_sample: Default::default(),
accepted_sample: Default::default(),
total_mass: Default::default(),
}
}
}
Expand All @@ -67,6 +70,7 @@ impl MiningCounters {
txs_sample: self.txs_sample.load(Ordering::Relaxed),
orphans_sample: self.orphans_sample.load(Ordering::Relaxed),
accepted_sample: self.accepted_sample.load(Ordering::Relaxed),
total_mass: self.total_mass.load(Ordering::Relaxed),
}
}

Expand Down Expand Up @@ -102,6 +106,7 @@ pub struct MempoolCountersSnapshot {
pub txs_sample: u64,
pub orphans_sample: u64,
pub accepted_sample: u64,
pub total_mass: u64,
}

impl MempoolCountersSnapshot {
Expand Down Expand Up @@ -157,6 +162,7 @@ impl core::ops::Sub for &MempoolCountersSnapshot {
txs_sample: (self.txs_sample + rhs.txs_sample) / 2,
orphans_sample: (self.orphans_sample + rhs.orphans_sample) / 2,
accepted_sample: (self.accepted_sample + rhs.accepted_sample) / 2,
total_mass: self.total_mass.checked_sub(rhs.total_mass).unwrap_or_default(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions mining/src/mempool/remove_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::mempool::{
use kaspa_consensus_core::tx::TransactionId;
use kaspa_core::{debug, warn};
use kaspa_utils::iter::IterExtensions;
use std::sync::atomic::Ordering;

impl Mempool {
pub(crate) fn remove_transaction(
Expand Down Expand Up @@ -33,6 +34,7 @@ impl Mempool {
for tx_id in removed_transactions.iter() {
// Remove the tx from the transaction pool and the UTXO set (handled within the pool)
let tx = self.transaction_pool.remove_transaction(tx_id)?;
self.counters.total_mass.fetch_sub(tx.mtx.tx.mass(), Ordering::Relaxed);
// Update/remove descendent orphan txs (depending on `remove_redeemers`)
let txs = self.orphan_pool.update_orphans_after_transaction_removed(&tx, remove_redeemers)?;
removed_orphans.extend(txs.into_iter().map(|x| x.id()));
Expand Down
2 changes: 2 additions & 0 deletions mining/src/mempool/validate_and_insert_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use kaspa_consensus_core::{
tx::{MutableTransaction, Transaction, TransactionId, TransactionOutpoint, UtxoEntry},
};
use kaspa_core::{debug, info};
use std::sync::atomic::Ordering;
use std::sync::Arc;

impl Mempool {
Expand Down Expand Up @@ -78,6 +79,7 @@ impl Mempool {
// Add the transaction to the mempool as a MempoolTransaction and return a clone of the embedded Arc<Transaction>
let accepted_transaction =
self.transaction_pool.add_transaction(transaction, consensus.get_virtual_daa_score(), priority)?.mtx.tx.clone();
self.counters.total_mass.fetch_add(accepted_transaction.mass(), Ordering::Relaxed);
Ok(Some(accepted_transaction))
}

Expand Down
2 changes: 1 addition & 1 deletion mining/src/testutils/consensus_mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl ConsensusApi for ConsensusMock {
);
let mutable_block = MutableBlock::new(header, txs);

Ok(BlockTemplate::new(mutable_block, miner_data, coinbase.has_red_reward, now, 0, ZERO_HASH))
Ok(BlockTemplate::new(mutable_block, miner_data, coinbase.has_red_reward, now, 0, ZERO_HASH, vec![]))
}

fn validate_mempool_transaction(&self, mutable_tx: &mut MutableTransaction) -> TxResult<()> {
Expand Down
2 changes: 2 additions & 0 deletions rpc/core/src/api/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub enum RpcApiOps {
GetCoinSupply,
/// Get DAA Score timestamp estimate
GetDaaScoreTimestampEstimate,
/// Get priority fee estimate
GetFeeInfo,

// Subscription commands for starting/stopping notifications
NotifyBlockAdded,
Expand Down
5 changes: 5 additions & 0 deletions rpc/core/src/api/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ pub trait RpcApi: Sync + Send + AnySync {
request: GetDaaScoreTimestampEstimateRequest,
) -> RpcResult<GetDaaScoreTimestampEstimateResponse>;

async fn get_fee_info(&self) -> RpcResult<GetFeeInfoResponse> {
self.get_fee_info_call(GetFeeInfoRequest {}).await
}
async fn get_fee_info_call(&self, request: GetFeeInfoRequest) -> RpcResult<GetFeeInfoResponse>;

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Notification API

Expand Down
31 changes: 31 additions & 0 deletions rpc/core/src/model/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,37 @@ impl GetDaaScoreTimestampEstimateResponse {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetFeeInfoRequest {}

#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct VirtualFeePerMass {
pub max: f64,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these still represent any kind of SLA? If so, please add comments as to what these would mean in terms of time-to-inclusion

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed method.
this value represents max value fee/mass in the current mempool.
I will add some description from internal discussion:

What I came to this:
When the network is overloaded, we need to know, and it will have meaning like:
min from bbt - there's a chance my tx will be included
Something in the middle - likely tx will be included
Max - very likely

In case of normal load:
Max - it's almost included, check Ur balance 
Middle - very likely 
Min - likely 
ComputeFeeOnly - there's a chance 

For that we only  need fee_per_mass based on bbt - already in pr

Flag if mempool is overloaded - I want to calculate  and expose  mempool_total_mass.

mempool_total_mass/(block_mass_limit*bps) let's call it mempool load factor - Mlf.

If Mlf < 1 (mempool is underload) then even tx with compute fee only will be included

Mlf in [1,1.5) - normal mempool load
Mlf > 1.5 - overload

pub median: f64,
pub min: f64,
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub enum FeePerMass {
VirtualFeePerMass(VirtualFeePerMass),
}

#[derive(Clone, Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetFeeInfoResponse {
pub fee_per_mass: FeePerMass,
pub mempool_total_mass: u64,
}

impl GetFeeInfoResponse {
pub fn new(fee_per_mass: FeePerMass, mempool_total_mass: u64) -> Self {
Self { fee_per_mass, mempool_total_mass }
}
}

// ----------------------------------------------------------------------------
// Subscriptions & notifications
// ----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions rpc/grpc/client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ impl RpcApi for GrpcClient {
route!(get_mempool_entries_by_addresses_call, GetMempoolEntriesByAddresses);
route!(get_coin_supply_call, GetCoinSupply);
route!(get_daa_score_timestamp_estimate_call, GetDaaScoreTimestampEstimate);
route!(get_fee_info_call, GetFeeInfo);

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Notification API
Expand Down
2 changes: 2 additions & 0 deletions rpc/grpc/core/proto/messages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ message KaspadRequest {
GetServerInfoRequestMessage getServerInfoRequest = 1092;
GetSyncStatusRequestMessage getSyncStatusRequest = 1094;
GetDaaScoreTimestampEstimateRequestMessage GetDaaScoreTimestampEstimateRequest = 1096;
GetFeeInfoRequestMessage getFeeInfoRequest = 1098;
}
}

Expand Down Expand Up @@ -118,6 +119,7 @@ message KaspadResponse {
GetServerInfoResponseMessage getServerInfoResponse = 1093;
GetSyncStatusResponseMessage getSyncStatusResponse = 1095;
GetDaaScoreTimestampEstimateResponseMessage GetDaaScoreTimestampEstimateResponse = 1097;
GetFeeInfoResponseMessage getFeeInfoResponse = 1099;
}
}

Expand Down
16 changes: 16 additions & 0 deletions rpc/grpc/core/proto/rpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -851,3 +851,19 @@ message GetDaaScoreTimestampEstimateResponseMessage{
repeated uint64 timestamps = 1;
RPCError error = 1000;
}

message GetFeeInfoRequestMessage {}

message GetFeeInfoResponseMessage {
oneof fee_per_mass {
Virtual virtual = 2; // 1 is reserved for `All`
}
uint64 mempool_total_mass = 11;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be the usage of total mass from the client side? I'm not sure we want to commit ourselves to such an API. I have another suggestion for a more abstracted response

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on this value we can track both: absolute values/relative to bps and block limit. that's more flexible

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wdyt @aspect?

RPCError error = 1000;
}

message Virtual {
double max = 1;
double median = 2;
double min = 3;
}
2 changes: 2 additions & 0 deletions rpc/grpc/core/src/convert/kaspad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub mod kaspad_request_convert {
impl_into_kaspad_request!(GetServerInfo);
impl_into_kaspad_request!(GetSyncStatus);
impl_into_kaspad_request!(GetDaaScoreTimestampEstimate);
impl_into_kaspad_request!(GetFeeInfo);

impl_into_kaspad_request!(NotifyBlockAdded);
impl_into_kaspad_request!(NotifyNewBlockTemplate);
Expand Down Expand Up @@ -188,6 +189,7 @@ pub mod kaspad_response_convert {
impl_into_kaspad_response!(GetServerInfo);
impl_into_kaspad_response!(GetSyncStatus);
impl_into_kaspad_response!(GetDaaScoreTimestampEstimate);
impl_into_kaspad_response!(GetFeeInfo);

impl_into_kaspad_notify_response!(NotifyBlockAdded);
impl_into_kaspad_notify_response!(NotifyNewBlockTemplate);
Expand Down
Loading