From 9928d96814efc4cfbe9deb9c4ed7e991c0541a76 Mon Sep 17 00:00:00 2001 From: Omar Kilani Date: Mon, 12 Dec 2022 22:26:04 -0800 Subject: [PATCH] Remove `omitempty` from `DataSlice` fields. (#114) * 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 --- rpc/get_account_info_test.go | 81 ------------------------------- rpc/get_multiple_accounts_test.go | 50 ------------------- rpc/types.go | 4 +- 3 files changed, 2 insertions(+), 133 deletions(-) diff --git a/rpc/get_account_info_test.go b/rpc/get_account_info_test.go index ab72a11b..74475e3e 100644 --- a/rpc/get_account_info_test.go +++ b/rpc/get_account_info_test.go @@ -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}`, diff --git a/rpc/get_multiple_accounts_test.go b/rpc/get_multiple_accounts_test.go index 9370b242..42e8a895 100644 --- a/rpc/get_multiple_accounts_test.go +++ b/rpc/get_multiple_accounts_test.go @@ -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}`, diff --git a/rpc/types.go b/rpc/types.go index c89d5ccd..9e32e541 100644 --- a/rpc/types.go +++ b/rpc/types.go @@ -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 {