From bcb3b426cff9db6c19fa1d8d2b2c925eb133b407 Mon Sep 17 00:00:00 2001 From: Viacheslav Date: Wed, 9 Aug 2023 11:03:24 +0300 Subject: [PATCH] fix(rpc): fix commands handling (#2515) --- cmd/celestia/blob.go | 16 ++++++++-------- cmd/celestia/rpc.go | 26 ++++++++++++++++++++++++-- share/getters/cascade.go | 8 +++++++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/cmd/celestia/blob.go b/cmd/celestia/blob.go index 9dcd72dcb7..0b94fdcfdc 100644 --- a/cmd/celestia/blob.go +++ b/cmd/celestia/blob.go @@ -14,22 +14,22 @@ import ( "github.com/celestiaorg/celestia-node/share" ) -var plaintext bool +var base64Flag bool func init() { blobCmd.AddCommand(getCmd, getAllCmd, submitCmd, getProofCmd) getCmd.PersistentFlags().BoolVar( - &plaintext, - "plaintext", + &base64Flag, + "base64", false, - "printed blob's data as a plain text", + "printed blob's data a base64 string", ) getAllCmd.PersistentFlags().BoolVar( - &plaintext, - "plaintext", + &base64Flag, + "base64", false, - "printed blob's data as a plain text", + "printed blob's data as a base64 string", ) } @@ -170,7 +170,7 @@ func printOutput(data interface{}, err error) { data = err } - if plaintext && err == nil { + if !base64Flag && err == nil { data = formatData(data) } diff --git a/cmd/celestia/rpc.go b/cmd/celestia/rpc.go index 05439ad7b4..dd50f07fc2 100644 --- a/cmd/celestia/rpc.go +++ b/cmd/celestia/rpc.go @@ -119,9 +119,17 @@ var rpcCmd = &cobra.Command{ func parseParams(method string, params []string) []interface{} { parsedParams := make([]interface{}, len(params)) - + validateParamsFn := func(has, want int) error { + if has != want { + return fmt.Errorf("rpc: invalid amount of params. has=%d, want=%d", has, want) + } + return nil + } switch method { case "GetSharesByNamespace": + if err := validateParamsFn(len(params), 2); err != nil { + panic(err) + } // 1. Share Root root, err := parseJSON(params[0]) if err != nil { @@ -134,8 +142,12 @@ func parseParams(method string, params []string) []interface{} { panic(fmt.Sprintf("Error parsing namespace: %v", err)) } parsedParams[1] = namespace + return parsedParams case "QueryDelegation", "QueryUnbonding", "BalanceForAddress": var err error + if err = validateParamsFn(len(params), 2); err != nil { + panic(err) + } parsedParams[0], err = parseAddressFromString(params[0]) if err != nil { panic(fmt.Errorf("error parsing address: %w", err)) @@ -155,6 +167,9 @@ func parseParams(method string, params []string) []interface{} { case "Transfer", "Delegate", "Undelegate": // 1. Address var err error + if err = validateParamsFn(len(params), 4); err != nil { + panic(err) + } parsedParams[0], err = parseAddressFromString(params[0]) if err != nil { panic(fmt.Errorf("error parsing address: %w", err)) @@ -172,6 +187,9 @@ func parseParams(method string, params []string) []interface{} { case "CancelUnbondingDelegation": // 1. Validator Address var err error + if err = validateParamsFn(len(params), 5); err != nil { + panic(err) + } parsedParams[0], err = parseAddressFromString(params[0]) if err != nil { panic(fmt.Errorf("error parsing address: %w", err)) @@ -186,9 +204,13 @@ func parseParams(method string, params []string) []interface{} { panic("Error parsing gas limit: uint64 could not be parsed.") } parsedParams[4] = num + return parsedParams case "BeginRedelegate": // 1. Source Validator Address var err error + if err = validateParamsFn(len(params), 5); err != nil { + panic(err) + } parsedParams[0], err = parseAddressFromString(params[0]) if err != nil { panic(fmt.Errorf("error parsing address: %w", err)) @@ -207,6 +229,7 @@ func parseParams(method string, params []string) []interface{} { panic("Error parsing gas limit: uint64 could not be parsed.") } parsedParams[4] = num + return parsedParams default: } @@ -228,7 +251,6 @@ func parseParams(method string, params []string) []interface{} { parsedParams[i] = param } } - return parsedParams } diff --git a/share/getters/cascade.go b/share/getters/cascade.go index ef59946666..98f29c87ca 100644 --- a/share/getters/cascade.go +++ b/share/getters/cascade.go @@ -3,6 +3,7 @@ package getters import ( "context" "errors" + "fmt" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/trace" @@ -37,7 +38,12 @@ func (cg *CascadeGetter) GetShare(ctx context.Context, root *share.Root, row, co attribute.Int("col", col), )) defer span.End() - + if row >= len(root.RowRoots) || col >= len(root.ColumnRoots) { + err := fmt.Errorf("cascade/get-share: invalid indexes were provided:rowIndex=%d, colIndex=%d."+ + "squarewidth=%d", row, col, len(root.RowRoots)) + span.RecordError(err) + return nil, err + } get := func(ctx context.Context, get share.Getter) (share.Share, error) { return get.GetShare(ctx, root, row, col) }