Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes #906

Merged
merged 12 commits into from
Jan 28, 2022
3 changes: 2 additions & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ func main() {

err := app.Run(os.Args)
if err != nil {
panic(err)
fmt.Printf("an error was found while executing the command. Err: [%s] \n", err)
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}
}

Expand Down
53 changes: 40 additions & 13 deletions cmd/drand-cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,10 @@ func CLI() *cli.App {
}

func resetCmd(c *cli.Context) error {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return err
}

fmt.Fprintf(output, "You are about to delete your local share, group file and generated random beacons. "+
"Are you sure you wish to perform this operation? [y/N]")
Expand Down Expand Up @@ -613,7 +616,10 @@ func askPort() string {
}

func runMigration(c *cli.Context) error {
config := contextToConfig(c)
config, err := contextToConfig(c)
if err != nil {
return err
}
if err := migration.MigrateSBFolderStructure(config.ConfigFolder()); err != nil {
return err
}
Expand All @@ -622,7 +628,11 @@ func runMigration(c *cli.Context) error {
}

func checkMigration(c *cli.Context) error {
config := contextToConfig(c)
config, err := contextToConfig(c)
if err != nil {
return err
}

if isPresent := migration.CheckSBFolderStructure(config.ConfigFolder()); isPresent {
return fmt.Errorf("single-beacon drand folder structure was not migrated, " +
"please first do it with 'drand util migrate' command")
Expand Down Expand Up @@ -666,7 +676,10 @@ func keygenCmd(c *cli.Context) error {
priv = key.NewTLSKeyPair(addr)
}

config := contextToConfig(c)
config, err := contextToConfig(c)
if err != nil {
return err
}
beaconID := getBeaconID(c)
fileStore := key.NewFileStore(config.ConfigFolderMB(), beaconID)

Expand All @@ -688,8 +701,9 @@ func keygenCmd(c *cli.Context) error {

var buff bytes.Buffer
if err := toml.NewEncoder(&buff).Encode(priv.Public.TOML()); err != nil {
panic(err)
return err
}

buff.WriteString("\n")
fmt.Println(buff.String())
return nil
Expand Down Expand Up @@ -728,6 +742,7 @@ func getThreshold(c *cli.Context) (int, error) {
return threshold, nil
}

//nolint:gocyclo
func checkConnection(c *cli.Context) error {
var names []string
beaconID := ""
Expand Down Expand Up @@ -761,7 +776,10 @@ func checkConnection(c *cli.Context) error {
return fmt.Errorf("drand: check-group expects a list of identities or %s flag", groupFlag.Name)
}

conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return err
}
isVerbose := c.IsSet(verboseFlag.Name)
allGood := true
isIdentityCheck := c.IsSet(groupFlag.Name) || c.IsSet(beaconIDFlag.Name)
Expand Down Expand Up @@ -820,7 +838,10 @@ func checkIdentityAddress(conf *core.Config, addr string, tls bool, beaconID str
// deleteBeaconCmd deletes all beacon in the database from the given round until
// the head of the chain
func deleteBeaconCmd(c *cli.Context) error {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return err
}

startRoundStr := c.Args().First()
sr, err := strconv.Atoi(startRoundStr)
Expand Down Expand Up @@ -882,7 +903,7 @@ func getGroup(c *cli.Context) (*key.Group, error) {
return g, nil
}

func contextToConfig(c *cli.Context) *core.Config {
func contextToConfig(c *cli.Context) (*core.Config, error) {
var opts []core.ConfigOption
version := common.GetAppVersion()

Expand Down Expand Up @@ -911,7 +932,7 @@ func contextToConfig(c *cli.Context) *core.Config {
if c.Bool("tls-disable") {
opts = append(opts, core.WithInsecure())
if c.IsSet("tls-cert") || c.IsSet("tls-key") {
panic("option 'tls-disable' used with 'tls-cert' or 'tls-key': combination is not valid")
return nil, fmt.Errorf("option 'tls-disable' used with 'tls-cert' or 'tls-key': combination is not valid")
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
certPath, keyPath := c.String("tls-cert"), c.String("tls-key")
Expand All @@ -920,15 +941,15 @@ func contextToConfig(c *cli.Context) *core.Config {
if c.IsSet("certs-dir") {
paths, err := fs.Files(c.String("certs-dir"))
if err != nil {
panic(err)
return nil, err
}
opts = append(opts, core.WithTrustedCerts(paths...))
}
if c.Bool(enablePrivateRand.Name) {
opts = append(opts, core.WithPrivateRandomness())
}
conf := core.NewConfig(opts...)
return conf
return conf, nil
}

func getNodes(c *cli.Context) ([]*key.Node, error) {
Expand Down Expand Up @@ -986,7 +1007,10 @@ func getBeaconID(c *cli.Context) string {
}

func getDBStoresPaths(c *cli.Context) (map[string]string, error) {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return nil, err
}
stores := make(map[string]string)

if c.IsSet(allBeaconsFlag.Name) {
Expand Down Expand Up @@ -1014,7 +1038,10 @@ func getDBStoresPaths(c *cli.Context) (map[string]string, error) {
}

func getKeyStores(c *cli.Context) (map[string]key.Store, error) {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return nil, err
}

if c.IsSet(allBeaconsFlag.Name) {
return key.NewFileStores(conf.ConfigFolderMB())
Expand Down
17 changes: 14 additions & 3 deletions cmd/drand-cli/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func getShareArgs(c *cli.Context) (*shareArgs, error) {
return nil, fmt.Errorf("error getting entropy source: %w", err)
}

args.conf = contextToConfig(c)
args.conf, err = contextToConfig(c)
if err != nil {
return nil, err
}

return args, nil
}
Expand Down Expand Up @@ -501,7 +504,11 @@ func statusCmd(c *cli.Context) error {
}

func migrateCmd(c *cli.Context) error {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return err
}

if err := migration.MigrateSBFolderStructure(conf.ConfigFolder()); err != nil {
return fmt.Errorf("cannot migrate folder structure, please try again. err: %s", err)
}
Expand Down Expand Up @@ -674,7 +681,11 @@ func entropyInfoFromReader(c *cli.Context) (*control.EntropyInfo, error) {
}

func selfSign(c *cli.Context) error {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return err
}

beaconID := getBeaconID(c)

fs := key.NewFileStore(conf.ConfigFolderMB(), beaconID)
Expand Down
5 changes: 4 additions & 1 deletion cmd/drand-cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
)

func startCmd(c *cli.Context) error {
conf := contextToConfig(c)
conf, err := contextToConfig(c)
if err != nil {
return err
}

// Create and start drand daemon
drandDaemon, err := core.NewDrandDaemon(conf)
Expand Down
50 changes: 28 additions & 22 deletions cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func Relay(c *cli.Context) error {
}
}

hashFlagSet := c.IsSet(lib.HashFlag.Name)
hashListFlagSet := c.IsSet(lib.HashListFlag.Name)
if hashFlagSet && hashListFlagSet {
return fmt.Errorf("--%s and --%s flag cannot be set at the same time", lib.HashFlag.Name, lib.HashListFlag.Name)
}

client, err := lib.Create(c, c.IsSet(metricsFlag.Name))
if err != nil {
return err
Expand All @@ -69,32 +75,32 @@ func Relay(c *cli.Context) error {
return fmt.Errorf("failed to create rest handler: %w", err)
}

if c.IsSet(lib.HashFlag.Name) {
hash, err := hex.DecodeString(c.String(lib.HashFlag.Name))
hashesList := make([]string, 0)
if hashFlagSet {
hashesList = append(hashesList, c.String(lib.HashFlag.Name))
} else if hashListFlagSet {
hashesList = c.StringSlice(lib.HashListFlag.Name)
} else {
hashesList = append(hashesList, common.DefaultChainHash)
}

for _, hash := range hashesList {
if hash == common.DefaultChainHash {
handler.RegisterNewBeaconHandler(client, common.DefaultChainHash)
continue
}

hashHex, err := hex.DecodeString(hash)
if err != nil {
return fmt.Errorf("failed to decode hash flag: %w", err)
}
handler.RegisterNewBeaconHandler(client, string(hash))
} else {
if c.IsSet(lib.HashListFlag.Name) {
hashList := c.StringSlice(lib.HashListFlag.Name)
for _, hashHex := range hashList {
hash, err := hex.DecodeString(hashHex)
if err != nil {
return fmt.Errorf("failed to decode hash flag: %w", err)
}

c, err := lib.Create(c, c.IsSet(metricsFlag.Name), dclient.WithChainHash(hash))
if err != nil {
return err
}

handler.RegisterNewBeaconHandler(c, fmt.Sprintf("%x", hash))
}
} else {
fmt.Println("hash flags were not set. Registering beacon handler for default chain hash")
handler.RegisterNewBeaconHandler(client, common.DefaultChainHash)

c, err := lib.Create(c, c.IsSet(metricsFlag.Name), dclient.WithChainHash(hashHex))
if err != nil {
return err
}

handler.RegisterNewBeaconHandler(c, fmt.Sprintf("%x", hashHex))
}

if c.IsSet(accessLogFlag.Name) {
Expand Down
5 changes: 3 additions & 2 deletions http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ func (h *DrandHandler) ChainInfo(w http.ResponseWriter, r *http.Request) {
info := h.getChainInfo(r.Context(), chainHashHex)
if info == nil {
h.log.Warnw("", "http_server", "failed to serve group", "client", r.RemoteAddr, "req", url.PathEscape(r.URL.Path))
http.Error(w, "group not found", http.StatusNoContent)
http.Error(w, "group not found", http.StatusNotFound)
return
}

Expand Down Expand Up @@ -561,7 +561,8 @@ func (h *DrandHandler) getBeaconHandler(chainHash []byte) (*beaconHandler, error
bh, exists := h.beacons[chainHashStr]

if !exists {
return nil, fmt.Errorf("there's no BeaconHandler for beaconHash %s", chainHash)
return nil, fmt.Errorf("there is no BeaconHandler for beaconHash [%s]. "+
emmanuelm41 marked this conversation as resolved.
Show resolved Hide resolved
"Is the chain hash correct?. Please check it", chainHashStr)
}

return bh, nil
Expand Down
7 changes: 5 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"os"
"sync"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -38,6 +39,8 @@ const (
// logger.
const DefaultLevel = LogInfo

var isDefaultLoggerSet sync.Once

// ConfigureDefaultLogger updates the default logger to wrap a provided kit logger.
func ConfigureDefaultLogger(output zapcore.WriteSyncer, level int, jsonFormat bool) {
if jsonFormat {
Expand All @@ -49,9 +52,9 @@ func ConfigureDefaultLogger(output zapcore.WriteSyncer, level int, jsonFormat bo

// DefaultLogger is the default logger that only logs at the `DefaultLevel`.
func DefaultLogger() Logger {
if zap.S() == nil {
isDefaultLoggerSet.Do(func() {
zap.ReplaceGlobals(NewZapLogger(nil, getConsoleEncoder(), DefaultLevel))
}
})

return zap.S()
}
Expand Down