From 852a229f11f0f269021b36f7621609f432bb858b Mon Sep 17 00:00:00 2001 From: Nguyen Nhu Viet Date: Mon, 30 Jan 2023 18:25:17 +0100 Subject: [PATCH] feat: add testRandBlob CLI for testground tests (#1311) ## Overview ATM, `test-infra` repo is using my fork of celestia-app, which is not ok. We need a CLI command for tests to trigger PFBs from apps' side, hence we have added another command named `TestRandBlob` This PR contains another renaming, touching removal of `Wire` prefixes Ref: https://github.com/celestiaorg/test-infra/issues/159 ## Checklist - [x] New and updated code has appropriate documentation - [x] New and updated code has new and/or updated testing - [x] Required CI checks are passing - [x] Visual proof for any user facing features like CLI or documentation updates - [x] Linked issues closed with keywords --------- Co-authored-by: Rootul P Co-authored-by: Evan Forbes <42654277+evan-forbes@users.noreply.github.com> --- .../cli/{wirepayforblob.go => payforblob.go} | 72 ++++++++++--------- x/blob/client/cli/testrandblob.go | 46 ++++++++++++ x/blob/client/cli/tx.go | 3 +- x/blob/client/testutil/integration_test.go | 2 +- 4 files changed, 88 insertions(+), 35 deletions(-) rename x/blob/client/cli/{wirepayforblob.go => payforblob.go} (73%) create mode 100644 x/blob/client/cli/testrandblob.go diff --git a/x/blob/client/cli/wirepayforblob.go b/x/blob/client/cli/payforblob.go similarity index 73% rename from x/blob/client/cli/wirepayforblob.go rename to x/blob/client/cli/payforblob.go index 6e9d39154c..37e6cc5523 100644 --- a/x/blob/client/cli/wirepayforblob.go +++ b/x/blob/client/cli/payforblob.go @@ -18,17 +18,12 @@ import ( coretypes "github.com/tendermint/tendermint/types" ) -func CmdWirePayForBlob() *cobra.Command { +func CmdPayForBlob() *cobra.Command { cmd := &cobra.Command{ Use: "PayForBlobs [hexNamespace] [hexBlob]", Short: "Pay for a data blob to be published to the Celestia blockchain", Args: cobra.ExactArgs(2), RunE: func(cmd *cobra.Command, args []string) error { - clientCtx, err := client.GetClientTxContext(cmd) - if err != nil { - return err - } - // decode the namespace namespace, err := hex.DecodeString(args[0]) if err != nil { @@ -47,41 +42,52 @@ func CmdWirePayForBlob() *cobra.Command { return err } - // TODO: allow the user to override the share version via a new flag - // See https://github.com/celestiaorg/celestia-app/issues/1041 - pfbMsg, err := types.NewMsgPayForBlobs(clientCtx.FromAddress.String(), blob) - if err != nil { - return err - } + return broadcastPFB(cmd, blob) + }, + } - // run message checks - if err = pfbMsg.ValidateBasic(); err != nil { - return err - } + flags.AddTxFlagsToCmd(cmd) - txBytes, err := writeTx(clientCtx, sdktx.NewFactoryCLI(clientCtx, cmd.Flags()), pfbMsg) - if err != nil { - return err - } + return cmd +} - blobTx, err := coretypes.MarshalBlobTx(txBytes, blob) - if err != nil { - return err - } +// broadcastPFB creates the new PFB message type that will later be broadcast to tendermint nodes +// this private func is used in CmdPayForBlob and CmdTestRandBlob +func broadcastPFB(cmd *cobra.Command, blob *types.Blob) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } - // broadcast to a Tendermint node - res, err := clientCtx.BroadcastTx(blobTx) - if err != nil { - return err - } + // TODO: allow the user to override the share version via a new flag + // See https://github.com/celestiaorg/celestia-app/issues/1041 + pfbMsg, err := types.NewMsgPayForBlobs(clientCtx.FromAddress.String(), blob) + if err != nil { + return err + } - return clientCtx.PrintProto(res) - }, + // run message checks + if err = pfbMsg.ValidateBasic(); err != nil { + return err } - flags.AddTxFlagsToCmd(cmd) + txBytes, err := writeTx(clientCtx, sdktx.NewFactoryCLI(clientCtx, cmd.Flags()), pfbMsg) + if err != nil { + return err + } - return cmd + blobTx, err := coretypes.MarshalBlobTx(txBytes, blob) + if err != nil { + return err + } + + // broadcast to a Tendermint node + res, err := clientCtx.BroadcastTx(blobTx) + if err != nil { + return err + } + + return clientCtx.PrintProto(res) } // writeTx attempts to generate and sign a transaction using the normal diff --git a/x/blob/client/cli/testrandblob.go b/x/blob/client/cli/testrandblob.go new file mode 100644 index 0000000000..568b4875db --- /dev/null +++ b/x/blob/client/cli/testrandblob.go @@ -0,0 +1,46 @@ +package cli + +import ( + "fmt" + "strconv" + + "github.com/celestiaorg/celestia-app/testutil/namespace" + "github.com/celestiaorg/celestia-app/testutil/testfactory" + "github.com/celestiaorg/celestia-app/x/blob/types" + + "github.com/spf13/cobra" + + "github.com/cosmos/cosmos-sdk/client/flags" +) + +// CmdTestRandBlob is triggered by testground's tests as part of apps' node scenario +// to increase the block size by user-defined amount. +// +// CAUTION: This func should not be used in production env! +func CmdTestRandBlob() *cobra.Command { + cmd := &cobra.Command{ + Use: "TestRandBlob [blobSize]", + Short: "Generates a random blob for a random namespace to be published to the Celestia blockchain", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + // decode the blob size + size, err := strconv.Atoi(args[0]) + if err != nil { + return fmt.Errorf("failure to decode blob size: %w", err) + } + + nid := namespace.RandomBlobNamespace() + coreBlob := testfactory.GenerateBlobsWithNamespace(1, size, nid) + blob, err := types.NewBlob(coreBlob[0].NamespaceID, coreBlob[0].Data) + if err != nil { + return fmt.Errorf("failure on generating random blob: %w", err) + } + + return broadcastPFB(cmd, blob) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/blob/client/cli/tx.go b/x/blob/client/cli/tx.go index 894f55b82c..88b5a5d563 100644 --- a/x/blob/client/cli/tx.go +++ b/x/blob/client/cli/tx.go @@ -21,7 +21,8 @@ func GetTxCmd() *cobra.Command { RunE: client.ValidateCmd, } - cmd.AddCommand(CmdWirePayForBlob()) + cmd.AddCommand(CmdPayForBlob()) + cmd.AddCommand(CmdTestRandBlob()) return cmd } diff --git a/x/blob/client/testutil/integration_test.go b/x/blob/client/testutil/integration_test.go index 7f8e12db52..a4ba2f3c3c 100644 --- a/x/blob/client/testutil/integration_test.go +++ b/x/blob/client/testutil/integration_test.go @@ -91,7 +91,7 @@ func (s *IntegrationTestSuite) TestSubmitWirePayForBlob() { tc := tc s.Require().NoError(s.network.WaitForNextBlock()) s.Run(tc.name, func() { - cmd := paycli.CmdWirePayForBlob() + cmd := paycli.CmdPayForBlob() clientCtx := val.ClientCtx out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args)