From 18b97be92a4238ff24538905b42428b7cc1b28f0 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 12 Sep 2024 13:22:59 -0700 Subject: [PATCH] Modify omitempty fields to be pointer-based --- .../integrationtest/infrastructure/client.go | 12 ++++++----- .../simulate_transaction_test.go | 20 +++++++++---------- .../internal/methods/simulate_transaction.go | 10 ++++++---- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cmd/soroban-rpc/internal/integrationtest/infrastructure/client.go b/cmd/soroban-rpc/internal/integrationtest/infrastructure/client.go index 9b08bb52..7621286a 100644 --- a/cmd/soroban-rpc/internal/integrationtest/infrastructure/client.go +++ b/cmd/soroban-rpc/internal/integrationtest/infrastructure/client.go @@ -159,11 +159,13 @@ func PreflightTransactionParamsLocally(t *testing.T, params txnbuild.Transaction SorobanData: &transactionData, } var auth []xdr.SorobanAuthorizationEntry - for _, b64 := range response.Results[0].AuthXDR { - var a xdr.SorobanAuthorizationEntry - err := xdr.SafeUnmarshalBase64(b64, &a) - require.NoError(t, err) - auth = append(auth, a) + if response.Results[0].AuthXDR != nil { + for _, b64 := range *response.Results[0].AuthXDR { + var a xdr.SorobanAuthorizationEntry + err := xdr.SafeUnmarshalBase64(b64, &a) + require.NoError(t, err) + auth = append(auth, a) + } } v.Auth = auth case *txnbuild.ExtendFootprintTtl: diff --git a/cmd/soroban-rpc/internal/integrationtest/simulate_transaction_test.go b/cmd/soroban-rpc/internal/integrationtest/simulate_transaction_test.go index c64f1bea..67cbf08a 100644 --- a/cmd/soroban-rpc/internal/integrationtest/simulate_transaction_test.go +++ b/cmd/soroban-rpc/internal/integrationtest/simulate_transaction_test.go @@ -71,8 +71,8 @@ func TestSimulateTransactionSucceeds(t *testing.T) { // Then decode and check the result xdr, separately so we get a decent diff if it fails. require.Len(t, result.Results, 1) var resultXdr xdr.ScVal - err = xdr.SafeUnmarshalBase64(result.Results[0].ReturnValueXDR, &resultXdr) - require.NoError(t, err) + require.NotNil(t, result.Results[0].ReturnValueXDR) + require.NoError(t, xdr.SafeUnmarshalBase64(*result.Results[0].ReturnValueXDR, &resultXdr)) require.Equal(t, expectedXdr, resultXdr) // Check state diff @@ -134,7 +134,7 @@ func TestSimulateTransactionWithAuth(t *testing.T) { require.Empty(t, deployContractOp.Auth) var auth xdr.SorobanAuthorizationEntry - require.NoError(t, xdr.SafeUnmarshalBase64(response.Results[0].AuthXDR[0], &auth)) + require.NoError(t, xdr.SafeUnmarshalBase64((*response.Results[0].AuthXDR)[0], &auth)) require.Equal(t, auth.Credentials.Type, xdr.SorobanCredentialsTypeSorobanCredentialsSourceAccount) deployContractOp.Auth = append(deployContractOp.Auth, auth) deployContractParams.Operations = []txnbuild.Operation{deployContractOp} @@ -193,8 +193,8 @@ func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) { // check the result require.Len(t, response.Results, 1) var obtainedResult xdr.ScVal - err = xdr.SafeUnmarshalBase64(response.Results[0].ReturnValueXDR, &obtainedResult) - require.NoError(t, err) + require.NotNil(t, response.Results[0].ReturnValueXDR) + require.NoError(t, xdr.SafeUnmarshalBase64(*response.Results[0].ReturnValueXDR, &obtainedResult)) require.Equal(t, xdr.ScValTypeScvAddress, obtainedResult.Type) require.NotNil(t, obtainedResult.Address) require.Equal(t, authAccountIDArg, obtainedResult.Address.MustAccountId()) @@ -227,7 +227,7 @@ func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) { // check the auth require.Len(t, response.Results[0].AuthXDR, 1) var obtainedAuth xdr.SorobanAuthorizationEntry - err = xdr.SafeUnmarshalBase64(response.Results[0].AuthXDR[0], &obtainedAuth) + err = xdr.SafeUnmarshalBase64((*response.Results[0].AuthXDR)[0], &obtainedAuth) require.NoError(t, err) require.Equal(t, xdr.SorobanCredentialsTypeSorobanCredentialsAddress, obtainedAuth.Credentials.Type) require.Equal(t, xdr.ScValTypeScvVoid, obtainedAuth.Credentials.Address.Signature.Type) @@ -564,8 +564,8 @@ func TestSimulateInvokePrng_u64_in_range(t *testing.T) { // check the result require.Len(t, response.Results, 1) var obtainedResult xdr.ScVal - err = xdr.SafeUnmarshalBase64(response.Results[0].ReturnValueXDR, &obtainedResult) - require.NoError(t, err) + require.NotNil(t, response.Results[0].ReturnValueXDR) + require.NoError(t, xdr.SafeUnmarshalBase64(*response.Results[0].ReturnValueXDR, &obtainedResult)) require.Equal(t, xdr.ScValTypeScvU64, obtainedResult.Type) require.LessOrEqual(t, uint64(*obtainedResult.U64), uint64(high)) require.GreaterOrEqual(t, uint64(*obtainedResult.U64), uint64(low)) @@ -612,8 +612,8 @@ func TestSimulateSystemEvent(t *testing.T) { // check the result require.Len(t, response.Results, 1) var obtainedResult xdr.ScVal - err = xdr.SafeUnmarshalBase64(response.Results[0].ReturnValueXDR, &obtainedResult) - require.NoError(t, err) + require.NotNil(t, response.Results[0].ReturnValueXDR) + require.NoError(t, xdr.SafeUnmarshalBase64(*response.Results[0].ReturnValueXDR, &obtainedResult)) var transactionData xdr.SorobanTransactionData err = xdr.SafeUnmarshalBase64(response.TransactionDataXDR, &transactionData) diff --git a/cmd/soroban-rpc/internal/methods/simulate_transaction.go b/cmd/soroban-rpc/internal/methods/simulate_transaction.go index b4a332ce..b9968f61 100644 --- a/cmd/soroban-rpc/internal/methods/simulate_transaction.go +++ b/cmd/soroban-rpc/internal/methods/simulate_transaction.go @@ -33,10 +33,10 @@ type SimulateTransactionCost struct { // SimulateHostFunctionResult contains the simulation result of each HostFunction // within the single InvokeHostFunctionOp allowed in a Transaction type SimulateHostFunctionResult struct { - AuthXDR []string `json:"auth,omitempty"` + AuthXDR *[]string `json:"auth,omitempty"` AuthJSON []json.RawMessage `json:"authJson,omitempty"` - ReturnValueXDR string `json:"xdr,omitempty"` + ReturnValueXDR *string `json:"xdr,omitempty"` ReturnValueJSON json.RawMessage `json:"returnValueJson,omitempty"` } @@ -345,9 +345,11 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge }) default: + rv := base64.StdEncoding.EncodeToString(result.Result) + auth := base64EncodeSlice(result.Auth) results = append(results, SimulateHostFunctionResult{ - ReturnValueXDR: base64.StdEncoding.EncodeToString(result.Result), - AuthXDR: base64EncodeSlice(result.Auth), + ReturnValueXDR: &rv, + AuthXDR: &auth, }) } }