-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathcontract.go
More file actions
384 lines (328 loc) · 10.2 KB
/
contract.go
File metadata and controls
384 lines (328 loc) · 10.2 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package contract
import (
"context"
"fmt"
"math/big"
"strings"
"time"
"github.com/0gfoundation/0g-storage-client/common/blockchain"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/openweb3/web3go"
"github.com/openweb3/web3go/types"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type FlowContract struct {
*blockchain.Contract
*Flow
clientWithSigner *web3go.Client
}
type TxRetryOption struct {
Timeout time.Duration
MaxNonGasRetries int
MaxGasPrice *big.Int
Step int64
}
var SpecifiedBlockError = "Specified block header does not exist"
var DefaultTimeout = 15 * time.Minute
var DefaultMaxNonGasRetries = 20
var DefaultStep = int64(15)
func IsRetriableSubmitLogEntryError(msg string) bool {
return strings.Contains(msg, SpecifiedBlockError) || strings.Contains(msg, "mempool") || strings.Contains(msg, "timeout")
}
func NewFlowContract(flowAddress common.Address, clientWithSigner *web3go.Client) (*FlowContract, error) {
backend, signer := clientWithSigner.ToClientForContract()
contract, err := blockchain.NewContract(clientWithSigner, signer)
if err != nil {
return nil, err
}
flow, err := NewFlow(flowAddress, backend)
if err != nil {
return nil, err
}
return &FlowContract{contract, flow, clientWithSigner}, nil
}
func (f *FlowContract) GetSubmitterAddress() (common.Address, error) {
sm, err := f.clientWithSigner.GetSignerManager()
if err != nil {
return common.Address{}, err
}
return sm.List()[0].Address(), nil
}
func (f *FlowContract) GetNonce(ctx context.Context) (*big.Int, error) {
sm, err := f.clientWithSigner.GetSignerManager()
if err != nil {
return nil, err
}
addr := sm.List()[0].Address()
pending := types.BlockNumberOrHashWithNumber(types.PendingBlockNumber)
nonce, err := f.clientWithSigner.Eth.TransactionCount(addr, &pending)
if err != nil {
return nil, err
}
return nonce, nil
}
func (f *FlowContract) GetGasPrice() (*big.Int, error) {
gasPrice, err := f.clientWithSigner.Eth.GasPrice()
if err != nil {
return nil, err
}
return gasPrice, nil
}
func (f *FlowContract) GetMarketContract(ctx context.Context) (*Market, error) {
marketAddr, err := f.Market(&bind.CallOpts{Context: ctx})
if err != nil {
return nil, err
}
backend, _ := f.clientWithSigner.ToClientForContract()
market, err := NewMarket(marketAddr, backend)
if err != nil {
return nil, err
}
return market, nil
}
func (submission Submission) String() string {
var heights []uint64
for _, v := range submission.Data.Nodes {
heights = append(heights, v.Height.Uint64())
}
return fmt.Sprintf("{ Size: %v, Heights: %v }", submission.Data.Length, heights)
}
func (submission Submission) Root() common.Hash {
numNodes := len(submission.Data.Nodes)
// should be never occur
if numNodes == 0 {
return common.Hash{}
}
// calculate root in reverse order
root := submission.Data.Nodes[numNodes-1].Root
for i := 1; i < numNodes; i++ {
left := submission.Data.Nodes[numNodes-1-i]
root = crypto.Keccak256Hash(left.Root[:], root[:])
}
return root
}
func TransactWithGasAdjustment(
contract *FlowContract,
method string,
opts *bind.TransactOpts,
retryOpts *TxRetryOption,
params ...any,
) (*types.Receipt, error) {
// Set timeout and max non-gas retries from retryOpts if provided.
if retryOpts == nil {
retryOpts = &TxRetryOption{
MaxNonGasRetries: DefaultMaxNonGasRetries,
}
}
if retryOpts.MaxNonGasRetries == 0 {
retryOpts.MaxNonGasRetries = DefaultMaxNonGasRetries
}
if retryOpts.Step == 0 {
retryOpts.Step = DefaultStep
}
if t, ok := opts.Context.Deadline(); ok {
retryOpts.Timeout = time.Until(t)
}
if opts.Nonce == nil {
// Get the current nonce if not set.
nonce, err := contract.GetNonce(opts.Context)
if err != nil {
return nil, err
}
// add one to the nonce
opts.Nonce = nonce
}
if opts.GasPrice == nil {
// Get the current gas price if not set.
gasPrice, err := contract.GetGasPrice()
if err != nil {
return nil, errors.WithMessage(err, "failed to get gas price")
}
opts.GasPrice = gasPrice
logrus.WithField("gasPrice", opts.GasPrice).Debug("Receive current gas price from chain node")
}
logrus.WithFields(logrus.Fields{
"timeout": retryOpts.Timeout,
"maxNonGasRetries": retryOpts.MaxNonGasRetries,
"nonce": opts.Nonce,
"gasPrice": opts.GasPrice,
}).Info("Set tx params")
receiptCh := make(chan *types.Receipt, 1)
errCh := make(chan error, 1)
failCh := make(chan error, 1)
var ctx context.Context
var cancel context.CancelFunc
if retryOpts.Timeout > 0 {
ctx, cancel = context.WithTimeout(context.Background(), retryOpts.Timeout)
} else {
ctx, cancel = context.WithCancel(context.Background())
}
// calculate number of gas retry by dividing max gas price by current gas price and the ration
nGasRetry := 0
if retryOpts.MaxGasPrice != nil {
gasPrice := opts.GasPrice
for gasPrice.Cmp(retryOpts.MaxGasPrice) <= 0 {
gasPrice = new(big.Int).Mul(gasPrice, big.NewInt(retryOpts.Step))
gasPrice.Div(gasPrice, big.NewInt(10))
nGasRetry++
}
}
go func() {
nRetries := 0
for {
select {
case <-ctx.Done():
// main or another goroutine canceled the context
logrus.Info("Context canceled; stopping outer loop")
return
default:
}
tx, err := contract.FlowTransactor.contract.Transact(opts, method, params...)
if err == nil {
// Wait for successful execution in a separate goroutine.
// Use local variables to avoid racing with the outer loop's err.
go func() {
r, e := contract.WaitForReceipt(ctx, tx.Hash(), true, blockchain.RetryOption{NRetries: retryOpts.MaxNonGasRetries})
if e == nil {
receiptCh <- r
return
}
errCh <- e
}()
// even if the receipt is received, this loop will continue until the context is canceled
time.Sleep(30 * time.Second)
err = fmt.Errorf("timeout")
}
errStr := strings.ToLower(err.Error())
if !IsRetriableSubmitLogEntryError(errStr) {
if strings.Contains(errStr, "invalid nonce") {
return
}
failCh <- errors.WithMessage(err, "failed to send transaction")
return
}
// If the error is due to mempool full or timeout, retry with a higher gas price
if strings.Contains(errStr, "mempool") || strings.Contains(errStr, "timeout") {
if retryOpts.MaxGasPrice == nil {
failCh <- errors.WithMessage(err, "mempool full and no max gas price is set, failed to send transaction")
return
} else if opts.GasPrice.Cmp(retryOpts.MaxGasPrice) >= 0 {
return
} else {
newGasPrice := new(big.Int).Mul(opts.GasPrice, big.NewInt(retryOpts.Step))
newGasPrice.Div(newGasPrice, big.NewInt(10))
if newGasPrice.Cmp(retryOpts.MaxGasPrice) > 0 {
opts.GasPrice = new(big.Int).Set(retryOpts.MaxGasPrice)
} else {
opts.GasPrice = newGasPrice
}
logrus.WithError(err).Infof("Increasing gas price to %v due to mempool/timeout error", opts.GasPrice)
}
} else {
nRetries++
if nRetries >= retryOpts.MaxNonGasRetries {
failCh <- errors.WithMessage(err, "failed to send transaction")
return
}
logrus.WithError(err).Infof("Retrying with same gas price %v, attempt %d", opts.GasPrice, nRetries)
}
}
}()
nErr := 0
for {
select {
case receipt := <-receiptCh:
cancel()
return receipt, nil
case err := <-errCh:
nErr++
if nErr >= nGasRetry {
failCh <- errors.WithMessage(err, "All gas price retries failed")
cancel()
return nil, err
}
case err := <-failCh:
cancel()
return nil, err
}
}
}
func TransactWithGasAdjustmentNoReceipt(
contract *FlowContract,
method string,
opts *bind.TransactOpts,
retryOpts *TxRetryOption,
params ...any,
) (*types.Transaction, error) {
// Set timeout and max non-gas retries from retryOpts if provided.
if retryOpts == nil {
retryOpts = &TxRetryOption{
MaxNonGasRetries: DefaultMaxNonGasRetries,
}
}
if retryOpts.MaxNonGasRetries == 0 {
retryOpts.MaxNonGasRetries = DefaultMaxNonGasRetries
}
if retryOpts.Step == 0 {
retryOpts.Step = DefaultStep
}
if t, ok := opts.Context.Deadline(); ok {
retryOpts.Timeout = time.Until(t)
}
logrus.WithFields(logrus.Fields{
"timeout": retryOpts.Timeout,
"maxNonGasRetries": retryOpts.MaxNonGasRetries,
"nonce": opts.Nonce,
"gasPrice": opts.GasPrice,
}).Info("Set tx params")
nRetries := 0
for {
if retryOpts.Timeout > 0 && opts.Context != nil && opts.Context.Err() != nil {
return nil, errors.WithMessage(opts.Context.Err(), "transaction submission canceled")
}
tx, err := contract.FlowTransactor.contract.Transact(opts, method, params...)
if err == nil {
return tx, nil
}
errStr := strings.ToLower(err.Error())
if !IsRetriableSubmitLogEntryError(errStr) {
if strings.Contains(errStr, "invalid nonce") {
return nil, err
}
return nil, errors.WithMessage(err, "failed to send transaction")
}
// If the error is due to mempool full or timeout, retry with a higher gas price
if strings.Contains(errStr, "mempool") || strings.Contains(errStr, "timeout") {
if retryOpts.MaxGasPrice == nil {
return nil, errors.WithMessage(err, "mempool full and no max gas price is set, failed to send transaction")
} else if opts.GasPrice.Cmp(retryOpts.MaxGasPrice) >= 0 {
return nil, errors.WithMessage(err, "reached max gas price, failed to send transaction")
} else {
newGasPrice := new(big.Int).Mul(opts.GasPrice, big.NewInt(retryOpts.Step))
newGasPrice.Div(newGasPrice, big.NewInt(10))
if newGasPrice.Cmp(retryOpts.MaxGasPrice) > 0 {
opts.GasPrice = new(big.Int).Set(retryOpts.MaxGasPrice)
} else {
opts.GasPrice = newGasPrice
}
logrus.WithError(err).Infof("Increasing gas price to %v due to mempool/timeout error", opts.GasPrice)
}
} else {
nRetries++
if nRetries >= retryOpts.MaxNonGasRetries {
return nil, errors.WithMessage(err, "failed to send transaction")
}
logrus.WithError(err).Infof("Retrying with same gas price %v, attempt %d", opts.GasPrice, nRetries)
}
}
}
func (submission Submission) Fee(pricePerSector *big.Int) *big.Int {
var sectors int64
for _, node := range submission.Data.Nodes {
sectors += 1 << node.Height.Int64()
}
return big.NewInt(0).Mul(big.NewInt(sectors), pricePerSector)
}