github.com/sunriselayer/sunrise-da@v0.13.1-sr3/cmd/cel-shed/header.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/spf13/cobra"
    10  
    11  	"github.com/celestiaorg/go-header/store"
    12  
    13  	"github.com/sunriselayer/sunrise-da/header"
    14  	"github.com/sunriselayer/sunrise-da/nodebuilder"
    15  	"github.com/sunriselayer/sunrise-da/nodebuilder/node"
    16  )
    17  
    18  func init() {
    19  	headerCmd.AddCommand(headerStoreInit)
    20  }
    21  
    22  var headerCmd = &cobra.Command{
    23  	Use:   "header [subcommand]",
    24  	Short: "Collection of header module related utilities",
    25  }
    26  
    27  var headerStoreInit = &cobra.Command{
    28  	Use: "store-init [node-type] [network] [height]",
    29  	Short: `Forcefully initialize header store head to be of the given height. Requires the node being stopped. 
    30  Custom store path is not supported yet.`,
    31  	SilenceUsage: true,
    32  	RunE: func(cmd *cobra.Command, args []string) error {
    33  		if len(args) != 3 {
    34  			return errors.New("not enough arguments")
    35  		}
    36  
    37  		tp := node.ParseType(args[0])
    38  		if !tp.IsValid() {
    39  			return errors.New("invalid node-type")
    40  		}
    41  
    42  		network := args[1]
    43  
    44  		height, err := strconv.Atoi(args[2])
    45  		if err != nil {
    46  			return fmt.Errorf("invalid height: %w", err)
    47  		}
    48  
    49  		s, err := nodebuilder.OpenStore(fmt.Sprintf("~/.sunrise-%s-%s", strings.ToLower(tp.String()),
    50  			strings.ToLower(network)), nil)
    51  		if err != nil {
    52  			return err
    53  		}
    54  
    55  		ds, err := s.Datastore()
    56  		if err != nil {
    57  			return err
    58  		}
    59  
    60  		hstore, err := store.NewStore[*header.ExtendedHeader](ds)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		newHead, err := hstore.GetByHeight(cmd.Context(), uint64(height))
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		return hstore.Init(cmd.Context(), newHead)
    71  	},
    72  }