github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/u2u/genesisstore/store_genesis.go (about)

     1  package genesisstore
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/unicornultrafoundation/go-u2u/log"
     8  	"github.com/unicornultrafoundation/go-u2u/rlp"
     9  
    10  	"github.com/unicornultrafoundation/go-u2u/native/ibr"
    11  	"github.com/unicornultrafoundation/go-u2u/native/ier"
    12  	"github.com/unicornultrafoundation/go-u2u/u2u/genesis"
    13  	"github.com/unicornultrafoundation/go-u2u/utils/iodb"
    14  )
    15  
    16  type (
    17  	Blocks struct {
    18  		fMap FilesMap
    19  	}
    20  	Epochs struct {
    21  		fMap FilesMap
    22  	}
    23  	RawEvmItems struct {
    24  		fMap FilesMap
    25  	}
    26  )
    27  
    28  func (s *Store) Genesis() genesis.Genesis {
    29  	return genesis.Genesis{
    30  		Header:      s.head,
    31  		Blocks:      s.Blocks(),
    32  		Epochs:      s.Epochs(),
    33  		RawEvmItems: s.RawEvmItems(),
    34  	}
    35  }
    36  
    37  func getSectionName(base string, i int) string {
    38  	if i == 0 {
    39  		return base
    40  	}
    41  	return fmt.Sprintf("%s-%d", base, i)
    42  }
    43  
    44  func (s Store) Header() genesis.Header {
    45  	return s.head
    46  }
    47  
    48  func (s *Store) Blocks() genesis.Blocks {
    49  	return Blocks{s.fMap}
    50  }
    51  
    52  func (s Blocks) ForEach(fn func(ibr.LlrIdxFullBlockRecord) bool) {
    53  	for i := 1000; i >= 0; i-- {
    54  		f, err := s.fMap(BlocksSection(i))
    55  		if err != nil {
    56  			continue
    57  		}
    58  		stream := rlp.NewStream(f, 0)
    59  		for {
    60  			br := ibr.LlrIdxFullBlockRecord{}
    61  			err = stream.Decode(&br)
    62  			if err == io.EOF {
    63  				break
    64  			}
    65  			if err != nil {
    66  				log.Crit("Failed to decode Blocks genesis section", "err", err)
    67  			}
    68  			if !fn(br) {
    69  				break
    70  			}
    71  		}
    72  	}
    73  }
    74  
    75  func (s *Store) Epochs() genesis.Epochs {
    76  	return Epochs{s.fMap}
    77  }
    78  
    79  func (s Epochs) ForEach(fn func(ier.LlrIdxFullEpochRecord) bool) {
    80  	for i := 1000; i >= 0; i-- {
    81  		f, err := s.fMap(EpochsSection(i))
    82  		if err != nil {
    83  			continue
    84  		}
    85  		stream := rlp.NewStream(f, 0)
    86  		for {
    87  			er := ier.LlrIdxFullEpochRecord{}
    88  			err = stream.Decode(&er)
    89  			if err == io.EOF {
    90  				break
    91  			}
    92  			if err != nil {
    93  				log.Crit("Failed to decode Epochs genesis section", "err", err)
    94  			}
    95  			if !fn(er) {
    96  				break
    97  			}
    98  		}
    99  	}
   100  }
   101  
   102  func (s *Store) RawEvmItems() genesis.EvmItems {
   103  	return RawEvmItems{s.fMap}
   104  }
   105  
   106  func (s RawEvmItems) ForEach(fn func(key, value []byte) bool) {
   107  	for i := 1000; i >= 0; i-- {
   108  		f, err := s.fMap(EvmSection(i))
   109  		if err != nil {
   110  			continue
   111  		}
   112  		it := iodb.NewIterator(f)
   113  		for it.Next() {
   114  			if !fn(it.Key(), it.Value()) {
   115  				break
   116  			}
   117  		}
   118  		if it.Error() != nil {
   119  			log.Crit("Failed to decode RawEvmItems genesis section", "err", it.Error())
   120  		}
   121  		it.Release()
   122  	}
   123  }