Skip to content

Commit

Permalink
fix: get pending tx count by number (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo authored Nov 29, 2023
1 parent 0375d87 commit 36d941e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
8 changes: 6 additions & 2 deletions core/api/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,15 @@ where
}
}

async fn get_pending_tx_count(&self, ctx: Context, address: H160) -> ProtocolResult<U256> {
async fn get_pending_tx_count(
&self,
ctx: Context,
address: H160,
) -> ProtocolResult<(U256, Option<BlockNumber>)> {
self.mempool
.get_tx_count_by_address(ctx, address)
.await
.map(U256::from)
.map(|(n, b)| (U256::from(n), b))
}

async fn evm_call(
Expand Down
4 changes: 2 additions & 2 deletions core/api/src/jsonrpc/impl/web3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,14 @@ impl<Adapter: APIAdapter + 'static> Web3RpcServer for Web3RpcImpl<Adapter> {
) -> RpcResult<U256> {
match block_id.unwrap_or_default() {
BlockId::Pending => {
let pending_tx_count = self
let (pending_tx_count, block_number) = self
.adapter
.get_pending_tx_count(Context::new(), address)
.await
.map_err(|e| RpcError::Internal(e.to_string()))?;
Ok(self
.adapter
.get_account(Context::new(), address, BlockId::Pending.into())
.get_account(Context::new(), address, block_number)
.await
.map(|account| account.nonce + pending_tx_count)
.unwrap_or_default())
Expand Down
6 changes: 5 additions & 1 deletion core/mempool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ where
Ok(())
}

async fn get_tx_count_by_address(&self, _ctx: Context, address: H160) -> ProtocolResult<usize> {
async fn get_tx_count_by_address(
&self,
_ctx: Context,
address: H160,
) -> ProtocolResult<(usize, Option<BlockNumber>)> {
Ok(self.pool.get_tx_count_by_address(address))
}

Expand Down
17 changes: 14 additions & 3 deletions core/mempool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ use crate::MemPoolError;

pub struct PriorityPool {
sys_tx_bucket: BuiltInContractTxBucket,
// The transaction data in this queue should be consistent with the real queue.
// The difference is that it will be grouped by Sender and sorted by nonce to implement
// the replace by fee function.
pending_queue: Arc<DashMap<H160, PendingQueue>>,
// The transactions in this queue have not been processed yet and cannot be packaged.
co_queue: Arc<ArrayQueue<(TxPtr, U256)>>,
// Transactions in this queue will be packaged into blocks
real_queue: Arc<Mutex<Vec<TxPtr>>>,
// Record all transactions in the transaction pool
tx_map: DashMap<Hash, TxPtr>,
stock_len: AtomicUsize,
// Record the height at which the transaction first enters the transaction pool
// There must be a record for each block height, keeping timeout_config
pub(crate) timeout_gap: Mutex<BTreeMap<BlockNumber, HashSet<Hash>>>,
// When a transaction is not submitted for more than timeout_config blocks
// in the transaction pool, the transaction will be discarded.
timeout_config: u64,

flush_lock: Arc<RwLock<()>>,
Expand Down Expand Up @@ -79,12 +89,13 @@ impl PriorityPool {
pool
}

pub fn get_tx_count_by_address(&self, address: H160) -> usize {
pub fn get_tx_count_by_address(&self, address: H160) -> (usize, Option<BlockNumber>) {
let number = self.timeout_gap.lock().last_entry().map(|f| *f.key());
if let Some(set) = self.pending_queue.get(&address) {
return set.count();
return (set.count(), number);
}

0usize
(0usize, number)
}

pub fn insert_system_script_tx(&self, stx: SignedTransaction) -> ProtocolResult<()> {
Expand Down
6 changes: 5 additions & 1 deletion protocol/src/traits/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ pub trait APIAdapter: Send + Sync {
number: Option<BlockNumber>,
) -> ProtocolResult<Account>;

async fn get_pending_tx_count(&self, ctx: Context, address: H160) -> ProtocolResult<U256>;
async fn get_pending_tx_count(
&self,
ctx: Context,
address: H160,
) -> ProtocolResult<(U256, Option<BlockNumber>)>;

async fn evm_call(
&self,
Expand Down
6 changes: 5 additions & 1 deletion protocol/src/traits/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ pub trait MemPool: Send + Sync {
order_tx_hashes: &[Hash],
) -> ProtocolResult<()>;

async fn get_tx_count_by_address(&self, ctx: Context, address: H160) -> ProtocolResult<usize>;
async fn get_tx_count_by_address(
&self,
ctx: Context,
address: H160,
) -> ProtocolResult<(usize, Option<BlockNumber>)>;

fn get_tx_from_mem(&self, ctx: Context, tx_hash: &Hash) -> Option<SignedTransaction>;
fn set_args(&self, context: Context, state_root: MerkleRoot, gas_limit: u64, max_tx_size: u64);
Expand Down

0 comments on commit 36d941e

Please sign in to comment.