github.com/vay007/go-ethereum@v1.9.7/trie/sync.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 trie 18 19 import ( 20 "errors" 21 "fmt" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/common/prque" 25 "github.com/ethereum/go-ethereum/ethdb" 26 ) 27 28 // ErrNotRequested is returned by the trie sync when it's requested to process a 29 // node it did not request. 30 var ErrNotRequested = errors.New("not requested") 31 32 // ErrAlreadyProcessed is returned by the trie sync when it's requested to process a 33 // node it already processed previously. 34 var ErrAlreadyProcessed = errors.New("already processed") 35 36 // request represents a scheduled or already in-flight state retrieval request. 37 type request struct { 38 hash common.Hash // Hash of the node data content to retrieve 39 data []byte // Data content of the node, cached until all subtrees complete 40 raw bool // Whether this is a raw entry (code) or a trie node 41 42 parents []*request // Parent state nodes referencing this entry (notify all upon completion) 43 depth int // Depth level within the trie the node is located to prioritise DFS 44 deps int // Number of dependencies before allowed to commit this node 45 46 callback LeafCallback // Callback to invoke if a leaf node it reached on this branch 47 } 48 49 // SyncResult is a simple list to return missing nodes along with their request 50 // hashes. 51 type SyncResult struct { 52 Hash common.Hash // Hash of the originally unknown trie node 53 Data []byte // Data content of the retrieved node 54 } 55 56 // syncMemBatch is an in-memory buffer of successfully downloaded but not yet 57 // persisted data items. 58 type syncMemBatch struct { 59 batch map[common.Hash][]byte // In-memory membatch of recently completed items 60 } 61 62 // newSyncMemBatch allocates a new memory-buffer for not-yet persisted trie nodes. 63 func newSyncMemBatch() *syncMemBatch { 64 return &syncMemBatch{ 65 batch: make(map[common.Hash][]byte), 66 } 67 } 68 69 // Sync is the main state trie synchronisation scheduler, which provides yet 70 // unknown trie hashes to retrieve, accepts node data associated with said hashes 71 // and reconstructs the trie step by step until all is done. 72 type Sync struct { 73 database ethdb.KeyValueReader // Persistent database to check for existing entries 74 membatch *syncMemBatch // Memory buffer to avoid frequent database writes 75 requests map[common.Hash]*request // Pending requests pertaining to a key hash 76 queue *prque.Prque // Priority queue with the pending requests 77 bloom *SyncBloom // Bloom filter for fast node existence checks 78 } 79 80 // NewSync creates a new trie data download scheduler. 81 func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallback, bloom *SyncBloom) *Sync { 82 ts := &Sync{ 83 database: database, 84 membatch: newSyncMemBatch(), 85 requests: make(map[common.Hash]*request), 86 queue: prque.New(nil), 87 bloom: bloom, 88 } 89 ts.AddSubTrie(root, 0, common.Hash{}, callback) 90 return ts 91 } 92 93 // AddSubTrie registers a new trie to the sync code, rooted at the designated parent. 94 func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callback LeafCallback) { 95 // Short circuit if the trie is empty or already known 96 if root == emptyRoot { 97 return 98 } 99 if _, ok := s.membatch.batch[root]; ok { 100 return 101 } 102 if s.bloom.Contains(root[:]) { 103 // Bloom filter says this might be a duplicate, double check 104 blob, _ := s.database.Get(root[:]) 105 if local, err := decodeNode(root[:], blob); local != nil && err == nil { 106 return 107 } 108 // False positive, bump fault meter 109 bloomFaultMeter.Mark(1) 110 } 111 // Assemble the new sub-trie sync request 112 req := &request{ 113 hash: root, 114 depth: depth, 115 callback: callback, 116 } 117 // If this sub-trie has a designated parent, link them together 118 if parent != (common.Hash{}) { 119 ancestor := s.requests[parent] 120 if ancestor == nil { 121 panic(fmt.Sprintf("sub-trie ancestor not found: %x", parent)) 122 } 123 ancestor.deps++ 124 req.parents = append(req.parents, ancestor) 125 } 126 s.schedule(req) 127 } 128 129 // AddRawEntry schedules the direct retrieval of a state entry that should not be 130 // interpreted as a trie node, but rather accepted and stored into the database 131 // as is. This method's goal is to support misc state metadata retrievals (e.g. 132 // contract code). 133 func (s *Sync) AddRawEntry(hash common.Hash, depth int, parent common.Hash) { 134 // Short circuit if the entry is empty or already known 135 if hash == emptyState { 136 return 137 } 138 if _, ok := s.membatch.batch[hash]; ok { 139 return 140 } 141 if s.bloom.Contains(hash[:]) { 142 // Bloom filter says this might be a duplicate, double check 143 if ok, _ := s.database.Has(hash[:]); ok { 144 return 145 } 146 // False positive, bump fault meter 147 bloomFaultMeter.Mark(1) 148 } 149 // Assemble the new sub-trie sync request 150 req := &request{ 151 hash: hash, 152 raw: true, 153 depth: depth, 154 } 155 // If this sub-trie has a designated parent, link them together 156 if parent != (common.Hash{}) { 157 ancestor := s.requests[parent] 158 if ancestor == nil { 159 panic(fmt.Sprintf("raw-entry ancestor not found: %x", parent)) 160 } 161 ancestor.deps++ 162 req.parents = append(req.parents, ancestor) 163 } 164 s.schedule(req) 165 } 166 167 // Missing retrieves the known missing nodes from the trie for retrieval. 168 func (s *Sync) Missing(max int) []common.Hash { 169 var requests []common.Hash 170 for !s.queue.Empty() && (max == 0 || len(requests) < max) { 171 requests = append(requests, s.queue.PopItem().(common.Hash)) 172 } 173 return requests 174 } 175 176 // Process injects a batch of retrieved trie nodes data, returning if something 177 // was committed to the database and also the index of an entry if processing of 178 // it failed. 179 func (s *Sync) Process(results []SyncResult) (bool, int, error) { 180 committed := false 181 182 for i, item := range results { 183 // If the item was not requested, bail out 184 request := s.requests[item.Hash] 185 if request == nil { 186 return committed, i, ErrNotRequested 187 } 188 if request.data != nil { 189 return committed, i, ErrAlreadyProcessed 190 } 191 // If the item is a raw entry request, commit directly 192 if request.raw { 193 request.data = item.Data 194 s.commit(request) 195 committed = true 196 continue 197 } 198 // Decode the node data content and update the request 199 node, err := decodeNode(item.Hash[:], item.Data) 200 if err != nil { 201 return committed, i, err 202 } 203 request.data = item.Data 204 205 // Create and schedule a request for all the children nodes 206 requests, err := s.children(request, node) 207 if err != nil { 208 return committed, i, err 209 } 210 if len(requests) == 0 && request.deps == 0 { 211 s.commit(request) 212 committed = true 213 continue 214 } 215 request.deps += len(requests) 216 for _, child := range requests { 217 s.schedule(child) 218 } 219 } 220 return committed, 0, nil 221 } 222 223 // Commit flushes the data stored in the internal membatch out to persistent 224 // storage, returning any occurred error. 225 func (s *Sync) Commit(dbw ethdb.Batch) error { 226 // Dump the membatch into a database dbw 227 for key, value := range s.membatch.batch { 228 if err := dbw.Put(key[:], value); err != nil { 229 return err 230 } 231 s.bloom.Add(key[:]) 232 } 233 // Drop the membatch data and return 234 s.membatch = newSyncMemBatch() 235 return nil 236 } 237 238 // Pending returns the number of state entries currently pending for download. 239 func (s *Sync) Pending() int { 240 return len(s.requests) 241 } 242 243 // schedule inserts a new state retrieval request into the fetch queue. If there 244 // is already a pending request for this node, the new request will be discarded 245 // and only a parent reference added to the old one. 246 func (s *Sync) schedule(req *request) { 247 // If we're already requesting this node, add a new reference and stop 248 if old, ok := s.requests[req.hash]; ok { 249 old.parents = append(old.parents, req.parents...) 250 return 251 } 252 // Schedule the request for future retrieval 253 s.queue.Push(req.hash, int64(req.depth)) 254 s.requests[req.hash] = req 255 } 256 257 // children retrieves all the missing children of a state trie entry for future 258 // retrieval scheduling. 259 func (s *Sync) children(req *request, object node) ([]*request, error) { 260 // Gather all the children of the node, irrelevant whether known or not 261 type child struct { 262 node node 263 depth int 264 } 265 var children []child 266 267 switch node := (object).(type) { 268 case *shortNode: 269 children = []child{{ 270 node: node.Val, 271 depth: req.depth + len(node.Key), 272 }} 273 case *fullNode: 274 for i := 0; i < 17; i++ { 275 if node.Children[i] != nil { 276 children = append(children, child{ 277 node: node.Children[i], 278 depth: req.depth + 1, 279 }) 280 } 281 } 282 default: 283 panic(fmt.Sprintf("unknown node: %+v", node)) 284 } 285 // Iterate over the children, and request all unknown ones 286 requests := make([]*request, 0, len(children)) 287 for _, child := range children { 288 // Notify any external watcher of a new key/value node 289 if req.callback != nil { 290 if node, ok := (child.node).(valueNode); ok { 291 if err := req.callback(node, req.hash); err != nil { 292 return nil, err 293 } 294 } 295 } 296 // If the child references another node, resolve or schedule 297 if node, ok := (child.node).(hashNode); ok { 298 // Try to resolve the node from the local database 299 hash := common.BytesToHash(node) 300 if _, ok := s.membatch.batch[hash]; ok { 301 continue 302 } 303 if s.bloom.Contains(node) { 304 // Bloom filter says this might be a duplicate, double check 305 if ok, _ := s.database.Has(node); ok { 306 continue 307 } 308 // False positive, bump fault meter 309 bloomFaultMeter.Mark(1) 310 } 311 // Locally unknown node, schedule for retrieval 312 requests = append(requests, &request{ 313 hash: hash, 314 parents: []*request{req}, 315 depth: child.depth, 316 callback: req.callback, 317 }) 318 } 319 } 320 return requests, nil 321 } 322 323 // commit finalizes a retrieval request and stores it into the membatch. If any 324 // of the referencing parent requests complete due to this commit, they are also 325 // committed themselves. 326 func (s *Sync) commit(req *request) (err error) { 327 // Write the node content to the membatch 328 s.membatch.batch[req.hash] = req.data 329 330 delete(s.requests, req.hash) 331 332 // Check all parents for completion 333 for _, parent := range req.parents { 334 parent.deps-- 335 if parent.deps == 0 { 336 if err := s.commit(parent); err != nil { 337 return err 338 } 339 } 340 } 341 return nil 342 }