From e86bd25d1135c0f7c284004114e579b0108d7485 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Mon, 7 Oct 2024 18:39:28 -0400 Subject: [PATCH 1/3] Add test creating a transaction with constructor --- .../infrastructure/contract.go | 52 ++++++++++++++++-- .../integrationtest/infrastructure/test.go | 5 ++ .../integrationtest/transaction_test.go | 20 +++++++ wasms/test_no_arg_constructor.wasm | Bin 0 -> 656 bytes 4 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 wasms/test_no_arg_constructor.wasm diff --git a/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go b/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go index 9afd5ae1..46e6fda1 100644 --- a/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go +++ b/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go @@ -15,18 +15,28 @@ import ( var testSalt = sha256.Sum256([]byte("a1")) -func GetHelloWorldContract() []byte { - contractFile := path.Join(GetCurrentDirectory(), "../../../../../wasms/test_hello_world.wasm") +func getTestContract(name string) []byte { + contractFile := path.Join(GetCurrentDirectory(), "../../../../../wasms/test_"+name+".wasm") ret, err := os.ReadFile(contractFile) if err != nil { str := fmt.Sprintf( - "unable to read test_hello_world.wasm (%v) please run `make build-test-wasms` at the project root directory", - err) + "unable to read %s.wasm (%v) please run `make build-test-wasms` at the project root directory", + name, + err, + ) panic(str) } return ret } +func GetHelloWorldContract() []byte { + return getTestContract("hello_world") +} + +func GetNoArgConstructorContract() []byte { + return getTestContract("no_arg_constructor") +} + func CreateInvokeHostOperation(sourceAccount string, contractID xdr.Hash, method string, args ...xdr.ScVal) *txnbuild.InvokeHostFunction { return &txnbuild.InvokeHostFunction{ HostFunction: xdr.HostFunction{ @@ -116,3 +126,37 @@ func createCreateContractOperation(sourceAccount string, salt xdr.Uint256, contr SourceAccount: sourceAccount, } } + +func CreateCreateNoArgConstructorContractOperation(sourceAccount string) *txnbuild.InvokeHostFunction { + contractHash := xdr.Hash(sha256.Sum256(GetNoArgConstructorContract())) + salt := xdr.Uint256(testSalt) + return createCreateContractV2Operation(sourceAccount, salt, contractHash) +} + +func createCreateContractV2Operation(sourceAccount string, salt xdr.Uint256, contractHash xdr.Hash) *txnbuild.InvokeHostFunction { + sourceAccountID := xdr.MustAddress(sourceAccount) + return &txnbuild.InvokeHostFunction{ + HostFunction: xdr.HostFunction{ + Type: xdr.HostFunctionTypeHostFunctionTypeCreateContractV2, + CreateContractV2: &xdr.CreateContractArgsV2{ + ContractIdPreimage: xdr.ContractIdPreimage{ + Type: xdr.ContractIdPreimageTypeContractIdPreimageFromAddress, + FromAddress: &xdr.ContractIdPreimageFromAddress{ + Address: xdr.ScAddress{ + Type: xdr.ScAddressTypeScAddressTypeAccount, + AccountId: &sourceAccountID, + }, + Salt: salt, + }, + }, + Executable: xdr.ContractExecutable{ + Type: xdr.ContractExecutableTypeContractExecutableWasm, + WasmHash: &contractHash, + }, + ConstructorArgs: nil, + }, + }, + Auth: []xdr.SorobanAuthorizationEntry{}, + SourceAccount: sourceAccount, + } +} diff --git a/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go b/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go index a800bd0e..a8c4aa58 100644 --- a/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go +++ b/cmd/soroban-rpc/internal/integrationtest/infrastructure/test.go @@ -652,6 +652,11 @@ func (i *Test) UploadHelloWorldContract() (methods.GetTransactionResponse, xdr.H return i.uploadContract(contractBinary) } +func (i *Test) UploadNoArgConstructorContract() (methods.GetTransactionResponse, xdr.Hash) { + contractBinary := GetNoArgConstructorContract() + return i.uploadContract(contractBinary) +} + func (i *Test) uploadContract(contractBinary []byte) (methods.GetTransactionResponse, xdr.Hash) { contractHash := xdr.Hash(sha256.Sum256(contractBinary)) op := CreateUploadWasmOperation(i.MasterAccount().GetAccountID(), contractBinary) diff --git a/cmd/soroban-rpc/internal/integrationtest/transaction_test.go b/cmd/soroban-rpc/internal/integrationtest/transaction_test.go index 7dc0be0e..4a2af0e1 100644 --- a/cmd/soroban-rpc/internal/integrationtest/transaction_test.go +++ b/cmd/soroban-rpc/internal/integrationtest/transaction_test.go @@ -202,3 +202,23 @@ func TestSendTransactionFailedInvalidXDR(t *testing.T) { require.Equal(t, "invalid_xdr", jsonRPCErr.Message) require.Equal(t, jrpc2.InvalidParams, jsonRPCErr.Code) } + +func TestContractCreationWithConstructor(t *testing.T) { + test := infrastructure.NewTest(t, nil) + + test.UploadNoArgConstructorContract() + + client := test.GetRPCLient() + + params := infrastructure.PreflightTransactionParams(t, client, + infrastructure.CreateTransactionParams( + test.MasterAccount(), + infrastructure.CreateCreateNoArgConstructorContractOperation(test.MasterAccount().GetAccountID()), + ), + ) + + tx, err := txnbuild.NewTransaction(params) + require.NoError(t, err) + infrastructure.SendSuccessfulTransaction(t, client, test.MasterKey(), tx) + +} diff --git a/wasms/test_no_arg_constructor.wasm b/wasms/test_no_arg_constructor.wasm new file mode 100644 index 0000000000000000000000000000000000000000..14c0295da81a9dc610255912903744acfb59ed64 GIT binary patch literal 656 zcmZ8f&2H2%5S|$)*{ac&tWYb&0a-yDStZ+Sm)?j4st_l50k7kjzjk-qBrNJBTd4?f z;w?}mB;J8D@4;Ij6(`*vz?MBf-{udtJ>6 zaJF8XO6ICk;M%aAZP^;qOmhally8hWm6<9H!|h+_?t=L8``aB3-v$39oR1s>xa;ph ziUI?ck|4V?0Ra=_e?_7N)%3WT9In!9JPR@1IpUc9grl=AaInX`r(J9n44JsbOX0k1 z5n3S>VCZZRCiv&8khs&xcOi0m)+HOjmVOfnEFU&}!`49p zU-%5r2fbrER252B<*Ctg+u-&1uX}6%-^!+=_I;KBJ2vNy+OwaZi Date: Tue, 8 Oct 2024 09:50:26 -0400 Subject: [PATCH 2/3] Only run in protocol >= 22 --- cmd/soroban-rpc/internal/integrationtest/transaction_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/soroban-rpc/internal/integrationtest/transaction_test.go b/cmd/soroban-rpc/internal/integrationtest/transaction_test.go index 4a2af0e1..ff6515ff 100644 --- a/cmd/soroban-rpc/internal/integrationtest/transaction_test.go +++ b/cmd/soroban-rpc/internal/integrationtest/transaction_test.go @@ -204,6 +204,10 @@ func TestSendTransactionFailedInvalidXDR(t *testing.T) { } func TestContractCreationWithConstructor(t *testing.T) { + + if infrastructure.GetCoreMaxSupportedProtocol() < 22 { + t.Skip("Only test this for protocol >= 22") + } test := infrastructure.NewTest(t, nil) test.UploadNoArgConstructorContract() From 3c61c61357effca9261fb5c0eec9a66aa9cdba04 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Tue, 8 Oct 2024 11:55:32 -0400 Subject: [PATCH 3/3] Appease linter --- .../internal/integrationtest/infrastructure/contract.go | 4 +++- cmd/soroban-rpc/internal/integrationtest/transaction_test.go | 2 -- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go b/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go index 46e6fda1..e4c087fc 100644 --- a/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go +++ b/cmd/soroban-rpc/internal/integrationtest/infrastructure/contract.go @@ -133,7 +133,9 @@ func CreateCreateNoArgConstructorContractOperation(sourceAccount string) *txnbui return createCreateContractV2Operation(sourceAccount, salt, contractHash) } -func createCreateContractV2Operation(sourceAccount string, salt xdr.Uint256, contractHash xdr.Hash) *txnbuild.InvokeHostFunction { +func createCreateContractV2Operation( + sourceAccount string, salt xdr.Uint256, contractHash xdr.Hash, +) *txnbuild.InvokeHostFunction { sourceAccountID := xdr.MustAddress(sourceAccount) return &txnbuild.InvokeHostFunction{ HostFunction: xdr.HostFunction{ diff --git a/cmd/soroban-rpc/internal/integrationtest/transaction_test.go b/cmd/soroban-rpc/internal/integrationtest/transaction_test.go index ff6515ff..1dfd1f4e 100644 --- a/cmd/soroban-rpc/internal/integrationtest/transaction_test.go +++ b/cmd/soroban-rpc/internal/integrationtest/transaction_test.go @@ -204,7 +204,6 @@ func TestSendTransactionFailedInvalidXDR(t *testing.T) { } func TestContractCreationWithConstructor(t *testing.T) { - if infrastructure.GetCoreMaxSupportedProtocol() < 22 { t.Skip("Only test this for protocol >= 22") } @@ -224,5 +223,4 @@ func TestContractCreationWithConstructor(t *testing.T) { tx, err := txnbuild.NewTransaction(params) require.NoError(t, err) infrastructure.SendSuccessfulTransaction(t, client, test.MasterKey(), tx) - }