Skip to content

Commit

Permalink
feat: use logger object
Browse files Browse the repository at this point in the history
Signed-off-by: Smuu <[email protected]>
  • Loading branch information
smuu committed Dec 9, 2024
1 parent 34f8d11 commit 8f22051
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 85 deletions.
6 changes: 3 additions & 3 deletions test/e2e/benchmark/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type BenchmarkTest struct {

// NewBenchmarkTest wraps around testnet.New to create a new benchmark test.
// It may modify genesis consensus parameters based on manifest.
func NewBenchmarkTest(name string, manifest *Manifest) (*BenchmarkTest, error) {
func NewBenchmarkTest(logger *log.Logger, name string, manifest *Manifest) (*BenchmarkTest, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand All @@ -42,8 +42,8 @@ func NewBenchmarkTest(name string, manifest *Manifest) (*BenchmarkTest, error) {

log.Printf("Knuu initialized with scope %s", kn.Scope)

testNet, err := testnet.New(kn, testnet.Options{
Grafana: testnet.GetGrafanaInfoFromEnvVar(),
testNet, err := testnet.New(logger, kn, testnet.Options{
Grafana: testnet.GetGrafanaInfoFromEnvVar(logger),
ChainID: manifest.ChainID,
GenesisModifiers: manifest.GetGenesisModifiers(),
})
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/benchmark/throughput.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TwoNodeSimple(logger *log.Logger) error {
DisableBBR: true,
}

benchTest, err := NewBenchmarkTest(testName, &manifest)
benchTest, err := NewBenchmarkTest(logger, testName, &manifest)
testnet.NoError("failed to create benchmark test", err)

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -113,8 +113,8 @@ func TwoNodeSimple(logger *log.Logger) error {
func runBenchmarkTest(logger *log.Logger, testName string, manifest Manifest) error {
logger.Println("Running", testName)
manifest.ChainID = manifest.summary()
log.Println("ChainID: ", manifest.ChainID)
benchTest, err := NewBenchmarkTest(testName, &manifest)
logger.Println("ChainID: ", manifest.ChainID)
benchTest, err := NewBenchmarkTest(logger, testName, &manifest)
testnet.NoError("failed to create benchmark test", err)

ctx, cancel := context.WithCancel(context.Background())
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func runTest(logger *log.Logger, test Test) {
if err != nil {
logger.Fatalf("--- ERROR %s: %v", test.Name, err)
}
logger.Printf("--- ✅ PASS: %s \n\n", test.Name)
logger.Println("--- ✅ PASS: %s \n\n", test.Name)

Check failure on line 59 in test/e2e/main.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

printf: (*log.Logger).Println call has possible Printf formatting directive %s (govet)

Check failure on line 59 in test/e2e/main.go

View workflow job for this annotation

GitHub Actions / test / test

(*log.Logger).Println call has possible Printf formatting directive %s

Check failure on line 59 in test/e2e/main.go

View workflow job for this annotation

GitHub Actions / test / test-coverage

(*log.Logger).Println call has possible Printf formatting directive %s

Check failure on line 59 in test/e2e/main.go

View workflow job for this annotation

GitHub Actions / test / test-race

(*log.Logger).Println call has possible Printf formatting directive %s

Check failure on line 59 in test/e2e/main.go

View workflow job for this annotation

GitHub Actions / test / test-short

(*log.Logger).Println call has possible Printf formatting directive %s
}

func getTestNames(tests []Test) string {
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/major_upgrade_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func MajorUpgradeToV2(logger *log.Logger) error {
logger.Printf("Knuu initialized with scope %s", kn.Scope)

logger.Println("Creating testnet")
testNet, err := testnet.New(kn, testnet.Options{})
testNet, err := testnet.New(logger, kn, testnet.Options{})
testnet.NoError("failed to create testnet", err)

defer testNet.Cleanup(ctx)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/major_upgrade_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func MajorUpgradeToV3(logger *log.Logger) error {
logger.Printf("Knuu initialized with scope %s", kn.Scope)

logger.Println("Creating testnet")
testNet, err := testnet.New(kn, testnet.Options{})
testNet, err := testnet.New(logger, kn, testnet.Options{})
testnet.NoError("failed to create testnet", err)

defer testNet.Cleanup(ctx)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/minor_version_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func MinorVersionCompatibility(logger *log.Logger) error {
kn.HandleStopSignal(ctx)
logger.Printf("Knuu initialized with scope %s", kn.Scope)

testNet, err := testnet.New(kn, testnet.Options{Seed: seed})
testNet, err := testnet.New(logger, kn, testnet.Options{Seed: seed})
testnet.NoError("failed to create testnet", err)

defer testNet.Cleanup(ctx)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func E2ESimple(logger *log.Logger) error {
kn.HandleStopSignal(ctx)
logger.Printf("Knuu initialized with scope %s", kn.Scope)

testNet, err := testnet.New(kn, testnet.Options{})
testNet, err := testnet.New(logger, kn, testnet.Options{})
testnet.NoError("failed to create testnet", err)

defer testNet.Cleanup(ctx)
Expand Down
16 changes: 9 additions & 7 deletions test/e2e/testnet/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package testnet
import (
"context"
"fmt"
"log"
"os"
"path/filepath"

serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/rs/zerolog/log"
"github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/p2p"
Expand Down Expand Up @@ -55,13 +55,15 @@ type Node struct {
// FIXME: This does not work currently with the reverse proxy
// grpcProxyHost string
traceProxyHost string

logger *log.Logger
}

// PullRoundStateTraces retrieves the round state traces from a node.
// It will save them to the provided path.
func (n *Node) PullRoundStateTraces(path string) ([]trace.Event[schema.RoundState], error) {
addr := n.AddressTracing()
log.Info().Str("Address", addr).Msg("Pulling round state traces")
n.logger.Println("Pulling round state traces", "address", addr)

err := trace.GetTable(addr, schema.RoundState{}.Table(), path)
if err != nil {
Expand All @@ -74,7 +76,7 @@ func (n *Node) PullRoundStateTraces(path string) ([]trace.Event[schema.RoundStat
// It will save them to the provided path.
func (n *Node) PullBlockSummaryTraces(path string) ([]trace.Event[schema.BlockSummary], error) {
addr := n.AddressTracing()
log.Info().Str("Address", addr).Msg("Pulling block summary traces")
n.logger.Println("Pulling block summary traces", "address", addr)

err := trace.GetTable(addr, schema.BlockSummary{}.Table(), path)
if err != nil {
Expand All @@ -97,6 +99,7 @@ type Resources struct {

func NewNode(
ctx context.Context,
logger *log.Logger,
name string,
version string,
startHeight int64,
Expand Down Expand Up @@ -178,6 +181,7 @@ func NewNode(
NetworkKey: networkKey,
SelfDelegation: selfDelegation,
sidecars: sidecars,
logger: logger,
}, nil
}

Expand Down Expand Up @@ -205,9 +209,7 @@ func (n *Node) Init(ctx context.Context, genesis *types.GenesisDoc, peers []stri
}
defer os.RemoveAll(tmpDir)
nodeDir := filepath.Join(tmpDir, n.Name)
log.Info().Str("name", n.Name).
Str("directory", nodeDir).
Msg("Creating validator's config and data directories")
n.logger.Println("Creating validator's config and data directories", "name", n.Name, "directory", nodeDir)
for _, dir := range []string{
filepath.Join(nodeDir, "config"),
filepath.Join(nodeDir, "data"),
Expand Down Expand Up @@ -324,7 +326,7 @@ func (n Node) IsValidator() bool {
}

func (n Node) Client() (*http.HTTP, error) {
log.Debug().Str("RPC Address", n.AddressRPC()).Msg("Creating HTTP client for node")
n.logger.Println("Creating HTTP client for node", "rpc_address", n.AddressRPC())
return http.New(n.AddressRPC(), "/websocket")
}

Expand Down
72 changes: 25 additions & 47 deletions test/e2e/testnet/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"time"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/celestiaorg/knuu/pkg/preloader"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/rs/zerolog/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)

Expand All @@ -35,6 +35,8 @@ type Testnet struct {
knuu *knuu.Knuu
chainID string
genesisHash string

logger *log.Logger
}

type Options struct {
Expand All @@ -44,7 +46,7 @@ type Options struct {
GenesisModifiers []genesis.Modifier
}

func New(knuu *knuu.Knuu, opts Options) (*Testnet, error) {
func New(logger *log.Logger, knuu *knuu.Knuu, opts Options) (*Testnet, error) {
opts.setDefaults()
return &Testnet{
seed: opts.Seed,
Expand All @@ -55,6 +57,7 @@ func New(knuu *knuu.Knuu, opts Options) (*Testnet, error) {
knuu: knuu,
chainID: opts.ChainID,
genesisHash: "",
logger: logger,
}, nil
}

Expand All @@ -78,7 +81,7 @@ func (t *Testnet) SetConsensusMaxBlockSize(size int64) {
func (t *Testnet) CreateGenesisNode(ctx context.Context, version string, selfDelegation, upgradeHeightV2 int64, resources Resources, disableBBR bool) error {
signerKey := t.keygen.Generate(ed25519Type)
networkKey := t.keygen.Generate(ed25519Type)
node, err := NewNode(ctx, fmt.Sprintf("val%d", len(t.nodes)), version, 0, selfDelegation, nil, signerKey, networkKey, upgradeHeightV2, resources, t.grafana, t.knuu, disableBBR)
node, err := NewNode(ctx, t.logger, fmt.Sprintf("val%d", len(t.nodes)), version, 0, selfDelegation, nil, signerKey, networkKey, upgradeHeightV2, resources, t.grafana, t.knuu, disableBBR)
if err != nil {
return err
}
Expand Down Expand Up @@ -111,15 +114,10 @@ func (t *Testnet) CreateTxClients(ctx context.Context,
name := fmt.Sprintf("txsim%d", i)
err := t.CreateTxClient(ctx, name, version, sequences, blobRange, blobPerSequence, resources, grpcEndpoint, upgradeSchedule)
if err != nil {
log.Err(err).Str("name", name).
Str("grpc endpoint", grpcEndpoint).
Msg("txsim creation failed")
t.logger.Println("txsim creation failed", "name", name, "grpc_endpoint", grpcEndpoint, "error", err)
return err
}
log.Info().
Str("name", name).
Str("grpc endpoint", grpcEndpoint).
Msg("txsim created")
t.logger.Println("txsim created", "name", name, "grpc_endpoint", grpcEndpoint)
}
return nil
}
Expand Down Expand Up @@ -190,29 +188,22 @@ func (t *Testnet) CreateTxClient(
}
}

txsim, err := CreateTxClient(ctx, name, version, grpcEndpoint, t.seed, blobSequences, blobRange, blobPerSequence, 1, resources, remoteRootDir, t.knuu, upgradeSchedule)
txsim, err := CreateTxClient(ctx, t.logger, name, version, grpcEndpoint, t.seed, blobSequences, blobRange, blobPerSequence, 1, resources, remoteRootDir, t.knuu, upgradeSchedule)
if err != nil {
log.Err(err).
Str("name", name).
Msg("error creating txsim")
t.logger.Println("error creating txsim", "name", name, "error", err)
return err
}

err = txsim.Instance.Build().Commit(ctx)
if err != nil {
log.Err(err).
Str("name", name).
Msg("error committing txsim")
t.logger.Println("error committing txsim", "name", name, "error", err)
return err
}

// copy over the keyring directory to the txsim instance
err = txsim.Instance.Storage().AddFolder(txsimKeyringDir, remoteRootDir, "10001:10001")
if err != nil {
log.Err(err).
Str("directory", txsimKeyringDir).
Str("name", name).
Msg("error adding keyring dir to txsim")
t.logger.Println("error adding keyring dir to txsim", "directory", txsimKeyringDir, "name", name, "error", err)
return err
}

Expand All @@ -224,14 +215,10 @@ func (t *Testnet) StartTxClients(ctx context.Context) error {
for _, txsim := range t.txClients {
err := txsim.Instance.Execution().StartAsync(ctx)
if err != nil {
log.Err(err).
Str("name", txsim.Name).
Msg("txsim failed to start")
t.logger.Println("txsim failed to start", "name", txsim.Name, "error", err)
return err
}
log.Info().
Str("name", txsim.Name).
Msg("txsim started")
t.logger.Println("txsim started", "name", txsim.Name)
}
// wait for txsims to start
for _, txsim := range t.txClients {
Expand Down Expand Up @@ -279,17 +266,14 @@ func (t *Testnet) CreateAccount(name string, tokens int64, txsimKeyringDir strin
return nil, err
}

log.Info().
Str("name", name).
Str("pk", pk.String()).
Msg("txsim account created and added to genesis")
t.logger.Println("txsim account created and added to genesis", "name", name, "pk", pk.String())
return kr, nil
}

func (t *Testnet) CreateNode(ctx context.Context, version string, startHeight, upgradeHeight int64, resources Resources, disableBBR bool) error {
signerKey := t.keygen.Generate(ed25519Type)
networkKey := t.keygen.Generate(ed25519Type)
node, err := NewNode(ctx, fmt.Sprintf("val%d", len(t.nodes)), version, startHeight, 0, nil, signerKey, networkKey, upgradeHeight, resources, t.grafana, t.knuu, disableBBR)
node, err := NewNode(ctx, t.logger, fmt.Sprintf("val%d", len(t.nodes)), version, startHeight, 0, nil, signerKey, networkKey, upgradeHeight, resources, t.grafana, t.knuu, disableBBR)
if err != nil {
return err
}
Expand Down Expand Up @@ -386,8 +370,7 @@ func (t *Testnet) WaitToSync(ctx context.Context) error {
}

for _, node := range genesisNodes {
log.Info().Str("name", node.Name).Msg(
"waiting for node to sync")
t.logger.Println("waiting for node to sync", "name", node.Name)
client, err := node.Client()
if err != nil {
return fmt.Errorf("failed to initialize client for node %s: %w", node.Name, err)
Expand All @@ -396,15 +379,12 @@ func (t *Testnet) WaitToSync(ctx context.Context) error {
resp, err := client.Status(ctx)
if err == nil {
if resp != nil && resp.SyncInfo.LatestBlockHeight > 0 {
log.Info().Int("attempts", i).Str("name", node.Name).Int64("latest_block_height", resp.SyncInfo.LatestBlockHeight).Msg(
"node has synced")
t.logger.Println("node has synced", "name", node.Name, "attempts", i, "latest_block_height", resp.SyncInfo.LatestBlockHeight)
break
}
log.Info().Str("name", node.Name).Int("attempt", i).Msg(
"node status retrieved but not synced yet, waiting...")
t.logger.Println("node status retrieved but not synced yet, waiting...", "name", node.Name, "attempt", i)
} else {
log.Error().Str("name", node.Name).Int("attempt", i).Err(err).Msg(
"error getting status, retrying...")
t.logger.Printf("error getting status, retrying...", "name", node.Name, "attempt", i, "error", err)

Check failure on line 387 in test/e2e/testnet/testnet.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

printf: (*log.Logger).Printf call has arguments but no formatting directives (govet)

Check failure on line 387 in test/e2e/testnet/testnet.go

View workflow job for this annotation

GitHub Actions / test / test

(*log.Logger).Printf call has arguments but no formatting directives

Check failure on line 387 in test/e2e/testnet/testnet.go

View workflow job for this annotation

GitHub Actions / test / test-race

(*log.Logger).Printf call has arguments but no formatting directives

Check failure on line 387 in test/e2e/testnet/testnet.go

View workflow job for this annotation

GitHub Actions / test / test-short

(*log.Logger).Printf call has arguments but no formatting directives
}
if i == 19 {
return fmt.Errorf("timed out waiting for node %s to sync: %w", node.Name, err)
Expand Down Expand Up @@ -432,17 +412,15 @@ func (t *Testnet) StartNodes(ctx context.Context) error {
}
}

log.Info().Msg("create endpoint proxies for genesis nodes")
t.logger.Println("create endpoint proxies for genesis nodes")
// wait for instances to be running
for _, node := range genesisNodes {
err := node.WaitUntilStartedAndCreateProxy(ctx)
if err != nil {
log.Err(err).Str("name", node.Name).Str("version",
node.Version).Msg("failed to start and create proxy")
t.logger.Println("failed to start and create proxy", "name", node.Name, "version", node.Version, "error", err)
return fmt.Errorf("node %s failed to start: %w", node.Name, err)
}
log.Info().Str("name", node.Name).Str("version",
node.Version).Msg("started and created proxy")
t.logger.Println("started and created proxy", "name", node.Name, "version", node.Version)
}
return nil
}
Expand All @@ -454,7 +432,7 @@ func (t *Testnet) Start(ctx context.Context) error {
return err
}
// wait for nodes to sync
log.Info().Msg("waiting for genesis nodes to sync")
t.logger.Println("waiting for genesis nodes to sync")
err = t.WaitToSync(ctx)
if err != nil {
return err
Expand All @@ -465,7 +443,7 @@ func (t *Testnet) Start(ctx context.Context) error {

func (t *Testnet) Cleanup(ctx context.Context) {
if err := t.knuu.CleanUp(ctx); err != nil {
log.Err(err).Msg("failed to cleanup knuu")
t.logger.Println("failed to cleanup knuu", "error", err)
}
}

Expand Down
Loading

0 comments on commit 8f22051

Please sign in to comment.