github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/cmd/utils/bor_flags.go (about)

     1  package utils
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  
     8  	"github.com/ethereum/go-ethereum/core"
     9  	"github.com/ethereum/go-ethereum/eth"
    10  	"github.com/ethereum/go-ethereum/log"
    11  	"github.com/ethereum/go-ethereum/node"
    12  	"gopkg.in/urfave/cli.v1"
    13  )
    14  
    15  var (
    16  	//
    17  	// Bor Specific flags
    18  	//
    19  
    20  	// HeimdallURLFlag flag for heimdall url
    21  	HeimdallURLFlag = cli.StringFlag{
    22  		Name:  "bor.heimdall",
    23  		Usage: "URL of Heimdall service",
    24  		Value: "http://localhost:1317",
    25  	}
    26  
    27  	// WithoutHeimdallFlag no heimdall (for testing purpose)
    28  	WithoutHeimdallFlag = cli.BoolFlag{
    29  		Name:  "bor.withoutheimdall",
    30  		Usage: "Run without Heimdall service (for testing purpose)",
    31  	}
    32  
    33  	// BorFlags all bor related flags
    34  	BorFlags = []cli.Flag{
    35  		HeimdallURLFlag,
    36  		WithoutHeimdallFlag,
    37  	}
    38  )
    39  
    40  func getGenesis(genesisPath string) (*core.Genesis, error) {
    41  	log.Info("Reading genesis at ", "file", genesisPath)
    42  	file, err := os.Open(genesisPath)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	defer file.Close()
    47  
    48  	genesis := new(core.Genesis)
    49  	if err := json.NewDecoder(file).Decode(genesis); err != nil {
    50  		return nil, err
    51  	}
    52  	return genesis, nil
    53  }
    54  
    55  // SetBorConfig sets bor config
    56  func SetBorConfig(ctx *cli.Context, cfg *eth.Config) {
    57  	cfg.HeimdallURL = ctx.GlobalString(HeimdallURLFlag.Name)
    58  	cfg.WithoutHeimdall = ctx.GlobalBool(WithoutHeimdallFlag.Name)
    59  }
    60  
    61  // CreateBorEthereum Creates bor ethereum object from eth.Config
    62  func CreateBorEthereum(cfg *eth.Config) *eth.Ethereum {
    63  	workspace, err := ioutil.TempDir("", "bor-command-node-")
    64  	if err != nil {
    65  		Fatalf("Failed to create temporary keystore: %v", err)
    66  	}
    67  
    68  	// Create a networkless protocol stack and start an Ethereum service within
    69  	stack, err := node.New(&node.Config{DataDir: workspace, UseLightweightKDF: true, Name: "bor-command-node"})
    70  	if err != nil {
    71  		Fatalf("Failed to create node: %v", err)
    72  	}
    73  	ethereum, err := eth.New(stack, cfg)
    74  	if err != nil {
    75  		Fatalf("Failed to register Ethereum protocol: %v", err)
    76  	}
    77  
    78  	// Start the node and assemble the JavaScript console around it
    79  	if err = stack.Start(); err != nil {
    80  		Fatalf("Failed to start stack: %v", err)
    81  	}
    82  	_, err = stack.Attach()
    83  	if err != nil {
    84  		Fatalf("Failed to attach to node: %v", err)
    85  	}
    86  
    87  	return ethereum
    88  }