github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/gossip/evmstore/apply_genesis.go (about)

     1  package evmstore
     2  
     3  import (
     4  	"github.com/syndtr/goleveldb/leveldb/opt"
     5  
     6  	"github.com/unicornultrafoundation/go-helios/u2udb/batched"
     7  
     8  	"github.com/unicornultrafoundation/go-u2u/u2u/genesis"
     9  	"github.com/unicornultrafoundation/go-u2u/utils/adapters/ethdb2udb"
    10  	"github.com/unicornultrafoundation/go-u2u/utils/dbutil/autocompact"
    11  )
    12  
    13  // ApplyGenesis writes initial state.
    14  func (s *Store) ApplyGenesis(g genesis.Genesis) (err error) {
    15  	db := batched.Wrap(autocompact.Wrap2M(ethdb2udb.Wrap(s.EvmDb), opt.GiB, 16*opt.GiB, true, "evm"))
    16  	g.RawEvmItems.ForEach(func(key, value []byte) bool {
    17  		err = db.Put(key, value)
    18  		if err != nil {
    19  			return false
    20  		}
    21  		return true
    22  	})
    23  	if err != nil {
    24  		return err
    25  	}
    26  	return db.Write()
    27  }
    28  
    29  func (s *Store) WrapTablesAsBatched() (unwrap func()) {
    30  	origTables := s.table
    31  
    32  	batchedTxs := batched.Wrap(s.table.Txs)
    33  	s.table.Txs = batchedTxs
    34  
    35  	batchedTxPositions := batched.Wrap(s.table.TxPositions)
    36  	s.table.TxPositions = batchedTxPositions
    37  
    38  	unwrapLogs := s.EvmLogs.WrapTablesAsBatched()
    39  
    40  	batchedReceipts := batched.Wrap(autocompact.Wrap2M(s.table.Receipts, opt.GiB, 16*opt.GiB, false, "receipts"))
    41  	s.table.Receipts = batchedReceipts
    42  	return func() {
    43  		_ = batchedTxs.Flush()
    44  		_ = batchedTxPositions.Flush()
    45  		_ = batchedReceipts.Flush()
    46  		unwrapLogs()
    47  		s.table = origTables
    48  	}
    49  }