-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathavs_writer.go
More file actions
242 lines (207 loc) · 10.1 KB
/
avs_writer.go
File metadata and controls
242 lines (207 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package chainio
import (
"context"
"fmt"
"math/big"
"time"
"github.com/Layr-Labs/eigensdk-go/chainio/clients"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/avsregistry"
"github.com/Layr-Labs/eigensdk-go/chainio/clients/eth"
"github.com/Layr-Labs/eigensdk-go/logging"
"github.com/Layr-Labs/eigensdk-go/signer"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
servicemanager "github.com/yetanotherco/aligned_layer/contracts/bindings/AlignedLayerServiceManager"
retry "github.com/yetanotherco/aligned_layer/core"
"github.com/yetanotherco/aligned_layer/core/config"
"github.com/yetanotherco/aligned_layer/core/utils"
"github.com/yetanotherco/aligned_layer/metrics"
)
type AvsWriter struct {
*avsregistry.ChainWriter
AvsContractBindings *AvsServiceBindings
logger logging.Logger
Signer signer.Signer
Client eth.InstrumentedClient
ClientFallback eth.InstrumentedClient
metrics *metrics.Metrics
}
func NewAvsWriterFromConfig(baseConfig *config.BaseConfig, ecdsaConfig *config.EcdsaConfig, metrics *metrics.Metrics) (*AvsWriter, error) {
buildAllConfig := clients.BuildAllConfig{
EthHttpUrl: baseConfig.EthRpcUrl,
EthWsUrl: baseConfig.EthWsUrl,
RegistryCoordinatorAddr: baseConfig.AlignedLayerDeploymentConfig.AlignedLayerRegistryCoordinatorAddr.String(),
OperatorStateRetrieverAddr: baseConfig.AlignedLayerDeploymentConfig.AlignedLayerOperatorStateRetrieverAddr.String(),
AvsName: "AlignedLayer",
PromMetricsIpPortAddress: baseConfig.EigenMetricsIpPortAddress,
}
clients, err := clients.BuildAll(buildAllConfig, ecdsaConfig.PrivateKey, baseConfig.Logger)
if err != nil {
baseConfig.Logger.Error("Cannot build signer config", "err", err)
return nil, err
}
avsServiceBindings, err := NewAvsServiceBindings(baseConfig.AlignedLayerDeploymentConfig.AlignedLayerServiceManagerAddr, baseConfig.AlignedLayerDeploymentConfig.AlignedLayerOperatorStateRetrieverAddr, baseConfig.EthRpcClient, baseConfig.EthRpcClientFallback, baseConfig.Logger)
if err != nil {
baseConfig.Logger.Error("Cannot create avs service bindings", "err", err)
return nil, err
}
privateKeySigner, err := signer.NewPrivateKeySigner(ecdsaConfig.PrivateKey, baseConfig.ChainId)
if err != nil {
baseConfig.Logger.Error("Cannot create signer", "err", err)
return nil, err
}
chainWriter := clients.AvsRegistryChainWriter
return &AvsWriter{
ChainWriter: chainWriter,
AvsContractBindings: avsServiceBindings,
logger: baseConfig.Logger,
Signer: privateKeySigner,
Client: baseConfig.EthRpcClient,
ClientFallback: baseConfig.EthRpcClientFallback,
metrics: metrics,
}, nil
}
// Sends AggregatedResponse and waits for the receipt for three blocks, if not received
// it will try again bumping the last tx gas price based on `CalculateGasPriceBump`
// This process happens indefinitely until the transaction is included.
func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMerkleRoot [32]byte, senderAddress [20]byte, nonSignerStakesAndSignature servicemanager.IBLSSignatureCheckerNonSignerStakesAndSignature, gasBumpPercentage uint, gasBumpIncrementalPercentage uint, timeToWaitBeforeBump time.Duration, onGasPriceBumped func(*big.Int)) (*types.Receipt, error) {
txOpts := *w.Signer.GetTxOpts()
txOpts.NoSend = true // simulate the transaction
tx, err := w.RespondToTaskV2Retryable(&txOpts, batchMerkleRoot, senderAddress, nonSignerStakesAndSignature)
if err != nil {
return nil, err
}
// Set the nonce, as we might have to replace the transaction with a higher gas price
txNonce := big.NewInt(int64(tx.Nonce()))
txOpts.Nonce = txNonce
txOpts.GasPrice = tx.GasPrice()
txOpts.NoSend = false
i := 0
// Set Retry config for RespondToTaskV2
respondToTaskV2Config := retry.DefaultRetryConfig()
respondToTaskV2Config.MaxNumRetries = 0
respondToTaskV2Config.MaxElapsedTime = 0
// Set Retry config for WaitForTxRetryable
waitForTxConfig := retry.DefaultRetryConfig()
waitForTxConfig.MaxInterval = 2 * time.Second
waitForTxConfig.MaxNumRetries = 0
waitForTxConfig.MaxElapsedTime = timeToWaitBeforeBump
respondToTaskV2Func := func() (*types.Receipt, error) {
gasPrice, err := utils.GetGasPriceRetryable(w.Client, w.ClientFallback)
if err != nil {
return nil, err
}
bumpedGasPrice := utils.CalculateGasPriceBumpBasedOnRetry(gasPrice, gasBumpPercentage, gasBumpIncrementalPercentage, i)
// new bumped gas price must be higher than the last one (this should hardly ever happen though)
if bumpedGasPrice.Cmp(txOpts.GasPrice) > 0 {
txOpts.GasPrice = bumpedGasPrice
} else {
// bump the last tx gas price a little by `gasBumpIncrementalPercentage` to replace it.
txOpts.GasPrice = utils.CalculateGasPriceBumpBasedOnRetry(txOpts.GasPrice, gasBumpIncrementalPercentage, 0, 0)
}
if i > 0 {
onGasPriceBumped(txOpts.GasPrice)
}
// We compare both Aggregator funds and Batcher balance in Aligned against respondToTaskFeeLimit
// Both are required to have some balance, more details inside the function
err = w.checkAggAndBatcherHaveEnoughBalance(tx, txOpts, batchIdentifierHash, senderAddress)
if err != nil {
return nil, retry.PermanentError{Inner: err}
}
w.logger.Infof("Sending RespondToTask transaction with a gas price of %v", txOpts.GasPrice)
tx, err = w.RespondToTaskV2Retryable(&txOpts, batchMerkleRoot, senderAddress, nonSignerStakesAndSignature)
if err != nil {
return nil, err
}
receipt, err := utils.WaitForTransactionReceiptRetryable(w.Client, w.ClientFallback, tx.Hash(), waitForTxConfig)
if receipt != nil {
w.checkIfAggregatorHadToPaidForBatcher(tx, batchIdentifierHash)
return receipt, nil
}
// if we are here, it means we have reached the receipt waiting timeout
// we increment the i here to add an incremental percentage to increase the odds of being included in the next blocks
i++
w.logger.Infof("RespondToTask receipt waiting timeout has passed, will try again...")
if err != nil {
return nil, err
}
return nil, fmt.Errorf("transaction failed")
}
return retry.RetryWithData(respondToTaskV2Func, respondToTaskV2Config)
}
// Calculates the transaction cost from the receipt and compares it with the batcher respondToTaskFeeLimit
// if the tx cost was higher, then it means the aggregator has paid the difference for the batcher (txCost - respondToTaskFeeLimit) and so metrics are updated accordingly.
// otherwise nothing is done.
func (w *AvsWriter) checkIfAggregatorHadToPaidForBatcher(tx *types.Transaction, batchIdentifierHash [32]byte) {
batchState, err := w.BatchesStateRetryable(&bind.CallOpts{}, batchIdentifierHash)
if err != nil {
return
}
respondToTaskFeeLimit := batchState.RespondToTaskFeeLimit
// NOTE we are not using tx.Cost() because tx.Cost() includes tx.Value()
txCost := new(big.Int).Mul(big.NewInt(int64(tx.Gas())), tx.GasPrice())
if respondToTaskFeeLimit.Cmp(txCost) < 0 {
aggregatorDifferencePaid := new(big.Int).Sub(txCost, respondToTaskFeeLimit)
aggregatorDifferencePaidInEth := utils.WeiToEth(aggregatorDifferencePaid)
w.metrics.AddAggregatorGasPaidForBatcher(aggregatorDifferencePaidInEth)
w.metrics.IncAggregatorPaidForBatcher()
w.logger.Warnf("cost of transaction was higher than Batch.RespondToTaskFeeLimit, aggregator has paid the for the difference, aprox: %vethers", aggregatorDifferencePaidInEth)
}
}
func (w *AvsWriter) checkAggAndBatcherHaveEnoughBalance(tx *types.Transaction, txOpts bind.TransactOpts, batchIdentifierHash [32]byte, senderAddress [20]byte) error {
w.logger.Info("Checking if aggregator and batcher have enough balance for the transaction")
aggregatorAddress := txOpts.From
txCost := new(big.Int).Mul(new(big.Int).SetUint64(tx.Gas()), txOpts.GasPrice)
w.logger.Info("Transaction cost", "cost", txCost)
batchState, err := w.BatchesStateRetryable(&bind.CallOpts{}, batchIdentifierHash)
if err != nil {
w.logger.Error("Failed to get batch state", "error", err)
w.logger.Info("Proceeding to check balances against transaction cost")
return w.compareBalances(txCost, aggregatorAddress, senderAddress)
}
respondToTaskFeeLimit := batchState.RespondToTaskFeeLimit
w.logger.Info("Checking balance against Batch RespondToTaskFeeLimit", "RespondToTaskFeeLimit", respondToTaskFeeLimit)
// Note: we compare both Aggregator funds and Batcher balance in Aligned against respondToTaskFeeLimit
// Batcher will pay up to respondToTaskFeeLimit, for this he needs that amount of funds in Aligned
// Aggregator will pay any extra cost, for this he needs at least respondToTaskFeeLimit in his balance
return w.compareBalances(respondToTaskFeeLimit, aggregatorAddress, senderAddress)
}
func (w *AvsWriter) compareBalances(amount *big.Int, aggregatorAddress common.Address, senderAddress [20]byte) error {
if err := w.compareAggregatorBalance(amount, aggregatorAddress); err != nil {
return err
}
if err := w.compareBatcherBalance(amount, senderAddress); err != nil {
return err
}
return nil
}
func (w *AvsWriter) compareAggregatorBalance(amount *big.Int, aggregatorAddress common.Address) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
aggregatorBalance, err := w.BalanceAtRetryable(ctx, aggregatorAddress, nil)
if err != nil {
// Ignore and continue.
w.logger.Error("failed to get aggregator balance: %v", err)
return nil
}
w.logger.Info("Aggregator balance", "balance", aggregatorBalance)
if aggregatorBalance.Cmp(amount) < 0 {
return fmt.Errorf("cost is higher than Aggregator balance")
}
return nil
}
func (w *AvsWriter) compareBatcherBalance(amount *big.Int, senderAddress [20]byte) error {
// Get batcher balance
batcherBalance, err := w.BatcherBalancesRetryable(&bind.CallOpts{}, senderAddress)
if err != nil {
// Ignore and continue.
w.logger.Error("Failed to get batcherBalance", "error", err)
return nil
}
w.logger.Info("Batcher balance", "balance", batcherBalance)
if batcherBalance.Cmp(amount) < 0 {
return fmt.Errorf("cost is higher than Batcher balance")
}
return nil
}