RPC/GetBlock: check provided encoding before sending request.

This commit is contained in:
Slavomir 2021-07-25 22:04:49 +02:00
parent 4116a9ebd8
commit 28f7660fb8
2 changed files with 22 additions and 0 deletions

View File

@ -298,3 +298,13 @@ const (
EncodingJSON EncodingType = "json" // NOTE: not usable in almost-all places.
)
// IsAnyOfEncodingType checks whether the provided `candidate` is any of the `allowed`.
func IsAnyOfEncodingType(candidate EncodingType, allowed ...EncodingType) bool {
for _, v := range allowed {
if candidate == v {
return true
}
}
return false
}

View File

@ -2,6 +2,7 @@ package rpc
import (
"context"
"fmt"
bin "github.com/dfuse-io/binary"
"github.com/gagliardetto/solana-go"
@ -69,6 +70,7 @@ func (cl *Client) GetBlockWithOpts(
slot uint64,
opts *GetBlockOpts,
) (out *GetBlockResult, err error) {
obj := M{
"encoding": solana.EncodingJSON,
}
@ -84,6 +86,16 @@ func (cl *Client) GetBlockWithOpts(
obj["commitment"] = opts.Commitment
}
if opts.Encoding != "" {
if !solana.IsAnyOfEncodingType(
opts.Encoding,
// Valid encodings:
solana.EncodingJSON,
solana.EncodingJSONParsed,
solana.EncodingBase58,
solana.EncodingBase64,
) {
return nil, fmt.Errorf("provided encoding is not supported: %s", opts.Encoding)
}
obj["encoding"] = opts.Encoding
}
}