Skip to content

Commit

Permalink
Merge pull request #156 from lukso-network/feature/lighthouse-validator
Browse files Browse the repository at this point in the history
Feature/lighthouse validator
  • Loading branch information
Wolmin authored Jun 21, 2023
2 parents 4ecf282 + 652f81d commit f477770
Show file tree
Hide file tree
Showing 11 changed files with 362 additions and 155 deletions.
35 changes: 29 additions & 6 deletions cmd/lukso/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ const (
deployBlockMainnetConfigDependencyName = "mainnet deploy block"
deployBlockTestnetConfigDependencyName = "testnet deploy block"

validatorMainnetConfigDependencyName = "validator mainnet config"
validatorTestnetConfigDependencyName = "validator testnet config"
validatorMainnetConfigDependencyName = "validator mainnet config"
validatorTestnetConfigDependencyName = "validator testnet config"
lighthouseValidatorMainnetConfigDependencyName = "lighthouse validator mainnet config"
lighthouseValidatorTestnetConfigDependencyName = "lighthouse validator testnet config"
)

var (
Expand All @@ -75,7 +77,7 @@ var (
isBinary: true,
},
lighthouseDependencyName: {
baseUrl: "https://github.com/sigp/lighthouse/releases/download/|TAG|/lighthouse-|TAG|-x86_64-|OS-NAME|-|OS|-portable.tar.gz",
baseUrl: "https://github.com/sigp/lighthouse/releases/download/|TAG|/lighthouse-|TAG|-|ARCH|-|OS-NAME|-|OS|-portable.tar.gz",
name: lighthouseDependencyName,
filePath: "", // binary dir selected during runtime
isBinary: true,
Expand Down Expand Up @@ -213,6 +215,16 @@ var (
name: lighthouseTestnetConfigDependencyName,
filePath: testnetConfig + "/" + lighthouseTomlPath,
},
lighthouseValidatorMainnetConfigDependencyName: {
baseUrl: "https://raw.githubusercontent.com/lukso-network/network-configs/main/mainnet/lighthouse/validator.toml",
name: lighthouseValidatorMainnetConfigDependencyName,
filePath: mainnetConfig + "/" + lighthouseValidatorTomlPath,
},
lighthouseValidatorTestnetConfigDependencyName: {
baseUrl: "https://raw.githubusercontent.com/lukso-network/network-configs/main/testnet/lighthouse/validator.toml",
name: lighthouseValidatorTestnetConfigDependencyName,
filePath: testnetConfig + "/" + lighthouseValidatorTomlPath,
},
}

validatorConfigDependencies = map[string]*ClientDependency{
Expand All @@ -239,20 +251,31 @@ type ClientDependency struct {
func (dependency *ClientDependency) ParseUrl(tag, commitHash string) (url string) {
// for lighthouse
var (
systemName string
urlSystem = systemOs
systemName string
urlSystem = systemOs
alternativeArch = arch
)

if dependency.name == lighthouseDependencyName {
switch systemOs {
case ubuntu:
systemName = "unknown"
urlSystem += "-gnu"
alternativeArch = "x86_64"
if arch == "aarch64" {
alternativeArch = arch
}

case macos:
systemName = "apple"
alternativeArch = "x86_64"
default:
systemName = "unknown"
urlSystem += "-gnu"
alternativeArch = "x86_64"
if arch == "aarch64" {
alternativeArch = arch
}
}
}
baseUrl := dependency.baseUrl
Expand All @@ -266,7 +289,7 @@ func (dependency *ClientDependency) ParseUrl(tag, commitHash string) (url string
url = strings.Replace(url, "|OS|", urlSystem, -1)
url = strings.Replace(url, "|OS-NAME|", systemName, -1) // for lighthouse
url = strings.Replace(url, "|COMMIT|", commitHash, -1)
url = strings.Replace(url, "|ARCH|", arch, -1)
url = strings.Replace(url, "|ARCH|", alternativeArch, -1)

return
}
Expand Down
20 changes: 10 additions & 10 deletions cmd/lukso/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ func installBinaries(ctx *cli.Context) (err error) {
}

if !cfg.Exists() {
return cli.Exit(folderNotInitialized, 1)
return exit(folderNotInitialized, 1)
}

isRoot, err := isRoot()
if err != nil {
return cli.Exit(fmt.Sprintf("There was an error while checking user privileges: %v", err), 1)
return exit(fmt.Sprintf("There was an error while checking user privileges: %v", err), 1)
}
if !isRoot {
return cli.Exit(errNeedRoot, 1)
return exit(errNeedRoot.Error(), 1)
}

var (
Expand All @@ -189,13 +189,13 @@ func installBinaries(ctx *cli.Context) (err error) {
)

consensusMessage := "\nWhich consensus client do you want to install?\n" +
"1: prysm\n> "
"1: prysm\n2: lighthouse\n> "

executionMessage := "\nWhich execution client do you want to install?\n" +
"1: geth\n2: erigon\n> "

consensusInput = registerInputWithMessage(consensusMessage)
for consensusInput != "1" {
for consensusInput != "1" && consensusInput != "2" {
consensusInput = registerInputWithMessage("Please provide a valid option\n> ")
}

Expand Down Expand Up @@ -229,7 +229,7 @@ func installBinaries(ctx *cli.Context) (err error) {
fmt.Println("")
accepted := acceptTermsInteractive()
if !accepted {
return cli.Exit("❌ Terms of use not accepted - aborting...", 1)
return exit("❌ Terms of use not accepted - aborting...", 1)
}
} else {
log.Info("✅ You accepted Prysm's Terms of Use: https://github.com/prysmaticlabs/prysm/blob/develop/TERMS_OF_SERVICE.md")
Expand All @@ -239,26 +239,26 @@ func installBinaries(ctx *cli.Context) (err error) {
log.Infof("⬇️ Downloading %s...", selectedExecution)
err = clientDependencies[selectedExecution].Download(executionTag, commitHash, false, binaryPerms)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while downloading %s: %v", selectedExecution, err), 1)
return exit(fmt.Sprintf("❌ There was an error while downloading %s: %v", selectedExecution, err), 1)
}

log.Infof("⬇️ Downloading %s...", selectedConsensus)
err = clientDependencies[selectedConsensus].Download(consensusTag, "", false, binaryPerms)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while downloading %s: %v", selectedConsensus, err), 1)
return exit(fmt.Sprintf("❌ There was an error while downloading %s: %v", selectedConsensus, err), 1)
}

// for now, we also need to download a validator client
// when other validator clients will be implemented we will download the one bound to consensus clients
log.Infof("⬇️ Downloading %s...", validatorDependencyName)
err = clientDependencies[validatorDependencyName].Download(prysmTag, "", false, binaryPerms)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while downloading validator: %v", err), 1)
return exit(fmt.Sprintf("❌ There was an error while downloading validator: %v", err), 1)
}

err = cfg.Create(selectedExecution, selectedConsensus)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while creating configration file: %v", err), 1)
return exit(fmt.Sprintf("❌ There was an error while creating configration file: %v", err), 1)
}

log.Info("✅ Configuration files created!")
Expand Down
126 changes: 65 additions & 61 deletions cmd/lukso/flags.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package main

import (
"crypto/rand"
"encoding/hex"
"fmt"
"os"
"strings"
"syscall"

"github.com/urfave/cli/v2"
"golang.org/x/term"

"github.com/lukso-network/tools-lukso-cli/config"
)
Expand All @@ -36,9 +32,10 @@ const (
noSlasherFlag = "no-slasher"

// lighthouse related flag names
lighthouseTagFlag = "lighthouse-tag"
lighthouseConfigFileFlag = "lighthouse-config"
lighthouseDatadirFlag = "lighthouse-datadir"
lighthouseTagFlag = "lighthouse-tag"
lighthouseConfigFileFlag = "lighthouse-config"
lighthouseValidatorConfigFileFlag = "lighthouse-validator-config"
lighthouseDatadirFlag = "lighthouse-datadir"

// Validator related flag names
validatorTagFlag = "validator-tag"
Expand Down Expand Up @@ -96,13 +93,14 @@ const (

// structure inside configs/selected-network directory.
// we will select directory based on provided flag, by concatenating config path + file path
chainConfigYamlPath = "shared/config.yaml"
gethTomlPath = "geth/geth.toml"
erigonTomlPath = "erigon/erigon.toml"
prysmYamlPath = "prysm/prysm.yaml"
lighthouseTomlPath = "lighthouse/lighthouse.toml"
deployBlockPath = "shared/deploy_block.txt"
validatorYamlPath = "prysm/validator.yaml"
chainConfigYamlPath = "shared/config.yaml"
gethTomlPath = "geth/geth.toml"
erigonTomlPath = "erigon/erigon.toml"
prysmYamlPath = "prysm/prysm.yaml"
lighthouseTomlPath = "lighthouse/lighthouse.toml"
lighthouseValidatorTomlPath = "lighthouse/validator.toml"
deployBlockPath = "shared/deploy_block.txt"
validatorYamlPath = "prysm/validator.yaml"

// genesis related files
genesisJsonPath = "shared/genesis.json"
Expand Down Expand Up @@ -165,6 +163,11 @@ var (
Usage: "Selected wallet",
Hidden: true,
},
&cli.StringFlag{
Name: validatorDatadirFlag,
Usage: "Selected wallet", // wallet for lighthouse - incorporated into datadir
Hidden: true,
},
&cli.StringFlag{
Name: validatorKeysFlag,
Usage: "Location of your validator keys",
Expand Down Expand Up @@ -378,6 +381,11 @@ var (
Usage: "Lighthouse datadir",
Value: consensusMainnetDatadir,
},
&cli.StringFlag{
Name: lighthouseValidatorConfigFileFlag,
Usage: "Path to validator.toml config file",
Value: mainnetConfig + "/" + lighthouseValidatorTomlPath,
},
}

// VALIDATOR
Expand Down Expand Up @@ -580,9 +588,12 @@ func prepareLighthouseStartFlags(ctx *cli.Context) (startFlags []string, err err
return
}

defaults = append(defaults, fmt.Sprintf("--logfile=%s", logFile))
defaults = append(defaults, "--logfile-debug-level=info")
defaults = append(defaults, "--logfile-max-number=1")
defaults = append(defaults, "--logfile", logFile)
defaults = append(defaults, "--logfile-debug-level", "info")
defaults = append(defaults, "--logfile-max-number", "1")
if ctx.String(transactionFeeRecipientFlag) != "" {
defaults = append(defaults, "--suggested-fee-recipient", ctx.String(transactionFeeRecipientFlag))
}

userFlags := clientDependencies[lighthouseDependencyName].PassStartFlags(ctx)

Expand All @@ -608,55 +619,17 @@ func prepareValidatorStartFlags(ctx *cli.Context) (startFlags []string, password

validatorPasswordPath := ctx.String(validatorWalletPasswordFileFlag)
if validatorPasswordPath == "" {
var password []byte
fmt.Print("\nPlease enter your keystore password: ")
password, err = term.ReadPassword(0)
fmt.Println("")

if err != nil {
log.Errorf("Couldn't read password: %v", err)

return
}

b := make([]byte, 4)
_, err = rand.Read(b)
passwordPipe, err = readValidatorPassword(ctx)
if err != nil {
log.Errorf("Couldn't create random byte array: %v", err)

return
}

randPipe := hex.EncodeToString(b)

passwordPipePath := ctx.String(validatorKeysFlag) + fmt.Sprintf("/.%s", randPipe)
err = syscall.Mkfifo(passwordPipePath, 0600)
if err != nil {
log.Errorf("Couldn't create password pipe: %v", err)

return
}
var f *os.File
f, err = os.OpenFile(passwordPipePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Errorf("Couldn't open password pipe: %v", err)

return
}
_, err = f.Write(password)
if err != nil {
log.Errorf("Couldn't write password to pipe: %v", err)

return
err = exit(fmt.Sprintf("❌ There was an error while reading password: %v", err), 1)
}
}

err = ctx.Set(validatorWalletPasswordFileFlag, passwordPipePath)
defer func() {
if err != nil {
return
os.Remove(passwordPipe.Name())
}

passwordPipe = f
}
}()

startFlags = clientDependencies[validatorDependencyName].PassStartFlags(ctx)

Expand All @@ -681,3 +654,34 @@ func prepareValidatorStartFlags(ctx *cli.Context) (startFlags []string, password
}
return
}

func prepareLighthouseValidatorFlags(ctx *cli.Context) (startFlags []string, err error) {
validatorConfigExists := flagFileExists(ctx, validatorConfigFileFlag)
chainConfigExists := flagFileExists(ctx, prysmChainConfigFileFlag)
if !validatorConfigExists || !chainConfigExists {
err = errFlagPathInvalid

return
}

defaults, err := config.LoadLighthouseConfig(ctx.String(lighthouseValidatorConfigFileFlag))
if err != nil {
return
}

logFilePath, err := prepareTimestampedFile(ctx.String(logFolderFlag), validatorDependencyName)
if err != nil {
return
}

defaults = append(defaults, "--logfile", logFilePath)
defaults = append(defaults, "--logfile-debug-level", "info")
defaults = append(defaults, "--logfile-max-number", "1")
defaults = append(defaults, "--suggested-fee-recipient", ctx.String(transactionFeeRecipientFlag))

userFlags := clientDependencies[validatorDependencyName].PassStartFlags(ctx)

startFlags = mergeFlags(userFlags, defaults)

return
}
8 changes: 4 additions & 4 deletions cmd/lukso/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func initializeDirectory(ctx *cli.Context) error {

err := createJwtSecret(jwtSecretPath)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while creating JWT secret file: %v", err), 1)
return exit(fmt.Sprintf("❌ There was an error while creating JWT secret file: %v", err), 1)
}

err = os.MkdirAll(pid.FileDir, configPerms)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while preparing PID directory: %v", err), 1)
return exit(fmt.Sprintf("❌ There was an error while preparing PID directory: %v", err), 1)
}

switch cfg.Exists() {
Expand All @@ -71,7 +71,7 @@ func initializeDirectory(ctx *cli.Context) error {

err = cfg.Create("", "")
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was an error while preparing LUKSO configuration: %v", err), 1)
return exit(fmt.Sprintf("❌ There was an error while preparing LUKSO configuration: %v", err), 1)
}

log.Infof("✅ LUKSO configuration created under %s", config.Path)
Expand All @@ -87,7 +87,7 @@ func initConfigGroup(configDependencies map[string]*ClientDependency) error {
for _, dependency := range configDependencies {
err := dependency.Download("", "", false, configPerms)
if err != nil {
return cli.Exit(fmt.Sprintf("❌ There was error while downloading %s file: %v", dependency.name, err), 1)
return exit(fmt.Sprintf("❌ There was error while downloading %s file: %v", dependency.name, err), 1)
}
}

Expand Down
Loading

0 comments on commit f477770

Please sign in to comment.