-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement genesis transaction list command. (#2400)
- Loading branch information
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters