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 + ) # ################################################################ #