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

refactor: add config struct and use slog #354

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ linters-settings:
lll:
line-length: 100
tab-width: 4
gomnd:
ignored-functions:
- "^make"
22 changes: 15 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added verification to ensure CARTESI_BLOCKCHAIN_ID matches the id returned from the Ethereum node
- Added support for CARTESI_AUTH_PRIVATE_KEY and CARTESI_AUTH_PRIVATE_KEY_FILE

- Added verification to ensure `CARTESI_BLOCKCHAIN_ID` matches the id returned from the Ethereum node
- Added support for `CARTESI_AUTH_PRIVATE_KEY` and `CARTESI_AUTH_PRIVATE_KEY_FILE`
- Added `CARTESI_AUTH_KIND` environment variable to select the blockchain authetication method
- Added structured logging with slog. Colored logs can now be enabled with `CARTESI_LOG_PRETTY` environment variable.

### Changed

- Changed `CARTESI_BLOCKCHAIN_ID` type from int to uint64
- Changed `CARTESI_CONTRACTS_APPLICATION_DEPLOYMENT_BLOCK_NUMBER` type from string to int64.
- Changed `CARTESI_LOG_LEVEL` option `warning` to `warn`

### Removed

- Removed `CARTESI_EXPERIMENTAL_DISABLE_CONFIG_LOG` and `CARTESI_LOG_TIMESTAMP` environment variables

## [1.3.1] 2024-03-13

### Added

- Added `CARTESI_EXPERIMENTAL_SERVER_MANAGER_BYPASS_LOG` env var to allow `server-manager` output to bypass all log configuration
- Added `CARTESI_EXPERIMENTAL_DISABLE_CONFIG_LOG` env var to disable log entries related to the node's configuration

## Changed

- Changed CARTESI_BLOCKCHAIN_ID type from int to uint64

## [1.3.0] 2024-02-09

### Added
Expand Down
2 changes: 0 additions & 2 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ ENV PATH=$PATH:/usr/local/go/bin

# Copy gen-devnet script and dependencies
# This should be simplified in the future when `COPY --parents` is made available
COPY internal/config internal/config
COPY internal/services internal/services
COPY pkg/addresses pkg/addresses
COPY pkg/contracts pkg/contracts
Expand Down Expand Up @@ -226,7 +225,6 @@ HEALTHCHECK --interval=1s --timeout=1s --retries=5 \
# Start Anvil.
CMD anvil --block-time 1 --load-state $ANVIL_STATE_PATH


####################################################################################################
# TARGET: rollups-node
#
Expand Down
1 change: 1 addition & 0 deletions build/compose-devnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ services:
CARTESI_CONTRACTS_INPUT_BOX_ADDRESS: "0x59b22D57D4f067708AB0c00552767405926dc768"
CARTESI_CONTRACTS_INPUT_BOX_DEPLOYMENT_BLOCK_NUMBER: "20"
CARTESI_EPOCH_DURATION: "120"
CARTESI_AUTH_KIND: "mnemonic"
CARTESI_AUTH_MNEMONIC: "test test test test test test test test test test test junk"
2 changes: 1 addition & 1 deletion build/compose-node.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
- "10000:10000" # Supervisor
environment:
CARTESI_LOG_LEVEL: "info"
CARTESI_LOG_TIMESTAMP: "false"
CARTESI_LOG_PRETTY: "true"
CARTESI_FEATURE_HOST_MODE: "false"
CARTESI_FEATURE_DISABLE_CLAIMER: "false"
CARTESI_HTTP_ADDRESS: "0.0.0.0"
Expand Down
9 changes: 9 additions & 0 deletions cmd/cartesi-rollups-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@
package main

import (
"log/slog"
"os"

"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root"
"github.com/lmittmann/tint"
)

func main() {
opts := &tint.Options{
Level: slog.LevelInfo,
}
handler := tint.NewHandler(os.Stdout, opts)
logger := slog.New(handler)
slog.SetDefault(logger)

err := root.Cmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
6 changes: 2 additions & 4 deletions cmd/cartesi-rollups-cli/root/deps/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ package deps

import (
"context"
"log/slog"
"os/signal"
"syscall"

"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/internal/deps"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -48,18 +48,16 @@ func init() {
}

func run(cmd *cobra.Command, args []string) {

ctx, cancel := signal.NotifyContext(cmd.Context(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

depsContainers, err := deps.Run(ctx, *depsConfig)
cobra.CheckErr(err)

config.InfoLogger.Println("all deps are up")
slog.Info("All dependencies are up")

<-ctx.Done()

err = deps.Terminate(context.Background(), depsContainers)
cobra.CheckErr(err)

}
15 changes: 8 additions & 7 deletions cmd/cartesi-rollups-cli/root/execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package execute

import (
"log/slog"
"os"

"github.com/Khan/genqlient/graphql"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/pkg/addresses"
"github.com/cartesi/rollups-node/pkg/ethutil"
"github.com/cartesi/rollups-node/pkg/readerclient"
Expand Down Expand Up @@ -70,13 +70,13 @@ func run(cmd *cobra.Command, args []string) {
cobra.CheckErr(err)

if resp.Proof == nil {
config.InfoLogger.Printf("The voucher has no associated proof yet.\n")
slog.Warn("The voucher has no associated proof yet")
os.Exit(0)
}

client, err := ethclient.DialContext(ctx, ethEndpoint)
cobra.CheckErr(err)
config.InfoLogger.Printf("connected to %v\n", ethEndpoint)
slog.Info("Connected", "eth-endpoint", ethEndpoint)

signer, err := ethutil.NewMnemonicSigner(ctx, client, mnemonic, account)
cobra.CheckErr(err)
Expand All @@ -91,9 +91,10 @@ func run(cmd *cobra.Command, args []string) {

proof := readerclient.ConvertToContractProof(resp.Proof)

config.InfoLogger.Printf("executing voucher %d from input %d\n",
voucherIndex,
inputIndex,
slog.Info("Executing voucher",
"voucher-index", voucherIndex,
"input-index", inputIndex,
"application-address", book.CartesiDApp,
)
txHash, err := ethutil.ExecuteVoucher(
ctx,
Expand All @@ -106,5 +107,5 @@ func run(cmd *cobra.Command, args []string) {
)
cobra.CheckErr(err)

config.InfoLogger.Printf("The voucher was executed! (tx=%v)\n", txHash)
slog.Info("Voucher executed", "tx-hash", txHash)
}
2 changes: 0 additions & 2 deletions cmd/cartesi-rollups-cli/root/savesnapshot/savesnapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func init() {
}

func run(cmd *cobra.Command, args []string) {

err := machine.Save(sourceDockerImage, destDir, tempContainerName)

cobra.CheckErr(err)
}
9 changes: 5 additions & 4 deletions cmd/cartesi-rollups-cli/root/send/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
package send

import (
"github.com/cartesi/rollups-node/internal/config"
"log/slog"

"github.com/cartesi/rollups-node/pkg/addresses"
"github.com/cartesi/rollups-node/pkg/ethutil"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -56,7 +57,7 @@ func run(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
client, err := ethclient.DialContext(ctx, ethEndpoint)
cobra.CheckErr(err)
config.InfoLogger.Printf("connected to %v", ethEndpoint)
slog.Info("Connected", "eth-endpoint", ethEndpoint)

signer, err := ethutil.NewMnemonicSigner(ctx, client, mnemonic, account)
cobra.CheckErr(err)
Expand All @@ -69,9 +70,9 @@ func run(cmd *cobra.Command, args []string) {
book = addresses.GetTestBook()
}

config.InfoLogger.Printf("sending input to %x", book.CartesiDApp)
slog.Info("Sending input", "application-address", book.CartesiDApp)
inputIndex, err := ethutil.AddInput(ctx, client, book, signer, payload)
cobra.CheckErr(err)

config.InfoLogger.Printf("added input with index %v", inputIndex)
slog.Info("Input added", "input-index", inputIndex)
}
16 changes: 8 additions & 8 deletions cmd/cartesi-rollups-cli/root/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
package validate

import (
"log/slog"
"os"

"github.com/Khan/genqlient/graphql"
"github.com/cartesi/rollups-node/internal/config"
"github.com/cartesi/rollups-node/pkg/addresses"
"github.com/cartesi/rollups-node/pkg/ethutil"
"github.com/cartesi/rollups-node/pkg/readerclient"
Expand Down Expand Up @@ -62,13 +62,13 @@ func run(cmd *cobra.Command, args []string) {
cobra.CheckErr(err)

if resp.Proof == nil {
config.InfoLogger.Printf("The notice has no associated proof yet.\n")
slog.Warn("The notice has no associated proof yet")
os.Exit(0)
}

client, err := ethclient.DialContext(ctx, ethEndpoint)
cobra.CheckErr(err)
config.InfoLogger.Printf("connected to %v\n", ethEndpoint)
slog.Info("Connected", "eth-endpoint", ethEndpoint)

var book *addresses.Book
if addressBookFile != "" {
Expand All @@ -80,13 +80,13 @@ func run(cmd *cobra.Command, args []string) {

proof := readerclient.ConvertToContractProof(resp.Proof)

config.InfoLogger.Printf("validating notice %d from input %d with address %x\n",
noticeIndex,
inputIndex,
book.CartesiDApp,
slog.Info("Validating notice",
"notice-index", noticeIndex,
"input-index", inputIndex,
"application-address", book.CartesiDApp,
)
err = ethutil.ValidateNotice(ctx, client, book, resp.Payload, proof)
cobra.CheckErr(err)

config.InfoLogger.Printf("The notice is valid!\n")
slog.Info("Notice validated")
}
68 changes: 0 additions & 68 deletions cmd/cartesi-rollups-node/handlers.go

This file was deleted.

Loading
Loading