Skip to content

Commit

Permalink
Remove omitempty from DataSlice fields. (#114)
Browse files Browse the repository at this point in the history
* Remove `omitempty` from `DataSlice` fields.

`offset` and `length` can both be `0` in RPC calls (c.f. https://docs.solana.com/developing/clients/jsonrpc-api#results-27)

The current code stops JSON from encoding the fields if they're `0`, thus breaking RPC calls with:

{"code":-32602,"message":"Invalid params: missing field `offset`.","data":null}

* remove some impossible test cases

Co-authored-by: yihau <[email protected]>
  • Loading branch information
omarkilani and yihau authored Dec 13, 2022
1 parent 25fc86c commit 9928d96
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 133 deletions.
81 changes: 0 additions & 81 deletions rpc/get_account_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,87 +166,6 @@ func TestGetAccountInfo(t *testing.T) {
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getAccountInfo", "params":["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb", {"encoding": "base64+zstd"}]}`,
ResponseBody: `{"jsonrpc":"2.0","result":{"context":{"slot":77317717},"value":{"data":["KLUv/QBYjQEAhAIBAAAABj5w2ZFXmNyj7tuRN89kxw/6+2LN04KBBSUL12sdbN4ACQEAAgAAGXXBEw==","base64+zstd"],"executable":false,"lamports":1461600,"owner":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","rentEpoch":178}},"id":1}`,
RpcCall: func(rc RpcClient) (any, error) {
return rc.GetAccountInfoWithConfig(
context.Background(),
"F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb",
GetAccountInfoConfig{
Encoding: AccountEncodingBase64Zstd,
},
)
},
ExpectedResponse: JsonRpcResponse[GetAccountInfo]{
JsonRpc: "2.0",
Id: 1,
Error: nil,
Result: GetAccountInfo{
Context: Context{
Slot: 77317717,
},
Value: AccountInfo{
Lamports: 1461600,
Owner: "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
Executable: false,
RentEpoch: 178,
Data: []any{"KLUv/QBYjQEAhAIBAAAABj5w2ZFXmNyj7tuRN89kxw/6+2LN04KBBSUL12sdbN4ACQEAAgAAGXXBEw==", "base64+zstd"},
},
},
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getAccountInfo", "params":["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb", {"dataSlice": {"length": 32}}]}`,
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params: missing field` + "`offset`" + `."},"id":1}`,
RpcCall: func(rc RpcClient) (any, error) {
return rc.GetAccountInfoWithConfig(
context.Background(),
"F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb",
GetAccountInfoConfig{
DataSlice: &DataSlice{
Length: 32,
},
},
)
},
ExpectedResponse: JsonRpcResponse[GetAccountInfo]{
JsonRpc: "2.0",
Id: 1,
Error: &JsonRpcError{
Code: -32602,
Message: `Invalid params: missing field` + "`offset`" + `.`,
},
Result: GetAccountInfo{},
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getAccountInfo", "params":["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb", {"dataSlice": {"offset": 4}}]}`,
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params: missing field` + "`length`" + `."},"id":1}`,
RpcCall: func(rc RpcClient) (any, error) {
return rc.GetAccountInfoWithConfig(
context.Background(),
"F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb",
GetAccountInfoConfig{
DataSlice: &DataSlice{
Offset: 4,
},
},
)
},
ExpectedResponse: JsonRpcResponse[GetAccountInfo]{
JsonRpc: "2.0",
Id: 1,
Error: &JsonRpcError{
Code: -32602,
Message: `Invalid params: missing field` + "`length`" + `.`,
},
Result: GetAccountInfo{},
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getAccountInfo", "params":["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb", {"dataSlice": {"offset": 4, "length": 32}}]}`,
ResponseBody: `{"jsonrpc":"2.0","result":{"context":{"slot":77322439},"value":{"data":"RNfp4xTbBb4C3kcv2KqtAj8mu4YhMHxqm1Skg9uchZ7","executable":false,"lamports":1461600,"owner":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","rentEpoch":178}},"id":1}`,
Expand Down
50 changes: 0 additions & 50 deletions rpc/get_multiple_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,56 +210,6 @@ func TestGetMultipleAccounts(t *testing.T) {
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getMultipleAccounts", "params":[["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb"], {"dataSlice": {"length": 32}}]}`,
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params: missing field` + "`offset`" + `."},"id":1}`,
RpcCall: func(rc RpcClient) (any, error) {
return rc.GetMultipleAccountsWithConfig(
context.Background(),
[]string{"F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb"},
GetMultipleAccountsConfig{
DataSlice: &DataSlice{
Length: 32,
},
},
)
},
ExpectedResponse: JsonRpcResponse[GetMultipleAccounts]{
JsonRpc: "2.0",
Id: 1,
Error: &JsonRpcError{
Code: -32602,
Message: `Invalid params: missing field` + "`offset`" + `.`,
},
Result: GetMultipleAccounts{},
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getMultipleAccounts", "params":[["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb"], {"dataSlice": {"offset": 4}}]}`,
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params: missing field` + "`length`" + `."},"id":1}`,
RpcCall: func(rc RpcClient) (any, error) {
return rc.GetMultipleAccountsWithConfig(
context.Background(),
[]string{"F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb"},
GetMultipleAccountsConfig{
DataSlice: &DataSlice{
Offset: 4,
},
},
)
},
ExpectedResponse: JsonRpcResponse[GetMultipleAccounts]{
JsonRpc: "2.0",
Id: 1,
Error: &JsonRpcError{
Code: -32602,
Message: `Invalid params: missing field` + "`length`" + `.`,
},
Result: GetMultipleAccounts{},
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getMultipleAccounts", "params":[["F5RYi7FMPefkc7okJNh21Hcsch7RUaLVr8Rzc8SQqxUb"], {"dataSlice": {"offset": 4, "length": 32}}]}`,
ResponseBody: `{"jsonrpc":"2.0","result":{"context":{"slot":77322439},"value":[{"data":"RNfp4xTbBb4C3kcv2KqtAj8mu4YhMHxqm1Skg9uchZ7","executable":false,"lamports":1461600,"owner":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","rentEpoch":178}]},"id":1}`,
Expand Down
4 changes: 2 additions & 2 deletions rpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const (
)

type DataSlice struct {
Offset uint64 `json:"offset,omitempty"`
Length uint64 `json:"length,omitempty"`
Offset uint64 `json:"offset"`
Length uint64 `json:"length"`
}

type AccountInfo struct {
Expand Down

0 comments on commit 9928d96

Please sign in to comment.