Skip to content

Commit

Permalink
fix(vald): vote empty when the tx has failed (#2109)
Browse files Browse the repository at this point in the history
* fix(vald): vote empty when the tx has failed

* improve test
  • Loading branch information
fish-sammy authored Feb 23, 2024
1 parent 0d6928d commit 1dad615
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 9 additions & 2 deletions vald/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions vald/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 1dad615

Please sign in to comment.