Skip to content

Commit

Permalink
Feat/debug transaction (kkrt-labs#867)
Browse files Browse the repository at this point in the history
* add debug api debug transaction

* fix docker compose bug

* hail move

* boyscout commit to solve estimate gas if kakarot.basefee > 0

* add tests for debug api
  • Loading branch information
Eikix authored Mar 19, 2024
1 parent 4dec3f6 commit ad866f0
Show file tree
Hide file tree
Showing 19 changed files with 629 additions and 34 deletions.
2 changes: 0 additions & 2 deletions .github/.codecov.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @clementwalter @eikix
* @clementwalter @eikix @greged93
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
installer-parallel: true
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
Expand Down
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ ethers-solc = { version = "2.0.9", default-features = false }
jsonrpsee = { version = "0.21.0", features = ["macros", "server"] }
reth-primitives = { git = "https://github.com/paradigmxyz/reth.git", tag = "v0.1.0-alpha.18", default-features = false }
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth.git", tag = "v0.1.0-alpha.18", default-features = false }
reth-rpc-types-compat = { git = "https://github.com/paradigmxyz/reth.git", tag = "v0.1.0-alpha.18", default-features = false }

# Serde
serde = { version = "1.0.188", default-features = false, features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ services:
- MONGO_DATABASE_NAME=kakarot-local
volumes:
# Mount the volume on workdir and use .env stored in root of the volume
- deployments:/usr/app
- deployments:/usr/src/app
depends_on:
deployments-parser:
condition: service_completed_successfully
Expand Down
2 changes: 1 addition & 1 deletion docker/rpc/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN apt-get update && \
RUN cargo build --release --bin kakarot-rpc

FROM debian:bookworm-slim AS runtime
WORKDIR /usr/app
WORKDIR /usr/src/app
COPY --from=builder /app/target/release/kakarot-rpc /usr/local/bin

# Expose the port that the RPC server will run on
Expand Down
11 changes: 10 additions & 1 deletion src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,16 @@ where
let calldata: Vec<FieldElement> = data.into_iter().map_into().collect();

let gas_limit = into_via_try_wrapper!(request.gas.unwrap_or_else(|| U256::from(CALL_REQUEST_GAS_LIMIT)));
let gas_price = into_via_try_wrapper!(request.gas_price.unwrap_or_default());

// We cannot unwrap_or_default() here because Kakarot.eth_call will
// Reject transactions with gas_price < Kakarot.base_fee
let gas_price = {
let gas_price = match request.gas_price {
Some(gas_price) => gas_price,
None => self.gas_price().await?,
};
into_via_try_wrapper!(gas_price)
};

let value = into_via_try_wrapper!(request.value.unwrap_or_default());

Expand Down
33 changes: 33 additions & 0 deletions src/eth_rpc/api/debug_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use jsonrpsee::core::RpcResult as Result;
use jsonrpsee::proc_macros::rpc;
use reth_primitives::{Bytes, B256};
use reth_rpc_types::BlockId;

/// Debug API
/// Taken from Reth's DebugApi trait:
/// <https://github.com/paradigmxyz/reth/blob/5d6ac4c815c562677d7ae6ad6b422b55ef4ed8e2/crates/rpc/rpc-api/src/debug.rs#L14>
#[rpc(server, namespace = "debug")]
#[async_trait]
pub trait DebugApi {
/// Returns an RLP-encoded header.
#[method(name = "getRawHeader")]
async fn raw_header(&self, block_id: BlockId) -> Result<Bytes>;

/// Returns an RLP-encoded block.
#[method(name = "getRawBlock")]
async fn raw_block(&self, block_id: BlockId) -> Result<Bytes>;

/// Returns a EIP-2718 binary-encoded transaction.
///
/// If this is a pooled EIP-4844 transaction, the blob sidecar is included.
#[method(name = "getRawTransaction")]
async fn raw_transaction(&self, hash: B256) -> Result<Option<Bytes>>;

/// Returns an array of EIP-2718 binary-encoded transactions for the given [BlockId].
#[method(name = "getRawTransactions")]
async fn raw_transactions(&self, block_id: BlockId) -> Result<Vec<Bytes>>;

/// Returns an array of EIP-2718 binary-encoded receipts.
#[method(name = "getRawReceipts")]
async fn raw_receipts(&self, block_id: BlockId) -> Result<Vec<Bytes>>;
}
1 change: 1 addition & 0 deletions src/eth_rpc/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod alchemy_api;
pub mod debug_api;
pub mod eth_api;
pub mod net_api;
pub mod web3_api;
7 changes: 6 additions & 1 deletion src/eth_rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use jsonrpsee::{Methods, RpcModule};

use crate::eth_provider::provider::EthereumProvider;
use crate::eth_rpc::api::alchemy_api::AlchemyApiServer;
use crate::eth_rpc::api::debug_api::DebugApiServer;
use crate::eth_rpc::api::eth_api::EthApiServer;
use crate::eth_rpc::api::net_api::NetApiServer;
use crate::eth_rpc::api::web3_api::Web3ApiServer;
use crate::eth_rpc::servers::alchemy_rpc::AlchemyRpc;
use crate::eth_rpc::servers::debug_rpc::DebugRpc;
use crate::eth_rpc::servers::eth_rpc::KakarotEthRpc;
use crate::eth_rpc::servers::net_rpc::NetRpc;
use crate::eth_rpc::servers::web3_rpc::Web3Rpc;
Expand All @@ -22,6 +24,7 @@ pub enum KakarotRpcModule {
Alchemy,
Web3,
Net,
Debug,
}

pub struct KakarotRpcModuleBuilder<P>
Expand All @@ -41,14 +44,16 @@ where
let eth_rpc_module = KakarotEthRpc::new(eth_provider.clone()).into_rpc();
let alchemy_rpc_module = AlchemyRpc::new(eth_provider.clone()).into_rpc();
let web3_rpc_module = Web3Rpc::default().into_rpc();
let net_rpc_module = NetRpc::new(eth_provider).into_rpc();
let net_rpc_module = NetRpc::new(eth_provider.clone()).into_rpc();
let debug_rpc_module = DebugRpc::new(eth_provider).into_rpc();

let mut modules: HashMap<KakarotRpcModule, Methods> = HashMap::new();

modules.insert(KakarotRpcModule::Eth, eth_rpc_module.into());
modules.insert(KakarotRpcModule::Alchemy, alchemy_rpc_module.into());
modules.insert(KakarotRpcModule::Web3, web3_rpc_module.into());
modules.insert(KakarotRpcModule::Net, net_rpc_module.into());
modules.insert(KakarotRpcModule::Debug, debug_rpc_module.into());

Self { modules, _phantom: PhantomData }
}
Expand Down
66 changes: 66 additions & 0 deletions src/eth_rpc/servers/debug_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use crate::eth_provider::error::EthProviderError;
use crate::eth_rpc::api::debug_api::DebugApiServer;
use crate::{eth_provider::provider::EthereumProvider, models::transaction::rpc_transaction_to_primitive};
// use alloy_rlp::Encodable;
use jsonrpsee::core::{async_trait, RpcResult as Result};
use reth_primitives::{Bytes, TransactionSigned, B256};
use reth_rpc_types::BlockId;

/// The RPC module for the implementing Net api
pub struct DebugRpc<P: EthereumProvider> {
eth_provider: P,
}

impl<P: EthereumProvider> DebugRpc<P> {
pub const fn new(eth_provider: P) -> Self {
Self { eth_provider }
}
}

#[async_trait]
impl<P: EthereumProvider + Send + Sync + 'static> DebugApiServer for DebugRpc<P> {
/// Returns an RLP-encoded header.
async fn raw_header(&self, _block_id: BlockId) -> Result<Bytes> {
Err(EthProviderError::MethodNotSupported("debug_rawHeader".to_string()).into())
}

/// Returns an RLP-encoded block.
async fn raw_block(&self, _block_id: BlockId) -> Result<Bytes> {
Err(EthProviderError::MethodNotSupported("debug_rawBlock".to_string()).into())
}

/// Returns a EIP-2718 binary-encoded transaction.
///
/// If this is a pooled EIP-4844 transaction, the blob sidecar is included.
async fn raw_transaction(&self, hash: B256) -> Result<Option<Bytes>> {
let transaction = self.eth_provider.transaction_by_hash(hash).await?;

if let Some(tx) = transaction {
let mut raw_transaction = Vec::new();
let signature = tx.signature.ok_or(EthProviderError::ValueNotFound("signature".to_string()))?;
let tx = rpc_transaction_to_primitive(tx).map_err(EthProviderError::from)?;
TransactionSigned::from_transaction_and_signature(
tx,
reth_primitives::Signature {
r: signature.r,
s: signature.s,
odd_y_parity: signature.y_parity.unwrap_or(reth_rpc_types::Parity(false)).0,
},
)
.encode_enveloped(&mut raw_transaction);
Ok(Some(Bytes::from(raw_transaction)))
} else {
Ok(None)
}
}

/// Returns an array of EIP-2718 binary-encoded transactions for the given [BlockId].
async fn raw_transactions(&self, _block_id: BlockId) -> Result<Vec<Bytes>> {
Err(EthProviderError::MethodNotSupported("debug_rawTransactions".to_string()).into())
}

/// Returns an array of EIP-2718 binary-encoded receipts.
async fn raw_receipts(&self, _block_id: BlockId) -> Result<Vec<Bytes>> {
Err(EthProviderError::MethodNotSupported("debug_rawReceipts".to_string()).into())
}
}
1 change: 1 addition & 0 deletions src/eth_rpc/servers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod alchemy_rpc;
pub mod debug_rpc;
pub mod eth_rpc;
pub mod net_rpc;
pub mod web3_rpc;
3 changes: 3 additions & 0 deletions src/models/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum ConversionError {
/// Uint conversion error
#[error("Uint conversion error: {0}")]
UintConversionError(String),
/// Transaction conversion error
#[error("Transaction conversion error: {0}")]
TransactionConversionError(String),
/// Other conversion error
#[error("failed to convert value: {0}")]
Other(String),
Expand Down
1 change: 1 addition & 0 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod balance;
pub mod block;
pub mod errors;
pub mod felt;
pub mod transaction;
Loading

0 comments on commit ad866f0

Please sign in to comment.