Skip to content

Commit

Permalink
implement iterator for fake db (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
laizy authored Jun 1, 2022
1 parent f12c987 commit f3cc0fc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 12 deletions.
29 changes: 29 additions & 0 deletions evm/storage/fakedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,32 @@ func NewFakeDB() *FakeDB {
func (self *FakeDB) GetBlockHash(height uint64) web3.Hash {
return web3.Hash{}
}

func (self *FakeDB) NewIterator(prefix []byte) schema.StoreIterator {
return &fakeIter{}
}

type fakeIter struct{}

func (self *fakeIter) Next() bool {
return false
}

func (self *fakeIter) First() bool {
return false
}

func (self *fakeIter) Key() []byte {
return nil
}
func (self *fakeIter) Value() []byte {
return nil
}

func (self *fakeIter) Release() {
return
}

func (self *fakeIter) Error() error {
return nil
}
17 changes: 15 additions & 2 deletions evm/storage/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package storage

import (
"bytes"
"fmt"
"io"
"math/big"
Expand All @@ -27,6 +28,7 @@ import (
"github.com/laizy/web3/crypto"
"github.com/laizy/web3/evm/storage/overlaydb"
"github.com/laizy/web3/evm/storage/schema"
"github.com/laizy/web3/utils"
"github.com/laizy/web3/utils/codec"
"github.com/laizy/web3/utils/common/hexutil"
"github.com/laizy/web3/utils/common/uint256"
Expand Down Expand Up @@ -353,8 +355,19 @@ func (self *StateDB) AddPreimage(web3.Hash, []byte) {
// todo
}

func (self *StateDB) ForEachStorage(web3.Address, func(web3.Hash, web3.Hash) bool) error {
panic("todo")
func (self *StateDB) ForEachStorage(addr web3.Address, fn func(web3.Hash, web3.Hash) bool) error {
iter := self.cacheDB.NewIterator(addr[:])
for has := iter.First(); has; has = iter.Next() {
key, value := iter.Key(), iter.Value()
utils.EnsureTrue(len(key) == 20+32 && len(value) == 32 && bytes.Equal(key[:20], addr[:]))

if !fn(web3.BytesToHash(key), web3.BytesToHash(value)) {
break
}
}
iter.Release()

return iter.Error()
}

func (self *StateDB) CreateAccount(web3.Address) {
Expand Down
20 changes: 10 additions & 10 deletions jsonrpc/transport/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (

type Local struct {
db schema.ChainDB
exec *executor.Executor
Executor *executor.Executor
BlockNumber uint64
BlockHashes map[uint64]web3.Hash
Receipts map[web3.Hash]*web3.Receipt
Expand All @@ -28,7 +28,7 @@ type Local struct {
func NewLocal(db schema.ChainDB, chainID uint64) *Local {
return &Local{
db: db,
exec: executor.NewExecutor(db, chainID),
Executor: executor.NewExecutor(db, chainID),
BlockNumber: 0,
BlockHashes: make(map[uint64]web3.Hash),
Receipts: make(map[web3.Hash]*web3.Receipt),
Expand All @@ -42,14 +42,14 @@ func (self *Local) Close() error {
}

func (self *Local) GetBalance(acct web3.Address) (amount *big.Int) {
cacheDB := storage.NewCacheDB(self.exec.OverlayDB)
cacheDB := storage.NewCacheDB(self.Executor.OverlayDB)
val, err := cacheDB.GetEthAccount(acct)
utils.Ensure(err)
return val.Balance.ToBig()
}

func (self *Local) SetBalance(acct web3.Address, amount *big.Int) {
cacheDB := storage.NewCacheDB(self.exec.OverlayDB)
cacheDB := storage.NewCacheDB(self.Executor.OverlayDB)
val, err := cacheDB.GetEthAccount(acct)
utils.Ensure(err)
val.Balance, _ = uint256.FromBig(amount)
Expand All @@ -68,7 +68,7 @@ func (self *Local) Call(method string, out interface{}, params ...interface{}) e
switch method {
case "eth_getCode":
addr := params[0].(web3.Address)
cacheDB := storage.NewCacheDB(self.exec.OverlayDB)
cacheDB := storage.NewCacheDB(self.Executor.OverlayDB)
val, err := cacheDB.GetEthAccount(addr)
if err != nil {
return err
Expand All @@ -93,7 +93,7 @@ func (self *Local) Call(method string, out interface{}, params ...interface{}) e
result = utils.JsonBytes(hexutil.Uint64(res.UsedGas))
case "eth_sendTransaction":
txn := params[0].(*web3.Transaction)
_, receipt, err := self.exec.ExecuteTransaction(txn, executor.Eip155Context{
_, receipt, err := self.Executor.ExecuteTransaction(txn, executor.Eip155Context{
BlockHash: web3.Hash{},
Height: self.BlockNumber,
})
Expand All @@ -107,10 +107,10 @@ func (self *Local) Call(method string, out interface{}, params ...interface{}) e
rawTx := params[0].(string)
txn, err := web3.TransactionFromRlp(web3.Hex2Bytes(rawTx[2:]))
utils.Ensure(err)
sender, err := wallet.NewEIP155Signer(self.exec.ChainID).RecoverSender(txn)
sender, err := wallet.NewEIP155Signer(self.Executor.ChainID).RecoverSender(txn)
utils.Ensure(err)
txn.From = sender
_, receipt, err := self.exec.ExecuteTransaction(txn, executor.Eip155Context{
_, receipt, err := self.Executor.ExecuteTransaction(txn, executor.Eip155Context{
BlockHash: web3.Hash{},
Height: self.BlockNumber,
Timestamp: self.BlockNumber * 12,
Expand All @@ -123,7 +123,7 @@ func (self *Local) Call(method string, out interface{}, params ...interface{}) e
result = utils.JsonBytes(txn.Hash().String())
case "eth_getTransactionCount":
addr := params[0].(web3.Address)
cacheDB := storage.NewCacheDB(self.exec.OverlayDB)
cacheDB := storage.NewCacheDB(self.Executor.OverlayDB)
val, err := cacheDB.GetEthAccount(addr)
if err != nil {
return err
Expand All @@ -144,7 +144,7 @@ func (self *Local) Call(method string, out interface{}, params ...interface{}) e
}

func (self *Local) CallEvm(msg *web3.CallMsg) (*web3.ExecutionResult, error) {
res, _, err := self.exec.Call(CallMsg{msg}, executor.Eip155Context{
res, _, err := self.Executor.Call(CallMsg{msg}, executor.Eip155Context{
BlockHash: web3.Hash{},
Height: self.BlockNumber,
Timestamp: self.BlockNumber * 12,
Expand Down

0 comments on commit f3cc0fc

Please sign in to comment.