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

Add kvstore for contract code #2165

Open
wants to merge 4 commits into
base: verkle-kaustinen
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions nimbus/db/ledger/backend/accounts_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import
eth/common,
../../../../stateless/multi_keys,
"../.."/[core_db, distinct_tries],
../accounts_cache as impl,
../verkle_accounts_cache as impl,
".."/[base, base/base_desc],
./accounts_cache_desc as wrp

Expand Down Expand Up @@ -51,7 +51,7 @@ proc ledgerMethods(lc: impl.AccountsCache): LedgerFns =
lc.addLogEntry(log),

beginSavepointFn: proc(): LedgerSpRef =
wrp.SavePoint(sp: lc.beginSavepoint()),
wrp.SavePoint(sp: lc.beginSavePoint()),

clearStorageFn: proc(eAddr: EthAddress) =
lc.clearStorage(eAddr),
Expand Down Expand Up @@ -184,7 +184,7 @@ proc ledgerMethods(lc: impl.AccountsCache): LedgerFns =
proc ledgerExtras(lc: impl.AccountsCache): LedgerExtras =
LedgerExtras(
getMptFn: proc(): CoreDbMptRef =
lc.rawTrie.mpt,
lc.rawTrie.mpt, # -----------> needs to be fixed, MPT doesn't exist anymore

rawRootHashFn: proc(): Hash256 =
lc.rawTrie.rootHash())
Expand All @@ -195,7 +195,7 @@ proc newLegacyAccountsCache(
root: Hash256;
pruneTrie: bool): LedgerRef =
## Constructor
let lc = impl.AccountsCache.init(db, root, pruneTrie)
let lc = impl.AccountsCache.init()
wrp.AccountsCache(
ldgType: LegacyAccountsCache,
ac: lc,
Expand Down Expand Up @@ -231,7 +231,7 @@ iterator storageIt*(
): (UInt256,UInt256)
{.gcsafe, raises: [CoreDbApiError].} =
noRlpException "storage()":
for w in lc.ac.storage(eAddr):
for w in lc.ac.storage(eAddr): # -----------> needs to be fixed, storage() doesn't exist yet
yield w

# ------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion nimbus/db/ledger/backend/accounts_cache_desc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# according to those terms.

import
../accounts_cache as impl,
../verkle_accounts_cache as impl,
../base/base_desc

type
Expand Down
38 changes: 31 additions & 7 deletions nimbus/db/ledger/verkle_accounts_cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ proc rawTrie*(ac: AccountsCache): VerkleTrie {.inline.} = ac.trie
proc init*(x: typedesc[AccountsCache]): AccountsCache =
new result
result.trie = initVerkleTrie()
result.witnessCache = initTable[EthAddress, WitnessData]
result.witnessCache = initTable[EthAddress, WitnessData]()
discard result.beginSavePoint

proc rootHash*(ac: AccountsCache): KeccakHash =
Expand Down Expand Up @@ -261,6 +261,18 @@ proc storageValue(acc: RefAccount, address: EthAddress, slot: UInt256, db: Verkl
do:
result = acc.originalStorageValue(address, slot, db)

proc getCommittedStorage*(ac: AccountsCache, address: EthAddress, slot: UInt256): UInt256 {.inline.} =
let acc = ac.getAccount(address, false)
if acc.isNil:
return
acc.originalStorageValue(address, slot, ac.trie)

proc getStorage*(ac: AccountsCache, address: EthAddress, slot: UInt256): UInt256 {.inline.} =
let acc = ac.getAccount(address, false)
if acc.isNil:
return
acc.storageValue(address, slot, ac.trie)

proc kill(acc: RefAccount) =
acc.flags.excl Alive
acc.overlayStorage.clear()
Expand All @@ -283,12 +295,10 @@ proc persistMode(acc: RefAccount): PersistMode =
if IsNew notin acc.flags:
result = Remove

proc persistCode(acc: RefAccount, address: EthAddress, db: VerkleTrie) =
proc persistCode(acc: RefAccount, address: EthAddress, trie: VerkleTrie) =
if acc.code.len != 0:
when defined(geth):
VerkleTrieRef(db).updateContractCode(address, acc.account.codeHash, acc.code)
else:
VerkleTrieRef(db).updateContractCode(address, acc.account.codeHash, acc.code)
VerkleTrieRef(trie).updateContractCode(address, acc.account.codeHash, acc.code)
VerkleTrieRef(trie).db.put(acc.account.codeHash.data, acc.code)

proc persistStorage(acc: RefAccount, address: EthAddress, db: VerkleTrie, clearCache: bool) =
if acc.overlayStorage.len == 0:
Expand Down Expand Up @@ -330,10 +340,24 @@ proc getNonce*(ac: AccountsCache, address: EthAddress): AccountNonce {.inline.}
if acc.isNil: emptyAcc.nonce
else: acc.account.nonce

proc getCode*(ac: AccountsCache, address: EthAddress): seq[byte] =
let acc = ac.getAccount(address, false)
if acc.isNil:
return

if CodeLoaded in acc.flags or CodeChanged in acc.flags:
result = acc.code
else:
let data = VerkleTrieRef(ac.trie).db.get(acc.account.codeHash.data)

acc.code = data
acc.flags.incl CodeLoaded
result = acc.code

proc getCodeSize*(ac: AccountsCache, address: EthAddress): int {.inline.} =
let acc = ac.getAccount(address, false)
if acc.isNil: 0
else: acc.code.len
else: int(VerkleTrieRef(ac.trie).getCodeSize(address))

proc getCodeHash*(ac: AccountsCache, address: EthAddress): Hash256 {.inline.} =
let acc = ac.getAccount(address, false)
Expand Down
14 changes: 11 additions & 3 deletions nimbus/db/verkle/verkle_accounts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import
],
"../../../vendor/nim-eth-verkle/constantine/constantine"/[
serialization/codecs_banderwagon
]
],
".."/core_db

type
ChunkedCode* = seq[byte]
VerkleTrieRef* = ref object
root: BranchesNode
# db: <something>
db: CoreDbKvtRef
# persistcheck: <some-flag>

const
Expand All @@ -52,6 +53,10 @@ var MainStorageOffsetLshVerkleNodeWidth*: UInt256 = one shl twofourty
#
# ################################################################

# Returns the kv-store ref
proc db*(trie: VerkleTrieRef): CoreDbKvtRef =
return trie.db

# Check if the account loaded is empty or not
proc isEmptyVerkleAccount*(acc: Account): bool =
var zero: array[32, byte]
Expand Down Expand Up @@ -242,7 +247,10 @@ proc getTreeKeyStorageSlotWithEvaluatedAddress*(addressPoint: Point, storageKey:
return getTreeKeyWithEvaluatedAddress(addressPoint, treeIndex, subIndex)

proc newVerkleTrie*(): VerkleTrieRef =
result = VerkleTrieRef(root: newTree())
result = VerkleTrieRef(
root: newTree(),
db: LegacyDbMemory.newCoreDbRef().newKvt().CoreDbKvtRef
)

# ################################################################
#
Expand Down
Loading