From b19cf682f555dff0d05904b5f9883ba6f40433ba Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Wed, 24 Aug 2022 18:34:58 -0400 Subject: [PATCH 1/8] added --enable_api flag with pattern matching for eth, net, erigon otterscan, trace, web3 --- bin/akula.rs | 81 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/bin/akula.rs b/bin/akula.rs index 04a4e027..f0e3eb1d 100644 --- a/bin/akula.rs +++ b/bin/akula.rs @@ -97,6 +97,10 @@ pub struct Opt { #[clap(long)] pub no_rpc: bool, + /// Enable the ETH API + #[clap(long, max_values(6))] + pub enable_api: Vec, + /// Enable JSONRPC at this IP address and port. #[clap(long, default_value = "127.0.0.1:8545")] pub rpc_listen_address: SocketAddr, @@ -221,29 +225,62 @@ fn main() -> anyhow::Result<()> { .unwrap(); let mut api = Methods::new(); - api.merge( - EthApiServerImpl { - db: db.clone(), - call_gas_limit: 100_000_000, - } - .into_rpc(), - ) - .unwrap(); - api.merge(NetApiServerImpl { network_id }.into_rpc()) - .unwrap(); - api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) - .unwrap(); - api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) - .unwrap(); - api.merge( - TraceApiServerImpl { - db, - call_gas_limit: 100_000_000, + + for api_option in opt + .enable_api + .iter() + .map(|o| o.to_lowercase()) + .collect::>() + { + match api_option.as_str() { + "eth" => { + api.merge( + EthApiServerImpl { + db: db.clone(), + call_gas_limit: 100_000_000, + } + .into_rpc(), + ) + .unwrap(); + } + + "net" => { + api.merge(NetApiServerImpl { network_id }.into_rpc()) + .unwrap(); + } + + "erigon" => { + api.merge( + ErigonApiServerImpl { db: db.clone() }.into_rpc(), + ) + .unwrap(); + } + + "otterscan" => { + api.merge( + OtterscanApiServerImpl { db: db.clone() }.into_rpc(), + ) + .unwrap(); + } + + "trace" => { + api.merge( + TraceApiServerImpl { + db: db.clone(), + call_gas_limit: 100_000_000, + } + .into_rpc(), + ) + .unwrap(); + } + + "web3" => { + api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); + } + + _ => {} } - .into_rpc(), - ) - .unwrap(); - api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); + } let _server_handle = server.start(api).unwrap(); From 9cef49a0ddbf1bae89fec086eb939ee1674c68cc Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Wed, 24 Aug 2022 18:47:51 -0400 Subject: [PATCH 2/8] updated min values for --enable-api flag --- bin/akula.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/akula.rs b/bin/akula.rs index f0e3eb1d..146c6e98 100644 --- a/bin/akula.rs +++ b/bin/akula.rs @@ -97,8 +97,8 @@ pub struct Opt { #[clap(long)] pub no_rpc: bool, - /// Enable the ETH API - #[clap(long, max_values(6))] + /// Enable API options + #[clap(long, min_values(1))] pub enable_api: Vec, /// Enable JSONRPC at this IP address and port. From fccd1f417add87944e275af7bb6addb03d063b2b Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Thu, 25 Aug 2022 22:52:02 -0400 Subject: [PATCH 3/8] Changed enable-api arguments to transform into a hashset instead a Vec --- bin/akula.rs | 82 ++++++++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/bin/akula.rs b/bin/akula.rs index 146c6e98..971c23e4 100644 --- a/bin/akula.rs +++ b/bin/akula.rs @@ -21,7 +21,8 @@ use ethereum_jsonrpc::{ use http::Uri; use jsonrpsee::{core::server::rpc_module::Methods, http_server::HttpServerBuilder}; use std::{ - fs::OpenOptions, future::pending, io::Write, net::SocketAddr, panic, sync::Arc, time::Duration, + collections::HashSet, fs::OpenOptions, future::pending, io::Write, net::SocketAddr, panic, + sync::Arc, time::Duration, }; use tokio::time::sleep; use tracing::*; @@ -226,60 +227,47 @@ fn main() -> anyhow::Result<()> { let mut api = Methods::new(); - for api_option in opt - .enable_api - .iter() - .map(|o| o.to_lowercase()) - .collect::>() - { - match api_option.as_str() { - "eth" => { - api.merge( - EthApiServerImpl { - db: db.clone(), - call_gas_limit: 100_000_000, - } - .into_rpc(), - ) - .unwrap(); - } + let api_options: HashSet = HashSet::from_iter(opt.enable_api); - "net" => { - api.merge(NetApiServerImpl { network_id }.into_rpc()) - .unwrap(); + if api_options.get("eth").is_some() { + api.merge( + EthApiServerImpl { + db: db.clone(), + call_gas_limit: 100_000_000, } + .into_rpc(), + ) + .unwrap(); + } - "erigon" => { - api.merge( - ErigonApiServerImpl { db: db.clone() }.into_rpc(), - ) - .unwrap(); - } + if api_options.get("net").is_some() { + api.merge(NetApiServerImpl { network_id }.into_rpc()) + .unwrap(); + } - "otterscan" => { - api.merge( - OtterscanApiServerImpl { db: db.clone() }.into_rpc(), - ) - .unwrap(); - } + if api_options.get("erigon").is_some() { + api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) + .unwrap(); + } - "trace" => { - api.merge( - TraceApiServerImpl { - db: db.clone(), - call_gas_limit: 100_000_000, - } - .into_rpc(), - ) - .unwrap(); - } + if api_options.get("otterscan").is_some() { + api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) + .unwrap(); + } - "web3" => { - api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); + if api_options.get("trace").is_some() { + api.merge( + TraceApiServerImpl { + db: db.clone(), + call_gas_limit: 100_000_000, } + .into_rpc(), + ) + .unwrap(); + } - _ => {} - } + if api_options.get("web3").is_some() { + api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); } let _server_handle = server.start(api).unwrap(); From 308efe7077a585f33ae7c79cfdae3ffa64d29b4d Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Thu, 25 Aug 2022 22:55:41 -0400 Subject: [PATCH 4/8] added lowercase conversion to inputs for enable_api flag --- bin/akula.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bin/akula.rs b/bin/akula.rs index 971c23e4..992cfde5 100644 --- a/bin/akula.rs +++ b/bin/akula.rs @@ -227,7 +227,12 @@ fn main() -> anyhow::Result<()> { let mut api = Methods::new(); - let api_options: HashSet = HashSet::from_iter(opt.enable_api); + let api_options: HashSet = HashSet::from_iter( + opt.enable_api + .into_iter() + .map(|s| s.to_lowercase()) + .collect::>(), + ); if api_options.get("eth").is_some() { api.merge( From a45262a295e9cac463d6319858b1f0244dc58913 Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Fri, 26 Aug 2022 07:19:47 -0400 Subject: [PATCH 5/8] Updated transformation from Vec to HashSet. Updated logic to check if Id exists within Hashset. --- bin/akula.rs | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/bin/akula.rs b/bin/akula.rs index 992cfde5..ced35a87 100644 --- a/bin/akula.rs +++ b/bin/akula.rs @@ -227,14 +227,13 @@ fn main() -> anyhow::Result<()> { let mut api = Methods::new(); - let api_options: HashSet = HashSet::from_iter( - opt.enable_api - .into_iter() - .map(|s| s.to_lowercase()) - .collect::>(), - ); - - if api_options.get("eth").is_some() { + let api_options = opt + .enable_api + .into_iter() + .map(|s| s.to_lowercase()) + .collect::>(); + + if !api_options.is_empty() && api_options.contains("eth") { api.merge( EthApiServerImpl { db: db.clone(), @@ -245,22 +244,22 @@ fn main() -> anyhow::Result<()> { .unwrap(); } - if api_options.get("net").is_some() { + if !api_options.is_empty() && api_options.contains("net") { api.merge(NetApiServerImpl { network_id }.into_rpc()) .unwrap(); } - if api_options.get("erigon").is_some() { + if !api_options.is_empty() && api_options.contains("erigon") { api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) .unwrap(); } - if api_options.get("otterscan").is_some() { + if !api_options.is_empty() && api_options.contains("otterscan") { api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) .unwrap(); } - if api_options.get("trace").is_some() { + if !api_options.is_empty() && api_options.contains("trace") { api.merge( TraceApiServerImpl { db: db.clone(), @@ -271,7 +270,7 @@ fn main() -> anyhow::Result<()> { .unwrap(); } - if api_options.get("web3").is_some() { + if !api_options.is_empty() && api_options.contains("web3") { api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); } From 53af1f3163e96bf6834cf8e3fc7c63c06a52badb Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Tue, 30 Aug 2022 16:55:42 -0400 Subject: [PATCH 6/8] Duplicated logic to handle enable-api flag into akula-rpc --- bin/akula-rpc.rs | 70 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/bin/akula-rpc.rs b/bin/akula-rpc.rs index 22f7421b..cb0b6fc5 100644 --- a/bin/akula-rpc.rs +++ b/bin/akula-rpc.rs @@ -32,6 +32,10 @@ pub struct Opt { #[clap(long)] pub grpc_listen_address: SocketAddr, + + /// Enable API options + #[clap(long, min_values(1))] + pub enable_api: Vec, } #[tokio::main] @@ -63,29 +67,53 @@ async fn main() -> anyhow::Result<()> { .await?; let mut api = Methods::new(); - api.merge( - EthApiServerImpl { - db: db.clone(), - call_gas_limit: 100_000_000, - } - .into_rpc(), - ) - .unwrap(); - api.merge(NetApiServerImpl { network_id }.into_rpc()) - .unwrap(); - api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) + + let api_options = opt + .enable_api + .into_iter() + .map(|s| s.to_lowercase()) + .collect::>(); + + if !api_options.is_empty() && api_options.contains("eth") { + api.merge( + EthApiServerImpl { + db: db.clone(), + call_gas_limit: 100_000_000, + } + .into_rpc(), + ) .unwrap(); - api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) + } + + if !api_options.is_empty() && api_options.contains("net") { + api.merge(NetApiServerImpl { network_id }.into_rpc()) + .unwrap(); + } + + if !api_options.is_empty() && api_options.contains("erigon") { + api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) + .unwrap(); + } + + if !api_options.is_empty() && api_options.contains("otterscan") { + api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) + .unwrap(); + } + + if !api_options.is_empty() && api_options.contains("trace") { + api.merge( + TraceApiServerImpl { + db: db.clone(), + call_gas_limit: 100_000_000, + } + .into_rpc(), + ) .unwrap(); - api.merge( - TraceApiServerImpl { - db: db.clone(), - call_gas_limit: 100_000_000, - } - .into_rpc(), - ) - .unwrap(); - api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); + } + + if !api_options.is_empty() && api_options.contains("web3") { + api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); + } let _http_server_handle = http_server.start(api.clone())?; let _websocket_server_handle = websocket_server.start(api)?; From 2c1a0c1592e025de7830776da093b41359e664ab Mon Sep 17 00:00:00 2001 From: 0xKitsune <0xKitsune@protonmail.com> Date: Tue, 30 Aug 2022 17:27:06 -0400 Subject: [PATCH 7/8] Updated to use std::HashSet in akula-rpc --- bin/akula-rpc.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/akula-rpc.rs b/bin/akula-rpc.rs index cb0b6fc5..d8c45d00 100644 --- a/bin/akula-rpc.rs +++ b/bin/akula-rpc.rs @@ -15,7 +15,8 @@ use ethereum_jsonrpc::{ use jsonrpsee::{ core::server::rpc_module::Methods, http_server::HttpServerBuilder, ws_server::WsServerBuilder, }; -use std::{future::pending, net::SocketAddr, sync::Arc}; + +use std::{collections::HashSet, future::pending, net::SocketAddr, sync::Arc}; use tracing_subscriber::prelude::*; #[derive(Parser)] From a060037a02cae9aae96f27520f66a8aa9bf0fea4 Mon Sep 17 00:00:00 2001 From: Artem Vorotnikov Date: Wed, 31 Aug 2022 00:45:38 +0300 Subject: [PATCH 8/8] fix condition --- bin/akula-rpc.rs | 12 ++++++------ bin/akula.rs | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/bin/akula-rpc.rs b/bin/akula-rpc.rs index d8c45d00..fd22514f 100644 --- a/bin/akula-rpc.rs +++ b/bin/akula-rpc.rs @@ -75,7 +75,7 @@ async fn main() -> anyhow::Result<()> { .map(|s| s.to_lowercase()) .collect::>(); - if !api_options.is_empty() && api_options.contains("eth") { + if api_options.is_empty() || api_options.contains("eth") { api.merge( EthApiServerImpl { db: db.clone(), @@ -86,22 +86,22 @@ async fn main() -> anyhow::Result<()> { .unwrap(); } - if !api_options.is_empty() && api_options.contains("net") { + if api_options.is_empty() || api_options.contains("net") { api.merge(NetApiServerImpl { network_id }.into_rpc()) .unwrap(); } - if !api_options.is_empty() && api_options.contains("erigon") { + if api_options.is_empty() || api_options.contains("erigon") { api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) .unwrap(); } - if !api_options.is_empty() && api_options.contains("otterscan") { + if api_options.is_empty() || api_options.contains("otterscan") { api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) .unwrap(); } - if !api_options.is_empty() && api_options.contains("trace") { + if api_options.is_empty() || api_options.contains("trace") { api.merge( TraceApiServerImpl { db: db.clone(), @@ -112,7 +112,7 @@ async fn main() -> anyhow::Result<()> { .unwrap(); } - if !api_options.is_empty() && api_options.contains("web3") { + if api_options.is_empty() || api_options.contains("web3") { api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); } diff --git a/bin/akula.rs b/bin/akula.rs index ced35a87..b4cf9e95 100644 --- a/bin/akula.rs +++ b/bin/akula.rs @@ -233,7 +233,7 @@ fn main() -> anyhow::Result<()> { .map(|s| s.to_lowercase()) .collect::>(); - if !api_options.is_empty() && api_options.contains("eth") { + if api_options.is_empty() || api_options.contains("eth") { api.merge( EthApiServerImpl { db: db.clone(), @@ -244,22 +244,22 @@ fn main() -> anyhow::Result<()> { .unwrap(); } - if !api_options.is_empty() && api_options.contains("net") { + if api_options.is_empty() || api_options.contains("net") { api.merge(NetApiServerImpl { network_id }.into_rpc()) .unwrap(); } - if !api_options.is_empty() && api_options.contains("erigon") { + if api_options.is_empty() || api_options.contains("erigon") { api.merge(ErigonApiServerImpl { db: db.clone() }.into_rpc()) .unwrap(); } - if !api_options.is_empty() && api_options.contains("otterscan") { + if api_options.is_empty() || api_options.contains("otterscan") { api.merge(OtterscanApiServerImpl { db: db.clone() }.into_rpc()) .unwrap(); } - if !api_options.is_empty() && api_options.contains("trace") { + if api_options.is_empty() || api_options.contains("trace") { api.merge( TraceApiServerImpl { db: db.clone(), @@ -270,7 +270,7 @@ fn main() -> anyhow::Result<()> { .unwrap(); } - if !api_options.is_empty() && api_options.contains("web3") { + if api_options.is_empty() || api_options.contains("web3") { api.merge(Web3ApiServerImpl.into_rpc()).unwrap(); }