Skip to content

Commit

Permalink
Merge pull request #5880 from multiversx/fix-tx-cost-api-route-rc
Browse files Browse the repository at this point in the history
Fix `/transaction/cost` api route
  • Loading branch information
iulianpascalau authored Feb 16, 2024
2 parents e89fd5f + e097862 commit 71c9b7f
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 18 deletions.
3 changes: 2 additions & 1 deletion facade/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
coreData "github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
"github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-core-go/data/esdt"
Expand Down Expand Up @@ -106,7 +107,7 @@ type NodeHandler interface {

// TransactionSimulatorProcessor defines the actions which a transaction simulator processor has to implement
type TransactionSimulatorProcessor interface {
ProcessTx(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error)
ProcessTx(tx *transaction.Transaction, currentHeader coreData.HeaderHandler) (*txSimData.SimulationResultsWithVMOutput, error)
IsInterfaceNil() bool
}

Expand Down
5 changes: 5 additions & 0 deletions factory/processing/txSimulatorProcessComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (pcf *processComponentsFactory) createAPITransactionEvaluator() (factory.Tr
Accounts: simulationAccountsDB,
ShardCoordinator: pcf.bootstrapComponents.ShardCoordinator(),
EnableEpochsHandler: pcf.coreData.EnableEpochsHandler(),
BlockChain: pcf.data.Blockchain(),
})

return apiTransactionEvaluator, vmContainerFactory, err
Expand Down Expand Up @@ -141,6 +142,8 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorForMeta(
return args, nil, nil, err
}

args.BlockChainHook = vmContainerFactory.BlockChainHookImpl()

vmContainer, err := vmContainerFactory.Create()
if err != nil {
return args, nil, nil, err
Expand Down Expand Up @@ -301,6 +304,8 @@ func (pcf *processComponentsFactory) createArgsTxSimulatorProcessorShard(
return args, nil, nil, err
}

args.BlockChainHook = vmContainerFactory.BlockChainHookImpl()

err = builtInFuncFactory.SetPayableHandler(vmContainerFactory.BlockChainHookImpl())
if err != nil {
return args, nil, nil, err
Expand Down
2 changes: 2 additions & 0 deletions integrationTests/testProcessorNodeWithTestWebServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func createFacadeComponents(tpn *TestProcessorNode) nodeFacade.ApiResolver {
Hasher: TestHasher,
VMOutputCacher: &testscommon.CacherMock{},
DataFieldParser: dataFieldParser,
BlockChainHook: tpn.BlockchainHook,
}

txSimulator, err := transactionEvaluator.NewTransactionSimulator(argSimulator)
Expand All @@ -194,6 +195,7 @@ func createFacadeComponents(tpn *TestProcessorNode) nodeFacade.ApiResolver {
Accounts: wrappedAccounts,
ShardCoordinator: tpn.ShardCoordinator,
EnableEpochsHandler: tpn.EnableEpochsHandler,
BlockChain: tpn.BlockChain,
}
apiTransactionEvaluator, err := transactionEvaluator.NewAPITransactionEvaluator(argsTransactionEvaluator)
log.LogIfError(err)
Expand Down
8 changes: 8 additions & 0 deletions integrationTests/vm/testInitializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ func CreateTxProcessorWithOneSCExecutorWithVMs(
epochNotifierInstance process.EpochNotifier,
guardianChecker process.GuardianChecker,
roundNotifierInstance process.RoundNotifier,
chainHandler data.ChainHandler,
) (*ResultsCreateTxProcessor, error) {
if check.IfNil(poolsHolder) {
poolsHolder = dataRetrieverMock.NewPoolsHolderMock()
Expand Down Expand Up @@ -980,6 +981,7 @@ func CreateTxProcessorWithOneSCExecutorWithVMs(
Marshalizer: integrationtests.TestMarshalizer,
Hasher: integrationtests.TestHasher,
DataFieldParser: dataFieldParser,
BlockChainHook: blockChainHook,
}

argsNewSCProcessor.VMOutputCacher = txSimulatorProcessorArgs.VMOutputCacher
Expand All @@ -1006,6 +1008,7 @@ func CreateTxProcessorWithOneSCExecutorWithVMs(
Accounts: simulationAccountsDB,
ShardCoordinator: shardCoordinator,
EnableEpochsHandler: argsNewSCProcessor.EnableEpochsHandler,
BlockChain: chainHandler,
}
apiTransactionEvaluator, err := transactionEvaluator.NewAPITransactionEvaluator(argsTransactionEvaluator)
if err != nil {
Expand Down Expand Up @@ -1128,6 +1131,7 @@ func CreatePreparedTxProcessorAndAccountsWithVMsWithRoundsConfig(
epochNotifierInstance,
guardedAccountHandler,
roundNotifierInstance,
chainHandler,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1279,6 +1283,7 @@ func CreatePreparedTxProcessorWithVMConfigWithShardCoordinatorDBAndGasAndRoundCo
epochNotifierInstance,
guardedAccountHandler,
roundNotifierInstance,
chainHandler,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1374,6 +1379,7 @@ func CreateTxProcessorArwenVMWithGasScheduleAndRoundConfig(
epochNotifierInstance,
guardedAccountHandler,
roundNotifierInstance,
chainHandler,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1455,6 +1461,7 @@ func CreateTxProcessorArwenWithVMConfigAndRoundConfig(
epochNotifierInstance,
guardedAccountHandler,
roundNotifierInstance,
chainHandler,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1885,6 +1892,7 @@ func CreatePreparedTxProcessorWithVMsMultiShardRoundVMConfig(
epochNotifierInstance,
guardedAccountHandler,
roundNotifierInstance,
chainHandler,
)
if err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions process/mock/transactionSimulatorStub.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package mock

import (
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/transaction"
txSimData "github.com/multiversx/mx-chain-go/process/transactionEvaluator/data"
)

// TransactionSimulatorStub -
type TransactionSimulatorStub struct {
ProcessTxCalled func(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error)
ProcessTxCalled func(tx *transaction.Transaction, currentHeader data.HeaderHandler) (*txSimData.SimulationResultsWithVMOutput, error)
}

// ProcessTx -
func (tss *TransactionSimulatorStub) ProcessTx(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error) {
func (tss *TransactionSimulatorStub) ProcessTx(tx *transaction.Transaction, currentHeader data.HeaderHandler) (*txSimData.SimulationResultsWithVMOutput, error) {
if tss.ProcessTxCalled != nil {
return tss.ProcessTxCalled(tx)
return tss.ProcessTxCalled(tx, currentHeader)
}

return nil, nil
Expand Down
24 changes: 21 additions & 3 deletions process/transactionEvaluator/transactionEvaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/facade"
Expand All @@ -32,6 +33,7 @@ type ArgsApiTransactionEvaluator struct {
Accounts state.AccountsAdapterWithClean
ShardCoordinator sharding.Coordinator
EnableEpochsHandler common.EnableEpochsHandler
BlockChain data.ChainHandler
}

type apiTransactionEvaluator struct {
Expand All @@ -41,6 +43,7 @@ type apiTransactionEvaluator struct {
feeHandler process.FeeHandler
txSimulator facade.TransactionSimulatorProcessor
enableEpochsHandler common.EnableEpochsHandler
blockChain data.ChainHandler
mutExecution sync.RWMutex
}

Expand All @@ -64,6 +67,9 @@ func NewAPITransactionEvaluator(args ArgsApiTransactionEvaluator) (*apiTransacti
if check.IfNil(args.EnableEpochsHandler) {
return nil, process.ErrNilEnableEpochsHandler
}
if check.IfNil(args.BlockChain) {
return nil, process.ErrNilBlockChain
}
err := core.CheckHandlerCompatibility(args.EnableEpochsHandler, []core.EnableEpochFlag{
common.CleanUpInformativeSCRsFlag,
})
Expand All @@ -78,6 +84,7 @@ func NewAPITransactionEvaluator(args ArgsApiTransactionEvaluator) (*apiTransacti
accounts: args.Accounts,
shardCoordinator: args.ShardCoordinator,
enableEpochsHandler: args.EnableEpochsHandler,
blockChain: args.BlockChain,
}

return tce, nil
Expand All @@ -91,7 +98,9 @@ func (ate *apiTransactionEvaluator) SimulateTransactionExecution(tx *transaction
ate.mutExecution.Unlock()
}()

return ate.txSimulator.ProcessTx(tx)
currentHeader := ate.getCurrentBlockHeader()

return ate.txSimulator.ProcessTx(tx, currentHeader)
}

// ComputeTransactionGasLimit will calculate how many gas units a transaction will consume
Expand Down Expand Up @@ -140,8 +149,8 @@ func (ate *apiTransactionEvaluator) simulateTransactionCost(tx *transaction.Tran
}

costResponse := &transaction.CostResponse{}

res, err := ate.txSimulator.ProcessTx(tx)
currentHeader := ate.getCurrentBlockHeader()
res, err := ate.txSimulator.ProcessTx(tx, currentHeader)
if err != nil {
costResponse.ReturnMessage = err.Error()
return costResponse, nil
Expand Down Expand Up @@ -228,6 +237,15 @@ func (ate *apiTransactionEvaluator) addMissingFieldsIfNeeded(tx *transaction.Tra
return nil
}

func (ate *apiTransactionEvaluator) getCurrentBlockHeader() data.HeaderHandler {
currentHeader := ate.blockChain.GetCurrentBlockHeader()
if check.IfNil(currentHeader) {
return ate.blockChain.GetGenesisHeader()
}

return currentHeader
}

func (ate *apiTransactionEvaluator) getTxGasLimit(tx *transaction.Transaction) (uint64, error) {
selfShardID := ate.shardCoordinator.SelfId()
maxGasLimitPerBlock := ate.feeHandler.MaxGasLimitPerBlock(selfShardID) - 1
Expand Down
Loading

0 comments on commit 71c9b7f

Please sign in to comment.