Enable test_state_db (#2244)

* Make test_state_db compilable

* Enable test_state_db
This commit is contained in:
andri lim 2024-05-30 12:44:52 +07:00 committed by GitHub
parent 674394b924
commit 4b8219a0dd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 45 deletions

View File

@ -31,7 +31,7 @@ cliBuilder:
./test_op_memory, ./test_op_memory,
./test_op_misc, ./test_op_misc,
./test_op_custom, ./test_op_custom,
#./test_state_db, -- does not compile ./test_state_db,
./test_difficulty, ./test_difficulty,
./test_transaction_json, ./test_transaction_json,
#./test_blockchain_json, -- fails #./test_blockchain_json, -- fails

View File

@ -6,12 +6,15 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms. # at your option. This file may not be copied, modified, or distributed except according to those terms.
import import
std/importutils,
eth/trie/trie_defs, eth/trie/trie_defs,
eth/common/eth_types,
stew/[byteutils, endians2], stew/[byteutils, endians2],
unittest2, unittest2,
../nimbus/db/state_db/read_write ../nimbus/db/storage_types,
../nimbus/db/core_db,
include ../nimbus/db/ledger/accounts_cache ../nimbus/db/ledger,
../nimbus/db/ledger/accounts_ledger {.all.} # import all private symbols
func initAddr(z: int): EthAddress = func initAddr(z: int): EthAddress =
const L = sizeof(result) const L = sizeof(result)
@ -23,10 +26,8 @@ proc stateDBMain*() =
const emptyAcc {.used.} = newAccount() const emptyAcc {.used.} = newAccount()
var var
memDB = newCoreDbRef LegacyDbMemory memDB = newCoreDbRef DefaultDbMemory
acDB {.used.} = newCoreDbRef LegacyDbMemory stateDB {.used.} = LedgerRef.init(memDB, emptyRlpHash)
trie = memDB.mptPrune()
stateDB {.used.} = newAccountStateDB(memDB, trie.rootHash, true)
address {.used.} = hexToByteArray[20]("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6") address {.used.} = hexToByteArray[20]("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
code {.used.} = hexToSeqByte("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6") code {.used.} = hexToSeqByte("0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6")
rootHash {.used.} : KeccakHash rootHash {.used.} : KeccakHash
@ -35,28 +36,27 @@ proc stateDBMain*() =
check stateDB.accountExists(address) == false check stateDB.accountExists(address) == false
check stateDB.isDeadAccount(address) == true check stateDB.isDeadAccount(address) == true
var acc = stateDB.getAccount(address) stateDB.setBalance(address, 1000.u256)
acc.balance = 1000.u256
stateDB.setAccount(address, acc)
check stateDB.accountExists(address) == true check stateDB.accountExists(address) == true
check stateDB.isDeadAccount(address) == false check stateDB.isDeadAccount(address) == false
acc.balance = 0.u256 stateDB.setBalance(address, 0.u256)
acc.nonce = 1 stateDB.setNonce(address, 1)
stateDB.setAccount(address, acc)
check stateDB.isDeadAccount(address) == false check stateDB.isDeadAccount(address) == false
stateDB.setCode(address, code) stateDB.setCode(address, code)
stateDB.setNonce(address, 0) stateDB.setNonce(address, 0)
check stateDB.isDeadAccount(address) == false check stateDB.isDeadAccount(address) == false
stateDB.setCode(address, []) stateDB.setCode(address, newSeq[byte]())
check stateDB.isDeadAccount(address) == true check stateDB.isDeadAccount(address) == true
check stateDB.accountExists(address) == true check stateDB.accountExists(address) == true
test "clone storage": test "clone storage":
var x = RefAccount( # give access to private fields of AccountRef
privateAccess(AccountRef)
var x = AccountRef(
overlayStorage: initTable[UInt256, UInt256](), overlayStorage: initTable[UInt256, UInt256](),
originalStorage: newTable[UInt256, UInt256]() originalStorage: newTable[UInt256, UInt256]()
) )
@ -84,7 +84,7 @@ proc stateDBMain*() =
check y.originalStorage.len == 3 check y.originalStorage.len == 3
test "accounts cache": test "accounts cache":
var ac = init(AccountsCache, acDB, emptyRlpHash, true) var ac = LedgerRef.init(memDB, emptyRlpHash)
var addr1 = initAddr(1) var addr1 = initAddr(1)
check ac.isDeadAccount(addr1) == true check ac.isDeadAccount(addr1) == true
@ -116,51 +116,51 @@ proc stateDBMain*() =
ac.persist() ac.persist()
rootHash = ac.rootHash rootHash = ac.rootHash
var db = newAccountStateDB(memDB, emptyRlpHash, true) var db = LedgerRef.init(memDB, emptyRlpHash)
db.setBalance(addr1, 1100.u256) db.setBalance(addr1, 1100.u256)
db.setNonce(addr1, 2) db.setNonce(addr1, 2)
db.setCode(addr1, code) db.setCode(addr1, code)
db.setStorage(addr1, 1.u256, 10.u256) db.setStorage(addr1, 1.u256, 10.u256)
check rootHash == db.rootHash check rootHash == db.rootHash
test "accounts cache readonly operations": # accounts cache readonly operations
# use previous hash # use previous hash
var ac = init(AccountsCache, acDB, rootHash, true) var ac2 = LedgerRef.init(memDB, rootHash)
var addr2 = initAddr(2) var addr2 = initAddr(2)
check ac.getCodeHash(addr2) == emptyAcc.codeHash check ac2.getCodeHash(addr2) == emptyAcc.codeHash
check ac.getBalance(addr2) == emptyAcc.balance check ac2.getBalance(addr2) == emptyAcc.balance
check ac.getNonce(addr2) == emptyAcc.nonce check ac2.getNonce(addr2) == emptyAcc.nonce
check ac.getCode(addr2) == [] check ac2.getCode(addr2) == []
check ac.getCodeSize(addr2) == 0 check ac2.getCodeSize(addr2) == 0
check ac.getCommittedStorage(addr2, 1.u256) == 0.u256 check ac2.getCommittedStorage(addr2, 1.u256) == 0.u256
check ac.getStorage(addr2, 1.u256) == 0.u256 check ac2.getStorage(addr2, 1.u256) == 0.u256
check ac.contractCollision(addr2) == false check ac2.contractCollision(addr2) == false
check ac.accountExists(addr2) == false check ac2.accountExists(addr2) == false
check ac.isDeadAccount(addr2) == true check ac2.isDeadAccount(addr2) == true
ac.persist() ac2.persist()
# readonly operations should not modify # readonly operations should not modify
# state trie at all # state trie at all
check ac.rootHash == rootHash check ac2.rootHash == rootHash
test "accounts cache code retrieval after persist called": test "accounts cache code retrieval after persist called":
var ac = init(AccountsCache, acDB) var ac = LedgerRef.init(memDB, emptyRlpHash)
var addr2 = initAddr(2) var addr2 = initAddr(2)
ac.setCode(addr2, code) ac.setCode(addr2, code)
ac.persist() ac.persist()
check ac.getCode(addr2) == code check ac.getCode(addr2) == code
let key = contractHashKey(keccakHash(code)) let key = contractHashKey(keccakHash(code))
check acDB.kvt.get(key.toOpenArray) == code check memDB.kvt.get(key.toOpenArray) == code
test "accessList operations": test "accessList operations":
proc verifyAddrs(ac: AccountsCache, addrs: varargs[int]): bool = proc verifyAddrs(ac: LedgerRef, addrs: varargs[int]): bool =
for c in addrs: for c in addrs:
if not ac.inAccessList(c.initAddr): if not ac.inAccessList(c.initAddr):
return false return false
true true
proc verifySlots(ac: AccountsCache, address: int, slots: varargs[int]): bool = proc verifySlots(ac: LedgerRef, address: int, slots: varargs[int]): bool =
let a = address.initAddr let a = address.initAddr
if not ac.inAccessList(a): if not ac.inAccessList(a):
return false return false
@ -170,13 +170,13 @@ proc stateDBMain*() =
return false return false
true true
proc accessList(ac: AccountsCache, address: int) {.inline.} = proc accessList(ac: LedgerRef, address: int) {.inline.} =
ac.accessList(address.initAddr) ac.accessList(address.initAddr)
proc accessList(ac: AccountsCache, address, slot: int) {.inline.} = proc accessList(ac: LedgerRef, address, slot: int) {.inline.} =
ac.accessList(address.initAddr, slot.u256) ac.accessList(address.initAddr, slot.u256)
var ac = init(AccountsCache, acDB) var ac = LedgerRef.init(memDB, emptyRlpHash)
ac.accessList(0xaa) ac.accessList(0xaa)
ac.accessList(0xbb, 0x01) ac.accessList(0xbb, 0x01)
@ -218,15 +218,15 @@ proc stateDBMain*() =
check ac.verifySlots(0xdd, 0x04) check ac.verifySlots(0xdd, 0x04)
test "transient storage operations": test "transient storage operations":
var ac = init(AccountsCache, acDB) var ac = LedgerRef.init(memDB, emptyRlpHash)
proc tStore(ac: AccountsCache, address, slot, val: int) = proc tStore(ac: LedgerRef, address, slot, val: int) =
ac.setTransientStorage(address.initAddr, slot.u256, val.u256) ac.setTransientStorage(address.initAddr, slot.u256, val.u256)
proc tLoad(ac: AccountsCache, address, slot: int): UInt256 = proc tLoad(ac: LedgerRef, address, slot: int): UInt256 =
ac.getTransientStorage(address.initAddr, slot.u256) ac.getTransientStorage(address.initAddr, slot.u256)
proc vts(ac: AccountsCache, address, slot, val: int): bool = proc vts(ac: LedgerRef, address, slot, val: int): bool =
ac.tLoad(address, slot) == val.u256 ac.tLoad(address, slot) == val.u256
ac.tStore(0xaa, 3, 66) ac.tStore(0xaa, 3, 66)
@ -285,7 +285,7 @@ proc stateDBMain*() =
test "accounts cache contractCollision": test "accounts cache contractCollision":
# use previous hash # use previous hash
var ac = init(AccountsCache, acDB, emptyRlpHash, true) var ac = LedgerRef.init(memDB, emptyRlpHash)
let addr2 = initAddr(2) let addr2 = initAddr(2)
check ac.contractCollision(addr2) == false check ac.contractCollision(addr2) == false