From a05d765a4fd9166c61ed5a7cb89a0cd048d31a15 Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Wed, 14 Aug 2024 09:27:56 +0300 Subject: [PATCH 1/2] - optimized GenerateBlocksUntilTransactionIsProcessed --- pkg/facade/simulatorFacade.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/facade/simulatorFacade.go b/pkg/facade/simulatorFacade.go index f8dda28..c40c3c8 100644 --- a/pkg/facade/simulatorFacade.go +++ b/pkg/facade/simulatorFacade.go @@ -175,11 +175,6 @@ func (sf *simulatorFacade) GetObserversInfo() (map[uint32]*dtoc.ObserverInfo, er // GenerateBlocksUntilTransactionIsProcessed generate blocks until the status of the provided transaction hash is processed func (sf *simulatorFacade) GenerateBlocksUntilTransactionIsProcessed(txHash string) error { for i := 0; i < maxNumOfBlockToGenerateUntilTxProcessed; i++ { - err := sf.GenerateBlocks(1) - if err != nil { - return err - } - txStatusInfo, err := sf.transactionHandler.GetProcessedTransactionStatus(txHash) if err != nil { return err @@ -188,6 +183,11 @@ func (sf *simulatorFacade) GenerateBlocksUntilTransactionIsProcessed(txHash stri if txStatusInfo.Status != transaction.TxStatusPending.String() { return nil } + + err = sf.GenerateBlocks(1) + if err != nil { + return err + } } return errors.New("something went wrong, transaction is still in pending") From 6204c59874f8ae8a804fb2a4114a55e903027699 Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Wed, 14 Aug 2024 10:46:42 +0300 Subject: [PATCH 2/2] - added new optional parameter for the generate blocks until transactions processed endpoint --- README.md | 10 +++++++++- pkg/facade/simulatorFacade.go | 11 +++++++---- pkg/proxy/api/endpoints.go | 27 +++++++++++++++++++++++---- pkg/proxy/api/interface.go | 2 +- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 39e9b47..e1b9710 100644 --- a/README.md +++ b/README.md @@ -81,9 +81,17 @@ This endpoint initiates the generation of blocks for each shard until the status ##### Request - **Method:** POST - **Path:** `/simulator/generate-blocks-until-transaction-processed/:txHash` -- **Parameters:** +- **URL parameter** `maxNumBlocks` - `txHash` (path parameter): The hash of the targeted transaction. +##### URL Parameter: `maxNumBlocks` +- **Description:** + - **Type:** integer + - **Optional:** Yes + - **Default:** `20` + - **Behavior:** Setting the maxNumBlocks=`` is useful when the transaction is known to be executed on more than 20 blocks. + Example here are the transactions that generate complicate cross-shard async calls. Most transactions should finish in ~20 proposed blocks. + ##### Response - **Status Codes:** - `200 OK`: Blocks generated successfully, transaction was processed. diff --git a/pkg/facade/simulatorFacade.go b/pkg/facade/simulatorFacade.go index c40c3c8..a3b4aef 100644 --- a/pkg/facade/simulatorFacade.go +++ b/pkg/facade/simulatorFacade.go @@ -12,14 +12,16 @@ import ( "github.com/multiversx/mx-chain-core-go/core/check" "github.com/multiversx/mx-chain-core-go/data/transaction" "github.com/multiversx/mx-chain-go/node/chainSimulator/dtos" + logger "github.com/multiversx/mx-chain-logger-go" dtoc "github.com/multiversx/mx-chain-simulator-go/pkg/dtos" ) const ( - errMsgAccountNotFound = "account was not found" - maxNumOfBlockToGenerateUntilTxProcessed = 20 + errMsgAccountNotFound = "account was not found" ) +var log = logger.GetOrCreate("simulator/facade") + type simulatorFacade struct { simulator SimulatorHandler transactionHandler ProxyTransactionsHandler @@ -173,8 +175,9 @@ func (sf *simulatorFacade) GetObserversInfo() (map[uint32]*dtoc.ObserverInfo, er } // GenerateBlocksUntilTransactionIsProcessed generate blocks until the status of the provided transaction hash is processed -func (sf *simulatorFacade) GenerateBlocksUntilTransactionIsProcessed(txHash string) error { - for i := 0; i < maxNumOfBlockToGenerateUntilTxProcessed; i++ { +func (sf *simulatorFacade) GenerateBlocksUntilTransactionIsProcessed(txHash string, maxNumOfBlocksToGenerate int) error { + log.Debug("GenerateBlocksUntilTransactionIsProcessed", "tx hash", txHash, "maxNumOfBlocksToGenerate", maxNumOfBlocksToGenerate) + for i := 0; i < maxNumOfBlocksToGenerate; i++ { txStatusInfo, err := sf.transactionHandler.GetProcessedTransactionStatus(txHash) if err != nil { return err diff --git a/pkg/proxy/api/endpoints.go b/pkg/proxy/api/endpoints.go index e0f77fd..149a3f5 100644 --- a/pkg/proxy/api/endpoints.go +++ b/pkg/proxy/api/endpoints.go @@ -26,8 +26,11 @@ const ( observersInfo = "/simulator/observers" epochChange = "/simulator/force-epoch-change" - queryParamNoGenerate = "noGenerate" - queryParameterTargetEpoch = "targetEpoch" + queryParamNoGenerate = "noGenerate" + queryParamTargetEpoch = "targetEpoch" + queryParamMaxNumBlocks = "maxNumBlocks" + + maxNumOfBlockToGenerateUntilTxProcessed = 20 ) type endpointsProcessor struct { @@ -79,7 +82,7 @@ func (ep *endpointsProcessor) forceEpochChange(c *gin.Context) { } func getTargetEpochQueryParam(c *gin.Context) (int, error) { - epochStr := c.Request.URL.Query().Get(queryParameterTargetEpoch) + epochStr := c.Request.URL.Query().Get(queryParamTargetEpoch) if epochStr == "" { return 0, nil } @@ -139,7 +142,9 @@ func (ep *endpointsProcessor) generateBlocksUntilEpochReached(c *gin.Context) { func (ep *endpointsProcessor) generateBlocksUntilTransactionProcessed(c *gin.Context) { txHashStr := c.Param("txHash") - err := ep.facade.GenerateBlocksUntilTransactionIsProcessed(txHashStr) + + maxNumBlocks := getMaxNumBlocksToGenerate(c) + err := ep.facade.GenerateBlocksUntilTransactionIsProcessed(txHashStr, maxNumBlocks) if err != nil { shared.RespondWithInternalError(c, errors.New("cannot generate blocks"), err) return @@ -196,6 +201,20 @@ func getQueryParamNoGenerate(c *gin.Context) (bool, error) { return strconv.ParseBool(withResultsStr) } +func getMaxNumBlocksToGenerate(c *gin.Context) int { + withResultsStr := c.Request.URL.Query().Get(queryParamMaxNumBlocks) + if withResultsStr == "" { + return maxNumOfBlockToGenerateUntilTxProcessed + } + + value, err := strconv.Atoi(withResultsStr) + if err != nil { + return maxNumOfBlockToGenerateUntilTxProcessed + } + + return value +} + func (ep *endpointsProcessor) setStateMultiple(c *gin.Context) { var stateSlice []*dtos.AddressState diff --git a/pkg/proxy/api/interface.go b/pkg/proxy/api/interface.go index 78d11e7..8ff95e8 100644 --- a/pkg/proxy/api/interface.go +++ b/pkg/proxy/api/interface.go @@ -14,7 +14,7 @@ type SimulatorFacade interface { SetStateMultipleOverwrite(stateSlice []*dtos.AddressState, noGenerate bool) error AddValidatorKeys(validators *dtosc.ValidatorKeys) error GenerateBlocksUntilEpochIsReached(targetEpoch int32) error - GenerateBlocksUntilTransactionIsProcessed(txHash string) error + GenerateBlocksUntilTransactionIsProcessed(txHash string, maxNumOfBlocksToGenerate int) error ForceUpdateValidatorStatistics() error GetObserversInfo() (map[uint32]*dtosc.ObserverInfo, error) ForceChangeOfEpoch(targetEpoch uint32) error