Skip to content

Commit

Permalink
renaming to parseV0Namespace, dropping refactoring commits
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jun 16, 2023
1 parent 819315b commit beb10d5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
59 changes: 33 additions & 26 deletions cmd/celestia/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,15 @@ func parseParams(method string, params []string) []interface{} {
}
parsedParams[0] = root
// 2. NamespaceID
nID, err := parseNamespaceID(params[1])
nID, err := parseV0NamespaceID(params[1])
if err != nil {
panic(fmt.Sprintf("Error parsing namespace ID: %v", err))
}
parsedParams[1] = nID
case "Submit":
// 1. NamespaceID
var err error
nID, err := parseNamespaceID(params[0])
nID, err := parseV0NamespaceID(params[0])
if err != nil {
panic(fmt.Sprintf("Error parsing namespace ID: %v", err))
}
Expand Down Expand Up @@ -163,7 +163,7 @@ func parseParams(method string, params []string) []interface{} {
}
parsedParams[1] = num
// 3. NamespaceID
nID, err := parseNamespaceID(params[2])
nID, err := parseV0NamespaceID(params[2])
if err != nil {
panic(fmt.Sprintf("Error parsing namespace ID: %v", err))
}
Expand Down Expand Up @@ -202,7 +202,7 @@ func parseParams(method string, params []string) []interface{} {
}
parsedParams[0] = num
// 2. NamespaceID
nID, err := parseNamespaceID(params[1])
nID, err := parseV0NamespaceID(params[1])
if err != nil {
panic(fmt.Sprintf("Error parsing namespace ID: %v", err))
}
Expand All @@ -222,7 +222,7 @@ func parseParams(method string, params []string) []interface{} {
}
parsedParams[0] = num
// 2. NamespaceID
nID, err := parseNamespaceID(params[1])
nID, err := parseV0NamespaceID(params[1])
if err != nil {
panic(fmt.Sprintf("Error parsing namespace ID: %v", err))
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func sendJSONRPCRequest(namespace, method string, params []interface{}) {

rawResponseJSON, err := parseJSON(string(responseBody))
if err != nil {
panic(err)
log.Fatalf("Error parsing JSON-RPC response: %v", err)
}
if printRequest {
output, err := json.MarshalIndent(outputWithRequest{
Expand Down Expand Up @@ -419,34 +419,41 @@ func parseSignatureForHelpstring(methodSig reflect.StructField) string {
return simplifiedSignature
}

func parseNamespaceID(param string) (namespace.ID, error) {
var nID []byte
var err error
// parseV0NamespaceID parses a namespace ID from a base64 or hex string. The param
// is expected to be the user-specified portion of a v0 namespace ID (i.e. the
// last 10 bytes).
func parseV0NamespaceID(param string) (namespace.ID, error) {
userBytes, err := decodeToBytes(param)
if err != nil {
return nil, err
}

if len(userBytes) > appns.NamespaceVersionZeroIDSize {
return nil, fmt.Errorf(
"namespace ID %v is too large to be a v0 namespace, want <= %v bytes",
userBytes, appns.NamespaceVersionZeroIDSize,
)
}
// if the namespace ID is <= 10 bytes, left pad it with 0s
return share.NewNamespaceV0(userBytes)
}

// decodeToBytes decodes a Base64 or hex input string into a byte slice.
func decodeToBytes(param string) ([]byte, error) {
if strings.HasPrefix(param, "0x") {
decoded, err := hex.DecodeString(param[2:])
if err != nil {
return nil, fmt.Errorf("error decoding namespace ID: %w", err)
}
nID = decoded
} else {
// otherwise, it's just a base64 string
nID, err = base64.StdEncoding.DecodeString(param)
if err != nil {
return nil, fmt.Errorf("error decoding namespace ID: %w", err)
}
return decoded, nil
}
// if the namespace ID is <= 10 bytes, add v0 share + namespace prefix and zero pad
if len(nID) <= appns.NamespaceVersionZeroIDSize {
nID, err = share.NewNamespaceV0(nID)
if err != nil {
return nil, err
}
} else if len(nID) < appns.NamespaceSize {
return nil, fmt.Errorf("passed namespace is too large")
// otherwise, it's just a base64 string
decoded, err := base64.StdEncoding.DecodeString(param)
if err != nil {
return nil, fmt.Errorf("error decoding namespace ID: %w", err)
}
return nID, nil
return decoded, nil
}

func parseJSON(param string) (json.RawMessage, error) {
var raw json.RawMessage
err := json.Unmarshal([]byte(param), &raw)
Expand Down
4 changes: 2 additions & 2 deletions cmd/celestia/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_parseNamespaceID(t *testing.T) {
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, // v0 ID prefix
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // namespace ID
},
wantErr: false,
wantErr: true,
},
{
name: "11 byte hex encoded namespace ID returns error",
Expand All @@ -69,7 +69,7 @@ func Test_parseNamespaceID(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got, err := parseNamespaceID(tc.param)
got, err := parseV0NamespaceID(tc.param)
if tc.wantErr {
assert.Error(t, err)
return
Expand Down

0 comments on commit beb10d5

Please sign in to comment.