github.com/daefrom/go-dae@v1.0.1/light/postprocess.go (about) 1 // Copyright 2017 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 "bytes" 21 "context" 22 "encoding/binary" 23 "errors" 24 "fmt" 25 "math/big" 26 "time" 27 28 "github.com/daefrom/go-dae/common" 29 "github.com/daefrom/go-dae/common/bitutil" 30 "github.com/daefrom/go-dae/core" 31 "github.com/daefrom/go-dae/core/rawdb" 32 "github.com/daefrom/go-dae/core/types" 33 "github.com/daefrom/go-dae/ethdb" 34 "github.com/daefrom/go-dae/log" 35 "github.com/daefrom/go-dae/params" 36 "github.com/daefrom/go-dae/rlp" 37 "github.com/daefrom/go-dae/trie" 38 mapset "github.com/deckarep/golang-set" 39 ) 40 41 // IndexerConfig includes a set of configs for chain indexers. 42 type IndexerConfig struct { 43 // The block frequency for creating CHTs. 44 ChtSize uint64 45 46 // The number of confirmations needed to generate/accept a canonical hash help trie. 47 ChtConfirms uint64 48 49 // The block frequency for creating new bloom bits. 50 BloomSize uint64 51 52 // The number of confirmation needed before a bloom section is considered probably final and its rotated bits 53 // are calculated. 54 BloomConfirms uint64 55 56 // The block frequency for creating BloomTrie. 57 BloomTrieSize uint64 58 59 // The number of confirmations needed to generate/accept a bloom trie. 60 BloomTrieConfirms uint64 61 } 62 63 var ( 64 // DefaultServerIndexerConfig wraps a set of configs as a default indexer config for server side. 65 DefaultServerIndexerConfig = &IndexerConfig{ 66 ChtSize: params.CHTFrequency, 67 ChtConfirms: params.HelperTrieProcessConfirmations, 68 BloomSize: params.BloomBitsBlocks, 69 BloomConfirms: params.BloomConfirms, 70 BloomTrieSize: params.BloomTrieFrequency, 71 BloomTrieConfirms: params.HelperTrieProcessConfirmations, 72 } 73 // DefaultClientIndexerConfig wraps a set of configs as a default indexer config for client side. 74 DefaultClientIndexerConfig = &IndexerConfig{ 75 ChtSize: params.CHTFrequency, 76 ChtConfirms: params.HelperTrieConfirmations, 77 BloomSize: params.BloomBitsBlocksClient, 78 BloomConfirms: params.HelperTrieConfirmations, 79 BloomTrieSize: params.BloomTrieFrequency, 80 BloomTrieConfirms: params.HelperTrieConfirmations, 81 } 82 // TestServerIndexerConfig wraps a set of configs as a test indexer config for server side. 83 TestServerIndexerConfig = &IndexerConfig{ 84 ChtSize: 128, 85 ChtConfirms: 1, 86 BloomSize: 16, 87 BloomConfirms: 1, 88 BloomTrieSize: 128, 89 BloomTrieConfirms: 1, 90 } 91 // TestClientIndexerConfig wraps a set of configs as a test indexer config for client side. 92 TestClientIndexerConfig = &IndexerConfig{ 93 ChtSize: 128, 94 ChtConfirms: 8, 95 BloomSize: 128, 96 BloomConfirms: 8, 97 BloomTrieSize: 128, 98 BloomTrieConfirms: 8, 99 } 100 ) 101 102 var ( 103 errNoTrustedCht = errors.New("no trusted canonical hash trie") 104 errNoTrustedBloomTrie = errors.New("no trusted bloom trie") 105 errNoHeader = errors.New("header not found") 106 chtPrefix = []byte("chtRootV2-") // chtPrefix + chtNum (uint64 big endian) -> trie root hash 107 ChtTablePrefix = "cht-" 108 ) 109 110 // ChtNode structures are stored in the Canonical Hash Trie in an RLP encoded format 111 type ChtNode struct { 112 Hash common.Hash 113 Td *big.Int 114 } 115 116 // GetChtRoot reads the CHT root associated to the given section from the database 117 func GetChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash { 118 var encNumber [8]byte 119 binary.BigEndian.PutUint64(encNumber[:], sectionIdx) 120 data, _ := db.Get(append(append(chtPrefix, encNumber[:]...), sectionHead.Bytes()...)) 121 return common.BytesToHash(data) 122 } 123 124 // StoreChtRoot writes the CHT root associated to the given section into the database 125 func StoreChtRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) { 126 var encNumber [8]byte 127 binary.BigEndian.PutUint64(encNumber[:], sectionIdx) 128 db.Put(append(append(chtPrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes()) 129 } 130 131 // ChtIndexerBackend implements core.ChainIndexerBackend. 132 type ChtIndexerBackend struct { 133 disablePruning bool 134 diskdb, trieTable ethdb.Database 135 odr OdrBackend 136 triedb *trie.Database 137 trieset mapset.Set 138 section, sectionSize uint64 139 lastHash common.Hash 140 trie *trie.Trie 141 } 142 143 // NewChtIndexer creates a Cht chain indexer 144 func NewChtIndexer(db ethdb.Database, odr OdrBackend, size, confirms uint64, disablePruning bool) *core.ChainIndexer { 145 trieTable := rawdb.NewTable(db, ChtTablePrefix) 146 backend := &ChtIndexerBackend{ 147 diskdb: db, 148 odr: odr, 149 trieTable: trieTable, 150 triedb: trie.NewDatabaseWithConfig(trieTable, &trie.Config{Cache: 1}), // Use a tiny cache only to keep memory down 151 trieset: mapset.NewSet(), 152 sectionSize: size, 153 disablePruning: disablePruning, 154 } 155 return core.NewChainIndexer(db, rawdb.NewTable(db, "chtIndexV2-"), backend, size, confirms, time.Millisecond*100, "cht") 156 } 157 158 // fetchMissingNodes tries to retrieve the last entry of the latest trusted CHT from the 159 // ODR backend in order to be able to add new entries and calculate subsequent root hashes 160 func (c *ChtIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error { 161 batch := c.trieTable.NewBatch() 162 r := &ChtRequest{ChtRoot: root, ChtNum: section - 1, BlockNum: section*c.sectionSize - 1, Config: c.odr.IndexerConfig()} 163 for { 164 err := c.odr.Retrieve(ctx, r) 165 switch err { 166 case nil: 167 r.Proof.Store(batch) 168 return batch.Write() 169 case ErrNoPeers: 170 // if there are no peers to serve, retry later 171 select { 172 case <-ctx.Done(): 173 return ctx.Err() 174 case <-time.After(time.Second * 10): 175 // stay in the loop and try again 176 } 177 default: 178 return err 179 } 180 } 181 } 182 183 // Reset implements core.ChainIndexerBackend 184 func (c *ChtIndexerBackend) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error { 185 var root common.Hash 186 if section > 0 { 187 root = GetChtRoot(c.diskdb, section-1, lastSectionHead) 188 } 189 var err error 190 c.trie, err = trie.New(common.Hash{}, root, c.triedb) 191 192 if err != nil && c.odr != nil { 193 err = c.fetchMissingNodes(ctx, section, root) 194 if err == nil { 195 c.trie, err = trie.New(common.Hash{}, root, c.triedb) 196 } 197 } 198 c.section = section 199 return err 200 } 201 202 // Process implements core.ChainIndexerBackend 203 func (c *ChtIndexerBackend) Process(ctx context.Context, header *types.Header) error { 204 hash, num := header.Hash(), header.Number.Uint64() 205 c.lastHash = hash 206 207 td := rawdb.ReadTd(c.diskdb, hash, num) 208 if td == nil { 209 panic(nil) 210 } 211 var encNumber [8]byte 212 binary.BigEndian.PutUint64(encNumber[:], num) 213 data, _ := rlp.EncodeToBytes(ChtNode{hash, td}) 214 c.trie.Update(encNumber[:], data) 215 return nil 216 } 217 218 // Commit implements core.ChainIndexerBackend 219 func (c *ChtIndexerBackend) Commit() error { 220 root, _, err := c.trie.Commit(nil) 221 if err != nil { 222 return err 223 } 224 // Pruning historical trie nodes if necessary. 225 if !c.disablePruning { 226 // Flush the triedb and track the latest trie nodes. 227 c.trieset.Clear() 228 c.triedb.Commit(root, false, func(hash common.Hash) { c.trieset.Add(hash) }) 229 230 it := c.trieTable.NewIterator(nil, nil) 231 defer it.Release() 232 233 var ( 234 deleted int 235 remaining int 236 t = time.Now() 237 ) 238 for it.Next() { 239 trimmed := bytes.TrimPrefix(it.Key(), []byte(ChtTablePrefix)) 240 if !c.trieset.Contains(common.BytesToHash(trimmed)) { 241 c.trieTable.Delete(trimmed) 242 deleted += 1 243 } else { 244 remaining += 1 245 } 246 } 247 log.Debug("Prune historical CHT trie nodes", "deleted", deleted, "remaining", remaining, "elapsed", common.PrettyDuration(time.Since(t))) 248 } else { 249 c.triedb.Commit(root, false, nil) 250 } 251 log.Info("Storing CHT", "section", c.section, "head", fmt.Sprintf("%064x", c.lastHash), "root", fmt.Sprintf("%064x", root)) 252 StoreChtRoot(c.diskdb, c.section, c.lastHash, root) 253 return nil 254 } 255 256 // Prune implements core.ChainIndexerBackend which deletes all chain data 257 // (except hash<->number mappings) older than the specified threshold. 258 func (c *ChtIndexerBackend) Prune(threshold uint64) error { 259 // Short circuit if the light pruning is disabled. 260 if c.disablePruning { 261 return nil 262 } 263 t := time.Now() 264 // Always keep genesis header in database. 265 start, end := uint64(1), (threshold+1)*c.sectionSize 266 267 var batch = c.diskdb.NewBatch() 268 for { 269 numbers, hashes := rawdb.ReadAllCanonicalHashes(c.diskdb, start, end, 10240) 270 if len(numbers) == 0 { 271 break 272 } 273 for i := 0; i < len(numbers); i++ { 274 // Keep hash<->number mapping in database otherwise the hash based 275 // API(e.g. GetReceipt, GetLogs) will be broken. 276 // 277 // Storage size wise, the size of a mapping is ~41bytes. For one 278 // section is about 1.3MB which is acceptable. 279 // 280 // In order to totally get rid of this index, we need an additional 281 // flag to specify how many historical data light client can serve. 282 rawdb.DeleteCanonicalHash(batch, numbers[i]) 283 rawdb.DeleteBlockWithoutNumber(batch, hashes[i], numbers[i]) 284 } 285 if batch.ValueSize() > ethdb.IdealBatchSize { 286 if err := batch.Write(); err != nil { 287 return err 288 } 289 batch.Reset() 290 } 291 start = numbers[len(numbers)-1] + 1 292 } 293 if err := batch.Write(); err != nil { 294 return err 295 } 296 log.Debug("Prune history headers", "threshold", threshold, "elapsed", common.PrettyDuration(time.Since(t))) 297 return nil 298 } 299 300 var ( 301 bloomTriePrefix = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash 302 BloomTrieTablePrefix = "blt-" 303 ) 304 305 // GetBloomTrieRoot reads the BloomTrie root assoctiated to the given section from the database 306 func GetBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash { 307 var encNumber [8]byte 308 binary.BigEndian.PutUint64(encNumber[:], sectionIdx) 309 data, _ := db.Get(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...)) 310 return common.BytesToHash(data) 311 } 312 313 // StoreBloomTrieRoot writes the BloomTrie root assoctiated to the given section into the database 314 func StoreBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) { 315 var encNumber [8]byte 316 binary.BigEndian.PutUint64(encNumber[:], sectionIdx) 317 db.Put(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes()) 318 } 319 320 // BloomTrieIndexerBackend implements core.ChainIndexerBackend 321 type BloomTrieIndexerBackend struct { 322 disablePruning bool 323 diskdb, trieTable ethdb.Database 324 triedb *trie.Database 325 trieset mapset.Set 326 odr OdrBackend 327 section uint64 328 parentSize uint64 329 size uint64 330 bloomTrieRatio uint64 331 trie *trie.Trie 332 sectionHeads []common.Hash 333 } 334 335 // NewBloomTrieIndexer creates a BloomTrie chain indexer 336 func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uint64, disablePruning bool) *core.ChainIndexer { 337 trieTable := rawdb.NewTable(db, BloomTrieTablePrefix) 338 backend := &BloomTrieIndexerBackend{ 339 diskdb: db, 340 odr: odr, 341 trieTable: trieTable, 342 triedb: trie.NewDatabaseWithConfig(trieTable, &trie.Config{Cache: 1}), // Use a tiny cache only to keep memory down 343 trieset: mapset.NewSet(), 344 parentSize: parentSize, 345 size: size, 346 disablePruning: disablePruning, 347 } 348 backend.bloomTrieRatio = size / parentSize 349 backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio) 350 return core.NewChainIndexer(db, rawdb.NewTable(db, "bltIndex-"), backend, size, 0, time.Millisecond*100, "bloomtrie") 351 } 352 353 // fetchMissingNodes tries to retrieve the last entries of the latest trusted bloom trie from the 354 // ODR backend in order to be able to add new entries and calculate subsequent root hashes 355 func (b *BloomTrieIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error { 356 indexCh := make(chan uint, types.BloomBitLength) 357 type res struct { 358 nodes *NodeSet 359 err error 360 } 361 resCh := make(chan res, types.BloomBitLength) 362 for i := 0; i < 20; i++ { 363 go func() { 364 for bitIndex := range indexCh { 365 r := &BloomRequest{BloomTrieRoot: root, BloomTrieNum: section - 1, BitIdx: bitIndex, SectionIndexList: []uint64{section - 1}, Config: b.odr.IndexerConfig()} 366 for { 367 if err := b.odr.Retrieve(ctx, r); err == ErrNoPeers { 368 // if there are no peers to serve, retry later 369 select { 370 case <-ctx.Done(): 371 resCh <- res{nil, ctx.Err()} 372 return 373 case <-time.After(time.Second * 10): 374 // stay in the loop and try again 375 } 376 } else { 377 resCh <- res{r.Proofs, err} 378 break 379 } 380 } 381 } 382 }() 383 } 384 for i := uint(0); i < types.BloomBitLength; i++ { 385 indexCh <- i 386 } 387 close(indexCh) 388 batch := b.trieTable.NewBatch() 389 for i := uint(0); i < types.BloomBitLength; i++ { 390 res := <-resCh 391 if res.err != nil { 392 return res.err 393 } 394 res.nodes.Store(batch) 395 } 396 return batch.Write() 397 } 398 399 // Reset implements core.ChainIndexerBackend 400 func (b *BloomTrieIndexerBackend) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error { 401 var root common.Hash 402 if section > 0 { 403 root = GetBloomTrieRoot(b.diskdb, section-1, lastSectionHead) 404 } 405 var err error 406 b.trie, err = trie.New(common.Hash{}, root, b.triedb) 407 if err != nil && b.odr != nil { 408 err = b.fetchMissingNodes(ctx, section, root) 409 if err == nil { 410 b.trie, err = trie.New(common.Hash{}, root, b.triedb) 411 } 412 } 413 b.section = section 414 return err 415 } 416 417 // Process implements core.ChainIndexerBackend 418 func (b *BloomTrieIndexerBackend) Process(ctx context.Context, header *types.Header) error { 419 num := header.Number.Uint64() - b.section*b.size 420 if (num+1)%b.parentSize == 0 { 421 b.sectionHeads[num/b.parentSize] = header.Hash() 422 } 423 return nil 424 } 425 426 // Commit implements core.ChainIndexerBackend 427 func (b *BloomTrieIndexerBackend) Commit() error { 428 var compSize, decompSize uint64 429 430 for i := uint(0); i < types.BloomBitLength; i++ { 431 var encKey [10]byte 432 binary.BigEndian.PutUint16(encKey[0:2], uint16(i)) 433 binary.BigEndian.PutUint64(encKey[2:10], b.section) 434 var decomp []byte 435 for j := uint64(0); j < b.bloomTrieRatio; j++ { 436 data, err := rawdb.ReadBloomBits(b.diskdb, i, b.section*b.bloomTrieRatio+j, b.sectionHeads[j]) 437 if err != nil { 438 return err 439 } 440 decompData, err2 := bitutil.DecompressBytes(data, int(b.parentSize/8)) 441 if err2 != nil { 442 return err2 443 } 444 decomp = append(decomp, decompData...) 445 } 446 comp := bitutil.CompressBytes(decomp) 447 448 decompSize += uint64(len(decomp)) 449 compSize += uint64(len(comp)) 450 if len(comp) > 0 { 451 b.trie.Update(encKey[:], comp) 452 } else { 453 b.trie.Delete(encKey[:]) 454 } 455 } 456 root, _, err := b.trie.Commit(nil) 457 if err != nil { 458 return err 459 } 460 // Pruning historical trie nodes if necessary. 461 if !b.disablePruning { 462 // Flush the triedb and track the latest trie nodes. 463 b.trieset.Clear() 464 b.triedb.Commit(root, false, func(hash common.Hash) { b.trieset.Add(hash) }) 465 466 it := b.trieTable.NewIterator(nil, nil) 467 defer it.Release() 468 469 var ( 470 deleted int 471 remaining int 472 t = time.Now() 473 ) 474 for it.Next() { 475 trimmed := bytes.TrimPrefix(it.Key(), []byte(BloomTrieTablePrefix)) 476 if !b.trieset.Contains(common.BytesToHash(trimmed)) { 477 b.trieTable.Delete(trimmed) 478 deleted += 1 479 } else { 480 remaining += 1 481 } 482 } 483 log.Debug("Prune historical bloom trie nodes", "deleted", deleted, "remaining", remaining, "elapsed", common.PrettyDuration(time.Since(t))) 484 } else { 485 b.triedb.Commit(root, false, nil) 486 } 487 sectionHead := b.sectionHeads[b.bloomTrieRatio-1] 488 StoreBloomTrieRoot(b.diskdb, b.section, sectionHead, root) 489 log.Info("Storing bloom trie", "section", b.section, "head", fmt.Sprintf("%064x", sectionHead), "root", fmt.Sprintf("%064x", root), "compression", float64(compSize)/float64(decompSize)) 490 491 return nil 492 } 493 494 // Prune implements core.ChainIndexerBackend which deletes all 495 // bloombits which older than the specified threshold. 496 func (b *BloomTrieIndexerBackend) Prune(threshold uint64) error { 497 // Short circuit if the light pruning is disabled. 498 if b.disablePruning { 499 return nil 500 } 501 start := time.Now() 502 for i := uint(0); i < types.BloomBitLength; i++ { 503 rawdb.DeleteBloombits(b.diskdb, i, 0, threshold*b.bloomTrieRatio+b.bloomTrieRatio) 504 } 505 log.Debug("Prune history bloombits", "threshold", threshold, "elapsed", common.PrettyDuration(time.Since(start))) 506 return nil 507 }