github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/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  	mapset "github.com/deckarep/golang-set"
    29  	"github.com/ethereum/go-ethereum/common"
    30  	"github.com/ethereum/go-ethereum/common/bitutil"
    31  	"github.com/ethereum/go-ethereum/core"
    32  	"github.com/ethereum/go-ethereum/core/rawdb"
    33  	"github.com/ethereum/go-ethereum/core/types"
    34  	"github.com/ethereum/go-ethereum/ethdb"
    35  	"github.com/ethereum/go-ethereum/log"
    36  	"github.com/ethereum/go-ethereum/params"
    37  	"github.com/ethereum/go-ethereum/rlp"
    38  	"github.com/ethereum/go-ethereum/trie"
    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(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(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  // PruneSections implements core.ChainIndexerBackend which deletes all
   257  // chain data(except hash<->number mappings) older than the specified
   258  // threshold.
   259  func (c *ChtIndexerBackend) Prune(threshold uint64) error {
   260  	// Short circuit if the light pruning is disabled.
   261  	if c.disablePruning {
   262  		return nil
   263  	}
   264  	t := time.Now()
   265  	// Always keep genesis header in database.
   266  	start, end := uint64(1), (threshold+1)*c.sectionSize
   267  
   268  	var batch = c.diskdb.NewBatch()
   269  	for {
   270  		numbers, hashes := rawdb.ReadAllCanonicalHashes(c.diskdb, start, end, 10240)
   271  		if len(numbers) == 0 {
   272  			break
   273  		}
   274  		for i := 0; i < len(numbers); i++ {
   275  			// Keep hash<->number mapping in database otherwise the hash based
   276  			// API(e.g. GetReceipt, GetLogs) will be broken.
   277  			//
   278  			// Storage size wise, the size of a mapping is ~41bytes. For one
   279  			// section is about 1.3MB which is acceptable.
   280  			//
   281  			// In order to totally get rid of this index, we need an additional
   282  			// flag to specify how many historical data light client can serve.
   283  			rawdb.DeleteCanonicalHash(batch, numbers[i])
   284  			rawdb.DeleteBlockWithoutNumber(batch, hashes[i], numbers[i])
   285  		}
   286  		if batch.ValueSize() > ethdb.IdealBatchSize {
   287  			if err := batch.Write(); err != nil {
   288  				return err
   289  			}
   290  			batch.Reset()
   291  		}
   292  		start = numbers[len(numbers)-1] + 1
   293  	}
   294  	if err := batch.Write(); err != nil {
   295  		return err
   296  	}
   297  	log.Debug("Prune history headers", "threshold", threshold, "elapsed", common.PrettyDuration(time.Since(t)))
   298  	return nil
   299  }
   300  
   301  var (
   302  	bloomTriePrefix      = []byte("bltRoot-") // bloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
   303  	BloomTrieTablePrefix = "blt-"
   304  )
   305  
   306  // GetBloomTrieRoot reads the BloomTrie root assoctiated to the given section from the database
   307  func GetBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead common.Hash) common.Hash {
   308  	var encNumber [8]byte
   309  	binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
   310  	data, _ := db.Get(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...))
   311  	return common.BytesToHash(data)
   312  }
   313  
   314  // StoreBloomTrieRoot writes the BloomTrie root assoctiated to the given section into the database
   315  func StoreBloomTrieRoot(db ethdb.Database, sectionIdx uint64, sectionHead, root common.Hash) {
   316  	var encNumber [8]byte
   317  	binary.BigEndian.PutUint64(encNumber[:], sectionIdx)
   318  	db.Put(append(append(bloomTriePrefix, encNumber[:]...), sectionHead.Bytes()...), root.Bytes())
   319  }
   320  
   321  // BloomTrieIndexerBackend implements core.ChainIndexerBackend
   322  type BloomTrieIndexerBackend struct {
   323  	disablePruning    bool
   324  	diskdb, trieTable ethdb.Database
   325  	triedb            *trie.Database
   326  	trieset           mapset.Set
   327  	odr               OdrBackend
   328  	section           uint64
   329  	parentSize        uint64
   330  	size              uint64
   331  	bloomTrieRatio    uint64
   332  	trie              *trie.Trie
   333  	sectionHeads      []common.Hash
   334  }
   335  
   336  // NewBloomTrieIndexer creates a BloomTrie chain indexer
   337  func NewBloomTrieIndexer(db ethdb.Database, odr OdrBackend, parentSize, size uint64, disablePruning bool) *core.ChainIndexer {
   338  	trieTable := rawdb.NewTable(db, BloomTrieTablePrefix)
   339  	backend := &BloomTrieIndexerBackend{
   340  		diskdb:         db,
   341  		odr:            odr,
   342  		trieTable:      trieTable,
   343  		triedb:         trie.NewDatabaseWithConfig(trieTable, &trie.Config{Cache: 1}), // Use a tiny cache only to keep memory down
   344  		trieset:        mapset.NewSet(),
   345  		parentSize:     parentSize,
   346  		size:           size,
   347  		disablePruning: disablePruning,
   348  	}
   349  	backend.bloomTrieRatio = size / parentSize
   350  	backend.sectionHeads = make([]common.Hash, backend.bloomTrieRatio)
   351  	return core.NewChainIndexer(db, rawdb.NewTable(db, "bltIndex-"), backend, size, 0, time.Millisecond*100, "bloomtrie")
   352  }
   353  
   354  // fetchMissingNodes tries to retrieve the last entries of the latest trusted bloom trie from the
   355  // ODR backend in order to be able to add new entries and calculate subsequent root hashes
   356  func (b *BloomTrieIndexerBackend) fetchMissingNodes(ctx context.Context, section uint64, root common.Hash) error {
   357  	indexCh := make(chan uint, types.BloomBitLength)
   358  	type res struct {
   359  		nodes *NodeSet
   360  		err   error
   361  	}
   362  	resCh := make(chan res, types.BloomBitLength)
   363  	for i := 0; i < 20; i++ {
   364  		go func() {
   365  			for bitIndex := range indexCh {
   366  				r := &BloomRequest{BloomTrieRoot: root, BloomTrieNum: section - 1, BitIdx: bitIndex, SectionIndexList: []uint64{section - 1}, Config: b.odr.IndexerConfig()}
   367  				for {
   368  					if err := b.odr.Retrieve(ctx, r); err == ErrNoPeers {
   369  						// if there are no peers to serve, retry later
   370  						select {
   371  						case <-ctx.Done():
   372  							resCh <- res{nil, ctx.Err()}
   373  							return
   374  						case <-time.After(time.Second * 10):
   375  							// stay in the loop and try again
   376  						}
   377  					} else {
   378  						resCh <- res{r.Proofs, err}
   379  						break
   380  					}
   381  				}
   382  			}
   383  		}()
   384  	}
   385  	for i := uint(0); i < types.BloomBitLength; i++ {
   386  		indexCh <- i
   387  	}
   388  	close(indexCh)
   389  	batch := b.trieTable.NewBatch()
   390  	for i := uint(0); i < types.BloomBitLength; i++ {
   391  		res := <-resCh
   392  		if res.err != nil {
   393  			return res.err
   394  		}
   395  		res.nodes.Store(batch)
   396  	}
   397  	return batch.Write()
   398  }
   399  
   400  // Reset implements core.ChainIndexerBackend
   401  func (b *BloomTrieIndexerBackend) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error {
   402  	var root common.Hash
   403  	if section > 0 {
   404  		root = GetBloomTrieRoot(b.diskdb, section-1, lastSectionHead)
   405  	}
   406  	var err error
   407  	b.trie, err = trie.New(root, b.triedb)
   408  	if err != nil && b.odr != nil {
   409  		err = b.fetchMissingNodes(ctx, section, root)
   410  		if err == nil {
   411  			b.trie, err = trie.New(root, b.triedb)
   412  		}
   413  	}
   414  	b.section = section
   415  	return err
   416  }
   417  
   418  // Process implements core.ChainIndexerBackend
   419  func (b *BloomTrieIndexerBackend) Process(ctx context.Context, header *types.Header) error {
   420  	num := header.Number.Uint64() - b.section*b.size
   421  	if (num+1)%b.parentSize == 0 {
   422  		b.sectionHeads[num/b.parentSize] = header.Hash()
   423  	}
   424  	return nil
   425  }
   426  
   427  // Commit implements core.ChainIndexerBackend
   428  func (b *BloomTrieIndexerBackend) Commit() error {
   429  	var compSize, decompSize uint64
   430  
   431  	for i := uint(0); i < types.BloomBitLength; i++ {
   432  		var encKey [10]byte
   433  		binary.BigEndian.PutUint16(encKey[0:2], uint16(i))
   434  		binary.BigEndian.PutUint64(encKey[2:10], b.section)
   435  		var decomp []byte
   436  		for j := uint64(0); j < b.bloomTrieRatio; j++ {
   437  			data, err := rawdb.ReadBloomBits(b.diskdb, i, b.section*b.bloomTrieRatio+j, b.sectionHeads[j])
   438  			if err != nil {
   439  				return err
   440  			}
   441  			decompData, err2 := bitutil.DecompressBytes(data, int(b.parentSize/8))
   442  			if err2 != nil {
   443  				return err2
   444  			}
   445  			decomp = append(decomp, decompData...)
   446  		}
   447  		comp := bitutil.CompressBytes(decomp)
   448  
   449  		decompSize += uint64(len(decomp))
   450  		compSize += uint64(len(comp))
   451  		if len(comp) > 0 {
   452  			b.trie.Update(encKey[:], comp)
   453  		} else {
   454  			b.trie.Delete(encKey[:])
   455  		}
   456  	}
   457  	root, _, err := b.trie.Commit(nil)
   458  	if err != nil {
   459  		return err
   460  	}
   461  	// Pruning historical trie nodes if necessary.
   462  	if !b.disablePruning {
   463  		// Flush the triedb and track the latest trie nodes.
   464  		b.trieset.Clear()
   465  		b.triedb.Commit(root, false, func(hash common.Hash) { b.trieset.Add(hash) })
   466  
   467  		it := b.trieTable.NewIterator(nil, nil)
   468  		defer it.Release()
   469  
   470  		var (
   471  			deleted   int
   472  			remaining int
   473  			t         = time.Now()
   474  		)
   475  		for it.Next() {
   476  			trimmed := bytes.TrimPrefix(it.Key(), []byte(BloomTrieTablePrefix))
   477  			if !b.trieset.Contains(common.BytesToHash(trimmed)) {
   478  				b.trieTable.Delete(trimmed)
   479  				deleted += 1
   480  			} else {
   481  				remaining += 1
   482  			}
   483  		}
   484  		log.Debug("Prune historical bloom trie nodes", "deleted", deleted, "remaining", remaining, "elapsed", common.PrettyDuration(time.Since(t)))
   485  	} else {
   486  		b.triedb.Commit(root, false, nil)
   487  	}
   488  	sectionHead := b.sectionHeads[b.bloomTrieRatio-1]
   489  	StoreBloomTrieRoot(b.diskdb, b.section, sectionHead, root)
   490  	log.Info("Storing bloom trie", "section", b.section, "head", fmt.Sprintf("%064x", sectionHead), "root", fmt.Sprintf("%064x", root), "compression", float64(compSize)/float64(decompSize))
   491  
   492  	return nil
   493  }
   494  
   495  // Prune implements core.ChainIndexerBackend which deletes all
   496  // bloombits which older than the specified threshold.
   497  func (b *BloomTrieIndexerBackend) Prune(threshold uint64) error {
   498  	// Short circuit if the light pruning is disabled.
   499  	if b.disablePruning {
   500  		return nil
   501  	}
   502  	start := time.Now()
   503  	for i := uint(0); i < types.BloomBitLength; i++ {
   504  		rawdb.DeleteBloombits(b.diskdb, i, 0, threshold*b.bloomTrieRatio+b.bloomTrieRatio)
   505  	}
   506  	log.Debug("Prune history bloombits", "threshold", threshold, "elapsed", common.PrettyDuration(time.Since(start)))
   507  	return nil
   508  }