From c29f5c8a27e00db81bf987d0d39f2418472d82c5 Mon Sep 17 00:00:00 2001 From: zhangsoledad <787953403@qq.com> Date: Tue, 19 Oct 2021 10:28:52 +0800 Subject: [PATCH] feat: tx-pool entry timestamp --- rpc/README.md | 7 ++++++- rpc/src/module/pool.rs | 3 ++- rpc/src/tests/examples.rs | 6 ++++++ tx-pool/src/component/entry.rs | 5 +++++ util/jsonrpc-types/src/pool.rs | 6 ++++++ util/launcher/src/shared_builder.rs | 3 +++ util/types/src/core/service.rs | 2 ++ util/types/src/core/tx_pool.rs | 2 ++ 8 files changed, 32 insertions(+), 2 deletions(-) diff --git a/rpc/README.md b/rpc/README.md index ad9cee938b..d5ccd6ae33 100644 --- a/rpc/README.md +++ b/rpc/README.md @@ -2500,7 +2500,8 @@ Response "fee": "0x16923f7dcf", "ancestors_size": "0x112", "ancestors_cycles": "0x219", - "ancestors_count": "0x1" + "ancestors_count": "0x1", + "timestamp": "0x17c983e6e44" } }, "proposed": {} @@ -3939,6 +3940,8 @@ The transaction entry in the pool. * `fee`: [`Capacity`](#type-capacity) - The transaction fee. +* `timestamp`: [`Uint64`](#type-uint64) - The unix timestamp when entering the Txpool, unit: Millisecond + ### Type `PoolTransactionReject` @@ -4441,6 +4444,8 @@ Transaction entry info * `ancestors_count`: [`Uint64`](#type-uint64) - Number of in-tx-pool ancestor transactions +* `timestamp`: [`Uint64`](#type-uint64) - The unix timestamp when entering the Txpool, unit: Millisecond + ### Type `TxPoolIds` diff --git a/rpc/src/module/pool.rs b/rpc/src/module/pool.rs index 8a2262f0d1..3974e02c4c 100644 --- a/rpc/src/module/pool.rs +++ b/rpc/src/module/pool.rs @@ -201,7 +201,8 @@ pub trait PoolRpc { /// "fee": "0x16923f7dcf", /// "ancestors_size": "0x112", /// "ancestors_cycles": "0x219", - /// "ancestors_count": "0x1" + /// "ancestors_count": "0x1", + /// "timestamp": "0x17c983e6e44" /// } /// }, /// "proposed": {} diff --git a/rpc/src/tests/examples.rs b/rpc/src/tests/examples.rs index 504a45af4d..dbd6d07862 100644 --- a/rpc/src/tests/examples.rs +++ b/rpc/src/tests/examples.rs @@ -560,6 +560,8 @@ where fn mock_rpc_response(example: &RpcTestExample, response: &mut RpcTestResponse) { use ckb_jsonrpc_types::{BannedAddr, Capacity, LocalNode, RemoteNode, Uint64}; + let example_tx_hash = format!("{:#x}", EXAMPLE_TX_HASH); + match example.request.method.as_str() { "local_node_info" => replace_rpc_response::(example, response), "get_peers" => replace_rpc_response::>(example, response), @@ -579,6 +581,10 @@ fn mock_rpc_response(example: &RpcTestExample, response: &mut RpcTestResponse) { response.result["chain"] = example.response.result["chain"].clone() } "send_alert" => response.error["data"] = example.response.error["data"].clone(), + "get_raw_tx_pool" => { + response.result["pending"][example_tx_hash.as_str()]["timestamp"] = + example.response.result["pending"][example_tx_hash.as_str()]["timestamp"].clone() + } _ => {} } } diff --git a/tx-pool/src/component/entry.rs b/tx-pool/src/component/entry.rs index 1fae405c80..ad12fe6abe 100644 --- a/tx-pool/src/component/entry.rs +++ b/tx-pool/src/component/entry.rs @@ -4,6 +4,7 @@ use ckb_types::{ core::{cell::ResolvedTransaction, tx_pool::TxEntryInfo, Capacity, Cycle, TransactionView}, packed::{OutPoint, ProposalShortId}, }; +use faketime::unix_time_as_millis; use std::cmp::Ordering; use std::hash::{Hash, Hasher}; @@ -26,6 +27,8 @@ pub struct TxEntry { pub ancestors_cycles: Cycle, /// ancestors txs count pub ancestors_count: usize, + /// The unix timestamp when entering the Txpool, unit: Millisecond + pub timestamp: u64, } impl TxEntry { @@ -40,6 +43,7 @@ impl TxEntry { ancestors_fee: fee, ancestors_cycles: cycles, ancestors_count: 1, + timestamp: unix_time_as_millis(), } } @@ -109,6 +113,7 @@ impl TxEntry { ancestors_size: self.ancestors_size as u64, ancestors_cycles: self.ancestors_cycles as u64, ancestors_count: self.ancestors_count as u64, + timestamp: self.timestamp, } } } diff --git a/util/jsonrpc-types/src/pool.rs b/util/jsonrpc-types/src/pool.rs index 6e456ba76d..82108bd6cf 100644 --- a/util/jsonrpc-types/src/pool.rs +++ b/util/jsonrpc-types/src/pool.rs @@ -53,6 +53,8 @@ pub struct PoolTransactionEntry { pub size: Uint64, /// The transaction fee. pub fee: Capacity, + /// The unix timestamp when entering the Txpool, unit: Millisecond + pub timestamp: Uint64, } impl From for PoolTransactionEntry { @@ -62,6 +64,7 @@ impl From for PoolTransactionEntry { cycles: entry.cycles.into(), size: (entry.size as u64).into(), fee: entry.fee.into(), + timestamp: entry.timestamp.into(), } } } @@ -118,6 +121,8 @@ pub struct TxPoolEntry { pub ancestors_cycles: Uint64, /// Number of in-tx-pool ancestor transactions pub ancestors_count: Uint64, + /// The unix timestamp when entering the Txpool, unit: Millisecond + pub timestamp: Uint64, } impl From for TxPoolEntry { @@ -129,6 +134,7 @@ impl From for TxPoolEntry { ancestors_size: info.ancestors_size.into(), ancestors_cycles: info.ancestors_cycles.into(), ancestors_count: info.ancestors_count.into(), + timestamp: info.timestamp.into(), } } } diff --git a/util/launcher/src/shared_builder.rs b/util/launcher/src/shared_builder.rs index 4d8a76d8c8..2cd4086e45 100644 --- a/util/launcher/src/shared_builder.rs +++ b/util/launcher/src/shared_builder.rs @@ -408,6 +408,7 @@ fn register_tx_pool_callback(tx_pool_builder: &mut TxPoolServiceBuilder, notify: cycles: entry.cycles, size: entry.size, fee: entry.fee, + timestamp: entry.timestamp, }; notify_pending.notify_new_transaction(notify_tx_entry); })); @@ -426,6 +427,7 @@ fn register_tx_pool_callback(tx_pool_builder: &mut TxPoolServiceBuilder, notify: cycles: entry.cycles, size: entry.size, fee: entry.fee, + timestamp: entry.timestamp, }; notify_proposed.notify_proposed_transaction(notify_tx_entry); }, @@ -457,6 +459,7 @@ fn register_tx_pool_callback(tx_pool_builder: &mut TxPoolServiceBuilder, notify: cycles: entry.cycles, size: entry.size, fee: entry.fee, + timestamp: entry.timestamp, }; notify_reject.notify_reject_transaction(notify_tx_entry, reject); }, diff --git a/util/types/src/core/service.rs b/util/types/src/core/service.rs index 3ce3c131a5..41cefa876d 100644 --- a/util/types/src/core/service.rs +++ b/util/types/src/core/service.rs @@ -42,4 +42,6 @@ pub struct PoolTransactionEntry { pub size: usize, /// Transaction fee pub fee: Capacity, + /// The unix timestamp when entering the Txpool, unit: Millisecond + pub timestamp: u64, } diff --git a/util/types/src/core/tx_pool.rs b/util/types/src/core/tx_pool.rs index 410ea6f1d4..97f57b5b3a 100644 --- a/util/types/src/core/tx_pool.rs +++ b/util/types/src/core/tx_pool.rs @@ -102,6 +102,8 @@ pub struct TxEntryInfo { pub ancestors_cycles: u64, /// Number of in-tx-pool ancestor transactions pub ancestors_count: u64, + /// The unix timestamp when entering the Txpool, unit: Millisecond + pub timestamp: u64, } /// Array of transaction ids