From 49a7f2c9c6a9776bfaec6b34a7e0f69616372ff2 Mon Sep 17 00:00:00 2001 From: advaita-saha Date: Mon, 6 May 2024 14:10:03 +0530 Subject: [PATCH] add: CoreDbKvtRef as db, persistCode, getCode --- nimbus/db/ledger/verkle_accounts_cache.nim | 24 ++++++++++++++++------ nimbus/db/verkle/verkle_accounts.nim | 9 +++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/nimbus/db/ledger/verkle_accounts_cache.nim b/nimbus/db/ledger/verkle_accounts_cache.nim index cc397bb36..280f387ed 100644 --- a/nimbus/db/ledger/verkle_accounts_cache.nim +++ b/nimbus/db/ledger/verkle_accounts_cache.nim @@ -283,12 +283,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: @@ -330,10 +328,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) diff --git a/nimbus/db/verkle/verkle_accounts.nim b/nimbus/db/verkle/verkle_accounts.nim index 27b81e749..ce6f17860 100644 --- a/nimbus/db/verkle/verkle_accounts.nim +++ b/nimbus/db/verkle/verkle_accounts.nim @@ -53,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] @@ -243,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 + ) # ################################################################ #