Skip to content

Commit

Permalink
Merge pull request #371 from multiversx/relayers-transient-errors-bugfix
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
iulianpascalau authored Nov 5, 2024
2 parents cb1a7d6 + 336726f commit ca34306
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 173 deletions.
1 change: 1 addition & 0 deletions bridges/ethMultiversX/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type MultiversXClient interface {
GetActionIDForSetStatusOnPendingTransfer(ctx context.Context, batch *bridgeCore.TransferBatch) (uint64, error)
GetLastExecutedEthBatchID(ctx context.Context) (uint64, error)
GetLastExecutedEthTxID(ctx context.Context) (uint64, error)
GetLastMvxBatchID(ctx context.Context) (uint64, error)
GetCurrentNonce(ctx context.Context) (uint64, error)

ProposeSetStatus(ctx context.Context, batch *bridgeCore.TransferBatch) (string, error)
Expand Down
30 changes: 11 additions & 19 deletions clients/balanceValidator/balanceValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ func (validator *balanceValidator) CheckToken(ctx context.Context, ethToken comm
"amount", amount.String(),
)

// TODO(next PRs): fix here to not consider the pending batch in the mvx->eth direction that executed on eth.

if ethAmount.Cmp(mvxAmount) != 0 {
return fmt.Errorf("%w, balance for ERC20 token %s is %s and the balance for ESDT token %s is %s, direction %s",
ErrBalanceMismatch, ethToken.String(), ethAmount.String(), mvxToken, mvxAmount.String(), direction)
Expand Down Expand Up @@ -271,21 +269,13 @@ func getTotalAmountFromBatch(batch *bridgeCore.TransferBatch, token []byte) *big
}

func (validator *balanceValidator) getTotalTransferAmountInPendingMvxBatches(ctx context.Context, mvxToken []byte) (*big.Int, error) {
batch, err := validator.multiversXClient.GetPendingBatch(ctx)
if errors.Is(err, clients.ErrNoPendingBatchAvailable) {
return big.NewInt(0), nil
}
batchID, err := validator.multiversXClient.GetLastMvxBatchID(ctx)
if err != nil {
return nil, err
}

// check if the pending batch is executed on Ethereum and is not final
var batch *bridgeCore.TransferBatch
amount := big.NewInt(0)
if validator.batchExecutedAndNotFinalOnEth(ctx, batch.ID) {
amount.Add(amount, getTotalAmountFromBatch(batch, mvxToken))
}

batchID := batch.ID + 1
for {
batch, err = validator.multiversXClient.GetBatch(ctx, batchID)
if errors.Is(err, clients.ErrNoBatchAvailable) {
Expand All @@ -295,18 +285,20 @@ func (validator *balanceValidator) getTotalTransferAmountInPendingMvxBatches(ctx
return nil, err
}

wasExecuted, errWasExecuted := validator.ethereumClient.WasExecuted(ctx, batch.ID)
if errWasExecuted != nil {
return nil, errWasExecuted
}
if wasExecuted {
return amount, nil
}

amountFromBatch := getTotalAmountFromBatch(batch, mvxToken)
amount.Add(amount, amountFromBatch)
batchID++
batchID-- // go to the previous batch
}
}

func (validator *balanceValidator) batchExecutedAndNotFinalOnEth(ctx context.Context, nonce uint64) bool {
// TODO: analyze if we need to check the statuses returned
_, err := validator.ethereumClient.GetTransactionsStatuses(ctx, nonce)
return err != nil
}

func (validator *balanceValidator) getTotalTransferAmountInPendingEthBatches(ctx context.Context, ethToken common.Address) (*big.Int, error) {
batchID, err := validator.multiversXClient.GetLastExecutedEthBatchID(ctx)
if err != nil {
Expand Down
Loading

0 comments on commit ca34306

Please sign in to comment.