2018-04-06 14:52:10 +00:00
|
|
|
# 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.
|
|
|
|
|
2018-01-29 17:40:22 +00:00
|
|
|
import
|
2018-07-05 12:41:01 +00:00
|
|
|
sequtils, tables,
|
2018-08-23 03:38:00 +00:00
|
|
|
chronicles, eth_common, nimcrypto, rlp, eth_trie/[hexary, memdb],
|
|
|
|
../constants, ../errors, ../validation, ../account
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "state_db"
|
2018-01-29 17:40:22 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
AccountStateDB* = ref object
|
2018-08-08 12:48:34 +00:00
|
|
|
trie: SecureHexaryTrie
|
2018-01-29 17:40:22 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc rootHash*(accountDb: AccountStateDB): KeccakHash =
|
|
|
|
accountDb.trie.rootHash
|
|
|
|
|
|
|
|
# proc `rootHash=`*(db: var AccountStateDB, value: string) =
|
|
|
|
# TODO: self.Trie.rootHash = value
|
|
|
|
|
|
|
|
proc newAccountStateDB*(backingStore: TrieDatabaseRef,
|
|
|
|
root: KeccakHash, readOnly: bool = false): AccountStateDB =
|
|
|
|
result.new()
|
2018-08-08 12:48:34 +00:00
|
|
|
result.trie = initSecureHexaryTrie(backingStore, root)
|
2018-01-29 17:40:22 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
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-05-30 16:11:15 +00:00
|
|
|
proc getAccount(db: AccountStateDB, address: EthAddress): Account =
|
2018-07-05 12:41:01 +00:00
|
|
|
let recordFound = db.trie.get(createRangeFromAddress address)
|
|
|
|
if recordFound.len > 0:
|
|
|
|
result = rlp.decode(recordFound, Account)
|
|
|
|
else:
|
|
|
|
result = newAccount()
|
2018-01-29 17:40:22 +00:00
|
|
|
|
2018-08-08 12:48:34 +00:00
|
|
|
proc setAccount*(db: AccountStateDB, address: EthAddress, account: Account) =
|
2018-07-05 12:41:01 +00:00
|
|
|
db.trie.put createRangeFromAddress(address), rlp.encode(account)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
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-01-29 17:40:22 +00:00
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc getBalance*(db: AccountStateDB, address: EthAddress): UInt256 =
|
2018-01-29 17:40:22 +00:00
|
|
|
let account = db.getAccount(address)
|
|
|
|
account.balance
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc setBalance*(db: var AccountStateDB, address: EthAddress, balance: UInt256) =
|
2018-07-05 12:41:01 +00:00
|
|
|
var account = db.getAccount(address)
|
2018-01-29 17:40:22 +00:00
|
|
|
account.balance = balance
|
|
|
|
db.setAccount(address, account)
|
|
|
|
|
2018-05-30 16:11:15 +00:00
|
|
|
proc deltaBalance*(db: var AccountStateDB, address: EthAddress, delta: UInt256) =
|
2018-01-29 17:40:22 +00:00
|
|
|
db.setBalance(address, db.getBalance(address) + delta)
|
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
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:
|
|
|
|
@(keccak256.digest(slot.toByteArrayBE).data).toRange
|
|
|
|
# Original py-evm code:
|
|
|
|
# pad32(int_to_big_endian(slot))
|
|
|
|
|
2018-08-08 12:48:34 +00:00
|
|
|
template getAccountTrie(stateDb: AccountStateDB, account: Account): auto =
|
|
|
|
initSecureHexaryTrie(HexaryTrie(stateDb.trie).db, account.storageRoot)
|
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc setStorage*(db: var AccountStateDB,
|
|
|
|
address: EthAddress,
|
|
|
|
slot: UInt256, value: UInt256) =
|
2018-02-20 17:27:43 +00:00
|
|
|
#validateGte(value, 0, title="Storage Value")
|
|
|
|
#validateGte(slot, 0, title="Storage Slot")
|
2018-04-06 14:52:10 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
var account = db.getAccount(address)
|
2018-08-08 12:48:34 +00:00
|
|
|
var accountTrie = getAccountTrie(db, account)
|
2018-07-05 12:41:01 +00:00
|
|
|
let slotAsKey = createTrieKeyFromSlot slot
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-02-26 11:59:56 +00:00
|
|
|
if value > 0:
|
2018-07-05 12:41:01 +00:00
|
|
|
let encodedValue = rlp.encode value
|
|
|
|
accountTrie.put(slotAsKey, encodedValue)
|
2018-02-26 11:59:56 +00:00
|
|
|
else:
|
2018-07-05 12:41:01 +00:00
|
|
|
accountTrie.del(slotAsKey)
|
|
|
|
|
|
|
|
account.storageRoot = accountTrie.rootHash
|
|
|
|
db.setAccount(address, account)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc getStorage*(db: AccountStateDB, address: EthAddress, slot: UInt256): (UInt256, bool) =
|
|
|
|
let
|
|
|
|
account = db.getAccount(address)
|
|
|
|
slotAsKey = createTrieKeyFromSlot slot
|
2018-08-08 12:48:34 +00:00
|
|
|
accountTrie = getAccountTrie(db, account)
|
2018-04-06 14:52:10 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
let
|
|
|
|
foundRecord = accountTrie.get(slotAsKey)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
if foundRecord.len > 0:
|
|
|
|
result = (rlp.decode(foundRecord, UInt256), true)
|
2018-02-20 17:27:43 +00:00
|
|
|
else:
|
2018-02-26 11:59:56 +00:00
|
|
|
result = (0.u256, false)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-08-31 17:38:17 +00:00
|
|
|
proc setNonce*(db: var 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
|
|
|
|
2018-08-31 17:38:17 +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
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc toByteRange_Unnecessary*(h: KeccakHash): ByteRange =
|
|
|
|
## XXX: Another proc used to mark unnecessary conversions it the code
|
|
|
|
var s = @(h.data)
|
|
|
|
return s.toRange
|
|
|
|
|
|
|
|
proc setCode*(db: var AccountStateDB, address: EthAddress, code: ByteRange) =
|
2018-02-13 17:18:08 +00:00
|
|
|
var account = db.getAccount(address)
|
2018-08-09 09:25:37 +00:00
|
|
|
let newCodeHash = keccak256.digest code.toOpenArray
|
|
|
|
if newCodeHash != account.codeHash:
|
|
|
|
account.codeHash = newCodeHash
|
|
|
|
# XXX: this uses the journaldb in py-evm
|
|
|
|
db.trie.put(account.codeHash.toByteRange_Unnecessary, code)
|
|
|
|
db.setAccount(address, account)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-07-05 12:41:01 +00:00
|
|
|
proc getCode*(db: AccountStateDB, address: EthAddress): ByteRange =
|
2018-02-13 17:18:08 +00:00
|
|
|
let codeHash = db.getCodeHash(address)
|
2018-07-05 12:41:01 +00:00
|
|
|
result = db.trie.get(codeHash.toByteRange_Unnecessary)
|
2018-02-13 17:18:08 +00:00
|
|
|
|
2018-09-03 18:24:55 +00:00
|
|
|
proc hasCodeOrNonce*(account: AccountStateDB, address: EthAddress): bool {.inline.} =
|
|
|
|
account.getNonce(address) != 0 or account.getCodeHash(address) != EMPTY_SHA3
|