From 18610880bd2d83655b2dc4391bbd018b19c7d655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Mon, 15 Apr 2024 14:42:49 +0300 Subject: [PATCH 1/2] Integration tests for the new events for "claim developer rewards". --- go.mod | 2 +- go.sum | 4 +- .../developerRewards/developerRewards_test.go | 71 ++++++++++ .../developer-rewards/developer_rewards.c | 133 ++++++++++++++++++ .../developer_rewards.export | 7 + .../output/developer_rewards.wasm | Bin 0 -> 1171 bytes integrationTests/vm/wasm/utils.go | 16 ++- 7 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 integrationTests/vm/wasm/developerRewards/developerRewards_test.go create mode 100644 integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c create mode 100644 integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export create mode 100755 integrationTests/vm/wasm/testdata/developer-rewards/output/developer_rewards.wasm diff --git a/go.mod b/go.mod index 9de88775caa..36abca09af5 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/multiversx/mx-chain-logger-go v1.0.14-0.20240129144507-d00e967c890c github.com/multiversx/mx-chain-scenario-go v1.4.3-0.20240212160120-cc32d1580157 github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240321150623-3974ec1d6474 - github.com/multiversx/mx-chain-vm-common-go v1.5.12-0.20240328091908-c46c76dac779 + github.com/multiversx/mx-chain-vm-common-go v1.5.12-0.20240411132244-adf842b5e09e github.com/multiversx/mx-chain-vm-go v1.5.28-0.20240328092329-b5f2c7c059eb github.com/multiversx/mx-chain-vm-v1_2-go v1.2.66-0.20240321152247-79521988c8e6 github.com/multiversx/mx-chain-vm-v1_3-go v1.3.67-0.20240321152532-45da5eabdc38 diff --git a/go.sum b/go.sum index 96da81e0efb..69efd4b6287 100644 --- a/go.sum +++ b/go.sum @@ -399,8 +399,8 @@ github.com/multiversx/mx-chain-scenario-go v1.4.3-0.20240212160120-cc32d1580157 github.com/multiversx/mx-chain-scenario-go v1.4.3-0.20240212160120-cc32d1580157/go.mod h1:ndk45i9J9McuCJpTcgiaK4ocd0yhnBBCPrlFwO6GRcs= github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240321150623-3974ec1d6474 h1:x65Su8ojHwA+NICp9DrSVGLDDcAlW04DafkqCHY1QPE= github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240321150623-3974ec1d6474/go.mod h1:hnc6H4D5Ge1haRAQ6QHTXhyh+CT2DRiNJ0U0HQYI3DY= -github.com/multiversx/mx-chain-vm-common-go v1.5.12-0.20240328091908-c46c76dac779 h1:FSgAtNcml8kWdIEn8MxCfPkZ8ZE/wIFNKI5TZLEfcT0= -github.com/multiversx/mx-chain-vm-common-go v1.5.12-0.20240328091908-c46c76dac779/go.mod h1:G6daPJC6bFsvAw45RPMCRi2rP+8LjFxa8G+3alHuJow= +github.com/multiversx/mx-chain-vm-common-go v1.5.12-0.20240411132244-adf842b5e09e h1:SJmm+Lkxdj/eJ4t/CCcvhZCZtg2A1ieVoJV5FJooFKA= +github.com/multiversx/mx-chain-vm-common-go v1.5.12-0.20240411132244-adf842b5e09e/go.mod h1:G6daPJC6bFsvAw45RPMCRi2rP+8LjFxa8G+3alHuJow= github.com/multiversx/mx-chain-vm-go v1.5.28-0.20240328092329-b5f2c7c059eb h1:0WvWXqzliYS1yKW+6uTxZGMjQd08IQNPzlNNxxyNWHM= github.com/multiversx/mx-chain-vm-go v1.5.28-0.20240328092329-b5f2c7c059eb/go.mod h1:mZNRILxq51LVqwqE9jMJyDHgmy9W3x7otOGuFjOm82Q= github.com/multiversx/mx-chain-vm-v1_2-go v1.2.66-0.20240321152247-79521988c8e6 h1:7HqUo9YmpsfN/y9px6RmzREJm5O6ZzP9NqvFSrHTw24= diff --git a/integrationTests/vm/wasm/developerRewards/developerRewards_test.go b/integrationTests/vm/wasm/developerRewards/developerRewards_test.go new file mode 100644 index 00000000000..356ffc29db5 --- /dev/null +++ b/integrationTests/vm/wasm/developerRewards/developerRewards_test.go @@ -0,0 +1,71 @@ +package transfers + +import ( + "math/big" + "testing" + + "github.com/multiversx/mx-chain-go/integrationTests/vm/wasm" + "github.com/stretchr/testify/require" +) + +func TestClaimDeveloperRewards(t *testing.T) { + if testing.Short() { + t.Skip("this is not a short test") + } + + context := wasm.SetupTestContext(t) + defer context.Close() + + err := context.DeploySC("../testdata/developer-rewards/output/developer_rewards.wasm", "") + require.Nil(t, err) + + t.Run("rewards for user", func(t *testing.T) { + contractAddress := context.ScAddress + + err = context.ExecuteSC(&context.Owner, "doSomething") + require.Nil(t, err) + + ownerBalanceBefore := context.GetAccountBalance(&context.Owner).Uint64() + reward := context.GetAccount(contractAddress).GetDeveloperReward().Uint64() + + err = context.ExecuteSC(&context.Owner, "ClaimDeveloperRewards") + require.Nil(t, err) + + ownerBalanceAfter := context.GetAccountBalance(&context.Owner).Uint64() + require.Equal(t, ownerBalanceBefore-context.LastConsumedFee+reward, ownerBalanceAfter) + + events := context.LastLogs[0].GetLogEvents() + require.Equal(t, "ClaimDeveloperRewards", string(events[0].GetIdentifier())) + require.Equal(t, big.NewInt(0).SetUint64(reward).Bytes(), events[0].GetTopics()[0]) + require.Equal(t, context.Owner.Address, events[0].GetTopics()[1]) + }) + + t.Run("rewards for contract", func(t *testing.T) { + parentContractAddress := context.ScAddress + + err = context.ExecuteSC(&context.Owner, "deployChild") + require.Nil(t, err) + + chilContractdAddress := context.QuerySCBytes("getChildAddress", [][]byte{}) + require.NotNil(t, chilContractdAddress) + + context.ScAddress = chilContractdAddress + err = context.ExecuteSC(&context.Owner, "doSomething") + require.Nil(t, err) + + contractBalanceBefore := context.GetAccount(parentContractAddress).GetBalance().Uint64() + reward := context.GetAccount(chilContractdAddress).GetDeveloperReward().Uint64() + + context.ScAddress = parentContractAddress + err = context.ExecuteSC(&context.Owner, "claimDeveloperRewardsOnChild") + require.Nil(t, err) + + contractBalanceAfter := context.GetAccount(parentContractAddress).GetBalance().Uint64() + require.Equal(t, contractBalanceBefore+reward, contractBalanceAfter) + + events := context.LastLogs[0].GetLogEvents() + require.Equal(t, "ClaimDeveloperRewards", string(events[0].GetIdentifier())) + require.Equal(t, big.NewInt(0).SetUint64(reward).Bytes(), events[0].GetTopics()[0]) + require.Equal(t, parentContractAddress, events[0].GetTopics()[1]) + }) +} diff --git a/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c new file mode 100644 index 00000000000..194c5d68c1d --- /dev/null +++ b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c @@ -0,0 +1,133 @@ +typedef unsigned char byte; +typedef unsigned int i32; +typedef unsigned long long i64; + +void getSCAddress(byte *address); +int storageStore(byte *key, int keyLength, byte *data, int dataLength); +int storageLoad(byte *key, int keyLength, byte *data); +void finish(byte *data, int length); + +int deployFromSourceContract( + long long gas, + byte *value, + byte *sourceContractAddress, + byte *codeMetadata, + byte *newAddress, + int numInitArgs, + byte *initArgLengths, + byte *initArgs); + +i32 createAsyncCall( + byte *destination, + byte *value, + byte *data, + int dataLength, + byte *success, + int successLength, + byte *error, + int errorLength, + long long gas, + long long extraGasForCallback); + +static const i32 ADDRESS_LENGTH = 32; + +byte zero32_red[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +byte zero32_green[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; +byte zero32_blue[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +// E.g. can hold up to 64 addresses. +byte zero2048_red[2048] = {0}; +byte zero2048_green[2048] = {0}; +byte zero2048_blue[2048] = {0}; + +byte zeroEGLD[] = { + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +byte codeMetadataUpgradeableReadable[2] = {5, 0}; + +byte emptyArguments[0] = {}; +int emptyArgumentsLengths[0] = {}; +int gasLimitDeploySelf = 20000000; +int gasLimitUpgradeChild = 20000000; +int gasLimitClaimDeveloperRewards = 6000000; + +byte functionNameClaimDeveloperRewards[] = "ClaimDeveloperRewards"; +byte functionNameDoSomething[] = "doSomething"; +byte storageKeyChildAddress[] = "child"; +byte something[] = "something"; + +void init() +{ +} + +void upgrade() +{ +} + +void doSomething() +{ + finish(something, sizeof(something) - 1); +} + +void deployChild() +{ + byte *selfAddress = zero32_red; + byte *newAddress = zero32_blue; + + getSCAddress(selfAddress); + + deployFromSourceContract( + gasLimitDeploySelf, + zeroEGLD, + selfAddress, + codeMetadataUpgradeableReadable, + newAddress, + 0, + (byte *)emptyArgumentsLengths, + emptyArguments); + + storageStore(storageKeyChildAddress, sizeof(storageKeyChildAddress) - 1, newAddress, ADDRESS_LENGTH); +} + +void getChildAddress() +{ + byte *childAddress = zero32_red; + storageLoad(storageKeyChildAddress, sizeof(storageKeyChildAddress) - 1, childAddress); + finish(childAddress, ADDRESS_LENGTH); +} + +void callChild() +{ + byte *childAddress = zero32_red; + storageLoad(storageKeyChildAddress, sizeof(storageKeyChildAddress) - 1, childAddress); + + createAsyncCall( + childAddress, + zeroEGLD, + functionNameDoSomething, + sizeof(functionNameDoSomething) - 1, + 0, + 0, + 0, + 0, + 15000000, + 0); +} + +void claimDeveloperRewardsOnChild() +{ + byte *childAddress = zero32_red; + storageLoad(storageKeyChildAddress, sizeof(storageKeyChildAddress) - 1, childAddress); + + createAsyncCall( + childAddress, + zeroEGLD, + functionNameClaimDeveloperRewards, + sizeof(functionNameClaimDeveloperRewards) - 1, + 0, + 0, + 0, + 0, + gasLimitClaimDeveloperRewards, + 0); +} diff --git a/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export new file mode 100644 index 00000000000..79d27c49542 --- /dev/null +++ b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export @@ -0,0 +1,7 @@ +init +upgrade +doSomething +deployChild +getChildAddress +callChild +claimDeveloperRewardsOnChild diff --git a/integrationTests/vm/wasm/testdata/developer-rewards/output/developer_rewards.wasm b/integrationTests/vm/wasm/testdata/developer-rewards/output/developer_rewards.wasm new file mode 100755 index 0000000000000000000000000000000000000000..7aa8bd7d7a66c26b1aba23f9568f6600e429aed8 GIT binary patch literal 1171 zcmcIkJ#Q015S`uozCFih=R*i6W(7qW3YzpABP1jwBwW(u$9doyKl!AtWJ*{SUW_IVjr`dy~m@)uhJKi3zD%fK*{dr{u ztM=@AMbThm z_Oru$pnKV@%#%S0yg5;nS)L4apNs~pc8R6yX_lm5*Piwu*GZ{WaWWh9lF0-d%lBL} z9E$;WeO58`<=hrt>AO=s&GHitHX$p)^$rh)d6H`IM4I)psV?_NvmyAxAnWaqCMg7M zg5E4w=)+(@rA$=Z9ZZtZ^pQT&lk7m}&-HPVr^U0G2}j{?$Fra<>UoFpN_{(9EI@(^ zBtt+JFKAHgC$oZz&0E#I#z6_ONq8kW^81Zb{b Date: Mon, 15 Apr 2024 14:59:19 +0300 Subject: [PATCH 2/2] Improve tests. --- .../developerRewards/developerRewards_test.go | 27 +++++++++++------- .../developer-rewards/developer_rewards.c | 27 +----------------- .../developer_rewards.export | 1 - .../output/developer_rewards.wasm | Bin 1171 -> 973 bytes 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/integrationTests/vm/wasm/developerRewards/developerRewards_test.go b/integrationTests/vm/wasm/developerRewards/developerRewards_test.go index 356ffc29db5..a23493abc9f 100644 --- a/integrationTests/vm/wasm/developerRewards/developerRewards_test.go +++ b/integrationTests/vm/wasm/developerRewards/developerRewards_test.go @@ -1,7 +1,6 @@ package transfers import ( - "math/big" "testing" "github.com/multiversx/mx-chain-go/integrationTests/vm/wasm" @@ -13,20 +12,22 @@ func TestClaimDeveloperRewards(t *testing.T) { t.Skip("this is not a short test") } - context := wasm.SetupTestContext(t) - defer context.Close() - - err := context.DeploySC("../testdata/developer-rewards/output/developer_rewards.wasm", "") - require.Nil(t, err) + wasmPath := "../testdata/developer-rewards/output/developer_rewards.wasm" t.Run("rewards for user", func(t *testing.T) { + context := wasm.SetupTestContext(t) + defer context.Close() + + err := context.DeploySC(wasmPath, "") + require.Nil(t, err) contractAddress := context.ScAddress err = context.ExecuteSC(&context.Owner, "doSomething") require.Nil(t, err) ownerBalanceBefore := context.GetAccountBalance(&context.Owner).Uint64() - reward := context.GetAccount(contractAddress).GetDeveloperReward().Uint64() + rewardBig := context.GetAccount(contractAddress).GetDeveloperReward() + reward := rewardBig.Uint64() err = context.ExecuteSC(&context.Owner, "ClaimDeveloperRewards") require.Nil(t, err) @@ -36,11 +37,16 @@ func TestClaimDeveloperRewards(t *testing.T) { events := context.LastLogs[0].GetLogEvents() require.Equal(t, "ClaimDeveloperRewards", string(events[0].GetIdentifier())) - require.Equal(t, big.NewInt(0).SetUint64(reward).Bytes(), events[0].GetTopics()[0]) + require.Equal(t, rewardBig.Bytes(), events[0].GetTopics()[0]) require.Equal(t, context.Owner.Address, events[0].GetTopics()[1]) }) t.Run("rewards for contract", func(t *testing.T) { + context := wasm.SetupTestContext(t) + defer context.Close() + + err := context.DeploySC(wasmPath, "") + require.Nil(t, err) parentContractAddress := context.ScAddress err = context.ExecuteSC(&context.Owner, "deployChild") @@ -54,7 +60,8 @@ func TestClaimDeveloperRewards(t *testing.T) { require.Nil(t, err) contractBalanceBefore := context.GetAccount(parentContractAddress).GetBalance().Uint64() - reward := context.GetAccount(chilContractdAddress).GetDeveloperReward().Uint64() + rewardBig := context.GetAccount(chilContractdAddress).GetDeveloperReward() + reward := rewardBig.Uint64() context.ScAddress = parentContractAddress err = context.ExecuteSC(&context.Owner, "claimDeveloperRewardsOnChild") @@ -65,7 +72,7 @@ func TestClaimDeveloperRewards(t *testing.T) { events := context.LastLogs[0].GetLogEvents() require.Equal(t, "ClaimDeveloperRewards", string(events[0].GetIdentifier())) - require.Equal(t, big.NewInt(0).SetUint64(reward).Bytes(), events[0].GetTopics()[0]) + require.Equal(t, rewardBig.Bytes(), events[0].GetTopics()[0]) require.Equal(t, parentContractAddress, events[0].GetTopics()[1]) }) } diff --git a/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c index 194c5d68c1d..a5d3d70d891 100644 --- a/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c +++ b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c @@ -33,12 +33,6 @@ static const i32 ADDRESS_LENGTH = 32; byte zero32_red[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; byte zero32_green[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; -byte zero32_blue[32] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - -// E.g. can hold up to 64 addresses. -byte zero2048_red[2048] = {0}; -byte zero2048_green[2048] = {0}; -byte zero2048_blue[2048] = {0}; byte zeroEGLD[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; @@ -48,7 +42,6 @@ byte codeMetadataUpgradeableReadable[2] = {5, 0}; byte emptyArguments[0] = {}; int emptyArgumentsLengths[0] = {}; int gasLimitDeploySelf = 20000000; -int gasLimitUpgradeChild = 20000000; int gasLimitClaimDeveloperRewards = 6000000; byte functionNameClaimDeveloperRewards[] = "ClaimDeveloperRewards"; @@ -72,7 +65,7 @@ void doSomething() void deployChild() { byte *selfAddress = zero32_red; - byte *newAddress = zero32_blue; + byte *newAddress = zero32_green; getSCAddress(selfAddress); @@ -96,24 +89,6 @@ void getChildAddress() finish(childAddress, ADDRESS_LENGTH); } -void callChild() -{ - byte *childAddress = zero32_red; - storageLoad(storageKeyChildAddress, sizeof(storageKeyChildAddress) - 1, childAddress); - - createAsyncCall( - childAddress, - zeroEGLD, - functionNameDoSomething, - sizeof(functionNameDoSomething) - 1, - 0, - 0, - 0, - 0, - 15000000, - 0); -} - void claimDeveloperRewardsOnChild() { byte *childAddress = zero32_red; diff --git a/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export index 79d27c49542..b71813a510f 100644 --- a/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export +++ b/integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export @@ -3,5 +3,4 @@ upgrade doSomething deployChild getChildAddress -callChild claimDeveloperRewardsOnChild diff --git a/integrationTests/vm/wasm/testdata/developer-rewards/output/developer_rewards.wasm b/integrationTests/vm/wasm/testdata/developer-rewards/output/developer_rewards.wasm index 7aa8bd7d7a66c26b1aba23f9568f6600e429aed8..7d8df93e210f88c939641fcb41aaf4a0c0666071 100755 GIT binary patch delta 193 zcmbQtd6s>`J}GuK2w-JqWME?BV610!T+qqF&7Q$N@utUQT}C-!Zmu&dY|IS&+#oE? z$W-sRW^x>(wZMUnh6V;jW(6h%W-}%p#|M*FGCIpPbbxryp9Ztd_eO+V*HK^ zCUY{WGA^5}&lJRXVDbiLQ6PH;ljGzzW=$l%{N$(15{wO#|1o=W*fKIWHgIs8O%7yH W6G+a;%t>J=&d*IP$;ix0X8-^!1u~`p delta 367 zcmX@hKACgEK4}hiHV9y4W@KPu<6x|3bllL%!p&a7G4X~64`*^>PL6X%W=;wN_hbb| zIbj~IpRDZ64E)?6EX~MN@3>=fAfvUwg^q>>21RBCCIx0QCLYHRljkrx%TDM73Gz5L zfF*ey85H?|hJ(cT9XCw=!>G!*X|fVikT|Ml1ttw99z_<%O_O<;pj}wC;uL8RQhXSVpmjX8w@+j~M zv^D_UB;_ce$Om*kUzQ@j0{`SAOon)@GH0AH*@fAg!;_K0v4MlzY4UVtHJOzB;QZXw Ql8nr}bcSSbm=(kM03gU!D*ylh