diff --git a/x/blob/client/cli/payforblob.go b/x/blob/client/cli/payforblob.go index fdf19cc568..9ff4080205 100644 --- a/x/blob/client/cli/payforblob.go +++ b/x/blob/client/cli/payforblob.go @@ -19,9 +19,15 @@ import ( coretypes "github.com/tendermint/tendermint/types" ) -// FlagShareVersion allows the user to override the share version when -// submitting a PayForBlob. -const FlagShareVersion = "share-version" +const ( + // FlagShareVersion allows the user to override the share version when + // submitting a PayForBlob. + FlagShareVersion = "share-version" + + // FlagNamespaceVersion allows the user to override the namespace version when + // submitting a PayForBlob. + FlagNamespaceVersion = "namespace-version" +) func CmdPayForBlob() *cobra.Command { cmd := &cobra.Command{ @@ -37,12 +43,13 @@ func CmdPayForBlob() *cobra.Command { if err != nil { return fmt.Errorf("failure to decode hex namespace ID: %w", err) } - - // TODO: allow the user to override the namespace version via a new flag - // See https://github.com/celestiaorg/celestia-app/issues/1528 - namespace, err := appns.New(appns.NamespaceVersionZero, append(appns.NamespaceVersionZeroPrefix, namespaceID...)) + namespaceVersion, err := cmd.Flags().GetUint8(FlagNamespaceVersion) + if err != nil { + return err + } + namespace, err := getNamespace(namespaceID, namespaceVersion) if err != nil { - return fmt.Errorf("failure to create namespace: %w", err) + return err } shareVersion, _ := cmd.Flags().GetUint8(FlagShareVersion) @@ -63,11 +70,23 @@ func CmdPayForBlob() *cobra.Command { } flags.AddTxFlagsToCmd(cmd) + cmd.PersistentFlags().Uint8(FlagNamespaceVersion, 0, "Specify the namespace version") cmd.PersistentFlags().Uint8(FlagShareVersion, 0, "Specify the share version") - return cmd } +func getNamespace(namespaceID []byte, namespaceVersion uint8) (appns.Namespace, error) { + switch namespaceVersion { + case appns.NamespaceVersionZero: + id := make([]byte, 0, appns.NamespaceIDSize) + id = append(id, appns.NamespaceVersionZeroPrefix...) + id = append(id, namespaceID...) + return appns.New(namespaceVersion, id) + default: + return appns.Namespace{}, fmt.Errorf("namespace version %d is not supported", namespaceVersion) + } +} + // 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 { diff --git a/x/blob/client/testutil/integration_test.go b/x/blob/client/testutil/integration_test.go index 091c633780..358d4b8fd4 100644 --- a/x/blob/client/testutil/integration_test.go +++ b/x/blob/client/testutil/integration_test.go @@ -1,6 +1,7 @@ package testutil import ( + "bytes" "encoding/hex" "fmt" "strconv" @@ -63,8 +64,8 @@ func (s *IntegrationTestSuite) TestSubmitPayForBlob() { require := s.Require() val := s.network.Validators[0] hexNamespace := hex.EncodeToString(appns.RandomBlobNamespaceID()) + invalidNamespaceID := hex.EncodeToString(bytes.Repeat([]byte{0}, 8)) // invalid because ID is expected to be 10 bytes - // some hex blob hexBlob := "0204033704032c0b162109000908094d425837422c2116" testCases := []struct { @@ -103,6 +104,35 @@ func (s *IntegrationTestSuite) TestSubmitPayForBlob() { expectedCode: 0, respType: &sdk.TxResponse{}, }, + { + name: "invalid namespace ID", + args: []string{ + invalidNamespaceID, + hexBlob, + fmt.Sprintf("--from=%s", username), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(2))).String()), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + }, + expectErr: true, + expectedCode: 0, + respType: &sdk.TxResponse{}, + }, + { + name: "invalid namespace version", + args: []string{ + hexNamespace, + hexBlob, + fmt.Sprintf("--from=%s", username), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(2))).String()), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=1", cli.FlagNamespaceVersion), + }, + expectErr: true, + expectedCode: 0, + respType: &sdk.TxResponse{}, + }, } for _, tc := range testCases {