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 genesis transaction list command. #2400

Merged
merged 5 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
47 changes: 47 additions & 0 deletions gno.land/cmd/gnoland/genesis_txs_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"context"
"encoding/json"
"fmt"

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

// 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("unable to load genesis, %w", err)
}

gs, ok := genesis.AppState.(gnoland.GnoGenesisState)
if !ok {
return fmt.Errorf("genesis state is not using the correct Gno Genesis type")

Check warning on line 39 in gno.land/cmd/gnoland/genesis_txs_list.go

View check run for this annotation

Codecov / codecov/patch

gno.land/cmd/gnoland/genesis_txs_list.go#L39

Added line #L39 was not covered by tests
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
}

je := json.NewEncoder(io.Out())

je.SetIndent("", " ")

return je.Encode(gs.Txs)
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
}
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.ErrorContains(t, cmdErr, "unable to load genesis")
ajnavarro marked this conversation as resolved.
Show resolved Hide resolved
})

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(), 14743)
})
}
Loading