Skip to content

Commit

Permalink
Merge pull request #5397 from multiversx/fix-missing-complete-tx-event
Browse files Browse the repository at this point in the history
Fix missing log event.
  • Loading branch information
miiu96 authored Jul 11, 2023
2 parents 471c74b + 54229c4 commit 897f942
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//go:build !race
// +build !race

package multiShard

import (
"encoding/hex"
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/integrationTests/vm"
"github.com/multiversx/mx-chain-go/integrationTests/vm/txsFee/utils"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/stretchr/testify/require"
)

func TestDeployContractAndTransferValueSCProcessorV1(t *testing.T) {
testDeployContractAndTransferValue(t, 1000)
}

func TestDeployContractAndTransferValueSCProcessorV2(t *testing.T) {
testDeployContractAndTransferValue(t, 0)
}

func testDeployContractAndTransferValue(t *testing.T, scProcessorV2EnabledEpoch uint32) {
testContextSource, err := vm.CreatePreparedTxProcessorWithVMsMultiShard(0, config.EnableEpochs{})
require.Nil(t, err)
defer testContextSource.Close()

configEnabledEpochs := config.EnableEpochs{}
configEnabledEpochs.SCProcessorV2EnableEpoch = scProcessorV2EnabledEpoch

testContextDst, err := vm.CreatePreparedTxProcessorWithVMsMultiShard(1, configEnabledEpochs)
require.Nil(t, err)
defer testContextDst.Close()

pathToContract := "../testdata/transferValueContract/transferValue.wasm"

// deploy contract on the source context
scAddrShardSource, ownerAddress := utils.DoDeployWithCustomParams(t, testContextSource, pathToContract, big.NewInt(100000000000), 20000, nil)
utils.CleanAccumulatedIntermediateTransactions(t, testContextSource)

// deploy the same contract on the destination context (in this test we need a payable contract)
scAddressShardDestination, _ := utils.DoDeployWithCustomParams(t, testContextDst, pathToContract, big.NewInt(100000000000), 20000, nil)
utils.CleanAccumulatedIntermediateTransactions(t, testContextDst)
testContextDst.TxsLogsProcessor.Clean()

receiverHex := hex.EncodeToString(scAddressShardDestination)
tx := vm.CreateTransaction(1, big.NewInt(1000000), ownerAddress, scAddrShardSource, 10, 20000, []byte("send_egld@"+receiverHex))
retCode, err := testContextSource.TxProcessor.ProcessTransaction(tx)
require.Equal(t, vmcommon.Ok, retCode)
require.Nil(t, err)

intermediateTxs := testContextSource.GetIntermediateTransactions(t)
require.NotNil(t, intermediateTxs)

utils.ProcessSCRResult(t, testContextDst, intermediateTxs[0], vmcommon.Ok, nil)
logs := testContextDst.TxsLogsProcessor.GetAllCurrentLogs()
require.Len(t, logs, 1)

generatedLogIdentifier := string(logs[0].LogHandler.GetLogEvents()[0].GetIdentifier())
require.Equal(t, generatedLogIdentifier, core.CompletedTxEventIdentifier)
}
Binary file not shown.
27 changes: 23 additions & 4 deletions integrationTests/vm/txsFee/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package utils

import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"io/ioutil"
"math/big"
"math/rand"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -88,6 +88,23 @@ func doDeployInternal(
return scAddr, owner
}

func generateRandomArray(length int) []byte {
result := make([]byte, length)
_, _ = rand.Read(result)

return result
}

func generateAddressForContextShardID(testContext *vm.VMTestContext) []byte {
shardID := testContext.ShardCoordinator.SelfId()
for {
address := generateRandomArray(32)
if testContext.ShardCoordinator.ComputeId(address) == shardID {
return address
}
}
}

// DoDeployWithCustomParams -
func DoDeployWithCustomParams(
tb testing.TB,
Expand All @@ -97,13 +114,14 @@ func DoDeployWithCustomParams(
gasLimit uint64,
contractHexParams []string,
) (scAddr []byte, owner []byte) {
owner = []byte("12345678901234567890123456789011")
owner = generateAddressForContextShardID(testContext)
account, err := testContext.Accounts.LoadAccount(owner)
require.Nil(tb, err)
senderNonce := account.GetNonce()
gasPrice := uint64(10)

_, _ = vm.CreateAccount(testContext.Accounts, owner, 0, senderBalance)
_, err = vm.CreateAccount(testContext.Accounts, owner, 0, senderBalance)
require.Nil(tb, err)

scCode := wasm.GetSCCode(pathToContract)
txData := wasm.CreateDeployTxData(scCode)
Expand Down Expand Up @@ -371,7 +389,8 @@ const letterBytes = "abcdefghijklmnopqrstuvwxyz"
func randStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
idxBig, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letterBytes))))
b[i] = letterBytes[idxBig.Int64()]
}
return string(b)
}
Expand Down
4 changes: 3 additions & 1 deletion process/smartContract/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -2835,7 +2835,9 @@ func (sc *scProcessor) processSimpleSCR(
return err
}

if isReturnOKTxHandler(scResult) {
isTransferValueOnlySCR := len(scResult.Data) == 0 && scResult.Value.Cmp(zero) > 0
shouldGenerateCompleteEvent := isReturnOKTxHandler(scResult) || isTransferValueOnlySCR
if shouldGenerateCompleteEvent {
completedTxLog := createCompleteEventLog(scResult, txHash)
ignorableError := sc.txLogsProcessor.SaveLog(txHash, scResult, []*vmcommon.LogEntry{completedTxLog})
if ignorableError != nil {
Expand Down
4 changes: 3 additions & 1 deletion process/smartContract/processorV2/processV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,9 @@ func (sc *scProcessor) processSimpleSCR(
return err
}

if isReturnOKTxHandler(scResult) {
isTransferValueOnlySCR := len(scResult.Data) == 0 && scResult.Value.Cmp(zero) > 0
shouldGenerateCompleteEvent := isReturnOKTxHandler(scResult) || isTransferValueOnlySCR
if shouldGenerateCompleteEvent {
completedTxLog := createCompleteEventLog(scResult, txHash)
ignorableError := sc.txLogsProcessor.SaveLog(txHash, scResult, []*vmcommon.LogEntry{completedTxLog})
if ignorableError != nil {
Expand Down

0 comments on commit 897f942

Please sign in to comment.