Browse Source

libs/log: remove Must constructor (#8120)

* libs/log: remove Must constructor

* Update test/e2e/node/main.go

Co-authored-by: M. J. Fromberger <michael.j.fromberger@gmail.com>

* use stdlog

Co-authored-by: M. J. Fromberger <michael.j.fromberger@gmail.com>
pull/8123/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
b08dd93d88
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 35 deletions
  1. +3
    -3
      internal/state/execution.go
  2. +0
    -11
      libs/log/default.go
  3. +6
    -1
      test/e2e/app/app.go
  4. +27
    -11
      test/e2e/generator/main.go
  5. +21
    -9
      test/e2e/node/main.go

+ 3
- 3
internal/state/execution.go View File

@ -575,7 +575,7 @@ func fireEvents(
func ExecCommitBlock( func ExecCommitBlock(
ctx context.Context, ctx context.Context,
be *BlockExecutor, be *BlockExecutor,
appConnConsensus abciclient.Client,
appConn abciclient.Client,
block *types.Block, block *types.Block,
logger log.Logger, logger log.Logger,
store Store, store Store,
@ -583,7 +583,7 @@ func ExecCommitBlock(
s State, s State,
) ([]byte, error) { ) ([]byte, error) {
pbh := block.Header.ToProto() pbh := block.Header.ToProto()
finalizeBlockResponse, err := appConnConsensus.FinalizeBlock(
finalizeBlockResponse, err := appConn.FinalizeBlock(
ctx, ctx,
abci.RequestFinalizeBlock{ abci.RequestFinalizeBlock{
Hash: block.Hash(), Hash: block.Hash(),
@ -623,7 +623,7 @@ func ExecCommitBlock(
} }
// Commit block, get hash back // Commit block, get hash back
res, err := appConnConsensus.Commit(ctx)
res, err := appConn.Commit(ctx)
if err != nil { if err != nil {
logger.Error("client error during proxyAppConn.Commit", "err", res) logger.Error("client error during proxyAppConn.Commit", "err", res)
return nil, err return nil, err


+ 0
- 11
libs/log/default.go View File

@ -60,17 +60,6 @@ func NewDefaultLogger(format, level string) (Logger, error) {
}, nil }, nil
} }
// MustNewDefaultLogger delegates a call NewDefaultLogger where it panics on
// error.
func MustNewDefaultLogger(format, level string) Logger {
logger, err := NewDefaultLogger(format, level)
if err != nil {
panic(err)
}
return logger
}
func (l defaultLogger) Info(msg string, keyVals ...interface{}) { func (l defaultLogger) Info(msg string, keyVals ...interface{}) {
l.Logger.Info().Fields(getLogFields(keyVals...)).Msg(msg) l.Logger.Info().Fields(getLogFields(keyVals...)).Msg(msg)
} }


+ 6
- 1
test/e2e/app/app.go View File

@ -90,8 +90,13 @@ func NewApplication(cfg *Config) (*Application, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
if err != nil {
return nil, err
}
return &Application{ return &Application{
logger: log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo),
logger: logger,
state: state, state: state,
snapshots: snapshots, snapshots: snapshots,
cfg: cfg, cfg: cfg,


+ 27
- 11
test/e2e/generator/main.go View File

@ -2,7 +2,9 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
stdlog "log"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -17,21 +19,35 @@ const (
randomSeed int64 = 4827085738 randomSeed int64 = 4827085738
) )
var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
func main() { func main() {
NewCLI().Run()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := NewCLI()
if err != nil {
stdlog.Fatal(err)
}
cli.Run(ctx)
} }
// CLI is the Cobra-based command-line interface. // CLI is the Cobra-based command-line interface.
type CLI struct { type CLI struct {
root *cobra.Command
opts Options
root *cobra.Command
opts Options
logger log.Logger
} }
// NewCLI sets up the CLI. // NewCLI sets up the CLI.
func NewCLI() *CLI {
cli := &CLI{}
func NewCLI() (*CLI, error) {
logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
if err != nil {
return nil, err
}
cli := &CLI{
logger: logger,
}
cli.root = &cobra.Command{ cli.root = &cobra.Command{
Use: "generator", Use: "generator",
Short: "End-to-end testnet generator", Short: "End-to-end testnet generator",
@ -51,7 +67,7 @@ func NewCLI() *CLI {
cli.root.PersistentFlags().IntVarP(&cli.opts.MaxNetworkSize, "max-size", "", 0, cli.root.PersistentFlags().IntVarP(&cli.opts.MaxNetworkSize, "max-size", "", 0,
"Maxmum network size (nodes), 0 is unlimited") "Maxmum network size (nodes), 0 is unlimited")
return cli
return cli, nil
} }
// generate generates manifests in a directory. // generate generates manifests in a directory.
@ -90,9 +106,9 @@ func (cli *CLI) generate() error {
} }
// Run runs the CLI. // Run runs the CLI.
func (cli *CLI) Run() {
if err := cli.root.Execute(); err != nil {
logger.Error(err.Error())
func (cli *CLI) Run(ctx context.Context) {
if err := cli.root.ExecuteContext(ctx); err != nil {
cli.logger.Error(err.Error())
os.Exit(1) os.Exit(1)
} }
} }

+ 21
- 9
test/e2e/node/main.go View File

@ -34,8 +34,6 @@ import (
e2e "github.com/tendermint/tendermint/test/e2e/pkg" e2e "github.com/tendermint/tendermint/test/e2e/pkg"
) )
var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
// main is the binary entrypoint. // main is the binary entrypoint.
func main() { func main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
@ -51,7 +49,6 @@ func main() {
} }
if err := run(ctx, configFile); err != nil { if err := run(ctx, configFile); err != nil {
logger.Error(err.Error())
os.Exit(1) os.Exit(1)
} }
} }
@ -63,9 +60,20 @@ func run(ctx context.Context, configFile string) error {
return err return err
} }
logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
if err != nil {
// have print here because we can't log (yet), use the logger
// everywhere else.
fmt.Fprintln(os.Stderr, "ERROR:", err)
return err
}
// Start remote signer (must start before node if running builtin). // Start remote signer (must start before node if running builtin).
if cfg.PrivValServer != "" { if cfg.PrivValServer != "" {
if err = startSigner(ctx, cfg); err != nil {
if err = startSigner(ctx, logger, cfg); err != nil {
logger.Error("starting signer",
"server", cfg.PrivValServer,
"err", err)
return err return err
} }
if cfg.Protocol == "builtin" { if cfg.Protocol == "builtin" {
@ -76,11 +84,11 @@ func run(ctx context.Context, configFile string) error {
// Start app server. // Start app server.
switch cfg.Protocol { switch cfg.Protocol {
case "socket", "grpc": case "socket", "grpc":
err = startApp(ctx, cfg)
err = startApp(ctx, logger, cfg)
case "builtin": case "builtin":
switch cfg.Mode { switch cfg.Mode {
case string(e2e.ModeLight): case string(e2e.ModeLight):
err = startLightNode(ctx, cfg)
err = startLightNode(ctx, logger, cfg)
case string(e2e.ModeSeed): case string(e2e.ModeSeed):
err = startSeedNode(ctx) err = startSeedNode(ctx)
default: default:
@ -90,6 +98,10 @@ func run(ctx context.Context, configFile string) error {
err = fmt.Errorf("invalid protocol %q", cfg.Protocol) err = fmt.Errorf("invalid protocol %q", cfg.Protocol)
} }
if err != nil { if err != nil {
logger.Error("starting node",
"protocol", cfg.Protocol,
"mode", cfg.Mode,
"err", err)
return err return err
} }
@ -100,7 +112,7 @@ func run(ctx context.Context, configFile string) error {
} }
// startApp starts the application server, listening for connections from Tendermint. // startApp starts the application server, listening for connections from Tendermint.
func startApp(ctx context.Context, cfg *Config) error {
func startApp(ctx context.Context, logger log.Logger, cfg *Config) error {
app, err := app.NewApplication(cfg.App()) app, err := app.NewApplication(cfg.App())
if err != nil { if err != nil {
return err return err
@ -160,7 +172,7 @@ func startSeedNode(ctx context.Context) error {
return n.Start(ctx) return n.Start(ctx)
} }
func startLightNode(ctx context.Context, cfg *Config) error {
func startLightNode(ctx context.Context, logger log.Logger, cfg *Config) error {
tmcfg, nodeLogger, err := setupNode() tmcfg, nodeLogger, err := setupNode()
if err != nil { if err != nil {
return err return err
@ -218,7 +230,7 @@ func startLightNode(ctx context.Context, cfg *Config) error {
} }
// startSigner starts a signer server connecting to the given endpoint. // startSigner starts a signer server connecting to the given endpoint.
func startSigner(ctx context.Context, cfg *Config) error {
func startSigner(ctx context.Context, logger log.Logger, cfg *Config) error {
filePV, err := privval.LoadFilePV(cfg.PrivValKey, cfg.PrivValState) filePV, err := privval.LoadFilePV(cfg.PrivValKey, cfg.PrivValState)
if err != nil { if err != nil {
return err return err


Loading…
Cancel
Save