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

fix: check mempool when call eth_getTransactionByHash #1526

Merged
merged 2 commits into from
Nov 13, 2023
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
2 changes: 1 addition & 1 deletion .github/workflows/web3_compatible.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: axonweb3/axon-test
ref: 0e2e379ac17175c301115127ea679f1d0085ddc3
ref: be8b502423fa41d4656ec2369d3ee5a729f56326
Flouse marked this conversation as resolved.
Show resolved Hide resolved
path: axon-test

- uses: actions/setup-node@v4
Expand Down
6 changes: 5 additions & 1 deletion core/api/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ where
ctx: Context,
tx_hash: Hash,
) -> ProtocolResult<Option<SignedTransaction>> {
self.storage.get_transaction_by_hash(ctx, &tx_hash).await
if let Some(tx) = self.mempool.get_tx_from_mem(ctx.clone(), &tx_hash) {
Ok(Some(tx))
} else {
self.storage.get_transaction_by_hash(ctx, &tx_hash).await
}
}

async fn get_transactions_by_hashes(
Expand Down
4 changes: 0 additions & 4 deletions core/api/src/jsonrpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ pub enum RpcError {
GasLimitIsZero,
#[display(fmt = "Transaction is not signed")]
TransactionIsNotSigned,
#[display(fmt = "Cannot get receipt by hash")]
CannotGetReceiptByHash,
#[display(fmt = "Cannot get latest block")]
CannotGetLatestBlock,
#[display(fmt = "Invalid block hash")]
Expand Down Expand Up @@ -80,7 +78,6 @@ impl RpcError {
RpcError::GasLimitIsTooLarge => -40009,
RpcError::GasLimitIsZero => -40010,
RpcError::TransactionIsNotSigned => -40011,
RpcError::CannotGetReceiptByHash => -40012,
RpcError::CannotGetLatestBlock => -40013,
RpcError::InvalidBlockHash => -40014,
RpcError::InvalidFromBlockNumber(_) => -40015,
Expand Down Expand Up @@ -118,7 +115,6 @@ impl From<RpcError> for ErrorObjectOwned {
RpcError::GasLimitIsTooLarge => ErrorObject::owned(err_code, err, none_data),
RpcError::GasLimitIsZero => ErrorObject::owned(err_code, err, none_data),
RpcError::TransactionIsNotSigned => ErrorObject::owned(err_code, err, none_data),
RpcError::CannotGetReceiptByHash => ErrorObject::owned(err_code, err, none_data),
RpcError::CannotGetLatestBlock => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidBlockHash => ErrorObject::owned(err_code, err, none_data),
RpcError::InvalidFromBlockNumber(_) => ErrorObject::owned(err_code, err, none_data),
Expand Down
11 changes: 8 additions & 3 deletions core/api/src/jsonrpc/impl/web3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{sync::Arc, time::Duration};

use ckb_types::core::cell::{CellProvider, CellStatus};
use ckb_types::prelude::Entity;
Expand All @@ -13,7 +13,8 @@ use protocol::types::{
MIN_TRANSACTION_GAS_LIMIT, U256,
};
use protocol::{
async_trait, ckb_blake2b_256, codec::ProtocolCodec, lazy::PROTOCOL_VERSION, ProtocolResult,
async_trait, ckb_blake2b_256, codec::ProtocolCodec, lazy::PROTOCOL_VERSION, tokio::time::sleep,
ProtocolResult, MEMPOOL_REFRESH_TIMEOUT,
};

use core_executor::system_contract::DataProvider;
Expand Down Expand Up @@ -315,6 +316,10 @@ impl<Adapter: APIAdapter + 'static> Web3RpcServer for Web3RpcImpl<Adapter> {
.await
.map_err(|e| RpcError::Internal(e.to_string()))?;

// TODO `eth_getTransactionCount(..., "pending")` should be synchronous with
// `eth_sendRawTransaction`. Temporary solution for axonweb3/axon#1544.
sleep(Duration::from_millis(MEMPOOL_REFRESH_TIMEOUT)).await;

Ok(hash)
}

Expand All @@ -335,7 +340,7 @@ impl<Adapter: APIAdapter + 'static> Web3RpcServer for Web3RpcImpl<Adapter> {
{
Ok(Some((stx, receipt).into()))
} else {
Err(RpcError::CannotGetReceiptByHash.into())
Ok(Some(stx.into()))
}
} else {
Ok(None)
Expand Down
4 changes: 2 additions & 2 deletions core/mempool/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use parking_lot::{Mutex, RwLock};

use protocol::tokio::{self, time::sleep};
use protocol::types::{BlockNumber, Bytes, Hash, PackedTxHashes, SignedTransaction, H160, U256};
use protocol::ProtocolResult;
use protocol::{ProtocolResult, MEMPOOL_REFRESH_TIMEOUT};

use crate::tx_wrapper::{PendingQueue, TxPtr, TxWrapper};
use crate::MemPoolError;
Expand Down Expand Up @@ -72,7 +72,7 @@ impl PriorityPool {
}
}

sleep(Duration::from_millis(50)).await;
sleep(Duration::from_millis(MEMPOOL_REFRESH_TIMEOUT)).await;
}
});

Expand Down
2 changes: 2 additions & 0 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub use {
trie,
};

pub const MEMPOOL_REFRESH_TIMEOUT: u64 = 50;

#[derive(Copy, Clone, Debug)]
pub enum ProtocolErrorKind {
// traits
Expand Down