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

Call stack verification, rpc endpoints for eth_getUserOperation(ByHash|Receipt) #77

Merged
merged 4 commits into from
Mar 21, 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
33 changes: 31 additions & 2 deletions src/contracts/gen.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
use ethers::contract::abigen;
use ethers::{
contract::{abigen, EthCall},
types::Selector,
};
use lazy_static::lazy_static;
use std::collections::HashMap;

abigen!(EntryPointAPI, "$OUT_DIR/IEntryPoint.sol/IEntryPoint.json");
abigen!(
StakeManagerAPI,
"$OUT_DIR/IStakeManager.sol/IStakeManager.json"
);
abigen!(PaymasterAPI, "$OUT_DIR/IPaymaster.sol/IPaymaster.json");

lazy_static! {
pub static ref CONTRACTS_FUNCTIONS: HashMap<Selector, String> = {
let mut map = HashMap::new();
// entry point
map.insert(entry_point_api::AddStakeCall::selector(), entry_point_api::AddStakeCall::function_name().to_string());
map.insert(entry_point_api::BalanceOfCall::selector(), entry_point_api::BalanceOfCall::function_name().to_string());
map.insert(entry_point_api::DepositToCall::selector(), entry_point_api::DepositToCall::function_name().to_string());
map.insert(entry_point_api::GetDepositInfoCall::selector(), entry_point_api::GetDepositInfoCall::function_name().to_string());
map.insert(entry_point_api::GetSenderAddressCall::selector(), entry_point_api::GetSenderAddressCall::function_name().to_string());
map.insert(entry_point_api::GetUserOpHashCall::selector(), entry_point_api::GetUserOpHashCall::function_name().to_string());
map.insert(entry_point_api::HandleAggregatedOpsCall::selector(), entry_point_api::HandleAggregatedOpsCall::function_name().to_string());
map.insert(entry_point_api::HandleOpsCall::selector(), entry_point_api::HandleOpsCall::function_name().to_string());
map.insert(entry_point_api::SimulateHandleOpCall::selector(), entry_point_api::SimulateHandleOpCall::function_name().to_string());
map.insert(entry_point_api::SimulateValidationCall::selector(), entry_point_api::SimulateValidationCall::function_name().to_string());
map.insert(entry_point_api::UnlockStakeCall::selector(), entry_point_api::UnlockStakeCall::function_name().to_string());
map.insert(entry_point_api::WithdrawStakeCall::selector(), entry_point_api::WithdrawStakeCall::function_name().to_string());
map.insert(entry_point_api::WithdrawToCall::selector(), entry_point_api::WithdrawToCall::function_name().to_string());
// paymaster
map.insert(paymaster_api::PostOpCall::selector(), paymaster_api::PostOpCall::function_name().to_string());
map.insert(paymaster_api::ValidatePaymasterUserOpCall::selector(), paymaster_api::ValidatePaymasterUserOpCall::function_name().to_string());
map
};
}

// The below generations are not used now. So we comment them out for now.
// abigen!(
Expand All @@ -16,7 +46,6 @@ abigen!(
// Create2Deployer,
// "$OUT_DIR/ICreate2Deployer.sol/ICreate2Deployer.json"
// );
// abigen!(Paymaster, "$OUT_DIR/IPaymaster.sol/IPaymaster.json");
// abigen!(Account, "$OUT_DIR/IAccount.sol/IAccount.json");
// abigen!(
// UserOperation,
Expand Down
14 changes: 13 additions & 1 deletion src/contracts/tracer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::format_err;
use ethers::types::{Address, Bytes, GethTrace};
use ethers::types::{Address, Bytes, GethTrace, U256};
use serde::Deserialize;
use std::collections::HashMap;

Expand Down Expand Up @@ -55,6 +55,18 @@ pub struct Call {
pub to: Option<Address>,
pub method: Option<Bytes>,
pub gas: Option<u64>,
pub value: Option<U256>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)]
pub struct CallEntry {
pub typ: String,
pub from: Option<Address>,
pub to: Option<Address>,
pub method: Option<String>,
pub ret: Option<Bytes>,
pub rev: Option<Bytes>,
pub value: Option<U256>,
}

// https://github.com/eth-infinitism/bundler/blob/main/packages/bundler/src/BundlerCollectorTracer.ts
Expand Down
43 changes: 37 additions & 6 deletions src/rpc/eth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
rpc::eth_api::EthApiServer,
types::user_operation::{
UserOperation, UserOperationGasEstimation, UserOperationHash, UserOperationPartial,
UserOperationReceipt,
UserOperation, UserOperationByHash, UserOperationGasEstimation, UserOperationHash,
UserOperationPartial, UserOperationReceipt,
},
uopool::server::uopool::{
uo_pool_client::UoPoolClient, AddRequest, AddResult, EstimateUserOperationGasRequest,
Expand All @@ -17,10 +17,11 @@ use ethers::{
};
use jsonrpsee::{
core::RpcResult,
tracing::info,
types::{error::CallError, ErrorObject},
};

const USER_OPERATION_HASH_ERROR_CODE: i32 = -32601;

pub struct EthApiServerImpl {
pub call_gas_limit: u64,
pub uopool_grpc_client: UoPoolClient<tonic::transport::Channel>,
Expand Down Expand Up @@ -120,9 +121,39 @@ impl EthApiServer for EthApiServerImpl {

async fn get_user_operation_receipt(
&self,
user_operation_hash: UserOperationHash,
user_operation_hash: String,
) -> RpcResult<Option<UserOperationReceipt>> {
info!("{:?}", user_operation_hash);
Ok(None)
match serde_json::from_str::<UserOperationHash>(&user_operation_hash) {
Ok(_user_operation_hash) => {
// TODO: implement
Ok(None)
}
Err(_) => Err(jsonrpsee::core::Error::Call(CallError::Custom(
ErrorObject::owned(
USER_OPERATION_HASH_ERROR_CODE,
"Missing/invalid userOpHash".to_string(),
None::<bool>,
),
))),
}
}

async fn get_user_operation_by_hash(
&self,
user_operation_hash: String,
) -> RpcResult<Option<UserOperationByHash>> {
match serde_json::from_str::<UserOperationHash>(&user_operation_hash) {
Ok(_user_operation_hash) => {
// TODO: implement
Ok(None)
}
Err(_) => Err(jsonrpsee::core::Error::Call(CallError::Custom(
ErrorObject::owned(
USER_OPERATION_HASH_ERROR_CODE,
"Missing/invalid userOpHash".to_string(),
None::<bool>,
),
))),
}
}
}
12 changes: 9 additions & 3 deletions src/rpc/eth_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use ethers::types::{Address, U64};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};

use crate::types::user_operation::{
UserOperation, UserOperationGasEstimation, UserOperationHash, UserOperationPartial,
UserOperationReceipt,
UserOperation, UserOperationByHash, UserOperationGasEstimation, UserOperationHash,
UserOperationPartial, UserOperationReceipt,
};

#[rpc(server, namespace = "eth")]
Expand Down Expand Up @@ -31,6 +31,12 @@ pub trait EthApi {
#[method(name = "getUserOperationReceipt")]
async fn get_user_operation_receipt(
&self,
user_operation_hash: UserOperationHash,
user_operation_hash: String,
) -> RpcResult<Option<UserOperationReceipt>>;

#[method(name = "getUserOperationByHash")]
async fn get_user_operation_by_hash(
&self,
user_operation_hash: String,
) -> RpcResult<Option<UserOperationByHash>>;
}
8 changes: 8 additions & 0 deletions src/types/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ lazy_static! {
set
};
pub static ref CREATE2_OPCODE: String = "CREATE2".to_string();
pub static ref RETURN_OPCODE: String = "RETURN".to_string();
pub static ref REVERT_OPCODE: String = "REVERT".to_string();
pub static ref CREATE_OPCODE: String = "CREATE".to_string();
pub static ref PAYMASTER_VALIDATION_FUNCTION: String = "validatePaymasterUserOp".to_string();
}

pub struct StakeInfo {
Expand All @@ -53,6 +57,7 @@ pub enum SimulateValidationError<M: Middleware> {
OpcodeValidation { entity: String, opcode: String },
UserOperationExecution { message: String },
StorageAccessValidation { slot: String },
CallStackValidation { message: String },
Middleware(M::Error),
UnknownError { error: String },
}
Expand All @@ -76,6 +81,9 @@ impl<M: Middleware> From<SimulateValidationError<M>> for SimulationError {
format!("Storage access validation failed for slot: {slot}"),
None::<bool>,
),
SimulateValidationError::CallStackValidation { message } => {
SimulationError::owned(OPCODE_VALIDATION_ERROR_CODE, message, None::<bool>)
}
SimulateValidationError::Middleware(_) => {
SimulationError::from(ErrorCode::InternalError)
}
Expand Down
12 changes: 11 additions & 1 deletion src/types/user_operation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::contracts::gen::entry_point_api;
use ethers::abi::{AbiDecode, AbiEncode};
use ethers::prelude::{EthAbiCodec, EthAbiType};
use ethers::types::{Address, Bytes, TransactionReceipt, H256, U256};
use ethers::types::{Address, BlockNumber, Bytes, TransactionReceipt, H256, U256};
use ethers::utils::keccak256;
use reth_db::table::{Compress, Decode, Decompress, Encode};
use rustc_hex::FromHexError;
Expand Down Expand Up @@ -156,6 +156,16 @@ pub struct UserOperationReceipt {
pub receipt: TransactionReceipt,
}

#[derive(Serialize, Deserialize)]
pub struct UserOperationByHash {
#[serde(flatten)]
pub user_operation: UserOperation,
pub entry_point: Address,
pub block_number: BlockNumber,
pub block_hash: H256,
pub transaction_hash: H256,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserOperationPartial {
Expand Down
46 changes: 24 additions & 22 deletions src/uopool/memory_reputation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,31 +160,33 @@ impl Reputation for MemoryReputation {
}

if let Some(entity) = self.entities.get(&stake_info.address) {
let error = if entity.status == ReputationStatus::BANNED {
BadReputationError::EntityBanned {
if entity.status == ReputationStatus::BANNED {
return Err(BadReputationError::EntityBanned {
address: stake_info.address,
title: title.to_string(),
}
} else if stake_info.stake < self.min_stake {
BadReputationError::StakeTooLow {
address: stake_info.address,
title: title.to_string(),
min_stake: self.min_stake,
min_unstake_delay: self.min_unstake_delay,
}
} else if stake_info.unstake_delay < self.min_unstake_delay {
BadReputationError::UnstakeDelayTooLow {
address: stake_info.address,
title: title.to_string(),
min_stake: self.min_stake,
min_unstake_delay: self.min_unstake_delay,
}
} else {
return Ok(());
};

return Err(error);
});
}
}

let error = if stake_info.stake < self.min_stake {
BadReputationError::StakeTooLow {
address: stake_info.address,
title: title.to_string(),
min_stake: self.min_stake,
min_unstake_delay: self.min_unstake_delay,
}
} else if stake_info.unstake_delay < self.min_unstake_delay {
BadReputationError::UnstakeDelayTooLow {
address: stake_info.address,
title: title.to_string(),
min_stake: self.min_stake,
min_unstake_delay: self.min_unstake_delay,
}
} else {
return Ok(());
};

return Err(error);
}

Ok(())
Expand Down
Loading