Skip to content

Commit

Permalink
feat: Implement genesis transaction list command. (#2400)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnavarro authored Jul 2, 2024
1 parent 79a71a4 commit 6a78488
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
1 change: 1 addition & 0 deletions gno.land/cmd/gnoland/genesis_txs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions gno.land/cmd/gnoland/genesis_txs_list.go
Original file line number Diff line number Diff line change
@@ -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] [<arg>...]",
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
}
71 changes: 71 additions & 0 deletions gno.land/cmd/gnoland/genesis_txs_list_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
5 changes: 3 additions & 2 deletions tm2/pkg/amino/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6a78488

Please sign in to comment.