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

RPC: Add default and configurable response size limit to eth JSON-RPC server #2672

Merged
merged 5 commits into from
Nov 5, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub mod ffi {
fn getDatadir() -> String;
fn getNetwork() -> String;
fn getEthMaxConnections() -> u32;
fn getEthMaxResponseByteSize() -> u32;
fn getDifficulty(block_hash: [u8; 32]) -> u32;
fn getChainWork(block_hash: [u8; 32]) -> [u8; 32];
fn getPoolTransactions() -> Vec<TransactionData>;
Expand Down
7 changes: 7 additions & 0 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ mod ffi {
pub fn getEthMaxConnections() -> u32 {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn getEthMaxResponseByteSize() -> u32 {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn getNetwork() -> String {
unimplemented!("{}", UNIMPL_MSG)
}
Expand Down Expand Up @@ -146,6 +149,10 @@ pub fn get_max_connections() -> u32 {
ffi::getEthMaxConnections()
}

pub fn get_max_response_byte_size() -> u32 {
ffi::getEthMaxResponseByteSize()
}

pub fn get_network() -> String {
ffi::getNetwork()
}
Expand Down
4 changes: 4 additions & 0 deletions lib/ain-grpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub fn init_network_json_rpc_service(runtime: &Services, addr: &str) -> Result<(
info!("Starting JSON RPC server at {}", addr);
let addr = addr.parse::<SocketAddr>()?;
let max_connections = ain_cpp_imports::get_max_connections();
let max_response_size = ain_cpp_imports::get_max_response_byte_size();

let middleware = if !ain_cpp_imports::get_cors_allowed_origin().is_empty() {
let origin = ain_cpp_imports::get_cors_allowed_origin();
Expand All @@ -107,6 +108,7 @@ pub fn init_network_json_rpc_service(runtime: &Services, addr: &str) -> Result<(
ServerBuilder::default()
.set_middleware(middleware)
.max_connections(max_connections)
.max_response_body_size(max_response_size)
.custom_tokio_runtime(handle)
.build(addr),
)?;
Expand All @@ -133,11 +135,13 @@ pub fn init_network_subscriptions_service(runtime: &Services, addr: &str) -> Res
info!("Starting WebSockets server at {}", addr);
let addr = addr.parse::<SocketAddr>()?;
let max_connections = ain_cpp_imports::get_max_connections();
let max_response_size = ain_cpp_imports::get_max_response_byte_size();

let handle = runtime.ws_rt_handle.clone();
let server = runtime.ws_rt_handle.block_on(
ServerBuilder::default()
.max_subscriptions_per_connection(max_connections)
.max_response_body_size(max_response_size)
.custom_tokio_runtime(handle)
.set_id_provider(MetachainSubIdProvider)
.build(addr),
Expand Down
5 changes: 5 additions & 0 deletions src/ffi/ffiexports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ uint32_t getEthMaxConnections() {
return gArgs.GetArg("-ethmaxconnections", DEFAULT_ETH_MAX_CONNECTIONS);
}

uint32_t getEthMaxResponseByteSize() {
const auto max_response_size_mb = gArgs.GetArg("-ethmaxresponsesize", DEFAULT_ETH_MAX_RESPONSE_SIZE_MB);
return max_response_size_mb * 1024 * 1024;
}

rust::vec<DST20Token> getDST20Tokens(std::size_t mnview_ptr) {
LOCK(cs_main);

Expand Down
4 changes: 4 additions & 0 deletions src/ffi/ffiexports.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ static constexpr uint64_t DEFAULT_EVM_BLOCK_GAS_TARGET_FACTOR = 2;
static constexpr uint64_t DEFAULT_EVM_BLOCK_GAS_LIMIT = 30000000;
static constexpr uint64_t DEFAULT_EVM_FINALITY_COUNT = 100;
static constexpr CAmount DEFAULT_EVM_RBF_FEE_INCREMENT = COIN / 10;

// Defaults for attributes relating to networking
static constexpr uint32_t DEFAULT_ETH_MAX_CONNECTIONS = 100;
static constexpr uint32_t DEFAULT_ETH_MAX_RESPONSE_SIZE_MB = 25; // 25 megabytes

static constexpr uint32_t DEFAULT_ECC_LRU_CACHE_COUNT = 10000;
static constexpr uint32_t DEFAULT_EVMV_LRU_CACHE_COUNT = 10000;
Expand Down Expand Up @@ -65,6 +68,7 @@ rust::string getDatadir();
rust::string getNetwork();
uint32_t getDifficulty(std::array<uint8_t, 32> blockHash);
uint32_t getEthMaxConnections();
uint32_t getEthMaxResponseByteSize();
std::array<uint8_t, 32> getChainWork(std::array<uint8_t, 32> blockHash);
rust::vec<TransactionData> getPoolTransactions();
uint64_t getNativeTxSize(rust::Vec<uint8_t> rawTransaction);
Expand Down
1 change: 1 addition & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ void SetupServerArgs()
gArgs.AddArg("-ethrpcport=<port>", strprintf("Listen for ETH-JSON-RPC connections on <port>> (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", defaultBaseParams->ETHRPCPort(), testnetBaseParams->ETHRPCPort(), changiBaseParams->ETHRPCPort(), devnetBaseParams->ETHRPCPort(), regtestBaseParams->ETHRPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
gArgs.AddArg("-wsport=<port>", strprintf("Listen for ETH-WebSockets connections on <port>> (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", defaultBaseParams->WSPort(), testnetBaseParams->WSPort(), changiBaseParams->WSPort(), devnetBaseParams->WSPort(), regtestBaseParams->ETHRPCPort()), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
gArgs.AddArg("-ethmaxconnections=<connections>", strprintf("Set the maximum number of connections allowed by the ETH-RPC server (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS, DEFAULT_ETH_MAX_CONNECTIONS), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
gArgs.AddArg("-ethmaxresponsesize=<size>", strprintf("Set the maximum response size in MB by the ETH-RPC server (default: %u, testnet: %u, changi: %u, devnet: %u, regtest: %u)", DEFAULT_ETH_MAX_RESPONSE_SIZE_MB, DEFAULT_ETH_MAX_RESPONSE_SIZE_MB, DEFAULT_ETH_MAX_RESPONSE_SIZE_MB, DEFAULT_ETH_MAX_RESPONSE_SIZE_MB, DEFAULT_ETH_MAX_RESPONSE_SIZE_MB), ArgsManager::ALLOW_ANY | ArgsManager::NETWORK_ONLY, OptionsCategory::RPC);
gArgs.AddArg("-ethdebug", strprintf("Enable debug_* ETH RPCs (default: %b)", DEFAULT_ETH_DEBUG_ENABLED), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
gArgs.AddArg("-ethdebugtrace", strprintf("Enable debug_trace* ETH RPCs (default: %b)", DEFAULT_ETH_DEBUG_TRACE_ENABLED), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);

Expand Down