github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/db/kv/archived_point.go (about)

     1  package kv
     2  
     3  import (
     4  	"context"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     8  	bolt "go.etcd.io/bbolt"
     9  	"go.opencensus.io/trace"
    10  )
    11  
    12  // LastArchivedSlot from the db.
    13  func (s *Store) LastArchivedSlot(ctx context.Context) (types.Slot, error) {
    14  	ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedSlot")
    15  	defer span.End()
    16  	var index types.Slot
    17  	err := s.db.View(func(tx *bolt.Tx) error {
    18  		bkt := tx.Bucket(stateSlotIndicesBucket)
    19  		b, _ := bkt.Cursor().Last()
    20  		index = bytesutil.BytesToSlotBigEndian(b)
    21  		return nil
    22  	})
    23  
    24  	return index, err
    25  }
    26  
    27  // LastArchivedRoot from the db.
    28  func (s *Store) LastArchivedRoot(ctx context.Context) [32]byte {
    29  	ctx, span := trace.StartSpan(ctx, "BeaconDB.LastArchivedRoot")
    30  	defer span.End()
    31  
    32  	var blockRoot []byte
    33  	if err := s.db.View(func(tx *bolt.Tx) error {
    34  		bkt := tx.Bucket(stateSlotIndicesBucket)
    35  		_, blockRoot = bkt.Cursor().Last()
    36  		return nil
    37  	}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
    38  		panic(err)
    39  	}
    40  
    41  	return bytesutil.ToBytes32(blockRoot)
    42  }
    43  
    44  // ArchivedPointRoot returns the block root of an archived point from the DB.
    45  // This is essential for cold state management and to restore a cold state.
    46  func (s *Store) ArchivedPointRoot(ctx context.Context, slot types.Slot) [32]byte {
    47  	ctx, span := trace.StartSpan(ctx, "BeaconDB.ArchivedPointRoot")
    48  	defer span.End()
    49  
    50  	var blockRoot []byte
    51  	if err := s.db.View(func(tx *bolt.Tx) error {
    52  		bucket := tx.Bucket(stateSlotIndicesBucket)
    53  		blockRoot = bucket.Get(bytesutil.SlotToBytesBigEndian(slot))
    54  		return nil
    55  	}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
    56  		panic(err)
    57  	}
    58  
    59  	return bytesutil.ToBytes32(blockRoot)
    60  }
    61  
    62  // HasArchivedPoint returns true if an archived point exists in DB.
    63  func (s *Store) HasArchivedPoint(ctx context.Context, slot types.Slot) bool {
    64  	ctx, span := trace.StartSpan(ctx, "BeaconDB.HasArchivedPoint")
    65  	defer span.End()
    66  	var exists bool
    67  	if err := s.db.View(func(tx *bolt.Tx) error {
    68  		iBucket := tx.Bucket(stateSlotIndicesBucket)
    69  		exists = iBucket.Get(bytesutil.SlotToBytesBigEndian(slot)) != nil
    70  		return nil
    71  	}); err != nil { // This view never returns an error, but we'll handle anyway for sanity.
    72  		panic(err)
    73  	}
    74  	return exists
    75  }