-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6117 from multiversx/integrate-vm-common-events-0…
…4-15 Integrate new vm-common: events for "claim developer rewards"
- Loading branch information
Showing
7 changed files
with
206 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
integrationTests/vm/wasm/developerRewards/developerRewards_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package transfers | ||
|
||
import ( | ||
"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") | ||
} | ||
|
||
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() | ||
rewardBig := context.GetAccount(contractAddress).GetDeveloperReward() | ||
reward := rewardBig.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, 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") | ||
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() | ||
rewardBig := context.GetAccount(chilContractdAddress).GetDeveloperReward() | ||
reward := rewardBig.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, rewardBig.Bytes(), events[0].GetTopics()[0]) | ||
require.Equal(t, parentContractAddress, events[0].GetTopics()[1]) | ||
}) | ||
} |
108 changes: 108 additions & 0 deletions
108
integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
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 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 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_green; | ||
|
||
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 claimDeveloperRewardsOnChild() | ||
{ | ||
byte *childAddress = zero32_red; | ||
storageLoad(storageKeyChildAddress, sizeof(storageKeyChildAddress) - 1, childAddress); | ||
|
||
createAsyncCall( | ||
childAddress, | ||
zeroEGLD, | ||
functionNameClaimDeveloperRewards, | ||
sizeof(functionNameClaimDeveloperRewards) - 1, | ||
0, | ||
0, | ||
0, | ||
0, | ||
gasLimitClaimDeveloperRewards, | ||
0); | ||
} |
6 changes: 6 additions & 0 deletions
6
integrationTests/vm/wasm/testdata/developer-rewards/developer_rewards.export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
init | ||
upgrade | ||
doSomething | ||
deployChild | ||
getChildAddress | ||
claimDeveloperRewardsOnChild |
Binary file added
BIN
+973 Bytes
integrationTests/vm/wasm/testdata/developer-rewards/output/developer_rewards.wasm
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters