Skip to content

Commit

Permalink
Merge pull request from GHSA-7xpv-4pm9-xch2
Browse files Browse the repository at this point in the history
fixed issue on metachain when treating tx with invalid username
  • Loading branch information
iulianpascalau authored Apr 19, 2023
2 parents 3f77428 + 727ce94 commit 9729547
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions process/transaction/metaProcess.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package transaction

import (
"errors"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/transaction"
Expand Down Expand Up @@ -108,6 +110,13 @@ func (txProc *metaTxProcessor) ProcessTransaction(tx *transaction.Transaction) (

err = txProc.checkTxValues(tx, acntSnd, acntDst, false)
if err != nil {
if errors.Is(err, process.ErrUserNameDoesNotMatchInCrossShardTx) {
errProcessIfErr := txProc.processIfTxErrorCrossShard(tx, err.Error())
if errProcessIfErr != nil {
return 0, errProcessIfErr
}
return vmcommon.UserError, nil
}
return 0, err
}

Expand Down
42 changes: 42 additions & 0 deletions process/transaction/metaProcess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/coordinator"
"github.com/multiversx/mx-chain-go/process/mock"
txproc "github.com/multiversx/mx-chain-go/process/transaction"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/state"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/hashingMocks"
stateMock "github.com/multiversx/mx-chain-go/testscommon/state"
"github.com/multiversx/mx-chain-go/vm"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-common-go/builtInFunctions"
"github.com/multiversx/mx-chain-vm-common-go/parsers"
Expand Down Expand Up @@ -446,3 +449,42 @@ func TestMetaTxProcessor_ProcessTransactionBuiltInCallTxShouldWork(t *testing.T)
assert.True(t, builtInCalled)
assert.Equal(t, 0, saveAccountCalled)
}

func TestMetaTxProcessor_ProcessTransactionWithInvalidUsernameShouldNotError(t *testing.T) {
t.Parallel()

tx := &transaction.Transaction{}
tx.Nonce = 0
tx.SndAddr = bytes.Repeat([]byte{1}, 32)
tx.RcvAddr = vm.GovernanceSCAddress
tx.RcvUserName = []byte("username")
tx.Value = big.NewInt(45)
tx.GasPrice = 1
tx.GasLimit = 1

acntDst, err := state.NewUserAccount(tx.RcvAddr)
assert.Nil(t, err)

called := false
adb := createAccountStub(tx.SndAddr, tx.RcvAddr, acntDst, acntDst)
scProcessor := &testscommon.SCProcessorMock{
ProcessIfErrorCalled: func(acntSnd state.UserAccountHandler, txHash []byte, tx data.TransactionHandler, returnCode string, returnMessage []byte, snapshot int, gasLocked uint64) error {
called = true
return nil
},
}

args := createMockNewMetaTxArgs()
args.Accounts = adb
args.ScProcessor = scProcessor
args.ShardCoordinator, _ = sharding.NewMultiShardCoordinator(3, core.MetachainShardId)
txProc, _ := txproc.NewMetaTxProcessor(args)

err = txProc.VerifyTransaction(tx)
assert.Equal(t, err, process.ErrUserNameDoesNotMatchInCrossShardTx)

returnCode, err := txProc.ProcessTransaction(tx)
assert.Nil(t, err)
assert.Equal(t, vmcommon.UserError, returnCode)
assert.True(t, called)
}

0 comments on commit 9729547

Please sign in to comment.