Skip to content

Commit c4b524f

Browse files
authored
feat: chain and gas usage metrics (#38)
1 parent 070af99 commit c4b524f

File tree

7 files changed

+304
-67
lines changed

7 files changed

+304
-67
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ genmocks:
3737
mockgen -destination=./mock/gas.go -source=./chains/evm/transactor/gas/gas-pricer.go -package mock
3838
mockgen -destination=./mock/relayer.go -source=./relayer/relayer.go -package mock
3939
mockgen -source=chains/evm/transactor/transact.go -destination=./mock/transact.go -package mock
40-
mockgen -source=chains/evm/transactor/signAndSend/signAndSend.go -destination=./mock/signAndSend.go -package mock
40+
mockgen -source=chains/evm/transactor/monitored/monitored.go -destination=./mock/monitored.go -package mock
4141
mockgen -source=./store/store.go -destination=./mock/store.go -package mock
4242
mockgen -source=./relayer/message/handler.go -destination=./mock/message.go -package mock
4343
mockgen -source=./chains/evm/listener/listener.go -destination=./mock/evmListener.go -package mock

chains/evm/transactor/monitored/monitored.go

+34-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ethereum/go-ethereum/common"
1010
"github.com/ethereum/go-ethereum/core/types"
11+
"github.com/rs/zerolog"
1112
"github.com/rs/zerolog/log"
1213

1314
"github.com/sygmaprotocol/sygma-core/chains/evm/client"
@@ -19,6 +20,10 @@ type GasPricer interface {
1920
GasPrice(priority *uint8) ([]*big.Int, error)
2021
}
2122

23+
type GasTracker interface {
24+
TrackGasUsage(domainID uint8, gasUsed uint64, gasPrice *big.Int)
25+
}
26+
2227
type RawTx struct {
2328
nonce uint64
2429
to *common.Address
@@ -30,9 +35,26 @@ type RawTx struct {
3035
creationTime time.Time
3136
}
3237

38+
// GasPrice returns transaction gas price in gwei
39+
//
40+
// It returns base fee for both London and legacy transactions.
41+
func (tx *RawTx) GasPrice() *big.Int {
42+
var gasPrice *big.Int
43+
if len(tx.gasPrice) == 1 {
44+
gasPrice = tx.gasPrice[0]
45+
} else {
46+
gasPrice = tx.gasPrice[1]
47+
}
48+
return new(big.Int).Div(gasPrice, big.NewInt(1e9))
49+
}
50+
3351
type MonitoredTransactor struct {
52+
domainID uint8
53+
log zerolog.Logger
54+
3455
txFabric transaction.TxFabric
3556
gasPriceClient GasPricer
57+
gasTracker GasTracker
3658
client client.Client
3759

3860
maxGasPrice *big.Int
@@ -49,15 +71,20 @@ type MonitoredTransactor struct {
4971
// Gas price is increased by increasePercentage param which
5072
// is a percentage value with which old gas price should be increased (e.g 15)
5173
func NewMonitoredTransactor(
74+
domainID uint8,
5275
txFabric transaction.TxFabric,
5376
gasPriceClient GasPricer,
77+
gasTracker GasTracker,
5478
client client.Client,
5579
maxGasPrice *big.Int,
5680
increasePercentage *big.Int,
5781
) *MonitoredTransactor {
5882
return &MonitoredTransactor{
83+
domainID: domainID,
84+
log: log.With().Uint8("domainID", domainID).Logger(),
5985
client: client,
6086
gasPriceClient: gasPriceClient,
87+
gasTracker: gasTracker,
6188
txFabric: txFabric,
6289
pendingTxns: make(map[common.Hash]RawTx),
6390
maxGasPrice: maxGasPrice,
@@ -143,18 +170,20 @@ func (t *MonitoredTransactor) Monitor(
143170
for oldHash, tx := range pendingTxCopy {
144171
receipt, err := t.client.TransactionReceipt(context.Background(), oldHash)
145172
if err == nil {
173+
t.gasTracker.TrackGasUsage(t.domainID, receipt.GasUsed, tx.GasPrice())
174+
146175
if receipt.Status == types.ReceiptStatusSuccessful {
147-
log.Info().Uint64("nonce", tx.nonce).Msgf("Executed transaction %s with nonce %d", oldHash, tx.nonce)
176+
t.log.Info().Uint64("nonce", tx.nonce).Msgf("Executed transaction %s with nonce %d", oldHash, tx.nonce)
148177
} else {
149-
log.Error().Uint64("nonce", tx.nonce).Msgf("Transaction %s failed on chain", oldHash)
178+
t.log.Error().Uint64("nonce", tx.nonce).Msgf("Transaction %s failed on chain", oldHash)
150179
}
151180

152181
delete(t.pendingTxns, oldHash)
153182
continue
154183
}
155184

156185
if time.Since(tx.creationTime) > txTimeout {
157-
log.Error().Uint64("nonce", tx.nonce).Msgf("Transaction %s has timed out", oldHash)
186+
t.log.Error().Uint64("nonce", tx.nonce).Msgf("Transaction %s has timed out", oldHash)
158187
delete(t.pendingTxns, oldHash)
159188
continue
160189
}
@@ -164,7 +193,7 @@ func (t *MonitoredTransactor) Monitor(
164193

165194
hash, err := t.resendTransaction(&tx)
166195
if err != nil {
167-
log.Warn().Uint64("nonce", tx.nonce).Err(err).Msgf("Failed resending transaction %s", hash)
196+
t.log.Warn().Uint64("nonce", tx.nonce).Err(err).Msgf("Failed resending transaction %s", hash)
168197
continue
169198
}
170199

@@ -188,7 +217,7 @@ func (t *MonitoredTransactor) resendTransaction(tx *RawTx) (common.Hash, error)
188217
return common.Hash{}, err
189218
}
190219

191-
log.Debug().Uint64("nonce", tx.nonce).Msgf("Resent transaction with hash %s", hash)
220+
t.log.Debug().Uint64("nonce", tx.nonce).Msgf("Resent transaction with hash %s", hash)
192221

193222
return hash, nil
194223
}

chains/evm/transactor/monitored/monitored_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type TransactorTestSuite struct {
2121
suite.Suite
2222
gomockController *gomock.Controller
2323
mockClient *mock.MockClient
24+
mockGasTracker *mock.MockGasTracker
2425
mockTransactor *mock.MockTransactor
2526
mockGasPricer *mock.MockGasPricer
2627
}
@@ -34,6 +35,8 @@ func (s *TransactorTestSuite) SetupTest() {
3435
s.mockClient = mock.NewMockClient(s.gomockController)
3536
s.mockTransactor = mock.NewMockTransactor(s.gomockController)
3637
s.mockGasPricer = mock.NewMockGasPricer(s.gomockController)
38+
s.mockGasTracker = mock.NewMockGasTracker(s.gomockController)
39+
s.mockGasTracker.EXPECT().TrackGasUsage(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
3740
}
3841

3942
func (s *TransactorTestSuite) TestTransactor_SignAndSend_Success() {
@@ -47,8 +50,10 @@ func (s *TransactorTestSuite) TestTransactor_SignAndSend_Success() {
4750
s.mockClient.EXPECT().UnlockNonce()
4851

4952
t := monitored.NewMonitoredTransactor(
53+
1,
5054
transaction.NewTransaction,
5155
s.mockGasPricer,
56+
s.mockGasTracker,
5257
s.mockClient,
5358
big.NewInt(1000),
5459
big.NewInt(15))
@@ -72,8 +77,10 @@ func (s *TransactorTestSuite) TestTransactor_SignAndSend_Fail() {
7277
s.mockClient.EXPECT().UnlockNonce()
7378

7479
t := monitored.NewMonitoredTransactor(
80+
1,
7581
transaction.NewTransaction,
7682
s.mockGasPricer,
83+
s.mockGasTracker,
7784
s.mockClient,
7885
big.NewInt(1000),
7986
big.NewInt(15))
@@ -99,8 +106,10 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_SuccessfulExec
99106

100107
ctx, cancel := context.WithCancel(context.Background())
101108
t := monitored.NewMonitoredTransactor(
109+
1,
102110
transaction.NewTransaction,
103111
s.mockGasPricer,
112+
s.mockGasTracker,
104113
s.mockClient,
105114
big.NewInt(1000),
106115
big.NewInt(15))
@@ -134,8 +143,10 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_TxTimeout() {
134143

135144
ctx, cancel := context.WithCancel(context.Background())
136145
t := monitored.NewMonitoredTransactor(
146+
1,
137147
transaction.NewTransaction,
138148
s.mockGasPricer,
149+
s.mockGasTracker,
139150
s.mockClient,
140151
big.NewInt(1000),
141152
big.NewInt(15))
@@ -169,8 +180,10 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_TransactionRes
169180

170181
ctx, cancel := context.WithCancel(context.Background())
171182
t := monitored.NewMonitoredTransactor(
183+
1,
172184
transaction.NewTransaction,
173185
s.mockGasPricer,
186+
s.mockGasTracker,
174187
s.mockClient,
175188
big.NewInt(1000),
176189
big.NewInt(15))
@@ -208,8 +221,10 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_MaxGasPriceRea
208221

209222
ctx, cancel := context.WithCancel(context.Background())
210223
t := monitored.NewMonitoredTransactor(
224+
1,
211225
transaction.NewTransaction,
212226
s.mockGasPricer,
227+
s.mockGasTracker,
213228
s.mockClient,
214229
big.NewInt(10),
215230
big.NewInt(15))
@@ -233,8 +248,10 @@ func (s *TransactorTestSuite) TestTransactor_MonitoredTransaction_MaxGasPriceRea
233248

234249
func (s *TransactorTestSuite) TestTransactor_IncreaseGas_15PercentIncrease() {
235250
t := monitored.NewMonitoredTransactor(
251+
1,
236252
transaction.NewTransaction,
237253
s.mockGasPricer,
254+
s.mockGasTracker,
238255
s.mockClient,
239256
big.NewInt(150),
240257
big.NewInt(15))
@@ -246,8 +263,10 @@ func (s *TransactorTestSuite) TestTransactor_IncreaseGas_15PercentIncrease() {
246263

247264
func (s *TransactorTestSuite) TestTransactor_IncreaseGas_MaxGasReached() {
248265
t := monitored.NewMonitoredTransactor(
266+
1,
249267
transaction.NewTransaction,
250268
s.mockGasPricer,
269+
s.mockGasTracker,
251270
s.mockClient,
252271
big.NewInt(15),
253272
big.NewInt(15))

mock/signAndSend.go renamed to mock/monitored.go

+37-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)