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: implement txs generate and txs clear command #1988

Closed
wants to merge 15 commits into from
4 changes: 4 additions & 0 deletions gno.land/cmd/gnoland/genesis.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"errors"
"flag"

"github.com/gnolang/gno/tm2/pkg/commands"
)

var errUnableToLoadGenesis = errors.New("unable to load genesis")

func newGenesisCmd(io commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
Expand All @@ -23,6 +26,7 @@ func newGenesisCmd(io commands.IO) *commands.Command {
newValidatorCmd(io),
newVerifyCmd(io),
newBalancesCmd(io),
newPackagesCmd(io),
newTxsCmd(io),
)

Expand Down
3 changes: 3 additions & 0 deletions gno.land/cmd/gnoland/genesis_balances.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"errors"
"flag"

"github.com/gnolang/gno/tm2/pkg/commands"
)

var errBalanceNotFound = errors.New("genesis balances entry does not exist")

type balancesCfg struct {
commonCfg
}
Expand Down
6 changes: 0 additions & 6 deletions gno.land/cmd/gnoland/genesis_balances_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"errors"
"flag"
"fmt"

Expand All @@ -12,11 +11,6 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto"
)

var (
errUnableToLoadGenesis = errors.New("unable to load genesis")
errBalanceNotFound = errors.New("genesis balances entry does not exist")
)

type balancesRemoveCfg struct {
rootCfg *balancesCfg

Expand Down
50 changes: 50 additions & 0 deletions gno.land/cmd/gnoland/genesis_packages.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"flag"

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto"
"github.com/gnolang/gno/tm2/pkg/std"
)

var (
test1 = crypto.MustAddressFromString("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
defaultFee = std.NewFee(50000, std.MustParseCoin("1000000ugnot"))
)

const msgAddPkg = "add_package"

type packagesCfg struct {
commonCfg
}

// newPackagesCmd creates the genesis packages subcommand
func newPackagesCmd(io commands.IO) *commands.Command {
cfg := &packagesCfg{}

cmd := commands.NewCommand(
commands.Metadata{
Name: "packages",
ShortUsage: "packages <subcommand> [flags]",
ShortHelp: "manages genesis.json packages",
LongHelp: "Manipulates the initial genesis.json packages",
},
cfg,
commands.HelpExec,
)

cmd.AddSubCommands(
newPackagesAddCmd(cfg, io),
newPackagesClearCmd(cfg, io),
newPackagesListCmd(cfg, io),
newPackagesGetCmd(cfg, io),
newPackagesDelCmd(cfg, io),
)

return cmd
}

func (c *packagesCfg) RegisterFlags(fs *flag.FlagSet) {
c.commonCfg.RegisterFlags(fs)
}
94 changes: 94 additions & 0 deletions gno.land/cmd/gnoland/genesis_packages_add.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"context"
"errors"
"flag"
"fmt"

vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
)

type packagesAddCfg struct {
rootCfg *packagesCfg
}

var errUnableToLoadPackages = errors.New("unable to load packages")

// newPackagesAddCmd creates the genesis packages add subcommand
func newPackagesAddCmd(rootCfg *packagesCfg, io commands.IO) *commands.Command {
cfg := &packagesAddCfg{
rootCfg: rootCfg,
}

return commands.NewCommand(
commands.Metadata{
Name: "add",
ShortUsage: "packages add [flags] <path> [<path>...]",
ShortHelp: "adds new package(s) to the genesis.json",
},
cfg,
func(ctx context.Context, args []string) error {
return execPackagesAdd(cfg, args, io)
},
)
}

func (c *packagesAddCfg) RegisterFlags(fs *flag.FlagSet) {}

func execPackagesAdd(cfg *packagesAddCfg, args []string, io commands.IO) error {
if len(args) < 1 {
return flag.ErrHelp
}

// Load the genesis
genesis, err := types.GenesisDocFromFile(cfg.rootCfg.genesisPath)
if err != nil {
return fmt.Errorf("unable to load genesis, %w", err)
}

txs, err := gnoland.LoadPackagesFromDirs(args, test1, defaultFee, nil)
if err != nil {
return fmt.Errorf("%w: %w", errUnableToLoadPackages, err)
}

// Initialize genesis app state if it is not initialized already
if genesis.AppState == nil {
genesis.AppState = gnoland.GnoGenesisState{}
}

state := genesis.AppState.(gnoland.GnoGenesisState)
state.Txs = append(state.Txs, txs...)

// Save the txs
genesis.AppState = state

// Save the updated genesis
if err := genesis.SaveAs(cfg.rootCfg.genesisPath); err != nil {
return fmt.Errorf("unable to save genesis.json, %w", err)

Check warning on line 72 in gno.land/cmd/gnoland/genesis_packages_add.go

View check run for this annotation

Codecov / codecov/patch

gno.land/cmd/gnoland/genesis_packages_add.go#L72

Added line #L72 was not covered by tests
}

// Print packages and files
for _, tx := range txs {
for _, msg := range tx.Msgs {
msgAddPkg := msg.(vmm.MsgAddPackage)
io.Println(msgAddPkg.Package.Path)
for _, file := range msgAddPkg.Package.Files {
io.Printfln("\t- %s", file.Name)
}
}
}

io.Println()

io.Printfln(
"%d txs added!",
len(txs),
)

return nil
}
123 changes: 123 additions & 0 deletions gno.land/cmd/gnoland/genesis_packages_add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"context"
"flag"
"testing"

"github.com/gnolang/gno/gno.land/pkg/gnoland"
vmm "github.com/gnolang/gno/gno.land/pkg/sdk/vm"
"github.com/gnolang/gno/tm2/pkg/bft/types"
"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGenesis_Packages_Add(t *testing.T) {
t.Parallel()

t.Run("invalid genesis", func(t *testing.T) {
t.Parallel()

// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"packages",
"add",
"./",
"--genesis-path",
"dummy-path",
}

// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.ErrorContains(t, cmdErr, errUnableToLoadGenesis.Error())
})

t.Run("missing args", func(t *testing.T) {
t.Parallel()

// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"packages",
"add",
"--genesis-path",
"dummy-path",
}

// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.ErrorContains(t, cmdErr, flag.ErrHelp.Error())
})

t.Run("invalid dir", func(t *testing.T) {
t.Parallel()

tempGenesis, cleanup := testutils.NewTestFile(t)
t.Cleanup(cleanup)

genesis := getDefaultGenesis()
require.NoError(t, genesis.SaveAs(tempGenesis.Name()))

// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"packages",
"add",
"invalid",
"--genesis-path",
tempGenesis.Name(),
}

// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.ErrorContains(t, cmdErr, errUnableToLoadPackages.Error())
})

t.Run("packages from dir", func(t *testing.T) {
t.Parallel()

tempGenesis, cleanup := testutils.NewTestFile(t)
t.Cleanup(cleanup)

genesis := getDefaultGenesis()
require.NoError(t, genesis.SaveAs(tempGenesis.Name()))

// Create the command
cmd := newRootCmd(commands.NewTestIO())
args := []string{
"genesis",
"packages",
"add",
"../../../examples/gno.land/p/demo/avl",
"--genesis-path",
tempGenesis.Name(),
}

// Run the command
cmdErr := cmd.ParseAndRun(context.Background(), args)
require.NoError(t, cmdErr)

// Validate the genesis was updated
genesis, loadErr := types.GenesisDocFromFile(tempGenesis.Name())
require.NoError(t, loadErr)

require.NotNil(t, genesis.AppState)

state, ok := genesis.AppState.(gnoland.GnoGenesisState)
require.True(t, ok)

require.Equal(t, 1, len(state.Txs))
require.Equal(t, 1, len(state.Txs[0].Msgs))

msgAddPkg, ok := state.Txs[0].Msgs[0].(vmm.MsgAddPackage)
require.True(t, ok)

assert.Equal(t, "gno.land/p/demo/avl", msgAddPkg.Package.Path)
})
}
Loading
Loading