From 1dad61559ba7ad47237b09c72eeb5c5552572443 Mon Sep 17 00:00:00 2001 From: Sammy Date: Sat, 24 Feb 2024 05:24:34 +0800 Subject: [PATCH] fix(vald): vote empty when the tx has failed (#2109) * fix(vald): vote empty when the tx has failed * improve test --- vald/evm/evm.go | 11 +++++++++-- vald/evm/evm_test.go | 4 ++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/vald/evm/evm.go b/vald/evm/evm.go index cea7ea40b..556490e9d 100644 --- a/vald/evm/evm.go +++ b/vald/evm/evm.go @@ -42,6 +42,9 @@ var ( // ErrNotFinalized is returned when a transaction is not finalized var ErrNotFinalized = goerrors.New("not finalized") +// ErrTxFailed is returned when a transaction has failed +var ErrTxFailed = goerrors.New("transaction failed") + // Mgr manages all communication with Ethereum type Mgr struct { rpcs map[string]rpc.Client @@ -306,6 +309,10 @@ func (mgr Mgr) ProcessGatewayTxsConfirmation(event *types.ConfirmGatewayTxsStart logger.Debug(fmt.Sprintf("transaction %s not finalized", txID.Hex())) logger.Infof("broadcasting empty vote due to error: %s", result.Err().Error()) votes = append(votes, voteTypes.NewVoteRequest(mgr.proxy, pollID, types.NewVoteEvents(event.Chain))) + case ErrTxFailed: + logger.Debug(fmt.Sprintf("transaction %s failed", txID.Hex())) + logger.Infof("broadcasting empty vote due to error: %s", result.Err().Error()) + votes = append(votes, voteTypes.NewVoteRequest(mgr.proxy, pollID, types.NewVoteEvents(event.Chain))) case ethereum.NotFound: logger.Debug(fmt.Sprintf("transaction receipt %s not found", txID.Hex())) logger.Infof("broadcasting empty vote due to error: %s", result.Err().Error()) @@ -460,7 +467,7 @@ func (mgr Mgr) GetTxReceiptIfFinalized(chain nexus.ChainName, txID common.Hash, } if txReceipt.Status != geth.ReceiptStatusSuccessful { - return nil, errors.With(fmt.Errorf("transaction %s failed", txID.Hex()), keyvals...) + return nil, nil } isFinalized, err := mgr.isTxReceiptFinalized(chain, txReceipt, confHeight) @@ -491,7 +498,7 @@ func (mgr Mgr) GetTxReceiptsIfFinalized(chain nexus.ChainName, txIDs []common.Ha isFinalized := func(receipt *geth.Receipt) rs.Result[*geth.Receipt] { if receipt.Status != geth.ReceiptStatusSuccessful { - return rs.FromErr[*geth.Receipt](fmt.Errorf("transaction %s failed", receipt.TxHash.Hex())) + return rs.FromErr[*geth.Receipt](ErrTxFailed) } isFinalized, err := mgr.isTxReceiptFinalized(chain, receipt, confHeight) diff --git a/vald/evm/evm_test.go b/vald/evm/evm_test.go index f0c615397..034802b60 100644 --- a/vald/evm/evm_test.go +++ b/vald/evm/evm_test.go @@ -211,7 +211,7 @@ func TestMgr_GetTxReceiptIfFinalized(t *testing.T) { Then("tx is considered not finalized", func(t *testing.T) { txReceipt, err := mgr.GetTxReceiptIfFinalized(chain, tx.Hash(), confHeight) - assert.ErrorContains(t, err, "failed") + assert.NoError(t, err) assert.Nil(t, txReceipt) }). Run(t) @@ -920,7 +920,7 @@ func TestMgr_GetTxReceiptsIfFinalized(t *testing.T) { Then("should not retrieve receipts", func(t *testing.T) { receipts, err := mgr.GetTxReceiptsIfFinalized(chain, txHashes, confHeight) assert.NoError(t, err) - slices.ForEach(receipts, func(result results.Result[*geth.Receipt]) { assert.ErrorContains(t, result.Err(), "failed") }) + slices.ForEach(receipts, func(result results.Result[*geth.Receipt]) { assert.Equal(t, result.Err(), evm.ErrTxFailed) }) }), When("transactions are finalized", func() {