Skip to content

Commit

Permalink
feat: Introduce --namespace-version CLI flag (#1585)
Browse files Browse the repository at this point in the history
## Overview

Introduce --namespace-version CLI flag 
- Closes #1528 

I have introduced namespace flag. Currently this supports namespace
version zero only.
I think data structure for storing namespace versions can be updated.
Maybe a map with version details.
For example:
```
Key: Version[0], Value: { NamespaceVersionPrefixSize: ... , NamespaceVersionIDSize: ... }
Key: Version[1], Value: { NamespaceVersionPrefixSize: ... , NamespaceVersionIDSize: ... }
```
instead of constants
```
	NamespaceVersionZero = uint8(0)
	NamespaceVersionZeroPrefixSize = 22
	NamespaceVersionZeroIDSize = NamespaceIDSize - NamespaceVersionZeroPrefixSize
```

Then it would be possible to switch versions easily from cli. same for
share version.

Please let me know if this makes sense or am I missing something.

---------

Co-authored-by: Rootul P <rootulp@gmail.com>
Co-authored-by: Evan Forbes <42654277+evan-forbes@users.noreply.github.com>
Co-authored-by: evan-forbes <evan.samuel.forbes@gmail.com>
  • Loading branch information
4 people authored Apr 19, 2023
1 parent 952edae commit d88adf7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
37 changes: 28 additions & 9 deletions x/blob/client/cli/payforblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
32 changes: 31 additions & 1 deletion x/blob/client/testutil/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testutil

import (
"bytes"
"encoding/hex"
"fmt"
"strconv"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d88adf7

Please sign in to comment.