diff --git a/gno.land/cmd/gnoland/genesis_txs.go b/gno.land/cmd/gnoland/genesis_txs.go index 57b4ba93864..46b8d1bd29c 100644 --- a/gno.land/cmd/gnoland/genesis_txs.go +++ b/gno.land/cmd/gnoland/genesis_txs.go @@ -35,6 +35,7 @@ func newTxsCmd(io commands.IO) *commands.Command { newTxsAddCmd(cfg, io), newTxsRemoveCmd(cfg, io), newTxsExportCmd(cfg, io), + newTxsListCmd(cfg, io), ) return cmd diff --git a/gno.land/cmd/gnoland/genesis_txs_list.go b/gno.land/cmd/gnoland/genesis_txs_list.go new file mode 100644 index 00000000000..c68fbc30803 --- /dev/null +++ b/gno.land/cmd/gnoland/genesis_txs_list.go @@ -0,0 +1,55 @@ +package main + +import ( + "bytes" + "context" + "errors" + "fmt" + + "github.com/gnolang/gno/gno.land/pkg/gnoland" + "github.com/gnolang/gno/tm2/pkg/amino" + "github.com/gnolang/gno/tm2/pkg/bft/types" + "github.com/gnolang/gno/tm2/pkg/commands" +) + +var ErrWrongGenesisType = errors.New("genesis state is not using the correct Gno Genesis type") + +// newTxsListCmd list all transactions on the specified genesis file +func newTxsListCmd(txsCfg *txsCfg, io commands.IO) *commands.Command { + cmd := commands.NewCommand( + commands.Metadata{ + Name: "list", + ShortUsage: "txs list [flags] [...]", + ShortHelp: "lists transactions existing on genesis.json", + LongHelp: "Lists transactions existing on genesis.json", + }, + commands.NewEmptyConfig(), + func(ctx context.Context, args []string) error { + return execTxsListCmd(io, txsCfg) + }, + ) + + return cmd +} + +func execTxsListCmd(io commands.IO, cfg *txsCfg) error { + genesis, err := types.GenesisDocFromFile(cfg.genesisPath) + if err != nil { + return fmt.Errorf("%w, %w", errUnableToLoadGenesis, err) + } + + gs, ok := genesis.AppState.(gnoland.GnoGenesisState) + if !ok { + return ErrWrongGenesisType + } + + b, err := amino.MarshalJSONIndent(gs.Txs, "", " ") + if err != nil { + return errors.New("error marshalling data to amino JSON") + } + + buf := bytes.NewBuffer(b) + _, err = buf.WriteTo(io.Out()) + + return err +} diff --git a/gno.land/cmd/gnoland/genesis_txs_list_test.go b/gno.land/cmd/gnoland/genesis_txs_list_test.go new file mode 100644 index 00000000000..d18c2f4d641 --- /dev/null +++ b/gno.land/cmd/gnoland/genesis_txs_list_test.go @@ -0,0 +1,71 @@ +package main + +import ( + "bytes" + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/gnolang/gno/gno.land/pkg/gnoland" + "github.com/gnolang/gno/tm2/pkg/commands" + "github.com/gnolang/gno/tm2/pkg/testutils" +) + +func TestGenesis_List_All(t *testing.T) { + t.Parallel() + + t.Run("invalid genesis path", func(t *testing.T) { + t.Parallel() + + // Create the command + cmd := newRootCmd(commands.NewTestIO()) + args := []string{ + "genesis", + "txs", + "list", + "--genesis-path", + "", + } + + // Run the command + cmdErr := cmd.ParseAndRun(context.Background(), args) + assert.ErrorIs(t, cmdErr, errUnableToLoadGenesis) + }) + + t.Run("list all txs", func(t *testing.T) { + t.Parallel() + + tempGenesis, cleanup := testutils.NewTestFile(t) + t.Cleanup(cleanup) + + // Generate dummy txs + txs := generateDummyTxs(t, 10) + + genesis := getDefaultGenesis() + genesis.AppState = gnoland.GnoGenesisState{ + Txs: txs, + } + require.NoError(t, genesis.SaveAs(tempGenesis.Name())) + + cio := commands.NewTestIO() + buf := bytes.NewBuffer(nil) + cio.SetOut(commands.WriteNopCloser(buf)) + + cmd := newRootCmd(cio) + args := []string{ + "genesis", + "txs", + "list", + "--genesis-path", + tempGenesis.Name(), + } + + // Run the command + cmdErr := cmd.ParseAndRun(context.Background(), args) + require.NoError(t, cmdErr) + + require.Len(t, buf.String(), 4442) + }) +} diff --git a/tm2/pkg/amino/amino.go b/tm2/pkg/amino/amino.go index 1ca3427b85c..ecff955a582 100644 --- a/tm2/pkg/amino/amino.go +++ b/tm2/pkg/amino/amino.go @@ -11,11 +11,12 @@ import ( "runtime" "time" - "github.com/gnolang/gno/tm2/pkg/amino/pkg" - "github.com/gnolang/gno/tm2/pkg/errors" "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/durationpb" "google.golang.org/protobuf/types/known/timestamppb" + + "github.com/gnolang/gno/tm2/pkg/amino/pkg" + "github.com/gnolang/gno/tm2/pkg/errors" ) // Package "pkg" exists So dependencies can create Packages.