nimbus-eth1/nimbus/db/kvt/kvt_utils.nim
Jacek Sieka 768307d91d
Cache code and invalid jump destination tables (fixes #2268) (#2404)
It is common for many accounts to share the same code - at the database
level, code is stored by hash meaning only one copy exists per unique
program but when loaded in memory, a copy is made for each account.

Further, every time we execute the code, it must be scanned for invalid
jump destinations which slows down EVM exeuction.

Finally, the extcodesize call causes code to be loaded even if only the
size is needed.

This PR improves on all these points by introducing a shared
CodeBytesRef type whose code section is immutable and that can be shared
between accounts. Further, a dedicated `len` API call is added so that
the EXTCODESIZE opcode can operate without polluting the GC and code
cache, for cases where only the size is requested - rocksdb will in this
case cache the code itself in the row cache meaning that lookup of the
code itself remains fast when length is asked for first.

With 16k code entries, there's a 90% hit rate which goes up to 99%
during the 2.3M attack - the cache significantly lowers memory
consumption and execution time not only during this event but across the
board.
2024-06-21 09:44:10 +02:00

159 lines
4.5 KiB
Nim

# nimbus-eth1
# Copyright (c) 2023-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed
# except according to those terms.
## Kvt DB -- Common functions
## ==========================
##
{.push raises: [].}
import
std/tables,
eth/common,
results,
./kvt_desc/desc_backend,
"."/[kvt_desc, kvt_layers]
# ------------------------------------------------------------------------------
# Public functions, converters
# ------------------------------------------------------------------------------
proc getUbe*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[Blob,KvtError] =
## For the argument `key` return the associated value from the backend
## database if available.
##
let be = db.backend
if not be.isNil:
return be.getKvpFn key
err(GetNotFound)
proc getUbeLen*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[int,KvtError] =
## For the argument `key` return the associated value from the backend
## database if available.
##
let be = db.backend
if not be.isNil:
return be.lenKvpFn key
err(GetNotFound)
proc getBe*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[Blob,KvtError] =
## Get the vertex from the (filtered) backened if available.
if not db.balancer.isNil:
db.balancer.sTab.withValue(@key, w):
if w[].len == 0:
return err(GetNotFound)
return ok(w[])
db.getUbe key
proc getBeLen*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[int,KvtError] =
## Get the vertex from the (filtered) backened if available.
if not db.balancer.isNil:
db.balancer.sTab.withValue(@key, w):
if w[].len == 0:
return err(GetNotFound)
return ok(w[].len)
db.getUbeLen key
# ------------
proc put*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record to store
data: openArray[byte]; # Value of database record to store
): Result[void,KvtError] =
## For the argument `key` associated the argument `data` as value (which
## will be marked in the top layer cache.)
if key.len == 0:
return err(KeyInvalid)
if data.len == 0:
return err(DataInvalid)
db.layersPut(key, data)
ok()
proc del*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record to delete
): Result[void,KvtError] =
## For the argument `key` delete the associated value (which will be marked
## in the top layer cache.)
if key.len == 0:
return err(KeyInvalid)
db.layersPut(key, EmptyBlob)
ok()
# ------------
proc get*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[Blob,KvtError] =
## For the argument `key` return the associated value preferably from the
## top layer, or the database otherwise.
##
if key.len == 0:
return err(KeyInvalid)
var data = db.layersGet(key).valueOr:
return db.getBe key
return ok(move(data))
proc len*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[int,KvtError] =
## For the argument `key` return the associated value preferably from the
## top layer, or the database otherwise.
##
if key.len == 0:
return err(KeyInvalid)
let len = db.layersLen(key).valueOr:
return db.getBeLen key
ok(len)
proc hasKey*(
db: KvtDbRef; # Database
key: openArray[byte]; # Key of database record
): Result[bool,KvtError] =
## For the argument `key` return the associated value preferably from the
## top layer, or the database otherwise.
##
if key.len == 0:
return err(KeyInvalid)
if db.layersHasKey key:
return ok(true)
let rc = db.getBe key
if rc.isOk:
return ok(true)
if rc.error == GetNotFound:
return ok(false)
err(rc.error)
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------