go-blockchain/chain_commands.go

146 lines
5 KiB
Go

// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// SPDX-License-Identifier: EUPL-1.2
package blockchain
import (
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
coreio "dappco.re/go/core/io"
coreerr "dappco.re/go/core/log"
"dappco.re/go/core/blockchain/config"
"github.com/spf13/cobra"
)
// AppName is the CLI binary name used in executable examples and root wiring.
const AppName = "core-chain"
// ChainCommandName is the top-level command group exposed by the CLI.
const ChainCommandName = "chain"
const chainExplorerCommandName = "explorer"
const chainSyncCommandName = "sync"
const defaultChainSeed = "seeds.lthn.io:36942"
const defaultChainTestnetSeed = "localhost:46942"
const chainCommandPath = AppName + " " + ChainCommandName
const chainExplorerCommandPath = chainCommandPath + " " + chainExplorerCommandName
const chainSyncCommandPath = chainCommandPath + " " + chainSyncCommandName
const chainCommandShort = "Sync, inspect, and manage the blockchain"
const chainCommandLong = "Sync from peers, inspect the chain in a terminal UI, or manage a background sync."
const chainExplorerCommandShort = "Open the chain explorer"
const chainExplorerCommandLong = "Browse blocks and transactions in a terminal UI while the node keeps syncing."
const chainSyncCommandShort = "Start or stop chain sync"
const chainSyncCommandLong = "Start syncing from peers in the foreground by default. Use --daemon to keep syncing in the background, or --stop to stop the background sync for the selected data directory."
const chainExplorerExample = "Open the explorer:\n " + chainExplorerCommandPath + " --data-dir ~/.lethean/chain\n\n" +
"Open the explorer on testnet:\n " + chainExplorerCommandPath + " --testnet"
const chainSyncExample = "Start a foreground sync:\n " + chainSyncCommandPath + "\n\n" +
"Keep syncing in the background:\n " + chainSyncCommandPath + " --daemon\n\n" +
"Stop the background sync for a data directory:\n " + chainSyncCommandPath + " --stop --data-dir ~/.lethean/chain"
const chainCommandExample = chainExplorerExample + "\n" + chainSyncExample
// AddChainCommands registers the `chain` command group on a Cobra root.
//
// Usage: blockchain.AddChainCommands(root)
//
// Example:
//
// cli.WithCommands(ChainCommandName, blockchain.AddChainCommands)
//
// The command group owns the explorer and sync subcommands, so the
// command path documents the node features directly.
func AddChainCommands(root *cobra.Command) {
var (
dataDir string
seed string
testnet bool
)
chainCmd := &cobra.Command{
Use: ChainCommandName,
Short: chainCommandShort,
Long: chainCommandLong,
Example: chainCommandExample,
Args: cobra.NoArgs,
}
chainCmd.PersistentFlags().StringVar(&dataDir, "data-dir", defaultChainDataDirPath(), "store chain data and sync state in this directory")
chainCmd.PersistentFlags().StringVar(&seed, "seed", defaultChainSeed, "connect to this seed peer first (host:port)")
chainCmd.PersistentFlags().BoolVar(&testnet, "testnet", false, "use the Lethean testnet and its default seed ("+defaultChainTestnetSeed+")")
chainCmd.AddCommand(
newChainExplorerCommand(&dataDir, &seed, &testnet),
newChainSyncCommand(&dataDir, &seed, &testnet),
)
root.AddCommand(chainCmd)
}
func chainConfigForSeed(testnet bool, seed string) (config.ChainConfig, []config.HardFork, string) {
if testnet {
if seed == defaultChainSeed {
seed = defaultChainTestnetSeed
}
return config.Testnet, config.TestnetForks, seed
}
return config.Mainnet, config.MainnetForks, seed
}
func defaultChainDataDirPath() string {
home, err := os.UserHomeDir()
if err != nil {
return ".lethean"
}
return filepath.Join(home, ".lethean", "chain")
}
func ensureChainDataDirExists(dataDir string) error {
if err := coreio.Local.EnsureDir(dataDir); err != nil {
return coreerr.E("ensureChainDataDirExists", "create data dir", err)
}
return nil
}
func validateChainDataDir(dataDir string) error {
if strings.TrimSpace(dataDir) == "" {
return fmt.Errorf("blockchain: --data-dir must not be empty")
}
return nil
}
func validateChainSyncModeSelection(runAsDaemon, stopDaemon bool) error {
if runAsDaemon && stopDaemon {
return fmt.Errorf("blockchain: choose either --daemon to start background sync or --stop to stop it")
}
return nil
}
func validateChainRuntimeInputs(dataDir, seed string) error {
if err := validateChainDataDir(dataDir); err != nil {
return err
}
if strings.TrimSpace(seed) == "" {
return fmt.Errorf("blockchain: --seed must not be empty")
}
host, port, err := net.SplitHostPort(seed)
if err != nil || host == "" || port == "" {
return fmt.Errorf("blockchain: invalid --seed %q: expected host:port", seed)
}
portNumber, err := strconv.Atoi(port)
if err != nil {
return fmt.Errorf("blockchain: invalid --seed %q: expected host:port with a numeric port", seed)
}
if portNumber < 1 || portNumber > 65535 {
return fmt.Errorf("blockchain: invalid --seed %q: expected host:port with a port from 1 to 65535", seed)
}
return nil
}