Skip to content

Commit

Permalink
Merge pull request #6117 from multiversx/integrate-vm-common-events-0…
Browse files Browse the repository at this point in the history
…4-15

Integrate new vm-common: events for "claim developer rewards"
  • Loading branch information
andreibancioiu authored Apr 17, 2024
2 parents 8ddbfc8 + 046c7dc commit f0a917b
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
78 changes: 78 additions & 0 deletions integrationTests/vm/wasm/developerRewards/developerRewards_test.go
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])
})
}
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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
init
upgrade
doSomething
deployChild
getChildAddress
claimDeveloperRewardsOnChild
Binary file not shown.
16 changes: 11 additions & 5 deletions integrationTests/vm/wasm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (context *TestContext) initFeeHandlers() {
RewardsConfigByEpoch: []config.EpochRewardSettings{
{
LeaderPercentage: 0.1,
DeveloperPercentage: 0.0,
DeveloperPercentage: 0.3,
ProtocolSustainabilityPercentage: 0,
ProtocolSustainabilityAddress: testProtocolSustainabilityAddress,
TopUpGradientPoint: "1000000",
Expand Down Expand Up @@ -493,12 +493,18 @@ func (context *TestContext) TakeAccountBalanceSnapshot(participant *testParticip
participant.BalanceSnapshot = context.GetAccountBalance(participant)
}

// GetAccountBalance -
func (context *TestContext) GetAccountBalance(participant *testParticipant) *big.Int {
account, err := context.Accounts.GetExistingAccount(participant.Address)
// GetAccount -
func (context *TestContext) GetAccount(address []byte) state.UserAccountHandler {
account, err := context.Accounts.GetExistingAccount(address)
require.Nil(context.T, err)
accountAsUser := account.(state.UserAccountHandler)
return accountAsUser.GetBalance()
return accountAsUser
}

// GetAccountBalance -
func (context *TestContext) GetAccountBalance(participant *testParticipant) *big.Int {
account := context.GetAccount(participant.Address)
return account.GetBalance()
}

// GetAccountBalanceDelta -
Expand Down

0 comments on commit f0a917b

Please sign in to comment.