Skip to content
This repository was archived by the owner on May 11, 2024. It is now read-only.

Commit 077ccf2

Browse files
davidtaikochacyberhorseyYoGhurt111yuguo
authored
refactor(prover): refactor prover / guardian prover (#586)
Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com> Co-authored-by: Gavin Yu <623770278@qq.com> Co-authored-by: yuguo <yuguo@loopring.io>
1 parent 2644e60 commit 077ccf2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+6195
-1646
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ dbdata
2525
# Testing
2626
integration_test/nodes/deployments/mainnet.json
2727
.env
28+
29+
\.idea/

bindings/encoding/input.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,17 @@ var (
262262

263263
// Contract ABIs.
264264
var (
265-
TaikoL1ABI *abi.ABI
266-
TaikoL2ABI *abi.ABI
267-
GuardianProverABI *abi.ABI
268-
LibDepositingABI *abi.ABI
269-
LibProposingABI *abi.ABI
270-
LibProvingABI *abi.ABI
271-
LibUtilsABI *abi.ABI
272-
LibVerifyingABI *abi.ABI
273-
AssignmentHookABI *abi.ABI
265+
TaikoL1ABI *abi.ABI
266+
TaikoL2ABI *abi.ABI
267+
GuardianProverABI *abi.ABI
268+
LibDepositingABI *abi.ABI
269+
LibProposingABI *abi.ABI
270+
LibProvingABI *abi.ABI
271+
LibUtilsABI *abi.ABI
272+
LibVerifyingABI *abi.ABI
273+
AssignmentHookABI *abi.ABI
274+
SGXVerifierABI *abi.ABI
275+
GuardianVerifierABI *abi.ABI
274276

275277
customErrorMaps []map[string]abi.Error
276278
)
@@ -314,6 +316,14 @@ func init() {
314316
log.Crit("Get AssignmentHook ABI error", "error", err)
315317
}
316318

319+
if SGXVerifierABI, err = bindings.SgxVerifierMetaData.GetAbi(); err != nil {
320+
log.Crit("Get SGXVerifier ABI error", err)
321+
}
322+
323+
if GuardianVerifierABI, err = bindings.GuardianVerifierMetaData.GetAbi(); err != nil {
324+
log.Crit("Get GuardianVerifier ABI error", "error", err)
325+
}
326+
317327
customErrorMaps = []map[string]abi.Error{
318328
TaikoL1ABI.Errors,
319329
TaikoL2ABI.Errors,
@@ -324,6 +334,8 @@ func init() {
324334
LibUtilsABI.Errors,
325335
LibVerifyingABI.Errors,
326336
AssignmentHookABI.Errors,
337+
SGXVerifierABI.Errors,
338+
GuardianVerifierABI.Errors,
327339
}
328340
}
329341

bindings/gen_guardian_verifier.go

+1,737
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/gen_sgx_verifier.go

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

cmd/flags/prover.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ var (
8888
Usage: "Gas limit will be used for TaikoL1.proveBlock transactions",
8989
Category: proverCategory,
9090
}
91-
ProveBlockMaxTxGasTipCap = &cli.Uint64Flag{
92-
Name: "tx.maxGasTipCap",
93-
Usage: "Gas tip cap (in wei) for a TaikoL1.proveBlock transaction when doing the transaction replacement",
91+
ProveBlockMaxTxGasFeeCap = &cli.Uint64Flag{
92+
Name: "tx.maxGasFeeCap",
93+
Usage: "Gas fee cap (in wei) for a TaikoL1.proveBlock transaction when doing the transaction replacement",
9494
Category: proverCategory,
9595
}
96-
ProveBlockTxReplacementMultiplier = &cli.Uint64Flag{
97-
Name: "tx.replacementMultiplier",
98-
Value: 2,
99-
Usage: "Gas tip multiplier when replacing a TaikoL1.proveBlock transaction with same nonce",
96+
TxReplacementGasGrowthRate = &cli.Uint64Flag{
97+
Name: "tx.replacementProveBlockGasGrowthRate",
98+
Value: 50,
99+
Usage: "Gas tip growth rate when replacing a TaikoL1.proveBlock transaction with same nonce",
100100
Category: proverCategory,
101101
}
102102
// Running mode
@@ -198,8 +198,8 @@ var ProverFlags = MergeFlags(CommonFlags, []cli.Flag{
198198
GuardianProofSubmissionDelay,
199199
GuardianProverHealthCheckServerEndpoint,
200200
ProofSubmissionMaxRetry,
201-
ProveBlockTxReplacementMultiplier,
202-
ProveBlockMaxTxGasTipCap,
201+
TxReplacementGasGrowthRate,
202+
ProveBlockMaxTxGasFeeCap,
203203
Graffiti,
204204
ProveUnassignedBlocks,
205205
ContesterMode,

driver/chain_syncer/calldata/syncer.go

+21-24
Original file line numberDiff line numberDiff line change
@@ -137,29 +137,30 @@ func (s *Syncer) onBlockProposed(
137137

138138
if !s.progressTracker.Triggered() {
139139
// Check whether we need to reorg the L2 chain at first.
140-
// 1. Last verified block
141140
var (
142-
reorged bool
143-
l1CurrentToReset *types.Header
144-
lastInsertedBlockIDToReset *big.Int
145-
err error
141+
reorgCheckResult = new(rpc.ReorgCheckResult)
142+
err error
146143
)
147-
reorged, err = s.checkLastVerifiedBlockMismatch(ctx)
144+
// 1. The latest verified block
145+
reorgCheckResult.IsReorged, err = s.checkLastVerifiedBlockMismatch(ctx)
148146
if err != nil {
149147
return fmt.Errorf("failed to check if last verified block in L2 EE has been reorged: %w", err)
150148
}
151149

152-
// 2. Parent block
153-
if reorged {
150+
// If the latest verified block in chain is mismatched, we reset the L2 chain to genesis, and restart
151+
// the calldata sync process.
152+
// TODO(David): improve this approach.
153+
if reorgCheckResult.IsReorged {
154154
genesisL1Header, err := s.rpc.GetGenesisL1Header(ctx)
155155
if err != nil {
156156
return fmt.Errorf("failed to fetch genesis L1 header: %w", err)
157157
}
158158

159-
l1CurrentToReset = genesisL1Header
160-
lastInsertedBlockIDToReset = common.Big0
159+
reorgCheckResult.L1CurrentToReset = genesisL1Header
160+
reorgCheckResult.LastHandledBlockIDToReset = common.Big0
161161
} else {
162-
reorged, l1CurrentToReset, lastInsertedBlockIDToReset, err = s.rpc.CheckL1ReorgFromL2EE(
162+
// 2. Parent block
163+
reorgCheckResult, err = s.rpc.CheckL1Reorg(
163164
ctx,
164165
new(big.Int).Sub(event.BlockId, common.Big1),
165166
)
@@ -168,18 +169,18 @@ func (s *Syncer) onBlockProposed(
168169
}
169170
}
170171

171-
if reorged {
172+
if reorgCheckResult.IsReorged {
172173
log.Info(
173174
"Reset L1Current cursor due to L1 reorg",
174175
"l1CurrentHeightOld", s.state.GetL1Current().Number,
175176
"l1CurrentHashOld", s.state.GetL1Current().Hash(),
176-
"l1CurrentHeightNew", l1CurrentToReset.Number,
177-
"l1CurrentHashNew", l1CurrentToReset.Hash(),
177+
"l1CurrentHeightNew", reorgCheckResult.L1CurrentToReset.Number,
178+
"l1CurrentHashNew", reorgCheckResult.L1CurrentToReset.Hash(),
178179
"lastInsertedBlockIDOld", s.lastInsertedBlockID,
179-
"lastInsertedBlockIDNew", lastInsertedBlockIDToReset,
180+
"lastInsertedBlockIDNew", reorgCheckResult.LastHandledBlockIDToReset,
180181
)
181-
s.state.SetL1Current(l1CurrentToReset)
182-
s.lastInsertedBlockID = lastInsertedBlockIDToReset
182+
s.state.SetL1Current(reorgCheckResult.L1CurrentToReset)
183+
s.lastInsertedBlockID = reorgCheckResult.LastHandledBlockIDToReset
183184
s.reorgDetectedFlag = true
184185
endIter()
185186

@@ -222,20 +223,16 @@ func (s *Syncer) onBlockProposed(
222223

223224
log.Debug("Parent block", "height", parent.Number, "hash", parent.Hash())
224225

225-
tx, err := s.rpc.L1.TransactionInBlock(
226-
ctx,
227-
event.Raw.BlockHash,
228-
event.Raw.TxIndex,
229-
)
226+
tx, err := s.rpc.L1.TransactionInBlock(ctx, event.Raw.BlockHash, event.Raw.TxIndex)
230227
if err != nil {
231-
return fmt.Errorf("failed to fetch original TaikoL1.ProposeBlock transaction: %w", err)
228+
return fmt.Errorf("failed to fetch original TaikoL1.proposeBlock transaction: %w", err)
232229
}
233230

234231
var txListDecoder txlistfetcher.TxListFetcher
235232
if event.Meta.BlobUsed {
236233
txListDecoder = txlistfetcher.NewBlobTxListFetcher(s.rpc)
237234
} else {
238-
txListDecoder = &txlistfetcher.CalldataFetcher{}
235+
txListDecoder = new(txlistfetcher.CalldataFetcher)
239236
}
240237
txListBytes, err := txListDecoder.Fetch(ctx, tx, &event.Meta)
241238
if err != nil {

driver/driver_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,12 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
147147
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
148148
s.Greater(l1Head2.Number.Uint64(), l1Head1.Number.Uint64())
149149

150-
reorged, _, _, err := s.RPCClient.CheckL1ReorgFromL2EE(
150+
res, err := s.RPCClient.CheckL1Reorg(
151151
context.Background(),
152152
l2Head2.Number,
153153
)
154154
s.Nil(err)
155-
s.False(reorged)
155+
s.False(res.IsReorged)
156156

157157
// Reorg back to l2Head1
158158
s.RevertL1Snapshot(testnetL1SnapshotID)
@@ -209,12 +209,12 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
209209
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
210210
s.Greater(l1Head2.Number.Uint64(), l1Head1.Number.Uint64())
211211

212-
reorged, _, _, err := s.RPCClient.CheckL1ReorgFromL2EE(
212+
res, err := s.RPCClient.CheckL1Reorg(
213213
context.Background(),
214214
l2Head2.Number,
215215
)
216216
s.Nil(err)
217-
s.False(reorged)
217+
s.False(res.IsReorged)
218218

219219
// Reorg back to l2Head1
220220
s.RevertL1Snapshot(testnetL1SnapshotID)
@@ -267,12 +267,12 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {
267267
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
268268
s.Greater(l1Head2.Number.Uint64(), l1Head1.Number.Uint64())
269269

270-
reorged, _, _, err := s.RPCClient.CheckL1ReorgFromL2EE(
270+
res, err := s.RPCClient.CheckL1Reorg(
271271
context.Background(),
272272
l2Head2.Number,
273273
)
274274
s.Nil(err)
275-
s.False(reorged)
275+
s.False(res.IsReorged)
276276

277277
// Reorg back to l2Head1
278278
s.RevertL1Snapshot(testnetL1SnapshotID)

internal/sender/sender.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ func (s *Sender) SendTransaction(tx *types.Transaction) (string, error) {
269269
}
270270

271271
if err := s.send(txToConfirm, true); err != nil && !strings.Contains(err.Error(), "replacement transaction") {
272-
log.Error("Failed to send transaction",
272+
log.Error(
273+
"Failed to send transaction",
273274
"tx_id", txID,
274275
"nonce", txToConfirm.CurrentTx.Nonce(),
275276
"hash", tx.Hash(),
@@ -285,7 +286,7 @@ func (s *Sender) SendTransaction(tx *types.Transaction) (string, error) {
285286
return txID, nil
286287
}
287288

288-
// send is the internal method to send the given transaction.
289+
// send is the internal method to send the real transaction.
289290
func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
290291
s.mu.Lock()
291292
defer s.mu.Unlock()
@@ -312,14 +313,16 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
312313
if err != nil {
313314
if strings.Contains(err.Error(), "nonce too low") {
314315
if err := s.SetNonce(originalTx, true); err != nil {
315-
log.Error("Failed to set nonce when appear nonce too low",
316+
log.Error(
317+
"Failed to set nonce when appear nonce too low",
316318
"tx_id", tx.ID,
317319
"nonce", tx.CurrentTx.Nonce(),
318320
"hash", rawTx.Hash(),
319321
"err", err,
320322
)
321323
} else {
322-
log.Warn("Nonce is incorrect, retry sending the transaction with new nonce",
324+
log.Warn(
325+
"Nonce is incorrect, retry sending the transaction with new nonce",
323326
"tx_id", tx.ID,
324327
"nonce", tx.CurrentTx.Nonce(),
325328
"hash", rawTx.Hash(),
@@ -330,15 +333,17 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
330333
}
331334
if strings.Contains(err.Error(), "replacement transaction underpriced") {
332335
s.adjustGas(originalTx)
333-
log.Warn("Replacement transaction underpriced",
336+
log.Warn(
337+
"Replacement transaction underpriced",
334338
"tx_id", tx.ID,
335339
"nonce", tx.CurrentTx.Nonce(),
336340
"hash", rawTx.Hash(),
337341
"err", err,
338342
)
339343
continue
340344
}
341-
log.Error("Failed to send transaction",
345+
log.Error(
346+
"Failed to send transaction",
342347
"tx_id", tx.ID,
343348
"nonce", tx.CurrentTx.Nonce(),
344349
"hash", rawTx.Hash(),

internal/testutils/helper.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ func (s *ClientTestSuite) ProposeAndInsertEmptyBlocks(
7878
s.Nil(err)
7979
s.Greater(newL1Head.Number.Uint64(), l1Head.Number.Uint64())
8080

81-
syncProgress, err := s.RPCClient.L2.SyncProgress(context.Background())
82-
s.Nil(err)
83-
s.Nil(syncProgress)
84-
8581
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
8682
defer cancel()
8783

88-
s.Nil(calldataSyncer.ProcessL1Blocks(ctx, newL1Head))
84+
s.Nil(backoff.Retry(func() error {
85+
return calldataSyncer.ProcessL1Blocks(ctx, newL1Head)
86+
}, backoff.NewExponentialBackOff()))
87+
88+
s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background()))
8989

9090
return events
9191
}
@@ -146,13 +146,14 @@ func (s *ClientTestSuite) ProposeAndInsertValidBlock(
146146
s.Nil(err)
147147
s.Greater(newL1Head.Number.Uint64(), l1Head.Number.Uint64())
148148

149-
_, err = s.RPCClient.L2.SyncProgress(context.Background())
150-
s.Nil(err)
151-
152149
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
153150
defer cancel()
154151

155-
s.Nil(calldataSyncer.ProcessL1Blocks(ctx, newL1Head))
152+
s.Nil(backoff.Retry(func() error {
153+
return calldataSyncer.ProcessL1Blocks(ctx, newL1Head)
154+
}, backoff.NewExponentialBackOff()))
155+
156+
s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background()))
156157

157158
_, err = s.RPCClient.L2.HeaderByNumber(context.Background(), nil)
158159
s.Nil(err)
@@ -180,7 +181,6 @@ func (s *ClientTestSuite) NewTestProverServer(
180181
RPC: s.RPCClient,
181182
ProtocolConfigs: &protocolConfig,
182183
LivenessBond: protocolConfig.LivenessBond,
183-
IsGuardian: true,
184184
})
185185
s.Nil(err)
186186

internal/testutils/suite.go

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ func (s *ClientTestSuite) SetupTest() {
6464
s.Nil(err)
6565
s.RPCClient = rpcCli
6666

67+
s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background()))
68+
6769
l1ProverPrivKey, err := crypto.ToECDSA(common.FromHex(os.Getenv("L1_PROVER_PRIVATE_KEY")))
6870
s.Nil(err)
6971

0 commit comments

Comments
 (0)