Skip to content

Commit

Permalink
Merge pull request #76 from multiversx/optimized-generate-until-tx-pr…
Browse files Browse the repository at this point in the history
…ocessed

Optimized GenerateBlocksUntilTransactionIsProcessed
  • Loading branch information
iulianpascalau authored Aug 14, 2024
2 parents 4b5fb8a + 6204c59 commit 451da6d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=`<value>` 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.
Expand Down
21 changes: 12 additions & 9 deletions pkg/facade/simulatorFacade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -173,13 +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++ {
err := sf.GenerateBlocks(1)
if err != nil {
return err
}

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
Expand All @@ -188,6 +186,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")
Expand Down
27 changes: 23 additions & 4 deletions pkg/proxy/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion pkg/proxy/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 451da6d

Please sign in to comment.