Skip to content

Commit b9608c7

Browse files
authored
refactor: eliminate unnecessary clones during request handling (#4547)
1 parent 2d227f3 commit b9608c7

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

crates/edr_rpc_server/src/node.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -378,14 +378,11 @@ impl Node {
378378
pub async fn sign(
379379
&self,
380380
address: &Address,
381-
message: &ZeroXPrefixedBytes,
381+
message: ZeroXPrefixedBytes,
382382
) -> Result<Signature, NodeError> {
383383
let node_data = self.lock_data().await;
384384
match node_data.local_accounts.get(address) {
385-
Some(secret_key) => Ok(Signature::new(
386-
&Bytes::from(message.clone())[..],
387-
secret_key,
388-
)?),
385+
Some(secret_key) => Ok(Signature::new(&Bytes::from(message)[..], secret_key)?),
389386
None => Err(NodeError::UnknownAddress { address: *address }),
390387
}
391388
}

crates/edr_rpc_server/src/server.rs

+25-28
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,12 @@ fn handle_net_peer_count() -> ResponseData<U64> {
371371

372372
async fn handle_sign(
373373
node: Arc<Node>,
374-
address: &Address,
375-
message: &ZeroXPrefixedBytes,
374+
address: Address,
375+
message: ZeroXPrefixedBytes,
376376
) -> ResponseData<Signature> {
377377
event!(Level::INFO, "eth_sign({address:?}, {message:?})");
378378

379-
match node.sign(address, message).await {
379+
match node.sign(&address, message).await {
380380
Ok(signature) => ResponseData::Success { result: signature },
381381
Err(error) => match error {
382382
NodeError::UnknownAddress { .. } => {
@@ -437,15 +437,15 @@ fn handle_web3_sha3(message: ZeroXPrefixedBytes) -> ResponseData<B256> {
437437

438438
async fn handle_request(
439439
node: Arc<Node>,
440-
request: &RpcRequest<MethodInvocation>,
440+
request: RpcRequest<MethodInvocation>,
441441
) -> Result<serde_json::Value, String> {
442-
fn response<T>(id: &jsonrpc::Id, data: ResponseData<T>) -> Result<serde_json::Value, String>
442+
fn response<T>(id: jsonrpc::Id, data: ResponseData<T>) -> Result<serde_json::Value, String>
443443
where
444444
T: serde::Serialize,
445445
{
446446
let response: Response<T> = Response {
447447
jsonrpc: jsonrpc::Version::V2_0,
448-
id: id.clone(),
448+
id,
449449
data,
450450
};
451451
serde_json::to_value(response).map_err(|e| {
@@ -460,7 +460,7 @@ async fn handle_request(
460460
version,
461461
id,
462462
method: _,
463-
} if *version != jsonrpc::Version::V2_0 => response(
463+
} if version != jsonrpc::Version::V2_0 => response(
464464
id,
465465
error_response_data::<serde_json::Value>(
466466
0,
@@ -486,42 +486,39 @@ async fn handle_request(
486486
response(id, handle_coinbase(node).await)
487487
}
488488
MethodInvocation::Eth(EthMethodInvocation::EvmIncreaseTime(increment)) => {
489-
response(id, handle_evm_increase_time(node, increment.clone()).await)
489+
response(id, handle_evm_increase_time(node, increment).await)
490490
}
491491
MethodInvocation::Eth(EthMethodInvocation::EvmMine(timestamp)) => {
492-
response(id, handle_evm_mine(node, timestamp.clone()).await)
492+
response(id, handle_evm_mine(node, timestamp).await)
493493
}
494494
MethodInvocation::Eth(EthMethodInvocation::EvmSetNextBlockTimestamp(timestamp)) => {
495495
response(
496496
id,
497-
handle_evm_set_next_block_timestamp(node, timestamp.clone()).await,
497+
handle_evm_set_next_block_timestamp(node, timestamp).await,
498498
)
499499
}
500500
MethodInvocation::Eth(EthMethodInvocation::GetBalance(address, block)) => {
501-
response(id, handle_get_balance(node, *address, block.clone()).await)
501+
response(id, handle_get_balance(node, address, block).await)
502502
}
503503
MethodInvocation::Eth(EthMethodInvocation::GetCode(address, block)) => {
504-
response(id, handle_get_code(node, *address, block.clone()).await)
504+
response(id, handle_get_code(node, address, block).await)
505505
}
506506
MethodInvocation::Eth(EthMethodInvocation::GetFilterChanges(filter_id)) => {
507-
response(id, handle_get_filter_changes(node, *filter_id).await)
507+
response(id, handle_get_filter_changes(node, filter_id).await)
508508
}
509509
MethodInvocation::Eth(EthMethodInvocation::GetFilterLogs(filter_id)) => {
510-
response(id, handle_get_filter_logs(node, *filter_id).await)
510+
response(id, handle_get_filter_logs(node, filter_id).await)
511511
}
512512
MethodInvocation::Eth(EthMethodInvocation::GetStorageAt(
513513
address,
514514
position,
515515
block,
516516
)) => response(
517517
id,
518-
handle_get_storage_at(node, *address, *position, block.clone()).await,
518+
handle_get_storage_at(node, address, position, block).await,
519519
),
520520
MethodInvocation::Eth(EthMethodInvocation::GetTransactionCount(address, block)) => {
521-
response(
522-
id,
523-
handle_get_transaction_count(node, *address, block.clone()).await,
524-
)
521+
response(id, handle_get_transaction_count(node, address, block).await)
525522
}
526523
MethodInvocation::Eth(EthMethodInvocation::NetListening()) => {
527524
response(id, handle_net_listening())
@@ -545,38 +542,38 @@ async fn handle_request(
545542
response(id, handle_web3_sha3(message.clone()))
546543
}
547544
MethodInvocation::Eth(EthMethodInvocation::UninstallFilter(filter_id)) => {
548-
response(id, handle_uninstall_filter(node, *filter_id).await)
545+
response(id, handle_uninstall_filter(node, filter_id).await)
549546
}
550547
MethodInvocation::Eth(EthMethodInvocation::Unsubscribe(subscription_id)) => {
551-
response(id, handle_unsubscribe(node, *subscription_id).await)
548+
response(id, handle_unsubscribe(node, subscription_id).await)
552549
}
553550
MethodInvocation::Hardhat(HardhatMethodInvocation::ImpersonateAccount(address)) => {
554-
response(id, handle_impersonate_account(node, *address).await)
551+
response(id, handle_impersonate_account(node, address).await)
555552
}
556553
MethodInvocation::Hardhat(HardhatMethodInvocation::IntervalMine()) => {
557554
response(id, handle_interval_mine(node).await)
558555
}
559556
MethodInvocation::Hardhat(HardhatMethodInvocation::SetBalance(
560557
address,
561558
balance,
562-
)) => response(id, handle_set_balance(node, *address, *balance).await),
559+
)) => response(id, handle_set_balance(node, address, balance).await),
563560
MethodInvocation::Hardhat(HardhatMethodInvocation::SetCode(address, code)) => {
564-
response(id, handle_set_code(node, *address, code.clone()).await)
561+
response(id, handle_set_code(node, address, code).await)
565562
}
566563
MethodInvocation::Hardhat(HardhatMethodInvocation::SetNonce(address, nonce)) => {
567-
response(id, handle_set_nonce(node, *address, *nonce).await)
564+
response(id, handle_set_nonce(node, address, nonce).await)
568565
}
569566
MethodInvocation::Hardhat(HardhatMethodInvocation::SetStorageAt(
570567
address,
571568
position,
572569
value,
573570
)) => response(
574571
id,
575-
handle_set_storage_at(node, *address, *position, *value).await,
572+
handle_set_storage_at(node, address, position, value).await,
576573
),
577574
MethodInvocation::Hardhat(HardhatMethodInvocation::StopImpersonatingAccount(
578575
address,
579-
)) => response(id, handle_stop_impersonating_account(node, *address).await),
576+
)) => response(id, handle_stop_impersonating_account(node, address).await),
580577
// TODO: after adding all the methods here, eliminate this
581578
// catch-all match arm:
582579
_ => {
@@ -614,7 +611,7 @@ async fn router(node: Arc<Node>) -> Router {
614611
let responses = {
615612
let mut responses: Vec<serde_json::Value> =
616613
Vec::with_capacity(requests.len());
617-
for request in requests.iter() {
614+
for request in requests {
618615
match handle_request(Arc::clone(&node), request).await {
619616
Ok(response) => responses.push(response),
620617
Err(s) => {

0 commit comments

Comments
 (0)