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

feat: remove total_txs and rename total_weight in mempool #4474

Merged
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
3 changes: 1 addition & 2 deletions applications/tari_app_grpc/proto/base_node.proto
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,9 @@ enum TransactionLocation {
}

message MempoolStatsResponse {
uint64 total_txs = 1;
uint64 unconfirmed_txs = 2;
uint64 reorg_txs = 3;
uint64 total_weight = 4;
uint64 unconfirmed_weight = 4;
}

message GetConstitutionsRequest {
Expand Down
6 changes: 3 additions & 3 deletions applications/tari_base_node/src/commands/command/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ impl CommandContext {
format!(
"{}tx ({}g, +/- {}blks)",
mempool_stats.unconfirmed_txs,
mempool_stats.total_weight,
if mempool_stats.total_weight == 0 {
mempool_stats.unconfirmed_weight,
if mempool_stats.unconfirmed_weight == 0 {
0
} else {
1 + mempool_stats.total_weight / constants.get_max_block_transaction_weight()
1 + mempool_stats.unconfirmed_weight / constants.get_max_block_transaction_weight()
},
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1865,10 +1865,9 @@ impl tari_rpc::base_node_server::BaseNode for BaseNodeGrpcServer {
})?;

let response = tari_rpc::MempoolStatsResponse {
total_txs: mempool_stats.total_txs as u64,
unconfirmed_txs: mempool_stats.unconfirmed_txs as u64,
reorg_txs: mempool_stats.reorg_txs as u64,
total_weight: mempool_stats.total_weight,
unconfirmed_weight: mempool_stats.unconfirmed_weight,
};

Ok(Response::new(response))
Expand Down
8 changes: 1 addition & 7 deletions base_layer/core/src/mempool/mempool_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,13 @@ impl MempoolStorage {
.ok_or(MempoolError::TransactionNoKernels)
}

// Returns the total number of transactions in the Mempool.
fn len(&self) -> usize {
self.unconfirmed_pool.len() + self.reorg_pool.len()
}

/// Gathers and returns the stats of the Mempool.
pub fn stats(&self) -> StatsResponse {
let weighting = self.get_transaction_weighting(0);
StatsResponse {
total_txs: self.len() as u64,
unconfirmed_txs: self.unconfirmed_pool.len() as u64,
reorg_txs: self.reorg_pool.len() as u64,
total_weight: self.unconfirmed_pool.calculate_weight(&weighting),
unconfirmed_weight: self.unconfirmed_pool.calculate_weight(&weighting),
}
}

Expand Down
7 changes: 3 additions & 4 deletions base_layer/core/src/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,17 @@ use crate::{

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StatsResponse {
pub total_txs: u64,
pub unconfirmed_txs: u64,
pub reorg_txs: u64,
pub total_weight: u64,
pub unconfirmed_weight: u64,
}

impl Display for StatsResponse {
fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
write!(
fmt,
"Mempool stats: Total transactions: {}, Unconfirmed: {}, Published: {}, Total Weight: {}g",
self.total_txs, self.unconfirmed_txs, self.reorg_txs, self.total_weight
"Mempool stats: Unconfirmed: {}, In Reorg Pool: {}, Total Weight: {}g",
self.unconfirmed_txs, self.reorg_txs, self.unconfirmed_weight
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/mempool/proto/stats_response.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ syntax = "proto3";
package tari.mempool;

message StatsResponse {
uint64 total_txs = 1;
uint64 unconfirmed_txs = 2;
uint64 reorg_txs = 5;
uint64 total_weight = 6;
uint64 unconfirmed_weight = 6;
}
6 changes: 2 additions & 4 deletions base_layer/core/src/mempool/proto/stats_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ impl TryFrom<ProtoStatsResponse> for StatsResponse {

fn try_from(stats: ProtoStatsResponse) -> Result<Self, Self::Error> {
Ok(Self {
total_txs: stats.total_txs,
unconfirmed_txs: stats.unconfirmed_txs,
reorg_txs: stats.reorg_txs,
total_weight: stats.total_weight,
unconfirmed_weight: stats.unconfirmed_weight,
})
}
}

impl From<StatsResponse> for ProtoStatsResponse {
fn from(stats: StatsResponse) -> Self {
Self {
total_txs: stats.total_txs,
unconfirmed_txs: stats.unconfirmed_txs,
reorg_txs: stats.reorg_txs,
total_weight: stats.total_weight,
unconfirmed_weight: stats.unconfirmed_weight,
}
}
}
3 changes: 1 addition & 2 deletions base_layer/core/src/mempool/rpc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ mod get_stats {
async fn it_returns_the_stats() {
let (service, mempool, req_mock, _tmpdir) = setup();
let expected_stats = StatsResponse {
total_txs: 1,
unconfirmed_txs: 2,

reorg_txs: 5,
total_weight: 6,
unconfirmed_weight: 6,
};
mempool.set_get_stats_response(expected_stats.clone()).await;

Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/mempool/service/local_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,9 @@ mod test {

fn request_stats() -> StatsResponse {
StatsResponse {
total_txs: 10,
unconfirmed_txs: 3,
reorg_txs: 4,
total_weight: 1000,
unconfirmed_weight: 1000,
}
}

Expand Down
3 changes: 1 addition & 2 deletions base_layer/core/src/mempool/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ impl Default for MempoolMockState {
fn default() -> Self {
Self {
get_stats: Arc::new(Mutex::new(StatsResponse {
total_txs: 0,
unconfirmed_txs: 0,
reorg_txs: 0,
total_weight: 0,
unconfirmed_weight: 0,
})),
get_state: Arc::new(Mutex::new(StateResponse {
unconfirmed_pool: vec![],
Expand Down
12 changes: 5 additions & 7 deletions base_layer/core/tests/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ async fn test_insert_and_process_published_block() {
assert!(snapshot_txs.contains(&tx2));

let stats = mempool.stats().await.unwrap();
assert_eq!(stats.total_txs, 1);
assert_eq!(stats.unconfirmed_txs, 1);
assert_eq!(stats.reorg_txs, 0);
let expected_weight = consensus_manager.consensus_constants(0).transaction_weight().calculate(
Expand All @@ -187,7 +186,7 @@ async fn test_insert_and_process_published_block() {
2,
TestParams::new().get_size_for_default_metadata(2),
);
assert_eq!(stats.total_weight, expected_weight);
assert_eq!(stats.unconfirmed_weight, expected_weight);

// Spend tx2, so it goes in Reorg pool
generate_block(&store, &mut blocks, vec![tx2.deref().clone()], &consensus_manager).unwrap();
Expand Down Expand Up @@ -233,10 +232,9 @@ async fn test_insert_and_process_published_block() {
assert_eq!(snapshot_txs.len(), 0);

let stats = mempool.stats().await.unwrap();
assert_eq!(stats.total_txs, 1);
assert_eq!(stats.unconfirmed_txs, 0);
assert_eq!(stats.reorg_txs, 1);
assert_eq!(stats.total_weight, 0);
assert_eq!(stats.unconfirmed_weight, 0);
}

#[tokio::test]
Expand Down Expand Up @@ -607,7 +605,7 @@ async fn test_zero_conf() {

// Try to retrieve all transactions in the mempool (a couple of our transactions should be missing from retrieved)
let retrieved_txs = mempool
.retrieve(mempool.stats().await.unwrap().total_weight)
.retrieve(mempool.stats().await.unwrap().unconfirmed_weight)
.await
.unwrap();
assert_eq!(retrieved_txs.len(), 10);
Expand Down Expand Up @@ -659,7 +657,7 @@ async fn test_zero_conf() {

// Try to retrieve all transactions in the mempool (all transactions should be retrieved)
let retrieved_txs = mempool
.retrieve(mempool.stats().await.unwrap().total_weight)
.retrieve(mempool.stats().await.unwrap().unconfirmed_weight)
.await
.unwrap();
assert_eq!(retrieved_txs.len(), 16);
Expand All @@ -682,7 +680,7 @@ async fn test_zero_conf() {

// Verify that a higher priority transaction is not retrieved due to its zero-conf dependency instead of the lowest
// priority transaction
let weight = mempool.stats().await.unwrap().total_weight - 1;
let weight = mempool.stats().await.unwrap().unconfirmed_weight - 1;
let retrieved_txs = mempool.retrieve(weight).await.unwrap();
assert_eq!(retrieved_txs.len(), 15);
assert!(retrieved_txs.contains(&Arc::new(tx01)));
Expand Down