github.com/lmittmann/w3@v0.20.0/w3vm/db.go (about) 1 package w3vm 2 3 import ( 4 "errors" 5 6 "github.com/ethereum/go-ethereum/common" 7 "github.com/ethereum/go-ethereum/core/rawdb" 8 "github.com/ethereum/go-ethereum/core/state" 9 "github.com/ethereum/go-ethereum/core/state/snapshot" 10 "github.com/ethereum/go-ethereum/core/types" 11 "github.com/ethereum/go-ethereum/trie" 12 "github.com/ethereum/go-ethereum/trie/utils" 13 "github.com/ethereum/go-ethereum/triedb" 14 "github.com/holiman/uint256" 15 ) 16 17 var fakeTrieDB = triedb.NewDatabase(rawdb.NewMemoryDatabase(), &triedb.Config{}) 18 19 var fakeTrie, _ = trie.NewStateTrie(&trie.ID{}, triedb.NewDatabase(rawdb.NewMemoryDatabase(), nil)) 20 21 // db implements the [state.Reader] and [state.Database] interface. 22 type db struct { 23 fetcher Fetcher 24 } 25 26 func newDB(fetcher Fetcher) *db { 27 return &db{ 28 fetcher: fetcher, 29 } 30 } 31 32 //////////////////////////////////////////////////////////////////////////////////////////////////// 33 // state.Reader methods //////////////////////////////////////////////////////////////////////////// 34 //////////////////////////////////////////////////////////////////////////////////////////////////// 35 36 func (db *db) Account(addr common.Address) (*types.StateAccount, error) { 37 if db.fetcher == nil { 38 return &types.StateAccount{ 39 Balance: new(uint256.Int), 40 CodeHash: types.EmptyCodeHash[:], 41 }, nil 42 } 43 44 return db.fetcher.Account(addr) 45 } 46 47 func (db *db) Storage(addr common.Address, slot common.Hash) (common.Hash, error) { 48 if db.fetcher == nil { 49 return common.Hash{}, nil 50 } 51 52 val, err := db.fetcher.StorageAt(addr, slot) 53 if err != nil { 54 return common.Hash{}, err 55 } 56 return val, nil 57 } 58 59 func (db *db) Code(addr common.Address, codeHash common.Hash) ([]byte, error) { 60 if db.fetcher == nil { 61 return []byte{}, nil 62 } 63 64 code, err := db.fetcher.Code(codeHash) 65 if err != nil { 66 return nil, errors.New("not found") 67 } 68 return code, nil 69 } 70 71 func (db *db) CodeSize(addr common.Address, codeHash common.Hash) (int, error) { 72 code, err := db.Code(addr, codeHash) 73 if err != nil { 74 return 0, err 75 } 76 return len(code), nil 77 } 78 79 func (db *db) Copy() state.Reader { return db } 80 81 //////////////////////////////////////////////////////////////////////////////////////////////////// 82 // state.Database methods ////////////////////////////////////////////////////////////////////////// 83 //////////////////////////////////////////////////////////////////////////////////////////////////// 84 85 func (db *db) Reader(common.Hash) (state.Reader, error) { return db, nil } 86 87 func (db *db) OpenTrie(common.Hash) (state.Trie, error) { return fakeTrie, nil } 88 89 func (db *db) OpenStorageTrie(common.Hash, common.Address, common.Hash, state.Trie) (state.Trie, error) { 90 panic("not implemented") 91 } 92 93 func (db *db) ContractCode(addr common.Address, codeHash common.Hash) ([]byte, error) { 94 return db.Code(addr, codeHash) 95 } 96 97 func (db *db) ContractCodeSize(addr common.Address, codeHash common.Hash) (int, error) { 98 return db.CodeSize(addr, codeHash) 99 } 100 101 func (*db) PointCache() *utils.PointCache { panic("not implemented") } 102 103 func (*db) TrieDB() *triedb.Database { return fakeTrieDB } 104 105 func (*db) Snapshot() *snapshot.Tree { panic("not implemented") }