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

feat: Embed genesis files. #2129

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 12 additions & 10 deletions gno.land/cmd/gnoland/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import (
"errors"
"flag"
"fmt"
"log/slog"
"path/filepath"
"strings"
"time"

"go.uber.org/zap/zapcore"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/gno.land/pkg/log"
"github.com/gnolang/gno/gnovm/pkg/gnoenv"
Expand All @@ -25,7 +28,6 @@ import (
osm "github.com/gnolang/gno/tm2/pkg/os"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/telemetry"
"go.uber.org/zap/zapcore"
)

var startGraphic = strings.ReplaceAll(`
Expand Down Expand Up @@ -75,8 +77,6 @@ func newStartCmd(io commands.IO) *commands.Command {

func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
gnoroot := gnoenv.RootDir()
defaultGenesisBalancesFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_balances.txt")
defaultGenesisTxsFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_txs.jsonl")

fs.BoolVar(
&c.skipFailingGenesisTxs,
Expand All @@ -95,14 +95,14 @@ func (c *startCfg) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(
&c.genesisBalancesFile,
"genesis-balances-file",
defaultGenesisBalancesFile,
"",
"initial distribution file",
)

fs.StringVar(
&c.genesisTxsFile,
"genesis-txs-file",
defaultGenesisTxsFile,
"",
"initial txs to replay",
)

Expand Down Expand Up @@ -241,12 +241,18 @@ func execStart(c *startCfg, io commands.IO) error {
// Initialize the log format
logFormat := log.Format(strings.ToLower(c.logFormat))

if logFormat != log.JSONFormat {
io.Println(startGraphic)
}

// Initialize the zap logger
zapLogger := log.GetZapLoggerFn(logFormat)(io.Out(), logLevel)

// Wrap the zap logger
logger := log.ZapLoggerToSlog(zapLogger)

slog.SetDefault(logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this. I don't care if we use the slog default logger, but we should use it consistently throughout or not use it at all. I don't want to mix slog.Default and non-default logger.


// Initialize telemetry
telemetry.Init(*cfg.Telemetry)

Expand Down Expand Up @@ -281,17 +287,13 @@ func execStart(c *startCfg, io commands.IO) error {
}
cfg.LocalApp = gnoApp

if logFormat != log.JSONFormat {
io.Println(startGraphic)
}

gnoNode, err := node.DefaultNewNode(cfg, genesisPath, logger)
if err != nil {
return fmt.Errorf("error in creating node: %w", err)
}

if c.skipStart {
io.ErrPrintln("'--skip-start' is set. Exiting.")
io.ErrPrintln("'-skip-start' is set. Exiting.")
return nil
}

Expand Down
9 changes: 9 additions & 0 deletions gno.land/genesis/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package genesis

import _ "embed"

//go:embed genesis_balances.txt
var DefaultGenesisBalances []byte

//go:embed genesis_txs.jsonl
var DefaultGenesisTxs []byte
33 changes: 30 additions & 3 deletions gno.land/pkg/gnoland/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package gnoland
import (
"errors"
"fmt"
"log/slog"
"strings"

"github.com/gnolang/gno/gno.land/genesis"
vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/gnovm/pkg/gnomod"
Expand All @@ -17,8 +19,20 @@ import (

// LoadGenesisBalancesFile loads genesis balances from the provided file path.
func LoadGenesisBalancesFile(path string) ([]Balance, error) {
var content []byte
var err error
if path == "" {
slog.Warn("genesis file not specified, using embedded defaults")
content = genesis.DefaultGenesisBalances
} else {
content, err = osm.ReadFile(path)
}

if err != nil {
return nil, err
}

// each balance is in the form: g1xxxxxxxxxxxxxxxx=100000ugnot
content := osm.MustReadFile(path)
lines := strings.Split(string(content), "\n")

balances := make([]Balance, 0, len(lines))
Expand Down Expand Up @@ -61,9 +75,22 @@ func LoadGenesisBalancesFile(path string) ([]Balance, error) {
// LoadGenesisTxsFile loads genesis transactions from the provided file path.
// XXX: Improve the way we generate and load this file
func LoadGenesisTxsFile(path string, chainID string, genesisRemote string) ([]std.Tx, error) {
txs := []std.Tx{}
txsBz := osm.MustReadFile(path)
var txsBz []byte
var err error
if path == "" {
slog.Warn("genesis transactions file not specified, using embedded defaults")
txsBz = genesis.DefaultGenesisTxs
} else {
txsBz, err = osm.ReadFile(path)
}

if err != nil {
return nil, err
}

txsLines := strings.Split(string(txsBz), "\n")

txs := []std.Tx{}
for _, txLine := range txsLines {
if txLine == "" {
continue // Skip empty line.
Expand Down
25 changes: 25 additions & 0 deletions gno.land/pkg/gnoland/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gnoland

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestBalances(t *testing.T) {
b, err := LoadGenesisBalancesFile("")
require.NoError(t, err)
require.Len(t, b, 56)

_, err = LoadGenesisBalancesFile("DOES_NOT_EXIST")
require.Error(t, err)
}

func TestTransactions(t *testing.T) {
b, err := LoadGenesisTxsFile("", "id", "remote")
require.NoError(t, err)
require.Len(t, b, 17)

_, err = LoadGenesisTxsFile("DOES_NOT_EXIST", "id", "remote")
require.Error(t, err)
}
7 changes: 3 additions & 4 deletions gno.land/pkg/integration/testing_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import (
"path/filepath"
"time"

"github.com/stretchr/testify/require"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
abci "github.com/gnolang/gno/tm2/pkg/bft/abci/types"
tmcfg "github.com/gnolang/gno/tm2/pkg/bft/config"
"github.com/gnolang/gno/tm2/pkg/bft/node"
bft "github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/stretchr/testify/require"
)

const (
Expand Down Expand Up @@ -123,9 +124,7 @@ func LoadDefaultPackages(t TestingTS, creator bft.Address, gnoroot string) []std

// LoadDefaultGenesisBalanceFile loads the default genesis balance file for testing.
func LoadDefaultGenesisBalanceFile(t TestingTS, gnoroot string) []gnoland.Balance {
balanceFile := filepath.Join(gnoroot, "gno.land", "genesis", "genesis_balances.txt")

genesisBalances, err := gnoland.LoadGenesisBalancesFile(balanceFile)
genesisBalances, err := gnoland.LoadGenesisBalancesFile("")
require.NoError(t, err)

return genesisBalances
Expand Down
Loading