github.com/coltonfike/e2c@v21.1.0+incompatible/light/trie.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package light 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 24 "github.com/ethereum/go-ethereum/core/rawdb" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/core/state" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/ethdb" 31 "github.com/ethereum/go-ethereum/trie" 32 ) 33 34 func NewState(ctx context.Context, head *types.Header, odr OdrBackend) *state.StateDB { 35 state, _ := state.New(head.Root, NewStateDatabase(ctx, head, odr)) 36 return state 37 } 38 39 func NewStateDatabase(ctx context.Context, head *types.Header, odr OdrBackend) state.Database { 40 return &odrDatabase{ctx, StateTrieID(head), odr} 41 } 42 43 type odrDatabase struct { 44 ctx context.Context 45 id *TrieID 46 backend OdrBackend 47 } 48 49 func (db *odrDatabase) OpenTrie(root common.Hash) (state.Trie, error) { 50 return &odrTrie{db: db, id: db.id}, nil 51 } 52 53 func (db *odrDatabase) OpenStorageTrie(addrHash, root common.Hash) (state.Trie, error) { 54 return &odrTrie{db: db, id: StorageTrieID(db.id, addrHash, root)}, nil 55 } 56 57 func (db *odrDatabase) CopyTrie(t state.Trie) state.Trie { 58 switch t := t.(type) { 59 case *odrTrie: 60 cpy := &odrTrie{db: t.db, id: t.id} 61 if t.trie != nil { 62 cpytrie := *t.trie 63 cpy.trie = &cpytrie 64 } 65 return cpy 66 default: 67 panic(fmt.Errorf("unknown trie type %T", t)) 68 } 69 } 70 71 func (db *odrDatabase) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) { 72 if codeHash == sha3Nil { 73 return nil, nil 74 } 75 if code, err := db.backend.Database().Get(codeHash[:]); err == nil { 76 return code, nil 77 } 78 id := *db.id 79 id.AccKey = addrHash[:] 80 req := &CodeRequest{Id: &id, Hash: codeHash} 81 err := db.backend.Retrieve(db.ctx, req) 82 return req.Data, err 83 } 84 85 func (db *odrDatabase) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) { 86 code, err := db.ContractCode(addrHash, codeHash) 87 return len(code), err 88 } 89 90 func (db *odrDatabase) TrieDB() *trie.Database { 91 return nil 92 } 93 94 type stubAccountExtraDataLinker struct { 95 } 96 97 func newAccountExtraDataLinkerStub() rawdb.AccountExtraDataLinker { 98 return &stubAccountExtraDataLinker{} 99 } 100 101 func (pml *stubAccountExtraDataLinker) GetAccountExtraDataRoot(_ common.Hash) common.Hash { 102 return common.Hash{} 103 } 104 105 func (pml *stubAccountExtraDataLinker) Link(_, _ common.Hash) error { 106 return nil 107 } 108 109 func (db *odrDatabase) AccountExtraDataLinker() rawdb.AccountExtraDataLinker { 110 return newAccountExtraDataLinkerStub() 111 } 112 113 type odrTrie struct { 114 db *odrDatabase 115 id *TrieID 116 trie *trie.Trie 117 } 118 119 func (t *odrTrie) TryGet(key []byte) ([]byte, error) { 120 key = crypto.Keccak256(key) 121 var res []byte 122 err := t.do(key, func() (err error) { 123 res, err = t.trie.TryGet(key) 124 return err 125 }) 126 return res, err 127 } 128 129 func (t *odrTrie) TryUpdate(key, value []byte) error { 130 key = crypto.Keccak256(key) 131 return t.do(key, func() error { 132 return t.trie.TryUpdate(key, value) 133 }) 134 } 135 136 func (t *odrTrie) TryDelete(key []byte) error { 137 key = crypto.Keccak256(key) 138 return t.do(key, func() error { 139 return t.trie.TryDelete(key) 140 }) 141 } 142 143 func (t *odrTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) { 144 if t.trie == nil { 145 return t.id.Root, nil 146 } 147 return t.trie.Commit(onleaf) 148 } 149 150 func (t *odrTrie) Hash() common.Hash { 151 if t.trie == nil { 152 return t.id.Root 153 } 154 return t.trie.Hash() 155 } 156 157 func (t *odrTrie) NodeIterator(startkey []byte) trie.NodeIterator { 158 return newNodeIterator(t, startkey) 159 } 160 161 func (t *odrTrie) GetKey(sha []byte) []byte { 162 return nil 163 } 164 165 func (t *odrTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error { 166 return errors.New("not implemented, needs client/server interface split") 167 } 168 169 // do tries and retries to execute a function until it returns with no error or 170 // an error type other than MissingNodeError 171 func (t *odrTrie) do(key []byte, fn func() error) error { 172 for { 173 var err error 174 if t.trie == nil { 175 t.trie, err = trie.New(t.id.Root, trie.NewDatabase(t.db.backend.Database())) 176 } 177 if err == nil { 178 err = fn() 179 } 180 if _, ok := err.(*trie.MissingNodeError); !ok { 181 return err 182 } 183 r := &TrieRequest{Id: t.id, Key: key} 184 if err := t.db.backend.Retrieve(t.db.ctx, r); err != nil { 185 return err 186 } 187 } 188 } 189 190 type nodeIterator struct { 191 trie.NodeIterator 192 t *odrTrie 193 err error 194 } 195 196 func newNodeIterator(t *odrTrie, startkey []byte) trie.NodeIterator { 197 it := &nodeIterator{t: t} 198 // Open the actual non-ODR trie if that hasn't happened yet. 199 if t.trie == nil { 200 it.do(func() error { 201 t, err := trie.New(t.id.Root, trie.NewDatabase(t.db.backend.Database())) 202 if err == nil { 203 it.t.trie = t 204 } 205 return err 206 }) 207 } 208 it.do(func() error { 209 it.NodeIterator = it.t.trie.NodeIterator(startkey) 210 return it.NodeIterator.Error() 211 }) 212 return it 213 } 214 215 func (it *nodeIterator) Next(descend bool) bool { 216 var ok bool 217 it.do(func() error { 218 ok = it.NodeIterator.Next(descend) 219 return it.NodeIterator.Error() 220 }) 221 return ok 222 } 223 224 // do runs fn and attempts to fill in missing nodes by retrieving. 225 func (it *nodeIterator) do(fn func() error) { 226 var lasthash common.Hash 227 for { 228 it.err = fn() 229 missing, ok := it.err.(*trie.MissingNodeError) 230 if !ok { 231 return 232 } 233 if missing.NodeHash == lasthash { 234 it.err = fmt.Errorf("retrieve loop for trie node %x", missing.NodeHash) 235 return 236 } 237 lasthash = missing.NodeHash 238 r := &TrieRequest{Id: it.t.id, Key: nibblesToKey(missing.Path)} 239 if it.err = it.t.db.backend.Retrieve(it.t.db.ctx, r); it.err != nil { 240 return 241 } 242 } 243 } 244 245 func (it *nodeIterator) Error() error { 246 if it.err != nil { 247 return it.err 248 } 249 return it.NodeIterator.Error() 250 } 251 252 func nibblesToKey(nib []byte) []byte { 253 if len(nib) > 0 && nib[len(nib)-1] == 0x10 { 254 nib = nib[:len(nib)-1] // drop terminator 255 } 256 if len(nib)&1 == 1 { 257 nib = append(nib, 0) // make even 258 } 259 key := make([]byte, len(nib)/2) 260 for bi, ni := 0, 0; ni < len(nib); bi, ni = bi+1, ni+2 { 261 key[bi] = nib[ni]<<4 | nib[ni+1] 262 } 263 return key 264 }