This repository was archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 332
/
Copy pathproposer.go
424 lines (361 loc) · 11 KB
/
proposer.go
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package proposer
import (
"bytes"
"context"
"fmt"
"math/rand"
"sync"
"time"
"github.com/ethereum-optimism/optimism/op-service/txmgr"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/rlp"
"github.com/urfave/cli/v2"
"golang.org/x/sync/errgroup"
"github.com/taikoxyz/taiko-client/bindings"
"github.com/taikoxyz/taiko-client/bindings/encoding"
"github.com/taikoxyz/taiko-client/internal/metrics"
"github.com/taikoxyz/taiko-client/internal/utils"
"github.com/taikoxyz/taiko-client/pkg/rpc"
selector "github.com/taikoxyz/taiko-client/proposer/prover_selector"
builder "github.com/taikoxyz/taiko-client/proposer/transaction_builder"
)
var (
proverAssignmentTimeout = 30 * time.Minute
requestProverServerTimeout = 12 * time.Second
)
// Proposer keep proposing new transactions from L2 execution engine's tx pool at a fixed interval.
type Proposer struct {
// configurations
*Config
// RPC clients
rpc *rpc.Client
// Private keys and account addresses
proposerAddress common.Address
proposingTimer *time.Timer
tiers []*rpc.TierProviderTierWithID
tierFees []encoding.TierFee
// Prover selector
proverSelector selector.ProverSelector
// Transaction builder
txBuilder builder.ProposeBlockTransactionBuilder
// Protocol configurations
protocolConfigs *bindings.TaikoDataConfig
lastProposedAt time.Time
txmgr *txmgr.SimpleTxManager
ctx context.Context
wg sync.WaitGroup
}
// InitFromCli New initializes the given proposer instance based on the command line flags.
func (p *Proposer) InitFromCli(ctx context.Context, c *cli.Context) error {
cfg, err := NewConfigFromCliContext(c)
if err != nil {
return err
}
return p.InitFromConfig(ctx, cfg)
}
// InitFromConfig initializes the proposer instance based on the given configurations.
func (p *Proposer) InitFromConfig(ctx context.Context, cfg *Config) (err error) {
p.proposerAddress = crypto.PubkeyToAddress(cfg.L1ProposerPrivKey.PublicKey)
p.ctx = ctx
p.Config = cfg
p.lastProposedAt = time.Now()
// RPC clients
if p.rpc, err = rpc.NewClient(p.ctx, cfg.ClientConfig); err != nil {
return fmt.Errorf("initialize rpc clients error: %w", err)
}
// Protocol configs
protocolConfigs, err := p.rpc.TaikoL1.GetConfig(&bind.CallOpts{Context: ctx})
if err != nil {
return fmt.Errorf("failed to get protocol configs: %w", err)
}
p.protocolConfigs = &protocolConfigs
log.Info("Protocol configs", "configs", p.protocolConfigs)
if p.tiers, err = p.rpc.GetTiers(ctx); err != nil {
return err
}
if err := p.initTierFees(); err != nil {
return err
}
if p.txmgr, err = txmgr.NewSimpleTxManager(
"proposer",
log.Root(),
&metrics.TxMgrMetrics,
*cfg.TxmgrConfigs,
); err != nil {
return err
}
if p.proverSelector, err = selector.NewETHFeeEOASelector(
&protocolConfigs,
p.rpc,
p.proposerAddress,
cfg.TaikoL1Address,
cfg.AssignmentHookAddress,
p.tierFees,
cfg.TierFeePriceBump,
cfg.ProverEndpoints,
cfg.MaxTierFeePriceBumps,
proverAssignmentTimeout,
requestProverServerTimeout,
); err != nil {
return err
}
if cfg.BlobAllowed {
p.txBuilder = builder.NewBlobTransactionBuilder(
p.rpc,
p.L1ProposerPrivKey,
p.proverSelector,
p.Config.L1BlockBuilderTip,
cfg.TaikoL1Address,
cfg.L2SuggestedFeeRecipient,
cfg.AssignmentHookAddress,
cfg.ProposeBlockTxGasLimit,
cfg.ExtraData,
)
} else {
p.txBuilder = builder.NewCalldataTransactionBuilder(
p.rpc,
p.L1ProposerPrivKey,
p.proverSelector,
p.Config.L1BlockBuilderTip,
cfg.L2SuggestedFeeRecipient,
cfg.TaikoL1Address,
cfg.AssignmentHookAddress,
cfg.ProposeBlockTxGasLimit,
cfg.ExtraData,
)
}
return nil
}
// Start starts the proposer's main loop.
func (p *Proposer) Start() error {
p.wg.Add(1)
go p.eventLoop()
return nil
}
// eventLoop starts the main loop of Taiko proposer.
func (p *Proposer) eventLoop() {
defer func() {
p.proposingTimer.Stop()
p.wg.Done()
}()
for {
p.updateProposingTicker()
select {
case <-p.ctx.Done():
return
// proposing interval timer has been reached
case <-p.proposingTimer.C:
metrics.ProposerProposeEpochCounter.Add(1)
// Attempt a proposing operation
if err := p.ProposeOp(p.ctx); err != nil {
log.Error("Proposing operation error", "error", err)
continue
}
}
}
}
// Close closes the proposer instance.
func (p *Proposer) Close(_ context.Context) {
p.wg.Wait()
}
// fetchPoolContent fetches the transaction pool content from L2 execution engine.
func (p *Proposer) fetchPoolContent(filterPoolContent bool) ([]types.Transactions, error) {
// Fetch the pool content.
preBuiltTxList, err := p.rpc.GetPoolContent(
p.ctx,
p.proposerAddress,
p.protocolConfigs.BlockMaxGasLimit,
rpc.BlockMaxTxListBytes,
p.LocalAddresses,
p.MaxProposedTxListsPerEpoch,
)
if err != nil {
return nil, fmt.Errorf("failed to fetch transaction pool content: %w", err)
}
txLists := []types.Transactions{}
for i, txs := range preBuiltTxList {
// Filter the pool content if the filterPoolContent flag is set.
if txs.EstimatedGasUsed < p.MinGasUsed && txs.BytesLength < p.MinTxListBytes && filterPoolContent {
log.Info(
"Pool content skipped",
"index", i,
"estimatedGasUsed", txs.EstimatedGasUsed,
"minGasUsed", p.MinGasUsed,
"bytesLength", txs.BytesLength,
"minBytesLength", p.MinTxListBytes,
)
break
}
txLists = append(txLists, txs.TxList)
}
// If the pool content is empty and the checkPoolContent flag is not set, return an empty list.
if !filterPoolContent && len(txLists) == 0 {
log.Info(
"Pool content is empty, proposing an empty block",
"lastProposedAt", p.lastProposedAt,
"minProposingInternal", p.MinProposingInternal,
)
txLists = append(txLists, types.Transactions{})
}
// If LocalAddressesOnly is set, filter the transactions by the local addresses.
if p.LocalAddressesOnly {
var (
localTxsLists []types.Transactions
signer = types.LatestSignerForChainID(p.rpc.L2.ChainID)
)
for _, txs := range txLists {
var filtered types.Transactions
for _, tx := range txs {
sender, err := types.Sender(signer, tx)
if err != nil {
return nil, err
}
for _, localAddress := range p.LocalAddresses {
if sender == localAddress {
filtered = append(filtered, tx)
}
}
}
if filtered.Len() != 0 {
localTxsLists = append(localTxsLists, filtered)
}
}
txLists = localTxsLists
}
log.Info("Transactions lists count", "count", len(txLists))
return txLists, nil
}
// ProposeOp performs a proposing operation, fetching transactions
// from L2 execution engine's tx pool, splitting them by proposing constraints,
// and then proposing them to TaikoL1 contract.
func (p *Proposer) ProposeOp(ctx context.Context) error {
// Check if it's time to propose unfiltered pool content.
filterPoolContent := time.Now().Before(p.lastProposedAt.Add(p.MinProposingInternal))
// Wait until L2 execution engine is synced at first.
if err := p.rpc.WaitTillL2ExecutionEngineSynced(ctx); err != nil {
return fmt.Errorf("failed to wait until L2 execution engine synced: %w", err)
}
log.Info(
"Start fetching L2 execution engine's transaction pool content",
"filterPoolContent", filterPoolContent,
"lastProposedAt", p.lastProposedAt,
)
txLists, err := p.fetchPoolContent(filterPoolContent)
if err != nil {
return err
}
// If the pool content is empty, return.
if len(txLists) == 0 {
return nil
}
g, gCtx := errgroup.WithContext(ctx)
// Propose all L2 transactions lists.
for _, txs := range txLists[:utils.Min(p.MaxProposedTxListsPerEpoch, uint64(len(txLists)))] {
nonce, err := p.rpc.L1.PendingNonceAt(ctx, p.proposerAddress)
if err != nil {
log.Error("Failed to get proposer nonce", "error", err)
break
}
log.Info("Proposer current pending nonce", "nonce", nonce)
g.Go(func() error {
txListBytes, err := rlp.EncodeToBytes(txs)
if err != nil {
return fmt.Errorf("failed to encode transactions: %w", err)
}
if err := p.ProposeTxList(gCtx, txListBytes, uint(txs.Len())); err != nil {
return err
}
p.lastProposedAt = time.Now()
return nil
})
if err := p.rpc.WaitL1NewPendingTransaction(ctx, p.proposerAddress, nonce); err != nil {
log.Error("Failed to wait for new pending transaction", "error", err)
}
}
if err := g.Wait(); err != nil {
return err
}
return nil
}
// ProposeTxList proposes the given transactions list to TaikoL1 smart contract.
func (p *Proposer) ProposeTxList(
ctx context.Context,
txListBytes []byte,
txNum uint,
) error {
compressedTxListBytes, err := utils.Compress(txListBytes)
if err != nil {
return err
}
txCandidate, err := p.txBuilder.Build(
ctx,
p.tierFees,
p.IncludeParentMetaHash,
compressedTxListBytes,
)
if err != nil {
log.Warn("Failed to build TaikoL1.proposeBlock transaction", "error", encoding.TryParsingCustomError(err))
return err
}
receipt, err := p.txmgr.Send(ctx, *txCandidate)
if err != nil {
log.Warn("Failed to send TaikoL1.proposeBlock transaction", "error", encoding.TryParsingCustomError(err))
return err
}
if receipt.Status != types.ReceiptStatusSuccessful {
return fmt.Errorf("failed to propose block: %s", receipt.TxHash.Hex())
}
log.Info("📝 Propose transactions succeeded", "txs", txNum)
metrics.ProposerProposedTxListsCounter.Add(1)
metrics.ProposerProposedTxsCounter.Add(float64(txNum))
return nil
}
// updateProposingTicker updates the internal proposing timer.
func (p *Proposer) updateProposingTicker() {
if p.proposingTimer != nil {
p.proposingTimer.Stop()
}
var duration time.Duration
if p.ProposeInterval != 0 {
duration = p.ProposeInterval
} else {
// Random number between 12 - 120
randomSeconds := rand.Intn(120-11) + 12 // nolint: gosec
duration = time.Duration(randomSeconds) * time.Second
}
p.proposingTimer = time.NewTimer(duration)
}
// Name returns the application name.
func (p *Proposer) Name() string {
return "proposer"
}
// initTierFees initializes the proving fees for every proof tier configured in the protocol for the proposer.
func (p *Proposer) initTierFees() error {
for _, tier := range p.tiers {
log.Info(
"Protocol tier",
"id", tier.ID,
"name", string(bytes.TrimRight(tier.VerifierName[:], "\x00")),
"validityBond", utils.WeiToEther(tier.ValidityBond),
"contestBond", utils.WeiToEther(tier.ContestBond),
"provingWindow", tier.ProvingWindow,
"cooldownWindow", tier.CooldownWindow,
)
switch tier.ID {
case encoding.TierOptimisticID:
p.tierFees = append(p.tierFees, encoding.TierFee{Tier: tier.ID, Fee: p.OptimisticTierFee})
case encoding.TierSgxID:
p.tierFees = append(p.tierFees, encoding.TierFee{Tier: tier.ID, Fee: p.SgxTierFee})
case encoding.TierGuardianID:
// Guardian prover should not charge any fee.
p.tierFees = append(p.tierFees, encoding.TierFee{Tier: tier.ID, Fee: common.Big0})
default:
return fmt.Errorf("unknown tier: %d", tier.ID)
}
}
return nil
}