nimbus-eth1/nimbus/db/state_db.nim

213 lines
8.2 KiB
Nim
Raw Normal View History

# Nimbus
# Copyright (c) 2018 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.
import
sequtils, strformat, tables,
2019-02-26 07:04:12 +00:00
chronicles, eth/[common, rlp], eth/trie/[hexary, db],
../constants, ../errors, ../validation, ../utils,
2018-12-04 08:13:35 +00:00
storage_types
logScope:
topics = "state_db"
type
AccountStateDB* = ref object
2018-12-31 03:27:02 +00:00
trie: SecureHexaryTrie
2018-12-31 03:27:02 +00:00
ReadOnlyStateDB* = distinct AccountStateDB
2018-12-31 03:27:02 +00:00
proc rootHash*(db: AccountStateDB): KeccakHash =
db.trie.rootHash
proc `rootHash=`*(db: AccountStateDB, root: KeccakHash) =
db.trie = initSecureHexaryTrie(HexaryTrie(db.trie).db, root, db.trie.isPruning)
proc newAccountStateDB*(backingStore: TrieDatabaseRef,
2018-12-31 03:27:02 +00:00
root: KeccakHash, pruneTrie: bool): AccountStateDB =
result.new()
2018-11-30 10:07:20 +00:00
result.trie = initSecureHexaryTrie(backingStore, root, pruneTrie)
template createRangeFromAddress(address: EthAddress): ByteRange =
## XXX: The name of this proc is intentionally long, because it
## performs a memory allocation and data copying that may be eliminated
## in the future. Avoid renaming it to something similar as `toRange`, so
## it can remain searchable in the code.
toRange(@address)
2018-12-10 15:54:19 +00:00
proc getAccount*(db: AccountStateDB, address: EthAddress): Account =
let recordFound = db.trie.get(createRangeFromAddress address)
if recordFound.len > 0:
result = rlp.decode(recordFound, Account)
else:
result = newAccount()
proc setAccount*(db: AccountStateDB, address: EthAddress, account: Account) =
db.trie.put createRangeFromAddress(address), rlp.encode(account).toRange
2018-02-13 17:18:08 +00:00
proc deleteAccount*(db: AccountStateDB, address: EthAddress) =
db.trie.del createRangeFromAddress(address)
2018-05-30 16:11:15 +00:00
proc getCodeHash*(db: AccountStateDB, address: EthAddress): Hash256 =
2018-02-13 17:18:08 +00:00
let account = db.getAccount(address)
result = account.codeHash
2018-05-30 16:11:15 +00:00
proc getBalance*(db: AccountStateDB, address: EthAddress): UInt256 =
let account = db.getAccount(address)
account.balance
2018-05-30 16:11:15 +00:00
proc setBalance*(db: var AccountStateDB, address: EthAddress, balance: UInt256) =
var account = db.getAccount(address)
account.balance = balance
db.setAccount(address, account)
2018-09-10 08:44:07 +00:00
proc addBalance*(db: var AccountStateDB, address: EthAddress, delta: UInt256) =
db.setBalance(address, db.getBalance(address) + delta)
proc subBalance*(db: var AccountStateDB, address: EthAddress, delta: UInt256) =
db.setBalance(address, db.getBalance(address) - delta)
template createTrieKeyFromSlot(slot: UInt256): ByteRange =
# XXX: This is too expensive. Similar to `createRangeFromAddress`
# Converts a number to hex big-endian representation including
# prefix and leading zeros:
@(slot.toByteArrayBE).toRange
# Original py-evm code:
# pad32(int_to_big_endian(slot))
# morally equivalent to toByteRange_Unnecessary but with different types
template getAccountTrie(stateDb: AccountStateDB, account: Account): auto =
2019-02-16 07:00:50 +00:00
# TODO: implement `prefix-db` to solve issue #228 permanently.
# the `prefix-db` will automatically insert account address to the
# underlying-db key without disturb how the trie works.
# it will create virtual container for each account.
# see nim-eth#9
2019-02-14 14:53:14 +00:00
initSecureHexaryTrie(HexaryTrie(stateDb.trie).db, account.storageRoot, false)
# XXX: https://github.com/status-im/nimbus/issues/142#issuecomment-420583181
proc setStorageRoot*(db: var AccountStateDB, address: EthAddress, storageRoot: Hash256) =
var account = db.getAccount(address)
account.storageRoot = storageRoot
db.setAccount(address, account)
2018-12-04 08:13:35 +00:00
proc getStorageRoot*(db: AccountStateDB, address: EthAddress): Hash256 =
var account = db.getAccount(address)
account.storageRoot
proc setStorage*(db: var AccountStateDB,
address: EthAddress,
slot: UInt256, value: UInt256) =
var account = db.getAccount(address)
var accountTrie = getAccountTrie(db, account)
let slotAsKey = createTrieKeyFromSlot slot
2018-02-13 17:18:08 +00:00
2018-02-26 11:59:56 +00:00
if value > 0:
let encodedValue = rlp.encode(value).toRange
accountTrie.put(slotAsKey, encodedValue)
2018-02-26 11:59:56 +00:00
else:
accountTrie.del(slotAsKey)
2018-12-04 08:13:35 +00:00
# map slothash back to slot value
# see iterator storage below
var
triedb = HexaryTrie(db.trie).db
# slotHash can be obtained from accountTrie.put?
2019-03-07 15:53:09 +00:00
slotHash = keccakHash(slot.toByteArrayBE)
2018-12-04 08:13:35 +00:00
triedb.put(slotHashToSlotKey(slotHash.data).toOpenArray, rlp.encode(slot))
account.storageRoot = accountTrie.rootHash
db.setAccount(address, account)
2018-02-13 17:18:08 +00:00
2018-12-04 08:13:35 +00:00
iterator storage*(db: AccountStateDB, address: EthAddress): (UInt256, UInt256) =
let
storageRoot = db.getStorageRoot(address)
triedb = HexaryTrie(db.trie).db
var trie = initHexaryTrie(triedb, storageRoot)
for key, value in trie:
if key.len != 0:
var keyData = triedb.get(slotHashToSlotKey(key.toOpenArray).toOpenArray).toRange
yield (rlp.decode(keyData, UInt256), rlp.decode(value, UInt256))
proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) =
let
account = db.getAccount(address)
slotAsKey = createTrieKeyFromSlot slot
accountTrie = getAccountTrie(db, account)
let
foundRecord = accountTrie.get(slotAsKey)
2018-02-13 17:18:08 +00:00
if foundRecord.len > 0:
result = (rlp.decode(foundRecord, UInt256), true)
else:
2018-02-26 11:59:56 +00:00
result = (0.u256, false)
2018-02-13 17:18:08 +00:00
2018-12-31 03:27:02 +00:00
proc setNonce*(db: AccountStateDB, address: EthAddress, newNonce: AccountNonce) =
2018-02-13 17:18:08 +00:00
var account = db.getAccount(address)
2018-08-09 09:25:37 +00:00
if newNonce != account.nonce:
account.nonce = newNonce
db.setAccount(address, account)
2018-02-13 17:18:08 +00:00
proc getNonce*(db: AccountStateDB, address: EthAddress): AccountNonce =
2018-02-13 17:18:08 +00:00
let account = db.getAccount(address)
Refactor interpreter dispatch (#65) * move forks constants, rename errors * Move vm/utils to vm/interpreter/utils * initial opcodes refactoring * Add refactored Comparison & Bitwise Logic Operations * Add sha3 and address, simplify macro, support pop 0 * balance, origin, caller, callValue * fix gas copy opcodes gas costs, add callDataLoad/Size/Copy, CodeSize/Copy and gas price opcode * Update with 30s, 40s, 50s opcodes + impl of balance + stack improvement * add push, dup, swap, log, create and call operations * finish opcode implementation * Add the new dispatching logic * Pass the opcode test * Make test_vm_json compile * halt execution without exceptions for Return, Revert, selfdestruct (fix #62) * Properly catch and recover from EVM exceptions (stack underflow ...) * Fix byte op * Fix jump regressions * Update for latest devel, don't import old dispatch code as quasiBoolean macro is broken by latest devel * Fix sha3 regression on empty memory slice and until end of range slice * Fix padding / range error on expXY_success (gas computation left) * update logging procs * Add tracing - expXY_success is not a regression, sload stub was accidentally passing the test * Reuse the same stub as OO implementation * Delete previous opcode implementation * Delete object oriented fork code * Delete exceptions that were used as control flows * delete base.nim :fire:, yet another OO remnants * Delete opcode table * Enable omputed gotos and compile-time gas fees * Revert const gasCosts -> generates SIGSEGV * inline push, swap and dup opcodes * loggers are now template again, why does this pass new tests? * Trigger CI rebuild after rocksdb fix https://github.com/status-im/nim-rocksdb/pull/5 * Address review comment on "push" + VMTests in debug mode (not release) * Address review comment: don't tag fork by default, make opcode impl grepable * Static compilation fixes after rebasing * fix the initialization of the VM database * add a missing import * Deactivate balance and sload test following #59 * Reactivate stack check (deactivated in #59, necessary to pass tests) * Merge remaining opcodes implementation from #59 * Merge callDataLoad and codeCopy fixes, todo simplify see #67
2018-07-06 07:52:31 +00:00
account.nonce
2018-02-13 17:18:08 +00:00
2019-02-26 07:41:37 +00:00
proc incNonce*(db: AccountStateDB, address: EthAddress) {.inline.} =
db.setNonce(address, db.getNonce(address) + 1)
2018-12-31 03:27:02 +00:00
proc setCode*(db: AccountStateDB, address: EthAddress, code: ByteRange) =
2018-02-13 17:18:08 +00:00
var account = db.getAccount(address)
2018-12-31 03:27:02 +00:00
# TODO: implement JournalDB to store code and storage
# also use JournalDB to revert state trie
let
2019-03-07 15:53:09 +00:00
newCodeHash = keccakHash(code.toOpenArray)
2018-12-31 03:27:02 +00:00
triedb = HexaryTrie(db.trie).db
if code.len != 0:
triedb.put(contractHashKey(newCodeHash).toOpenArray, code.toOpenArray)
account.codeHash = newCodeHash
db.setAccount(address, account)
2018-02-13 17:18:08 +00:00
proc getCode*(db: AccountStateDB, address: EthAddress): ByteRange =
2018-12-31 03:27:02 +00:00
let triedb = HexaryTrie(db.trie).db
let data = triedb.get(contractHashKey(db.getCodeHash(address)).toOpenArray)
data.toRange
2018-02-13 17:18:08 +00:00
proc hasCodeOrNonce*(db: AccountStateDB, address: EthAddress): bool {.inline.} =
db.getNonce(address) != 0 or db.getCodeHash(address) != EMPTY_SHA3
proc dumpAccount*(db: AccountStateDB, addressS: string): string =
let address = addressS.parseAddress
return fmt"{addressS}: Storage: {db.getStorage(address, 0.u256)}; getAccount: {db.getAccount address}"
2019-02-02 09:17:57 +00:00
proc accountExists*(db: AccountStateDB, address: EthAddress): bool =
db.trie.get(createRangeFromAddress address).len > 0
proc isDeadAccount*(db: AccountStateDB, address: EthAddress): bool =
let recordFound = db.trie.get(createRangeFromAddress address)
if recordFound.len > 0:
let account = rlp.decode(recordFound, Account)
result = account.codeHash == EMPTY_SHA3 and
account.balance.isZero and
account.nonce == 0
else:
result = true
2018-12-31 03:27:02 +00:00
proc rootHash*(db: ReadOnlyStateDB): KeccakHash {.borrow.}
proc getAccount*(db: ReadOnlyStateDB, address: EthAddress): Account {.borrow.}
proc getCodeHash*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.}
proc getBalance*(db: ReadOnlyStateDB, address: EthAddress): UInt256 {.borrow.}
proc getStorageRoot*(db: ReadOnlyStateDB, address: EthAddress): Hash256 {.borrow.}
proc getStorage*(db: ReadOnlyStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) {.borrow.}
proc getNonce*(db: ReadOnlyStateDB, address: EthAddress): AccountNonce {.borrow.}
proc getCode*(db: ReadOnlyStateDB, address: EthAddress): ByteRange {.borrow.}
proc hasCodeOrNonce*(db: ReadOnlyStateDB, address: EthAddress): bool {.borrow.}
2019-02-02 09:17:57 +00:00
proc accountExists*(db: ReadOnlyStateDB, address: EthAddress): bool {.borrow.}
proc isDeadAccount*(db: ReadOnlyStateDB, address: EthAddress): bool {.borrow.}