Skip to content

Commit 32b3365

Browse files
authored
Merge pull request #7116 from multiversx/fix-tx-cost-endpoint
Fixes /transaction/cost endpoint
2 parents 8c3665e + 3e6825e commit 32b3365

27 files changed

+508
-60
lines changed

api/groups/transactionGroup.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/gin-gonic/gin"
1212
"github.com/multiversx/mx-chain-core-go/core"
1313
"github.com/multiversx/mx-chain-core-go/core/check"
14+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
1415
"github.com/multiversx/mx-chain-core-go/data/transaction"
1516
"github.com/multiversx/mx-chain-go/api/errors"
1617
"github.com/multiversx/mx-chain-go/api/middleware"
@@ -24,11 +25,13 @@ import (
2425
const (
2526
sendTransactionEndpoint = "/transaction/send"
2627
simulateTransactionEndpoint = "/transaction/simulate"
28+
simulateSCRCostEndpoint = "/transaction/cost-scr"
2729
sendMultipleTransactionsEndpoint = "/transaction/send-multiple"
2830
getTransactionEndpoint = "/transaction/:hash"
2931
getScrsByTxHashEndpoint = "/transaction/scrs-by-tx-hash/:txhash"
3032
sendTransactionPath = "/send"
3133
simulateTransactionPath = "/simulate"
34+
simulateSCRCostPath = "/cost-scr"
3235
costPath = "/cost"
3336
sendMultiplePath = "/send-multiple"
3437
getTransactionPath = "/:txhash"
@@ -51,6 +54,7 @@ type transactionFacadeHandler interface {
5154
ValidateTransactionForSimulation(tx *transaction.Transaction, checkSignature bool) error
5255
SendBulkTransactions([]*transaction.Transaction) (uint64, error)
5356
SimulateTransactionExecution(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error)
57+
SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error)
5458
GetTransaction(hash string, withResults bool) (*transaction.ApiTransactionResult, error)
5559
GetSCRsByTxHash(txHash string, scrHash string) ([]*transaction.ApiSmartContractResult, error)
5660
GetTransactionsPool(fields string) (*common.TransactionsPoolAPIResponse, error)
@@ -103,6 +107,17 @@ func NewTransactionGroup(facade transactionFacadeHandler) (*transactionGroup, er
103107
},
104108
},
105109
},
110+
{
111+
Path: simulateSCRCostPath,
112+
Method: http.MethodPost,
113+
Handler: tg.simulateSCR,
114+
AdditionalMiddlewares: []shared.AdditionalMiddleware{
115+
{
116+
Middleware: middleware.CreateEndpointThrottlerFromFacade(simulateSCRCostEndpoint, facade),
117+
Position: shared.Before,
118+
},
119+
},
120+
},
106121
{
107122
Path: costPath,
108123
Method: http.MethodPost,
@@ -168,6 +183,46 @@ type TxResponse struct {
168183
Timestamp uint64 `json:"timestamp"`
169184
}
170185

186+
func (tg *transactionGroup) simulateSCR(c *gin.Context) {
187+
var scr = smartContractResult.SmartContractResult{}
188+
err := c.ShouldBindJSON(&scr)
189+
if err != nil {
190+
c.JSON(
191+
http.StatusBadRequest,
192+
shared.GenericAPIResponse{
193+
Data: nil,
194+
Error: fmt.Sprintf("%s: %s", errors.ErrValidation.Error(), err.Error()),
195+
Code: shared.ReturnCodeRequestError,
196+
},
197+
)
198+
return
199+
}
200+
201+
start := time.Now()
202+
executionResults, err := tg.getFacade().SimulateSCRExecutionCost(&scr)
203+
logging.LogAPIActionDurationIfNeeded(start, "API call: SimulateSCRExecution")
204+
if err != nil {
205+
c.JSON(
206+
http.StatusInternalServerError,
207+
shared.GenericAPIResponse{
208+
Data: nil,
209+
Error: err.Error(),
210+
Code: shared.ReturnCodeInternalError,
211+
},
212+
)
213+
return
214+
}
215+
216+
c.JSON(
217+
http.StatusOK,
218+
shared.GenericAPIResponse{
219+
Data: executionResults,
220+
Error: "",
221+
Code: shared.ReturnCodeSuccess,
222+
},
223+
)
224+
}
225+
171226
// simulateTransaction will receive a transaction from the client and will simulate its execution and return the results
172227
func (tg *transactionGroup) simulateTransaction(c *gin.Context) {
173228
var ftx = transaction.FrontendTransaction{}

api/mock/facadeStub.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mock
22

33
import (
44
"encoding/hex"
5+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
56
"math/big"
67

78
"github.com/multiversx/mx-chain-core-go/core"
@@ -99,6 +100,16 @@ type FacadeStub struct {
99100
P2PPrometheusMetricsEnabledCalled func() bool
100101
AuctionListHandler func() ([]*common.AuctionListValidatorAPIResponse, error)
101102
GetSCRsByTxHashCalled func(txHash string, scrHash string) ([]*transaction.ApiSmartContractResult, error)
103+
SimulateSCRExecutionCostCalled func(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error)
104+
}
105+
106+
// SimulateSCRExecutionCost -
107+
func (f *FacadeStub) SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error) {
108+
if f.SimulateSCRExecutionCostCalled != nil {
109+
return f.SimulateSCRExecutionCostCalled(scr)
110+
}
111+
112+
return nil, nil
102113
}
103114

104115
// GetSCRsByTxHash -

api/shared/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
99
"github.com/multiversx/mx-chain-core-go/data/api"
1010
"github.com/multiversx/mx-chain-core-go/data/esdt"
11+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
1112
"github.com/multiversx/mx-chain-core-go/data/transaction"
1213
"github.com/multiversx/mx-chain-core-go/data/validator"
1314
"github.com/multiversx/mx-chain-core-go/data/vm"
@@ -112,6 +113,7 @@ type FacadeHandler interface {
112113
ValidateTransactionForSimulation(tx *transaction.Transaction, checkSignature bool) error
113114
SendBulkTransactions([]*transaction.Transaction) (uint64, error)
114115
SimulateTransactionExecution(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error)
116+
SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error)
115117
GetTransaction(hash string, withResults bool) (*transaction.ApiTransactionResult, error)
116118
ComputeTransactionGasLimit(tx *transaction.Transaction) (*transaction.CostResponse, error)
117119
EncodeAddressPubkey(pk []byte) (string, error)

cmd/node/config/api.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@
214214
# /transaction/cost will receive a single transaction in JSON format and will return the estimated cost of it
215215
{ Name = "/cost", Open = true },
216216

217+
# /transaction/cost-scr will receive a single smart contract result in JSON format and will return the estimated cost of it
218+
{ Name = "/cost-scr", Open = true },
219+
217220
# /transaction/pool will return the hashes of the transactions that are currently in the pool
218221
# /transaction/pool?fields=sender,receiver,gaslimit,gasprice will return hashes and all the optional fields mentioned that are currently in the pool
219222
# /transaction/pool?by-sender=erd1... will return the hashes of the transactions that are currently in the pool for the sender

consensus/spos/bls/v2/subroundBlock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ func (sr *subroundBlock) processReceivedBlock(
567567

568568
defer func() {
569569
sw.Stop("processReceivedBlock")
570-
log.Info("time measurements of processReceivedBlock", sw.GetMeasurements()...)
570+
log.Debug("time measurements of processReceivedBlock", sw.GetMeasurements()...)
571571

572572
sr.SetProcessingBlock(false)
573573
}()

facade/initial/initialNodeFacade.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
1010
"github.com/multiversx/mx-chain-core-go/data/api"
1111
"github.com/multiversx/mx-chain-core-go/data/esdt"
12+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
1213
"github.com/multiversx/mx-chain-core-go/data/transaction"
1314
"github.com/multiversx/mx-chain-core-go/data/validator"
1415
"github.com/multiversx/mx-chain-core-go/data/vm"
@@ -171,6 +172,11 @@ func (inf *initialNodeFacade) SimulateTransactionExecution(_ *transaction.Transa
171172
return nil, errNodeStarting
172173
}
173174

175+
// SimulateSCRExecutionCost returns nil and error
176+
func (inf *initialNodeFacade) SimulateSCRExecutionCost(_ *smartContractResult.SmartContractResult) (*transaction.CostResponse, error) {
177+
return nil, errNodeStarting
178+
}
179+
174180
// GetTransaction returns nil and error
175181
func (inf *initialNodeFacade) GetTransaction(_ string, _ bool) (*transaction.ApiTransactionResult, error) {
176182
return nil, errNodeStarting

facade/interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
1010
"github.com/multiversx/mx-chain-core-go/data/api"
1111
"github.com/multiversx/mx-chain-core-go/data/esdt"
12+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
1213
"github.com/multiversx/mx-chain-core-go/data/transaction"
1314
"github.com/multiversx/mx-chain-core-go/data/validator"
1415
"github.com/multiversx/mx-chain-go/common"
@@ -116,6 +117,7 @@ type NodeHandler interface {
116117

117118
// TransactionSimulatorProcessor defines the actions which a transaction simulator processor has to implement
118119
type TransactionSimulatorProcessor interface {
120+
ProcessSCR(scr *smartContractResult.SmartContractResult, currentHeader coreData.HeaderHandler) (*txSimData.SimulationResultsWithVMOutput, error)
119121
ProcessTx(tx *transaction.Transaction, currentHeader coreData.HeaderHandler) (*txSimData.SimulationResultsWithVMOutput, error)
120122
IsInterfaceNil() bool
121123
}
@@ -125,6 +127,7 @@ type ApiResolver interface {
125127
ExecuteSCQuery(query *process.SCQuery) (*vmcommon.VMOutput, common.BlockInfo, error)
126128
ComputeTransactionGasLimit(tx *transaction.Transaction) (*transaction.CostResponse, error)
127129
SimulateTransactionExecution(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error)
130+
SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error)
128131
StatusMetrics() external.StatusMetricsHandler
129132
GetTotalStakedValue(ctx context.Context) (*api.StakeValues, error)
130133
GetDirectStakedList(ctx context.Context) ([]*api.DirectStakedValue, error)

facade/mock/apiResolverStub.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package mock
22

33
import (
44
"context"
5+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
56

67
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
78
"github.com/multiversx/mx-chain-core-go/data/api"
@@ -51,6 +52,16 @@ type ApiResolverStub struct {
5152
GetWaitingManagedKeysCalled func() ([]string, error)
5253
GetWaitingEpochsLeftForPublicKeyCalled func(publicKey string) (uint32, error)
5354
GetSCRsByTxHashCalled func(txHash string, scrHash string) ([]*transaction.ApiSmartContractResult, error)
55+
SimulateSCRExecutionCostCalled func(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error)
56+
}
57+
58+
// SimulateSCRExecutionCost -
59+
func (ars *ApiResolverStub) SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error) {
60+
if ars.SimulateSCRExecutionCostCalled != nil {
61+
return ars.SimulateSCRExecutionCostCalled(scr)
62+
}
63+
64+
return nil, nil
5465
}
5566

5667
// GetSCRsByTxHash -

facade/nodeFacade.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/multiversx/mx-chain-core-go/data/alteredAccount"
1616
apiData "github.com/multiversx/mx-chain-core-go/data/api"
1717
"github.com/multiversx/mx-chain-core-go/data/esdt"
18+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
1819
"github.com/multiversx/mx-chain-core-go/data/transaction"
1920
"github.com/multiversx/mx-chain-core-go/data/validator"
2021
"github.com/multiversx/mx-chain-core-go/data/vm"
@@ -307,6 +308,11 @@ func (nf *nodeFacade) SimulateTransactionExecution(tx *transaction.Transaction)
307308
return nf.apiResolver.SimulateTransactionExecution(tx)
308309
}
309310

311+
// SimulateSCRExecutionCost will simulate a smart contract results and will return the gas cost
312+
func (nf *nodeFacade) SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error) {
313+
return nf.apiResolver.SimulateSCRExecutionCost(scr)
314+
}
315+
310316
// GetTransaction gets the transaction with a specified hash
311317
func (nf *nodeFacade) GetTransaction(hash string, withResults bool) (*transaction.ApiTransactionResult, error) {
312318
return nf.apiResolver.GetTransaction(hash, withResults)

factory/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
crypto "github.com/multiversx/mx-chain-crypto-go"
1717
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
1818

19+
"github.com/multiversx/mx-chain-core-go/data/smartContractResult"
1920
"github.com/multiversx/mx-chain-go/cmd/node/factory"
2021
"github.com/multiversx/mx-chain-go/common"
2122
cryptoCommon "github.com/multiversx/mx-chain-go/common/crypto"
@@ -267,6 +268,7 @@ type NetworkComponentsHandler interface {
267268

268269
// TransactionEvaluator defines the transaction evaluator actions
269270
type TransactionEvaluator interface {
271+
SimulateSCRExecutionCost(scr *smartContractResult.SmartContractResult) (*transaction.CostResponse, error)
270272
SimulateTransactionExecution(tx *transaction.Transaction) (*txSimData.SimulationResultsWithVMOutput, error)
271273
ComputeTransactionGasLimit(tx *transaction.Transaction) (*transaction.CostResponse, error)
272274
IsInterfaceNil() bool

0 commit comments

Comments
 (0)