Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soroban-rpc: update xdr #627

Merged
merged 5 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions cmd/soroban-rpc/internal/methods/get_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1071,9 +1071,14 @@ func ledgerCloseMetaWithEvents(sequence uint32, closeTimestamp int64, txMeta ...
Body: xdr.OperationBody{
Type: xdr.OperationTypeInvokeHostFunction,
InvokeHostFunctionOp: &xdr.InvokeHostFunctionOp{
Function: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeArgs: &xdr.ScVec{},
Functions: []xdr.HostFunction{
{
Args: xdr.HostFunctionArgs{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeContract: &xdr.ScVec{},
},
Auth: []xdr.ContractAuth{},
},
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/internal/test/get_ledger_entries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) {
txStatusResponse := getTransaction(t, client, sendTxResponse.Hash)
assert.Equal(t, methods.TransactionStatusSuccess, txStatusResponse.Status)

installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: testContract}.MarshalBinary()
uploadContractCodeArgs, err := xdr.UploadContractWasmArgs{Code: testContract}.MarshalBinary()
assert.NoError(t, err)
contractHash := sha256.Sum256(installContractCodeArgs)
contractHash := sha256.Sum256(uploadContractCodeArgs)
contractKeyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractCode,
ContractCode: &xdr.LedgerKeyContractCode{
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestGetLedgerEntriesSucceeds(t *testing.T) {
var result methods.GetLedgerEntriesResponse
err = client.CallResult(context.Background(), "getLedgerEntries", request, &result)
assert.NoError(t, err)
assert.Equal(t, 1, len(result.Entries))
require.Equal(t, 1, len(result.Entries))
assert.Greater(t, result.LatestLedger, int64(0))

var firstEntry xdr.LedgerEntryData
Expand Down
4 changes: 2 additions & 2 deletions cmd/soroban-rpc/internal/test/get_ledger_entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func TestGetLedgerEntrySucceeds(t *testing.T) {
txStatusResponse := getTransaction(t, client, sendTxResponse.Hash)
assert.Equal(t, methods.TransactionStatusSuccess, txStatusResponse.Status)

installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: testContract}.MarshalBinary()
uploadContractCodeArgs, err := xdr.UploadContractWasmArgs{Code: testContract}.MarshalBinary()
assert.NoError(t, err)
contractHash := sha256.Sum256(installContractCodeArgs)
contractHash := sha256.Sum256(uploadContractCodeArgs)
keyB64, err := xdr.MarshalBase64(xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractCode,
ContractCode: &xdr.LedgerKeyContractCode{
Expand Down
132 changes: 88 additions & 44 deletions cmd/soroban-rpc/internal/test/simulate_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func getHelloWorldContract(t *testing.T) []byte {
return ret
}

func createInvokeHostOperation(sourceAccount string, footprint xdr.LedgerFootprint, contractID xdr.Hash, method string, args ...xdr.ScVal) *txnbuild.InvokeHostFunction {
func createInvokeHostOperation(sourceAccount string, ext xdr.TransactionExt, contractID xdr.Hash, method string, args ...xdr.ScVal) *txnbuild.InvokeHostFunctions {
var contractIDBytes = xdr.ScBytes(contractID[:])
methodSymbol := xdr.ScSymbol(method)
parameters := xdr.ScVec{
Expand All @@ -56,23 +56,28 @@ func createInvokeHostOperation(sourceAccount string, footprint xdr.LedgerFootpri
},
}
parameters = append(parameters, args...)
return &txnbuild.InvokeHostFunction{
Footprint: footprint,
Function: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeArgs: &parameters,
return &txnbuild.InvokeHostFunctions{

Functions: []xdr.HostFunction{
{
Args: xdr.HostFunctionArgs{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeContract: &parameters,
},
},
},
Ext: ext,
SourceAccount: sourceAccount,
}
}

func createInstallContractCodeOperation(t *testing.T, sourceAccount string, contractCode []byte, includeFootprint bool) *txnbuild.InvokeHostFunction {
var footprint xdr.LedgerFootprint
if includeFootprint {
installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: contractCode}.MarshalBinary()
func createInstallContractCodeOperation(t *testing.T, sourceAccount string, contractCode []byte, includeExt bool) *txnbuild.InvokeHostFunctions {
var ext xdr.TransactionExt
if includeExt {
uploadContractCodeArgs, err := xdr.UploadContractWasmArgs{Code: contractCode}.MarshalBinary()
assert.NoError(t, err)
contractHash := sha256.Sum256(installContractCodeArgs)
footprint = xdr.LedgerFootprint{
contractHash := sha256.Sum256(uploadContractCodeArgs)
footprint := xdr.LedgerFootprint{
ReadWrite: []xdr.LedgerKey{
{
Type: xdr.LedgerEntryTypeContractCode,
Expand All @@ -82,29 +87,48 @@ func createInstallContractCodeOperation(t *testing.T, sourceAccount string, cont
},
},
}
ext = xdr.TransactionExt{
V: 1,
SorobanData: &xdr.SorobanTransactionData{
Resources: xdr.SorobanResources{
Footprint: footprint,
Instructions: 0,
ReadBytes: 0,
WriteBytes: 0,
ExtendedMetaDataSizeBytes: 0,
},
RefundableFee: 0,
Ext: xdr.ExtensionPoint{},
},
}
}

return &txnbuild.InvokeHostFunction{
Footprint: footprint,
Function: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeInstallContractCode,
InstallContractCodeArgs: &xdr.InstallContractCodeArgs{
Code: contractCode,
return &txnbuild.InvokeHostFunctions{
Ext: ext,
Functions: []xdr.HostFunction{
{
Args: xdr.HostFunctionArgs{
Type: xdr.HostFunctionTypeHostFunctionTypeUploadContractWasm,
UploadContractWasm: &xdr.UploadContractWasmArgs{
Code: contractCode,
},
},
Auth: []xdr.ContractAuth{},
},
},
SourceAccount: sourceAccount,
}
}

func createCreateContractOperation(t *testing.T, sourceAccount string, contractCode []byte, networkPassphrase string, includeFootprint bool) *txnbuild.InvokeHostFunction {
func createCreateContractOperation(t *testing.T, sourceAccount string, contractCode []byte, networkPassphrase string, includeExt bool) *txnbuild.InvokeHostFunctions {
saltParam := xdr.Uint256(testSalt)

var footprint xdr.LedgerFootprint
if includeFootprint {
installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: contractCode}.MarshalBinary()
var ext xdr.TransactionExt
if includeExt {
uploadContractCodeArgs, err := xdr.UploadContractWasmArgs{Code: contractCode}.MarshalBinary()
assert.NoError(t, err)
contractHash := xdr.Hash(sha256.Sum256(installContractCodeArgs))
footprint = xdr.LedgerFootprint{
contractHash := xdr.Hash(sha256.Sum256(uploadContractCodeArgs))
footprint := xdr.LedgerFootprint{
ReadWrite: []xdr.LedgerKey{
{
Type: xdr.LedgerEntryTypeContractData,
Expand All @@ -125,25 +149,45 @@ func createCreateContractOperation(t *testing.T, sourceAccount string, contractC
},
},
}
ext = xdr.TransactionExt{
V: 1,
SorobanData: &xdr.SorobanTransactionData{
Resources: xdr.SorobanResources{
Footprint: footprint,
Instructions: 0,
ReadBytes: 0,
WriteBytes: 0,
ExtendedMetaDataSizeBytes: 0,
},
RefundableFee: 0,
Ext: xdr.ExtensionPoint{},
},
}
}

installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: contractCode}.MarshalBinary()
uploadContractCodeArgs, err := xdr.UploadContractWasmArgs{Code: contractCode}.MarshalBinary()
assert.NoError(t, err)
contractHash := xdr.Hash(sha256.Sum256(installContractCodeArgs))

return &txnbuild.InvokeHostFunction{
Footprint: footprint,
Function: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeCreateContract,
CreateContractArgs: &xdr.CreateContractArgs{
ContractId: xdr.ContractId{
Type: xdr.ContractIdTypeContractIdFromSourceAccount,
Salt: &saltParam,
},
Source: xdr.ScContractExecutable{
Type: xdr.ScContractExecutableTypeSccontractExecutableWasmRef,
WasmId: &contractHash,
contractHash := xdr.Hash(sha256.Sum256(uploadContractCodeArgs))

return &txnbuild.InvokeHostFunctions{
Ext: ext,

Functions: []xdr.HostFunction{
{
Args: xdr.HostFunctionArgs{
Type: xdr.HostFunctionTypeHostFunctionTypeCreateContract,
CreateContract: &xdr.CreateContractArgs{
ContractId: xdr.ContractId{
Type: xdr.ContractIdTypeContractIdFromSourceAccount,
Salt: &saltParam,
},
Executable: xdr.ScContractExecutable{
Type: xdr.ScContractExecutableTypeSccontractExecutableWasmRef,
WasmId: &contractHash,
},
},
},
Auth: []xdr.ContractAuth{},
},
},
SourceAccount: sourceAccount,
Expand Down Expand Up @@ -349,7 +393,7 @@ func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
Operations: []txnbuild.Operation{
createInvokeHostOperation(
address,
xdr.LedgerFootprint{},
xdr.TransactionExt{},
contractID,
"auth",
xdr.ScVal{
Expand Down Expand Up @@ -404,9 +448,9 @@ func TestSimulateInvokeContractTransactionSucceeds(t *testing.T) {
assert.Equal(t, xdr.ScValTypeScvLedgerKeyContractExecutable, ro1.ContractData.Key.Type)
ro2 := obtainedFootprint.ReadOnly[2]
assert.Equal(t, xdr.LedgerEntryTypeContractCode, ro2.Type)
installContractCodeArgs, err := xdr.InstallContractCodeArgs{Code: helloWorldContract}.MarshalBinary()
uploadContractCodeArgs, err := xdr.UploadContractWasmArgs{Code: helloWorldContract}.MarshalBinary()
assert.NoError(t, err)
contractHash := sha256.Sum256(installContractCodeArgs)
contractHash := sha256.Sum256(uploadContractCodeArgs)
assert.Equal(t, xdr.Hash(contractHash), ro2.ContractCode.Hash)
assert.NoError(t, err)

Expand Down Expand Up @@ -458,8 +502,8 @@ func TestSimulateTransactionError(t *testing.T) {
client := jrpc2.NewClient(ch, nil)

sourceAccount := keypair.Root(StandaloneNetworkPassphrase).Address()
invokeHostOp := createInvokeHostOperation(sourceAccount, xdr.LedgerFootprint{}, xdr.Hash{}, "noMethod")
invokeHostOp.Function.InvokeArgs = &xdr.ScVec{}
invokeHostOp := createInvokeHostOperation(sourceAccount, xdr.TransactionExt{}, xdr.Hash{}, "noMethod")
invokeHostOp.Functions[0].Args.InvokeContract = &xdr.ScVec{}
tx, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
SourceAccount: &txnbuild.SimpleAccount{
AccountID: keypair.Root(StandaloneNetworkPassphrase).Address(),
Expand Down
6 changes: 4 additions & 2 deletions cmd/soroban-rpc/internal/test/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ func TestSendTransactionSucceedsWithResults(t *testing.T) {
expectedContractID, err := hex.DecodeString("ea9fcb81ae54a29f6b3bf293847d3fd7e9a369fd1c80acafec6abd571317e0c2")
assert.NoError(t, err)
contractIDBytes := xdr.ScBytes(expectedContractID)
expectedScVal := xdr.ScVal{Type: xdr.ScValTypeScvBytes, Bytes: &contractIDBytes}
assert.True(t, expectedScVal.Equals(resultVal))
expectedScVal := []xdr.ScVal{{Type: xdr.ScValTypeScvBytes, Bytes: &contractIDBytes}}
assert.Equal(t, len(expectedScVal), len(resultVal))
assert.Equal(t, len(expectedScVal), int(1))
assert.True(t, expectedScVal[0].Equals(resultVal[0]))

expectedResult := xdr.TransactionResult{
FeeCharged: 100,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/sirupsen/logrus v1.4.1
github.com/spf13/cobra v0.0.0-20160830174925-9c28e4bbd74e
github.com/spf13/viper v0.0.0-20150621231900-db7ff930a189
github.com/stellar/go v0.0.0-20230420161348-768b7a1aa986
github.com/stellar/go v0.0.0-20230504013330-a6e68f56e2c5
github.com/stretchr/testify v1.8.0
golang.org/x/mod v0.6.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ github.com/spf13/pflag v0.0.0-20161005214240-4bd69631f475 h1:RtZIgreTwcayPTOw7G5
github.com/spf13/pflag v0.0.0-20161005214240-4bd69631f475/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v0.0.0-20150621231900-db7ff930a189 h1:fvB1AFbBd6SfI9Rd0ooAJp8uLkZDbZaLFHi7ZnNP6uI=
github.com/spf13/viper v0.0.0-20150621231900-db7ff930a189/go.mod h1:A8kyI5cUJhb8N+3pkfONlcEcZbueH6nhAm0Fq7SrnBM=
github.com/stellar/go v0.0.0-20230420161348-768b7a1aa986 h1:tE4dGZgNRTY9qWt44rVUOLlw8AbMsI19hukfaHsHSp0=
github.com/stellar/go v0.0.0-20230420161348-768b7a1aa986/go.mod h1:DHmAo7QjGEJa0yef6NXOh+083h/S6OsdRhOwav6Om1A=
github.com/stellar/go v0.0.0-20230504013330-a6e68f56e2c5 h1:Q1EX3wSo1hdWzBjMLguMMJxs5DFvGzxVSD9FwiGWpEk=
github.com/stellar/go v0.0.0-20230504013330-a6e68f56e2c5/go.mod h1:DHmAo7QjGEJa0yef6NXOh+083h/S6OsdRhOwav6Om1A=
github.com/stellar/go-xdr v0.0.0-20211103144802-8017fc4bdfee h1:fbVs0xmXpBvVS4GBeiRmAE3Le70ofAqFMch1GTiq/e8=
github.com/stellar/go-xdr v0.0.0-20211103144802-8017fc4bdfee/go.mod h1:yoxyU/M8nl9LKeWIoBrbDPQ7Cy+4jxRcWcOayZ4BMps=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down