Skip to content

Commit

Permalink
Add channel to monitored transactor that returns error based on trans…
Browse files Browse the repository at this point in the history
…action result
  • Loading branch information
mpetrun5 committed Sep 17, 2024
1 parent aa7e4eb commit 22eac8d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions chains/evm/transactor/monitored/monitored.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package monitored

import (
"context"
"fmt"
"math/big"
"sync"
"time"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/sygmaprotocol/sygma-core/chains/evm/client"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor"
"github.com/sygmaprotocol/sygma-core/chains/evm/transactor/transaction"
"github.com/sygmaprotocol/sygma-core/utils"
)

type GasPricer interface {
Expand All @@ -26,6 +28,7 @@ type RawTx struct {
gasLimit uint64
gasPrice []*big.Int
data []byte
errChn chan error
submitTime time.Time
creationTime time.Time
}
Expand Down Expand Up @@ -94,6 +97,7 @@ func (t *MonitoredTransactor) Transact(to *common.Address, data []byte, opts tra
gasLimit: opts.GasLimit,
gasPrice: gp,
data: data,
errChn: opts.ErrChn,
submitTime: time.Now(),
creationTime: time.Now(),
}
Expand Down Expand Up @@ -145,8 +149,10 @@ func (t *MonitoredTransactor) Monitor(
if err == nil {
if receipt.Status == types.ReceiptStatusSuccessful {
log.Info().Uint64("nonce", tx.nonce).Msgf("Executed transaction %s with nonce %d", oldHash, tx.nonce)
utils.TrySendError(tx.errChn, nil)
} else {
log.Error().Uint64("nonce", tx.nonce).Msgf("Transaction %s failed on chain", oldHash)
utils.TrySendError(tx.errChn, fmt.Errorf("transaction %s failed on chain with nonce %d", oldHash, tx.nonce))
}

delete(t.pendingTxns, oldHash)
Expand All @@ -155,6 +161,7 @@ func (t *MonitoredTransactor) Monitor(

if time.Since(tx.creationTime) > txTimeout {
log.Error().Uint64("nonce", tx.nonce).Msgf("Transaction %s has timed out", oldHash)
utils.TrySendError(tx.errChn, fmt.Errorf("transaction %s has timed out", oldHash))
delete(t.pendingTxns, oldHash)
continue
}
Expand Down
10 changes: 9 additions & 1 deletion chains/evm/transactor/monitored/monitored_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_SuccessfulExec
big.NewInt(15))

go t.Monitor(ctx, time.Millisecond*50, time.Minute, time.Millisecond)
errChn := make(chan error, 1)
hash, err := t.Transact(
&common.Address{},
byteData,
Expand All @@ -119,6 +120,8 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_SuccessfulExec

time.Sleep(time.Millisecond * 150)
cancel()
err = <-errChn
s.Nil(err)
}

func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_TxTimeout() {
Expand All @@ -140,17 +143,22 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_TxTimeout() {
big.NewInt(1000),
big.NewInt(15))

errChn := make(chan error, 1)
go t.Monitor(ctx, time.Millisecond*50, time.Millisecond, time.Millisecond)
hash, err := t.Transact(
&common.Address{},
byteData,
transactor.TransactOptions{},
transactor.TransactOptions{
ErrChn: errChn,
},
)
s.mockClient.EXPECT().TransactionReceipt(gomock.Any(), *hash).Return(nil, fmt.Errorf("not found"))
s.Nil(err)

time.Sleep(time.Millisecond * 150)
cancel()
err = <-errChn
s.NotNil(err)
}

func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_TransactionResent() {
Expand Down
1 change: 1 addition & 0 deletions chains/evm/transactor/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type TransactOptions struct {
Nonce *big.Int
ChainID *big.Int
Priority uint8
ErrChn chan error
}

// to save on data, we encode uin8 for transaction priority
Expand Down
7 changes: 7 additions & 0 deletions utils/bytes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package utils

func SliceTo32Bytes(in []byte) [32]byte {
var res [32]byte
copy(res[:], in)
return res
}
12 changes: 12 additions & 0 deletions utils/channel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package utils

// TrySendError attempts to send error over the given channel.
// It prevents blocking when a channel is busy or nil.
func TrySendError(errChn chan error, err error) bool {
select {
case errChn <- err:
return true
default:
return false
}
}

0 comments on commit 22eac8d

Please sign in to comment.