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 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
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

Check warning on line 43 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#L43

Added line #L43 was not covered by tests
}

b, err := amino.MarshalJSONIndent(gs.Txs, "", " ")
if err != nil {
return errors.New("error marshalling data to amino JSON")

Check warning on line 48 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#L48

Added line #L48 was not covered by tests
}

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
Loading