Skip to content

Commit

Permalink
EVM: Add caching and concurrency control flags (#2632)
Browse files Browse the repository at this point in the history
* Add lru cache size and ecc cache params

* remove debug logs

* Refine defaults, add emv cache limit

* Rename to cache count instead of size limit

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
sieniven and prasannavl authored Oct 31, 2023
1 parent f04b905 commit 197b6d8
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lib/ain-cpp-imports/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub mod ffi {
fn getNumCores() -> i32;
fn getCORSAllowedOrigin() -> String;
fn getNumConnections() -> i32;
fn getEccLruCacheCount() -> usize;
fn getEvmValidationLruCacheCount() -> usize;
fn isEthDebugRPCEnabled() -> bool;
fn isEthDebugTraceRPCEnabled() -> bool;
}
Expand Down
14 changes: 14 additions & 0 deletions lib/ain-cpp-imports/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ mod ffi {
pub fn getNumConnections() -> i32 {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn getEccLruCacheCount() -> usize {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn getEvmValidationLruCacheCount() -> usize {
unimplemented!("{}", UNIMPL_MSG)
}
pub fn isEthDebugRPCEnabled() -> bool {
unimplemented!("{}", UNIMPL_MSG)
}
Expand Down Expand Up @@ -217,6 +223,14 @@ pub fn get_num_connections() -> i32 {
ffi::getNumConnections()
}

pub fn get_ecc_lru_cache_count() -> usize {
ffi::getEccLruCacheCount()
}

pub fn get_evmv_lru_cache_count() -> usize {
ffi::getEvmValidationLruCacheCount()
}

pub fn is_eth_debug_rpc_enabled() -> bool {
ffi::isEthDebugRPCEnabled()
}
Expand Down
6 changes: 2 additions & 4 deletions lib/ain-evm/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ pub struct SignedTxCache {
inner: spin::Mutex<LruCache<String, SignedTx>>,
}

const DEFAULT_CACHE_SIZE: usize = 10000;

impl Default for SignedTxCache {
fn default() -> Self {
Self::new(DEFAULT_CACHE_SIZE)
Self::new(ain_cpp_imports::get_ecc_lru_cache_count())
}
}

Expand Down Expand Up @@ -104,7 +102,7 @@ struct TxValidationCache {

impl Default for TxValidationCache {
fn default() -> Self {
Self::new(DEFAULT_CACHE_SIZE)
Self::new(ain_cpp_imports::get_evmv_lru_cache_count())
}
}

Expand Down
1 change: 1 addition & 0 deletions src/dfi/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <condition_variable>

static const int DEFAULT_DFTX_WORKERS = 0;
static const int DEFAULT_ECC_PRECACHE_WORKERS = -1;

// Until C++20x concurrency impls make it into standard, std::future and std::async impls
// doesn't have the primitives needed for working with many at the same time efficiently
Expand Down
12 changes: 10 additions & 2 deletions src/ffi/ffiexports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,18 @@ int32_t getNumConnections() {
return (int32_t)g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
}

size_t getEccLruCacheCount() {
return gArgs.GetArg("-ecclrucache", DEFAULT_ECC_LRU_CACHE_COUNT);
}

size_t getEvmValidationLruCacheCount() {
return gArgs.GetArg("-evmvlrucache", DEFAULT_EVMV_LRU_CACHE_COUNT);
}

bool isEthDebugRPCEnabled() {
return gArgs.GetBoolArg("-ethdebug", false);
return gArgs.GetBoolArg("-ethdebug", DEFAULT_ETH_DEBUG_ENABLED);
}

bool isEthDebugTraceRPCEnabled() {
return gArgs.GetBoolArg("-ethdebugtrace", true);
return gArgs.GetBoolArg("-ethdebugtrace", DEFAULT_ETH_DEBUG_TRACE_ENABLED);
}
9 changes: 7 additions & 2 deletions src/ffi/ffiexports.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ static constexpr uint64_t DEFAULT_EVM_BLOCK_GAS_LIMIT = 30000000;
static constexpr uint64_t DEFAULT_EVM_FINALITY_COUNT = 100;
static constexpr uint32_t DEFAULT_ETH_MAX_CONNECTIONS = 100;

static constexpr bool DEFAULT_DEBUG_ENABLED = false;
static constexpr bool DEFAULT_DEBUG_TRACE_ENABLED = true;
static constexpr uint32_t DEFAULT_ECC_LRU_CACHE_COUNT = 10000;
static constexpr uint32_t DEFAULT_EVMV_LRU_CACHE_COUNT = 10000;

static constexpr bool DEFAULT_ETH_DEBUG_ENABLED = false;
static constexpr bool DEFAULT_ETH_DEBUG_TRACE_ENABLED = true;

struct Attributes {
uint64_t blockGasTarget;
Expand Down Expand Up @@ -74,6 +77,8 @@ rust::string getClientVersion();
int32_t getNumCores();
rust::string getCORSAllowedOrigin();
int32_t getNumConnections();
size_t getEccLruCacheCount();
size_t getEvmValidationLruCacheCount();
bool isEthDebugRPCEnabled();
bool isEthDebugTraceRPCEnabled();

Expand Down
7 changes: 5 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ void SetupServerArgs()
gArgs.AddArg("-datadir=<dir>", "Specify data directory", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-dbbatchsize", strprintf("Maximum database write batch size in bytes (default: %u)", nDefaultDbBatchSize), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
gArgs.AddArg("-dbcache=<n>", strprintf("Maximum database cache size <n> MiB (%d to %d, default: %d). In addition, unused mempool memory is shared for this cache (see -maxmempool).", nMinDbCache, nMaxDbCache, nDefaultDbCache), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-ecclrucache=<n>", strprintf("Maximum ECC LRU cache size <n> items (default: %d).", DEFAULT_ECC_LRU_CACHE_COUNT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-evmvlrucache=<n>", strprintf("Maximum EVM TX Validator LRU cache size <n> items (default: %d).", DEFAULT_EVMV_LRU_CACHE_COUNT), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-eccprecache=<n>", strprintf("ECC pre-cache concurrency control (default: %d, (-1: auto, 0: disable, <n>: workers).", DEFAULT_ECC_PRECACHE_WORKERS), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-debuglogfile=<file>", strprintf("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (-nodebuglogfile to disable; default: %s)", DEFAULT_DEBUGLOGFILE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
gArgs.AddArg("-feefilter", strprintf("Tell other nodes to filter invs to us by our mempool min fee (default: %u)", DEFAULT_FEEFILTER), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::OPTIONS);
gArgs.AddArg("-includeconf=<file>", "Specify additional configuration file, relative to the -datadir path (only useable from configuration file, not command line)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
Expand Down Expand Up @@ -652,8 +655,8 @@ 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("-ethdebug", strprintf("Enable debug_* ETH RPCs (default: %b)", DEFAULT_DEBUG_ENABLED), ArgsManager::ALLOW_ANY, OptionsCategory::RPC);
gArgs.AddArg("-ethdebugtrace", strprintf("Enable debug_trace* ETH RPCs (default: %b)", DEFAULT_DEBUG_TRACE_ENABLED), ArgsManager::ALLOW_ANY, 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);

#if HAVE_DECL_DAEMON
gArgs.AddArg("-daemon", "Run in the background as a daemon and accept commands", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
Expand Down
4 changes: 3 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3016,7 +3016,9 @@ bool CChainState::ConnectBlock(const CBlock &block,
XResultThrowOnErr(evm_try_unsafe_update_state_in_template(
result, evmTemplate->GetTemplate(), static_cast<std::size_t>(reinterpret_cast<uintptr_t>(&mnview))));

{
auto eccPreCacheControl = gArgs.GetArg("-eccprecache", DEFAULT_ECC_PRECACHE_WORKERS);
auto isEccPreCacheEnabled = eccPreCacheControl == -1 || eccPreCacheControl > 0;
if (isEccPreCacheEnabled) {
// Pre-warm validation cache
auto &pool = DfTxTaskPool->pool;

Expand Down

0 comments on commit 197b6d8

Please sign in to comment.