Browse Source

libs/log: remove Must constructor

pull/8120/head
tycho garen 3 years ago
parent
commit
8d40e2a1a3
5 changed files with 56 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. +26
    -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(
ctx context.Context,
be *BlockExecutor,
appConnConsensus abciclient.Client,
appConn abciclient.Client,
block *types.Block,
logger log.Logger,
store Store,
@ -583,7 +583,7 @@ func ExecCommitBlock(
s State,
) ([]byte, error) {
pbh := block.Header.ToProto()
finalizeBlockResponse, err := appConnConsensus.FinalizeBlock(
finalizeBlockResponse, err := appConn.FinalizeBlock(
ctx,
abci.RequestFinalizeBlock{
Hash: block.Hash(),
@ -623,7 +623,7 @@ func ExecCommitBlock(
}
// Commit block, get hash back
res, err := appConnConsensus.Commit(ctx)
res, err := appConn.Commit(ctx)
if err != nil {
logger.Error("client error during proxyAppConn.Commit", "err", res)
return nil, err


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

@ -60,17 +60,6 @@ func NewDefaultLogger(format, level string) (Logger, error) {
}, 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{}) {
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 {
return nil, err
}
logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
if err != nil {
return nil, err
}
return &Application{
logger: log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo),
logger: logger,
state: state,
snapshots: snapshots,
cfg: cfg,


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

@ -2,6 +2,7 @@
package main
import (
"context"
"fmt"
"math/rand"
"os"
@ -17,21 +18,35 @@ const (
randomSeed int64 = 4827085738
)
var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
func main() {
NewCLI().Run()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cli, err := NewCLI()
if err != nil {
panic(err)
}
cli.Run(ctx)
}
// CLI is the Cobra-based command-line interface.
type CLI struct {
root *cobra.Command
opts Options
root *cobra.Command
opts Options
logger log.Logger
}
// 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{
Use: "generator",
Short: "End-to-end testnet generator",
@ -51,7 +66,7 @@ func NewCLI() *CLI {
cli.root.PersistentFlags().IntVarP(&cli.opts.MaxNetworkSize, "max-size", "", 0,
"Maxmum network size (nodes), 0 is unlimited")
return cli
return cli, nil
}
// generate generates manifests in a directory.
@ -90,9 +105,9 @@ func (cli *CLI) generate() error {
}
// 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)
}
}

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

@ -34,8 +34,6 @@ import (
e2e "github.com/tendermint/tendermint/test/e2e/pkg"
)
var logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
// main is the binary entrypoint.
func main() {
ctx, cancel := context.WithCancel(context.Background())
@ -51,7 +49,6 @@ func main() {
}
if err := run(ctx, configFile); err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}
@ -63,9 +60,20 @@ func run(ctx context.Context, configFile string) error {
return err
}
logger, err := log.NewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
if err != nil {
// have print here because we can log, logger
// everywhere else.
fmt.Println("ERROR:", err)
return err
}
// Start remote signer (must start before node if running builtin).
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
}
if cfg.Protocol == "builtin" {
@ -76,11 +84,11 @@ func run(ctx context.Context, configFile string) error {
// Start app server.
switch cfg.Protocol {
case "socket", "grpc":
err = startApp(ctx, cfg)
err = startApp(ctx, logger, cfg)
case "builtin":
switch cfg.Mode {
case string(e2e.ModeLight):
err = startLightNode(ctx, cfg)
err = startLightNode(ctx, logger, cfg)
case string(e2e.ModeSeed):
err = startSeedNode(ctx)
default:
@ -90,6 +98,10 @@ func run(ctx context.Context, configFile string) error {
err = fmt.Errorf("invalid protocol %q", cfg.Protocol)
}
if err != nil {
logger.Error("starting node",
"protocol", cfg.Protocol,
"mode", cfg.Mode,
"err", 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.
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())
if err != nil {
return err
@ -160,7 +172,7 @@ func startSeedNode(ctx context.Context) error {
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()
if err != nil {
return err
@ -218,7 +230,7 @@ func startLightNode(ctx context.Context, cfg *Config) error {
}
// 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)
if err != nil {
return err


Loading…
Cancel
Save