github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/cmd/utils/bor_flags.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"gopkg.in/urfave/cli.v1"
     9  
    10  	"github.com/ethereum/go-ethereum/core"
    11  	"github.com/ethereum/go-ethereum/eth"
    12  	"github.com/ethereum/go-ethereum/log"
    13  	"github.com/ethereum/go-ethereum/node"
    14  )
    15  
    16  var (
    17  	//
    18  	// Bor Specific flags
    19  	//
    20  
    21  	// HeimdallURLFlag flag for heimdall url
    22  	HeimdallURLFlag = cli.StringFlag{
    23  		Name:  "bor.heimdall",
    24  		Usage: "URL of Heimdall service",
    25  		Value: "http://localhost:1317",
    26  	}
    27  
    28  	// WithoutHeimdallFlag no heimdall (for testing purpose)
    29  	WithoutHeimdallFlag = cli.BoolFlag{
    30  		Name:  "bor.withoutheimdall",
    31  		Usage: "Run without Heimdall service (for testing purpose)",
    32  	}
    33  
    34  	// HeimdallgRPCAddressFlag flag for heimdall gRPC address
    35  	HeimdallgRPCAddressFlag = cli.StringFlag{
    36  		Name:  "bor.heimdallgRPC",
    37  		Usage: "Address of Heimdall gRPC service",
    38  		Value: "",
    39  	}
    40  
    41  	// RunHeimdallFlag flag for running heimdall internally from bor
    42  	RunHeimdallFlag = cli.BoolFlag{
    43  		Name:  "bor.runheimdall",
    44  		Usage: "Run Heimdall service as a child process",
    45  	}
    46  
    47  	RunHeimdallArgsFlag = cli.StringFlag{
    48  		Name:  "bor.runheimdallargs",
    49  		Usage: "Arguments to pass to Heimdall service",
    50  		Value: "",
    51  	}
    52  
    53  	// UseHeimdallApp flag for using internall heimdall app to fetch data
    54  	UseHeimdallAppFlag = cli.BoolFlag{
    55  		Name:  "bor.useheimdallapp",
    56  		Usage: "Use child heimdall process to fetch data, Only works when bor.runheimdall is true",
    57  	}
    58  
    59  	// BorFlags all bor related flags
    60  	BorFlags = []cli.Flag{
    61  		HeimdallURLFlag,
    62  		WithoutHeimdallFlag,
    63  		HeimdallgRPCAddressFlag,
    64  		RunHeimdallFlag,
    65  		RunHeimdallArgsFlag,
    66  		UseHeimdallAppFlag,
    67  	}
    68  )
    69  
    70  func getGenesis(genesisPath string) (*core.Genesis, error) {
    71  	log.Info("Reading genesis at ", "file", genesisPath)
    72  	file, err := os.Open(genesisPath)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	defer file.Close()
    77  
    78  	genesis := new(core.Genesis)
    79  	if err := json.NewDecoder(file).Decode(genesis); err != nil {
    80  		return nil, err
    81  	}
    82  	return genesis, nil
    83  }
    84  
    85  // SetBorConfig sets bor config
    86  func SetBorConfig(ctx *cli.Context, cfg *eth.Config) {
    87  	cfg.HeimdallURL = ctx.GlobalString(HeimdallURLFlag.Name)
    88  	cfg.WithoutHeimdall = ctx.GlobalBool(WithoutHeimdallFlag.Name)
    89  	cfg.HeimdallgRPCAddress = ctx.GlobalString(HeimdallgRPCAddressFlag.Name)
    90  	cfg.RunHeimdall = ctx.GlobalBool(RunHeimdallFlag.Name)
    91  	cfg.RunHeimdallArgs = ctx.GlobalString(RunHeimdallArgsFlag.Name)
    92  	cfg.UseHeimdallApp = ctx.GlobalBool(UseHeimdallAppFlag.Name)
    93  }
    94  
    95  // CreateBorEthereum Creates bor ethereum object from eth.Config
    96  func CreateBorEthereum(cfg *eth.Config) *eth.Ethereum {
    97  	workspace, err := ioutil.TempDir("", "bor-command-node-")
    98  	if err != nil {
    99  		Fatalf("Failed to create temporary keystore: %v", err)
   100  	}
   101  
   102  	// Create a networkless protocol stack and start an Ethereum service within
   103  	stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: "bor-command-node"})
   104  	if err != nil {
   105  		Fatalf("Failed to create node: %v", err)
   106  	}
   107  	ethereum, err := eth.New(stack, cfg)
   108  	if err != nil {
   109  		Fatalf("Failed to register Ethereum protocol: %v", err)
   110  	}
   111  
   112  	// Start the node and assemble the JavaScript console around it
   113  	if err = stack.Start(); err != nil {
   114  		Fatalf("Failed to start stack: %v", err)
   115  	}
   116  	_, err = stack.Attach()
   117  	if err != nil {
   118  		Fatalf("Failed to attach to node: %v", err)
   119  	}
   120  
   121  	return ethereum
   122  }