Skip to content

Commit

Permalink
Modify omitempty fields to be pointer-based
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Sep 12, 2024
1 parent 2aeb13c commit 18b97be
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down Expand Up @@ -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,
})
}
}
Expand Down

0 comments on commit 18b97be

Please sign in to comment.