-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathhandlers.rs
More file actions
492 lines (447 loc) · 16.7 KB
/
handlers.rs
File metadata and controls
492 lines (447 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
use crate::error::into_rpc_error;
use crate::rpc::error::ensure_success;
use alloy_consensus::ReceiptEnvelope;
use alloy_consensus::Transaction as TransactionTrait;
use alloy_eips::BlockId;
use alloy_primitives::{Address, U64};
use alloy_primitives::{Bytes, B256, U256};
use alloy_rpc_types::{
state::StateOverride, Block, BlockNumberOrTag, BlockOverrides, FeeHistory, Transaction,
TransactionReceipt, TransactionRequest,
};
use alloy_rpc_types_trace::geth::GethDebugTracingOptions;
use alloy_rpc_types_trace::geth::{GethTrace, TraceResult};
use jsonrpsee::core::RpcResult;
use revm::context::result::ResultAndState;
use revm::Database;
use revm_database_interface::TryDatabaseCommit;
use sov_address::{EthereumAddress, FromVmAddress};
use sov_modules_api::macros::{config_value, rpc_gen};
use sov_modules_api::prelude::UnwrapInfallible;
use sov_modules_api::{charge_write, ApiStateAccessor, GasMeter, GasSpec, Spec};
use sov_rpc_eth_types::{EthApiError, LogWithExecutionTimestamp};
use sov_state::{Accessory, CompileTimeNamespace, StateCodec, StateItemEncoder};
use tracing::trace;
use crate::{apply_margins, Evm};
use std::ops::DerefMut;
#[rpc_gen(client, server)]
impl<S: Spec> Evm<S>
where
S::Address: FromVmAddress<EthereumAddress>,
{
/// Handler for `net_version`
#[rpc_method(name = "net_version")]
pub fn net_version(&self, _state: &mut ApiStateAccessor<S>) -> RpcResult<String> {
trace!(method = "net_version", "EVM module JSON-RPC request");
// Network ID is the same as chain ID for most networks
let chain_id = config_value!("CHAIN_ID");
Ok(chain_id.to_string())
}
/// Handler for: `eth_chainId`
#[rpc_method(name = "eth_chainId")]
pub fn chain_id(&self, _state: &mut ApiStateAccessor<S>) -> RpcResult<Option<U64>> {
let chain_id = config_value!("CHAIN_ID");
trace!(
chain_id = chain_id,
method = "eth_chainId",
"EVM module JSON-RPC request"
);
Ok(Some(U64::from(chain_id)))
}
/// Handler for `eth_getBlockByHash`
#[rpc_method(name = "eth_getBlockByHash")]
pub fn get_block_by_hash(
&self,
block_hash: B256,
details: Option<bool>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<Block>> {
trace!(
?block_hash,
method = "eth_getBlockByHash",
"EVM module JSON-RPC request"
);
let block_number = self
.block_hash_to_number
.get(&block_hash, state)
.unwrap_infallible();
let kind = details.unwrap_or_default().into();
Ok(match block_number {
Some(number) => self.get_block(
Some(BlockId::Number(BlockNumberOrTag::Number(number))),
kind,
state,
)?,
None => None,
})
}
/// Handler for: `eth_getBlockByNumber`
#[rpc_method(name = "eth_getBlockByNumber")]
pub fn get_block_by_number(
&self,
block_id: Option<BlockId>,
details: Option<bool>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<Block>> {
trace!(
?block_id,
method = "eth_getBlockByNumber",
"EVM module JSON-RPC request"
);
let kind = details.unwrap_or_default().into();
Ok(self.get_block(block_id, kind, state)?)
}
/// Handler for: `eth_getBalance`
#[rpc_method(name = "eth_getBalance")]
pub fn get_balance(
&self,
address: Address,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<U256> {
let mut state = self.resolve_state_for_block_id(block_id, state)?;
let balance = self
.db(state.deref_mut())
.basic(address)
.map_err(EthApiError::from)?
.map(|account| account.balance)
.unwrap_or_default();
trace!(
%address,
%balance,
method = "eth_getBalance",
"EVM module JSON-RPC request"
);
Ok(balance)
}
/// Handler for: `eth_getStorageAt`
#[rpc_method(name = "eth_getStorageAt")]
pub fn get_storage_at(
&self,
address: Address,
index: U256,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<U256> {
trace!(method = "eth_getStorageAt", ?block_id, %address, %index, "EVM module JSON-RPC request");
let mut state = self.resolve_state_for_block_id(block_id, state)?;
let storage_slot = self
.account_storage
.get(&(&address, &index), state.deref_mut())
.unwrap_infallible()
.unwrap_or_default();
Ok(storage_slot)
}
/// Handler for: `eth_getTransactionCount`
#[rpc_method(name = "eth_getTransactionCount")]
pub fn get_transaction_count(
&self,
address: Address,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<U64> {
let block_id = block_id.unwrap_or_else(BlockId::latest);
let is_pending = block_id.is_latest() || block_id.is_pending();
let nonce = {
let mut resolved_state = self.resolve_state_for_block_id(Some(block_id), state)?;
let ethereum_address: EthereumAddress = address.into();
let credential_id = ethereum_address.as_credential_id();
self.uniqueness_module
.next_nonce(&credential_id, resolved_state.deref_mut())
.unwrap_or_default()
};
let pending_nonce = if is_pending {
let pending_txs: Vec<_> = self.pending_transactions.collect_infallible(state);
pending_txs
.iter()
.filter(|pending| pending.transaction.signer == address)
.map(|pending| pending.transaction.signed_transaction.nonce())
.max()
.map(|nonce| nonce.saturating_add(1))
.unwrap_or(0)
} else {
0
};
let nonce = nonce.max(pending_nonce);
trace!(%address, nonce, method = "eth_getTransactionCount", "EVM module JSON-RPC request");
Ok(U64::from(nonce))
}
/// Handler for: `eth_getCode`
#[rpc_method(name = "eth_getCode")]
pub fn get_code(
&self,
address: Address,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Bytes> {
trace!(method = "eth_getCode", %address, ?block_id, "EVM module JSON-RPC request");
let state = self.resolve_state_for_block_id(block_id, state)?;
Ok(self.get_contract_code(address, state).unwrap_or_default())
}
/// Handler for: `eth_feeHistory`
/// Returns historical gas price and usage data for recent blocks.
///
/// This endpoint helps wallets and users determine appropriate gas prices
/// by exposing the rollup's EIP-1559 style base fee history.
#[rpc_method(name = "eth_feeHistory")]
pub fn fee_history(
&self,
block_count: U64,
newest_block: BlockNumberOrTag,
reward_percentiles: Option<Vec<f64>>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<FeeHistory> {
let block_count = block_count.to::<u64>();
trace!(
block_count,
?newest_block,
?reward_percentiles,
method = "eth_feeHistory",
"EVM module JSON-RPC request"
);
Ok(self.get_fee_history(
block_count,
newest_block,
reward_percentiles.as_deref(),
state,
)?)
}
/// Handler for: `eth_getTransactionByHash`
#[rpc_method(name = "eth_getTransactionByHash")]
pub fn get_transaction_by_hash(
&self,
hash: B256,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<Transaction>> {
let transaction = self
.get_transaction(hash, state)
.or_else(|| self.get_pending_transaction(hash, state));
trace!(
%hash,
?transaction,
method = "eth_getTransactionByHash",
"EVM module JSON-RPC request"
);
Ok(transaction)
}
/// Handler for: `eth_getBlockReceipts`
#[rpc_method(name = "eth_getBlockReceipts")]
pub fn get_block_receipts(
&self,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<Vec<TransactionReceipt<ReceiptEnvelope<LogWithExecutionTimestamp>>>>>
{
trace!(
?block_id,
method = "eth_getBlockReceipts",
"EVM module JSON-RPC request"
);
Ok(self.get_receipts(block_id, state)?)
}
/// Handler for: `eth_getTransactionReceipt`
#[rpc_method(name = "eth_getTransactionReceipt")]
pub fn get_transaction_receipt(
&self,
hash: B256,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<TransactionReceipt<ReceiptEnvelope<LogWithExecutionTimestamp>>>> {
trace!(
%hash,
method = "eth_getTransactionReceipt",
"EVM module JSON-RPC request"
);
Ok(self.get_receipt_by_hash(hash, state))
}
/// Handler for: `eth_call`
//https://github.com/paradigmxyz/reth/blob/f577e147807a783438a3f16aad968b4396274483/crates/rpc/rpc/src/eth/api/transactions.rs#L502
#[rpc_method(name = "eth_call")]
pub fn eth_call(
&self,
request: TransactionRequest,
block_id: Option<BlockId>,
_state_overrides: Option<StateOverride>,
_block_overrides: Option<Box<BlockOverrides>>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Bytes> {
trace!(
method = "eth_call",
?block_id,
"EVM module JSON-RPC request"
);
let result = self.call(request, block_id, state)?.result;
Ok(ensure_success(result)?)
}
/// Handler for: `eth_blockNumber`
#[rpc_method(name = "eth_blockNumber")]
pub fn block_number(&self, state: &mut ApiStateAccessor<S>) -> RpcResult<U256> {
trace!(method = "eth_blockNumber", "EVM module JSON-RPC request");
let block_number_range = self.block_numbers(state);
Ok(U256::from(*block_number_range.end()))
}
/// Handler for: `eth_estimateGas`
// https://github.com/paradigmxyz/reth/blob/main/crates/rpc/rpc/src/eth/api/call.rs#L172
#[rpc_method(name = "eth_estimateGas")]
pub fn eth_estimate_gas(
&self,
request: TransactionRequest,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<U64> {
trace!(
?block_id,
method = "eth_estimateGas",
"EVM module JSON-RPC request"
);
// Add 1,000 bytes to account for all other data in the Transaction structure, apart from call data.
let tx_size = request.input.input().as_ref().map(|i| i.len()).unwrap_or(0) + 1000;
let ResultAndState {
result,
state: changes,
} = self.call(request, block_id, state)?;
self.db(state)
.try_commit(changes)
.expect("Gas meter is initialized with INF");
let gas_used = result.gas_used();
// Charge for logs storage in the receipt
// Other receipt fields are small and covered by the constant margin
let logs = result.logs();
let logs_size = self
.receipts
.codec()
.value_codec()
.encode_to_vec(&logs)
.len();
charge_write(
state,
Accessory::NAMESPACE,
&self.receipts.slot_key(&u64::MAX),
logs_size as u32,
)
.map_err(into_rpc_error)?;
let gas_meter = state
.try_as_basic_gas_meter()
.expect("ApiState has BasicGasMeter");
sov_modules_api::gas::charge_gas_for_sig(gas_meter, tx_size)
.expect("Gas meter is initialized with INF");
sov_modules_api::transaction::charge_tx_deserialization(gas_meter, tx_size)
.expect("Gas meter is initialized with INF");
gas_meter
.charge_linear_gas(<S as GasSpec>::gas_to_charge_per_evm_gas(), gas_used as u32)
.expect("Gas meter is initialized with INF");
let total_gas_used =
gas_meter.initial_gas.as_ref()[0] - gas_meter.remaining_gas.as_ref()[0];
Ok(U64::from(apply_margins(total_gas_used)?))
}
/// Handler for `debug_traceBlockByNumber`
#[rpc_method(name = "debug_traceBlockByNumber")]
pub fn debug_trace_block_by_number(
&self,
block: BlockNumberOrTag,
opts: Option<GethDebugTracingOptions>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Vec<TraceResult>> {
trace!(
method = "debug_traceBlockByNumber",
"EVM module JSON-RPC request"
);
Ok(self.trace_block_by_number(block, opts.unwrap_or_default(), state)?)
}
/// Handler for: `debug_traceTransaction`
#[rpc_method(name = "debug_traceTransaction")]
pub fn debug_trace_transaction(
&self,
tx_hash: B256,
opts: Option<GethDebugTracingOptions>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<GethTrace> {
trace!(method = "debug_traceTransaction", %tx_hash, "EVM module JSON-RPC request");
Ok(self.trace_transaction(tx_hash, opts.unwrap_or_default(), state)?)
}
// ========== web3 namespace ==========
/// Handler for: `web3_clientVersion`
/// Returns the current client version.
#[rpc_method(name = "web3_clientVersion")]
pub fn web3_client_version(&self, _state: &mut ApiStateAccessor<S>) -> RpcResult<String> {
trace!(method = "web3_clientVersion", "EVM module JSON-RPC request");
Ok(format!(
"sov-evm/{}",
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown")
))
}
/// Handler for: `web3_sha3`
/// Returns Keccak-256 hash of the given data.
#[rpc_method(name = "web3_sha3")]
pub fn web3_sha3(&self, data: Bytes, _state: &mut ApiStateAccessor<S>) -> RpcResult<B256> {
trace!(method = "web3_sha3", "EVM module JSON-RPC request");
Ok(alloy_primitives::keccak256(&data))
}
// ========== net namespace ==========
/// Handler for: `net_listening`
/// Returns true if client is actively listening for network connections.
#[rpc_method(name = "net_listening")]
pub fn net_listening(&self, _state: &mut ApiStateAccessor<S>) -> RpcResult<bool> {
trace!(method = "net_listening", "EVM module JSON-RPC request");
// Rollup is always accepting connections via RPC
Ok(true)
}
/// Handler for: `eth_maxPriorityFeePerGas`
/// Returns the current max priority fee per gas.
#[rpc_method(name = "eth_maxPriorityFeePerGas")]
pub fn eth_max_priority_fee_per_gas(
&self,
_state: &mut ApiStateAccessor<S>,
) -> RpcResult<U256> {
trace!(
method = "eth_maxPriorityFeePerGas",
"EVM module JSON-RPC request"
);
// Rollup uses preferred sequencer model, no priority fees
Ok(U256::ZERO)
}
/// Handler for: `eth_getBlockTransactionCountByNumber`
/// Returns the number of transactions in a block by block number.
#[rpc_method(name = "eth_getBlockTransactionCountByNumber")]
pub fn get_block_transaction_count_by_number(
&self,
block_id: Option<BlockId>,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<U64>> {
trace!(
?block_id,
method = "eth_getBlockTransactionCountByNumber",
"EVM module JSON-RPC request"
);
let block = self.get_block(block_id, false.into(), state)?;
Ok(block.map(|b| U64::from(b.transactions.len())))
}
/// Handler for: `eth_getBlockTransactionCountByHash`
/// Returns the number of transactions in a block by block hash.
#[rpc_method(name = "eth_getBlockTransactionCountByHash")]
pub fn get_block_transaction_count_by_hash(
&self,
block_hash: B256,
state: &mut ApiStateAccessor<S>,
) -> RpcResult<Option<U64>> {
trace!(
%block_hash,
method = "eth_getBlockTransactionCountByHash",
"EVM module JSON-RPC request"
);
let block_number = self
.block_hash_to_number
.get(&block_hash, state)
.unwrap_infallible();
match block_number {
Some(number) => {
let block = self.get_block(
Some(BlockId::Number(BlockNumberOrTag::Number(number))),
false.into(),
state,
)?;
Ok(block.map(|b| U64::from(b.transactions.len())))
}
None => Ok(None),
}
}
}