Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sstanculeanu committed Nov 11, 2024
1 parent 09a2751 commit 68dbf89
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 57 deletions.
14 changes: 14 additions & 0 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

import "github.com/multiversx/mx-chain-core-go/data"

// IsRelayedTxV3 returns true if the provided transaction is of type relayed v3
func IsRelayedTxV3(tx data.TransactionHandler) bool {
relayedTx, isRelayedV3 := tx.(data.RelayedTransactionHandler)
if !isRelayedV3 {
return false
}
hasValidRelayer := len(relayedTx.GetRelayerAddr()) == len(tx.GetSndAddr()) && len(relayedTx.GetRelayerAddr()) > 0
hasValidRelayerSignature := len(relayedTx.GetRelayerSignature()) == len(relayedTx.GetSignature()) && len(relayedTx.GetRelayerSignature()) > 0
return hasValidRelayer && hasValidRelayerSignature
}
41 changes: 41 additions & 0 deletions common/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package common

import (
"math/big"
"testing"

"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/stretchr/testify/require"
)

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

scr := &smartContractResult.SmartContractResult{}
require.False(t, IsRelayedTxV3(scr))

notRelayedTxV3 := &transaction.Transaction{
Nonce: 1,
Value: big.NewInt(100),
RcvAddr: []byte("receiver"),
SndAddr: []byte("sender0"),
GasPrice: 100,
GasLimit: 10,
Signature: []byte("signature"),
}
require.False(t, IsRelayedTxV3(notRelayedTxV3))

relayedTxV3 := &transaction.Transaction{
Nonce: 1,
Value: big.NewInt(100),
RcvAddr: []byte("receiver"),
SndAddr: []byte("sender1"),
GasPrice: 100,
GasLimit: 10,
Signature: []byte("signature"),
RelayerAddr: []byte("relayer"),
RelayerSignature: []byte("signature"),
}
require.True(t, IsRelayedTxV3(relayedTxV3))
}
1 change: 0 additions & 1 deletion integrationTests/multiShard/relayedTx/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ func createRelayedTxV3(
player.Nonce++
player.Balance.Sub(player.Balance, value)

relayer.Nonce++
txFee := economicsFee.ComputeTxFee(tx)
relayer.Balance.Sub(relayer.Balance, txFee)

Expand Down
9 changes: 1 addition & 8 deletions outport/process/transactionsfee/transactionsFeeProcessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,8 @@ func (tep *transactionsFeeProcessor) setGasUsedAndFeeBasedOnRefundValue(
refund *big.Int,
epoch uint32,
) {
tx := txWithResults.GetTxHandler()
relayedTx, isRelayedV3 := tx.(data.RelayedTransactionHandler)
if isRelayedV3 {
hasValidRelayer := len(relayedTx.GetRelayerAddr()) == len(tx.GetSndAddr()) && len(relayedTx.GetRelayerAddr()) > 0
hasValidRelayerSignature := len(relayedTx.GetRelayerSignature()) == len(relayedTx.GetSignature()) && len(relayedTx.GetRelayerSignature()) > 0
isRelayedV3 = hasValidRelayer && hasValidRelayerSignature
}
isValidUserTxAfterBaseCostActivation := !check.IfNilReflect(userTx) && tep.enableEpochsHandler.IsFlagEnabledInEpoch(common.FixRelayedBaseCostFlag, epoch)
if isValidUserTxAfterBaseCostActivation && !isRelayedV3 {
if isValidUserTxAfterBaseCostActivation && !common.IsRelayedTxV3(txWithResults.GetTxHandler()) {
gasUsed, fee := tep.txFeeCalculator.ComputeGasUsedAndFeeBasedOnRefundValue(userTx, refund)

tx := txWithResults.GetTxHandler()
Expand Down
10 changes: 2 additions & 8 deletions process/coordinator/transactionType.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,8 @@ func (tth *txTypeHandler) ComputeTransactionType(tx data.TransactionHandler) (pr
return process.InvalidTransaction, process.InvalidTransaction
}

relayedTxV3, ok := tx.(data.RelayedTransactionHandler)
if ok {
hasValidRelayer := len(relayedTxV3.GetRelayerAddr()) == len(tx.GetSndAddr()) && len(relayedTxV3.GetRelayerAddr()) > 0
hasValidRelayerSignature := len(relayedTxV3.GetRelayerSignature()) == len(relayedTxV3.GetSignature()) && len(relayedTxV3.GetRelayerSignature()) > 0
isRelayedV3 := hasValidRelayer && hasValidRelayerSignature
if isRelayedV3 {
return process.RelayedTxV3, process.RelayedTxV3
}
if common.IsRelayedTxV3(tx) {
return process.RelayedTxV3, process.RelayedTxV3
}

isEmptyAddress := tth.isDestAddressEmpty(tx)
Expand Down
30 changes: 13 additions & 17 deletions process/dataValidators/txValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"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"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
"github.com/multiversx/mx-chain-go/state"
Expand Down Expand Up @@ -108,24 +109,19 @@ func (txv *txValidator) getFeePayerAccount(
payerAccount := accountHandler

tx := interceptedTx.Transaction()
relayedTx, ok := tx.(data.RelayedTransactionHandler)
if ok {
hasValidRelayer := len(relayedTx.GetRelayerAddr()) == len(tx.GetSndAddr()) && len(relayedTx.GetRelayerAddr()) > 0
hasValidRelayerSignature := len(relayedTx.GetRelayerSignature()) == len(relayedTx.GetSignature()) && len(relayedTx.GetRelayerSignature()) > 0
isRelayedV3 := hasValidRelayer && hasValidRelayerSignature
if isRelayedV3 {
payerAddress = relayedTx.GetRelayerAddr()
relayerAccount, err := txv.accounts.GetExistingAccount(payerAddress)
if err != nil {
return nil, fmt.Errorf("%w for address %s and shard %d, err: %s",
process.ErrAccountNotFound,
txv.pubKeyConverter.SilentEncode(payerAddress, log),
txv.shardCoordinator.SelfId(),
err.Error(),
)
}
payerAccount = relayerAccount
if common.IsRelayedTxV3(tx) {
relayedTx := tx.(data.RelayedTransactionHandler)
payerAddress = relayedTx.GetRelayerAddr()
relayerAccount, err := txv.accounts.GetExistingAccount(payerAddress)
if err != nil {
return nil, fmt.Errorf("%w for address %s and shard %d, err: %s",
process.ErrAccountNotFound,
txv.pubKeyConverter.SilentEncode(payerAddress, log),
txv.shardCoordinator.SelfId(),
err.Error(),
)
}
payerAccount = relayerAccount
}
account, ok := payerAccount.(state.UserAccountHandler)
if !ok {
Expand Down
16 changes: 10 additions & 6 deletions process/economics/economicsData.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,7 @@ func (ed *economicsData) ComputeGasLimitInEpoch(tx data.TransactionWithFeeHandle
gasLimit += ed.getExtraGasLimitGuardedTx(epoch)
}

hasValidRelayer := len(txInstance.GetRelayerAddr()) == len(txInstance.GetSndAddr()) && len(txInstance.GetRelayerAddr()) > 0
hasValidRelayerSignature := len(txInstance.GetRelayerSignature()) == len(txInstance.GetSignature()) && len(txInstance.GetRelayerSignature()) > 0
isRelayedV3 := hasValidRelayer && hasValidRelayerSignature
if isRelayedV3 {
gasLimit += ed.MinGasLimitInEpoch(epoch)
}
gasLimit += ed.getExtraGasLimitRelayedTx(txInstance, epoch)
}

return gasLimit
Expand Down Expand Up @@ -614,6 +609,15 @@ func (ed *economicsData) ComputeGasLimitBasedOnBalanceInEpoch(tx data.Transactio
return totalGasLimit, nil
}

// getExtraGasLimitRelayedTx returns extra gas limit for relayed tx in a specific epoch
func (ed *economicsData) getExtraGasLimitRelayedTx(txInstance *transaction.Transaction, epoch uint32) uint64 {
if common.IsRelayedTxV3(txInstance) {
return ed.MinGasLimitInEpoch(epoch)
}

return 0
}

// IsInterfaceNil returns true if there is no value under the interface
func (ed *economicsData) IsInterfaceNil() bool {
return ed == nil
Expand Down
12 changes: 6 additions & 6 deletions process/transaction/baseProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,6 @@ func (txProc *baseTxProcessor) checkTxValues(
return err
}

// early exit for relayed v3, the cost will be compared with the sender balance
if isRelayedV3 {
return nil
}

if feePayer.GetBalance().Cmp(txFee) < 0 {
return fmt.Errorf("%w, has: %s, wanted: %s",
process.ErrInsufficientFee,
Expand All @@ -176,6 +171,11 @@ func (txProc *baseTxProcessor) checkTxValues(
txFee = core.SafeMul(tx.GasLimit, tx.GasPrice)
}

// early exit for relayed v3, the cost will be compared with the sender balance
if isRelayedV3 {
return nil
}

cost := big.NewInt(0).Add(txFee, tx.Value)
if feePayer.GetBalance().Cmp(cost) < 0 {
return process.ErrInsufficientFunds
Expand All @@ -188,7 +188,7 @@ func (txProc *baseTxProcessor) getFeePayer(
tx *transaction.Transaction,
acntSnd state.UserAccountHandler,
) (state.UserAccountHandler, bool, error) {
if !isRelayedTxV3(tx) {
if !common.IsRelayedTxV3(tx) {
return acntSnd, false, nil
}

Expand Down
10 changes: 3 additions & 7 deletions process/transaction/interceptedTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-crypto-go"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
logger "github.com/multiversx/mx-chain-logger-go"
Expand Down Expand Up @@ -211,7 +212,7 @@ func (inTx *InterceptedTransaction) CheckValidity() error {
}

func (inTx *InterceptedTransaction) checkRecursiveRelayed(userTx *transaction.Transaction) error {
if isRelayedTxV3(userTx) {
if common.IsRelayedTxV3(userTx) {
return process.ErrRecursiveRelayedTxIsNotAllowed
}

Expand All @@ -232,13 +233,8 @@ func isRelayedTx(funcName string) bool {
core.RelayedTransactionV2 == funcName
}

func isRelayedTxV3(tx *transaction.Transaction) bool {
return len(tx.RelayerAddr) == len(tx.SndAddr) && len(tx.RelayerAddr) > 0 &&
len(tx.RelayerSignature) == len(tx.Signature) && len(tx.RelayerSignature) > 0
}

func (inTx *InterceptedTransaction) verifyIfRelayedTxV3(tx *transaction.Transaction) error {
if !isRelayedTxV3(tx) {
if !common.IsRelayedTxV3(tx) {
return nil
}

Expand Down
16 changes: 12 additions & 4 deletions process/transaction/shardProcess.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,14 +605,20 @@ func (txProc *txProcessor) finishExecutionOfRelayedTx(
tx *transaction.Transaction,
userTx *transaction.Transaction,
) (vmcommon.ReturnCode, error) {
isUserTxOfRelayedV3 := isRelayedTxV3(tx)
isUserTxOfRelayedV3 := common.IsRelayedTxV3(tx)
relayedValue := tx.Value
if isUserTxOfRelayedV3 {
relayedValue = big.NewInt(0)
}

computedFees := txProc.computeRelayedTxFees(tx, userTx, isUserTxOfRelayedV3)
txHash, err := txProc.processTxAtRelayer(relayerAcnt, computedFees.totalFee, computedFees.relayerFee, tx, relayedValue)
txHash, err := txProc.processTxAtRelayer(
relayerAcnt,
computedFees.totalFee,
computedFees.relayerFee,
tx,
relayedValue,
isUserTxOfRelayedV3)
if err != nil {
return 0, err
}
Expand All @@ -629,7 +635,6 @@ func (txProc *txProcessor) finishExecutionOfRelayedTx(
relayedNonce := tx.Nonce
relayerAddr := tx.SndAddr
if isUserTxOfRelayedV3 && !check.IfNil(relayerAcnt) {
relayedNonce = relayerAcnt.GetNonce() - 1 // nonce already increased inside processTxAtRelayer
relayerAddr = tx.RelayerAddr
}

Expand All @@ -642,6 +647,7 @@ func (txProc *txProcessor) processTxAtRelayer(
relayerFee *big.Int,
tx *transaction.Transaction,
valueToSubFromRelayer *big.Int,
isUserTxOfRelayedV3 bool,
) ([]byte, error) {
txHash, err := core.CalculateHash(txProc.marshalizer, txProc.hasher, tx)
if err != nil {
Expand All @@ -659,7 +665,9 @@ func (txProc *txProcessor) processTxAtRelayer(
return nil, err
}

relayerAcnt.IncreaseNonce(1)
if !isUserTxOfRelayedV3 { // won't increase relayer nonce for v3
relayerAcnt.IncreaseNonce(1)
}
err = txProc.accounts.SaveAccount(relayerAcnt)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions process/transaction/shardProcess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2768,6 +2768,7 @@ func TestTxProcessor_ProcessRelayedTransactionV3(t *testing.T) {
IsGuardedCalled: func() bool {
return true
},
Balance: big.NewInt(1),
}, nil
}

Expand Down

0 comments on commit 68dbf89

Please sign in to comment.