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

Commit

Permalink
refactor(prover): refactor prover / guardian prover (#586)
Browse files Browse the repository at this point in the history
Co-authored-by: jeff <[email protected]>
Co-authored-by: Gavin Yu <[email protected]>
Co-authored-by: yuguo <[email protected]>
  • Loading branch information
4 people authored Mar 8, 2024
1 parent 2644e60 commit 077ccf2
Show file tree
Hide file tree
Showing 47 changed files with 6,195 additions and 1,646 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ dbdata
# Testing
integration_test/nodes/deployments/mainnet.json
.env

\.idea/
30 changes: 21 additions & 9 deletions bindings/encoding/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,17 @@ var (

// Contract ABIs.
var (
TaikoL1ABI *abi.ABI
TaikoL2ABI *abi.ABI
GuardianProverABI *abi.ABI
LibDepositingABI *abi.ABI
LibProposingABI *abi.ABI
LibProvingABI *abi.ABI
LibUtilsABI *abi.ABI
LibVerifyingABI *abi.ABI
AssignmentHookABI *abi.ABI
TaikoL1ABI *abi.ABI
TaikoL2ABI *abi.ABI
GuardianProverABI *abi.ABI
LibDepositingABI *abi.ABI
LibProposingABI *abi.ABI
LibProvingABI *abi.ABI
LibUtilsABI *abi.ABI
LibVerifyingABI *abi.ABI
AssignmentHookABI *abi.ABI
SGXVerifierABI *abi.ABI
GuardianVerifierABI *abi.ABI

customErrorMaps []map[string]abi.Error
)
Expand Down Expand Up @@ -314,6 +316,14 @@ func init() {
log.Crit("Get AssignmentHook ABI error", "error", err)
}

if SGXVerifierABI, err = bindings.SgxVerifierMetaData.GetAbi(); err != nil {
log.Crit("Get SGXVerifier ABI error", err)
}

if GuardianVerifierABI, err = bindings.GuardianVerifierMetaData.GetAbi(); err != nil {
log.Crit("Get GuardianVerifier ABI error", "error", err)
}

customErrorMaps = []map[string]abi.Error{
TaikoL1ABI.Errors,
TaikoL2ABI.Errors,
Expand All @@ -324,6 +334,8 @@ func init() {
LibUtilsABI.Errors,
LibVerifyingABI.Errors,
AssignmentHookABI.Errors,
SGXVerifierABI.Errors,
GuardianVerifierABI.Errors,
}
}

Expand Down
1,737 changes: 1,737 additions & 0 deletions bindings/gen_guardian_verifier.go

Large diffs are not rendered by default.

2,346 changes: 2,346 additions & 0 deletions bindings/gen_sgx_verifier.go

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions cmd/flags/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ var (
Usage: "Gas limit will be used for TaikoL1.proveBlock transactions",
Category: proverCategory,
}
ProveBlockMaxTxGasTipCap = &cli.Uint64Flag{
Name: "tx.maxGasTipCap",
Usage: "Gas tip cap (in wei) for a TaikoL1.proveBlock transaction when doing the transaction replacement",
ProveBlockMaxTxGasFeeCap = &cli.Uint64Flag{
Name: "tx.maxGasFeeCap",
Usage: "Gas fee cap (in wei) for a TaikoL1.proveBlock transaction when doing the transaction replacement",
Category: proverCategory,
}
ProveBlockTxReplacementMultiplier = &cli.Uint64Flag{
Name: "tx.replacementMultiplier",
Value: 2,
Usage: "Gas tip multiplier when replacing a TaikoL1.proveBlock transaction with same nonce",
TxReplacementGasGrowthRate = &cli.Uint64Flag{
Name: "tx.replacementProveBlockGasGrowthRate",
Value: 50,
Usage: "Gas tip growth rate when replacing a TaikoL1.proveBlock transaction with same nonce",
Category: proverCategory,
}
// Running mode
Expand Down Expand Up @@ -198,8 +198,8 @@ var ProverFlags = MergeFlags(CommonFlags, []cli.Flag{
GuardianProofSubmissionDelay,
GuardianProverHealthCheckServerEndpoint,
ProofSubmissionMaxRetry,
ProveBlockTxReplacementMultiplier,
ProveBlockMaxTxGasTipCap,
TxReplacementGasGrowthRate,
ProveBlockMaxTxGasFeeCap,
Graffiti,
ProveUnassignedBlocks,
ContesterMode,
Expand Down
45 changes: 21 additions & 24 deletions driver/chain_syncer/calldata/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,29 +137,30 @@ func (s *Syncer) onBlockProposed(

if !s.progressTracker.Triggered() {
// Check whether we need to reorg the L2 chain at first.
// 1. Last verified block
var (
reorged bool
l1CurrentToReset *types.Header
lastInsertedBlockIDToReset *big.Int
err error
reorgCheckResult = new(rpc.ReorgCheckResult)
err error
)
reorged, err = s.checkLastVerifiedBlockMismatch(ctx)
// 1. The latest verified block
reorgCheckResult.IsReorged, err = s.checkLastVerifiedBlockMismatch(ctx)
if err != nil {
return fmt.Errorf("failed to check if last verified block in L2 EE has been reorged: %w", err)
}

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

l1CurrentToReset = genesisL1Header
lastInsertedBlockIDToReset = common.Big0
reorgCheckResult.L1CurrentToReset = genesisL1Header
reorgCheckResult.LastHandledBlockIDToReset = common.Big0
} else {
reorged, l1CurrentToReset, lastInsertedBlockIDToReset, err = s.rpc.CheckL1ReorgFromL2EE(
// 2. Parent block
reorgCheckResult, err = s.rpc.CheckL1Reorg(
ctx,
new(big.Int).Sub(event.BlockId, common.Big1),
)
Expand All @@ -168,18 +169,18 @@ func (s *Syncer) onBlockProposed(
}
}

if reorged {
if reorgCheckResult.IsReorged {
log.Info(
"Reset L1Current cursor due to L1 reorg",
"l1CurrentHeightOld", s.state.GetL1Current().Number,
"l1CurrentHashOld", s.state.GetL1Current().Hash(),
"l1CurrentHeightNew", l1CurrentToReset.Number,
"l1CurrentHashNew", l1CurrentToReset.Hash(),
"l1CurrentHeightNew", reorgCheckResult.L1CurrentToReset.Number,
"l1CurrentHashNew", reorgCheckResult.L1CurrentToReset.Hash(),
"lastInsertedBlockIDOld", s.lastInsertedBlockID,
"lastInsertedBlockIDNew", lastInsertedBlockIDToReset,
"lastInsertedBlockIDNew", reorgCheckResult.LastHandledBlockIDToReset,
)
s.state.SetL1Current(l1CurrentToReset)
s.lastInsertedBlockID = lastInsertedBlockIDToReset
s.state.SetL1Current(reorgCheckResult.L1CurrentToReset)
s.lastInsertedBlockID = reorgCheckResult.LastHandledBlockIDToReset
s.reorgDetectedFlag = true
endIter()

Expand Down Expand Up @@ -222,20 +223,16 @@ func (s *Syncer) onBlockProposed(

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

tx, err := s.rpc.L1.TransactionInBlock(
ctx,
event.Raw.BlockHash,
event.Raw.TxIndex,
)
tx, err := s.rpc.L1.TransactionInBlock(ctx, event.Raw.BlockHash, event.Raw.TxIndex)
if err != nil {
return fmt.Errorf("failed to fetch original TaikoL1.ProposeBlock transaction: %w", err)
return fmt.Errorf("failed to fetch original TaikoL1.proposeBlock transaction: %w", err)
}

var txListDecoder txlistfetcher.TxListFetcher
if event.Meta.BlobUsed {
txListDecoder = txlistfetcher.NewBlobTxListFetcher(s.rpc)
} else {
txListDecoder = &txlistfetcher.CalldataFetcher{}
txListDecoder = new(txlistfetcher.CalldataFetcher)
}
txListBytes, err := txListDecoder.Fetch(ctx, tx, &event.Meta)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ func (s *DriverTestSuite) TestCheckL1ReorgToHigherFork() {
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
s.Greater(l1Head2.Number.Uint64(), l1Head1.Number.Uint64())

reorged, _, _, err := s.RPCClient.CheckL1ReorgFromL2EE(
res, err := s.RPCClient.CheckL1Reorg(
context.Background(),
l2Head2.Number,
)
s.Nil(err)
s.False(reorged)
s.False(res.IsReorged)

// Reorg back to l2Head1
s.RevertL1Snapshot(testnetL1SnapshotID)
Expand Down Expand Up @@ -209,12 +209,12 @@ func (s *DriverTestSuite) TestCheckL1ReorgToLowerFork() {
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
s.Greater(l1Head2.Number.Uint64(), l1Head1.Number.Uint64())

reorged, _, _, err := s.RPCClient.CheckL1ReorgFromL2EE(
res, err := s.RPCClient.CheckL1Reorg(
context.Background(),
l2Head2.Number,
)
s.Nil(err)
s.False(reorged)
s.False(res.IsReorged)

// Reorg back to l2Head1
s.RevertL1Snapshot(testnetL1SnapshotID)
Expand Down Expand Up @@ -267,12 +267,12 @@ func (s *DriverTestSuite) TestCheckL1ReorgToSameHeightFork() {
s.Greater(l2Head2.Number.Uint64(), l2Head1.Number.Uint64())
s.Greater(l1Head2.Number.Uint64(), l1Head1.Number.Uint64())

reorged, _, _, err := s.RPCClient.CheckL1ReorgFromL2EE(
res, err := s.RPCClient.CheckL1Reorg(
context.Background(),
l2Head2.Number,
)
s.Nil(err)
s.False(reorged)
s.False(res.IsReorged)

// Reorg back to l2Head1
s.RevertL1Snapshot(testnetL1SnapshotID)
Expand Down
17 changes: 11 additions & 6 deletions internal/sender/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ func (s *Sender) SendTransaction(tx *types.Transaction) (string, error) {
}

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

// send is the internal method to send the given transaction.
// send is the internal method to send the real transaction.
func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
s.mu.Lock()
defer s.mu.Unlock()
Expand All @@ -312,14 +313,16 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
if err != nil {
if strings.Contains(err.Error(), "nonce too low") {
if err := s.SetNonce(originalTx, true); err != nil {
log.Error("Failed to set nonce when appear nonce too low",
log.Error(
"Failed to set nonce when appear nonce too low",
"tx_id", tx.ID,
"nonce", tx.CurrentTx.Nonce(),
"hash", rawTx.Hash(),
"err", err,
)
} else {
log.Warn("Nonce is incorrect, retry sending the transaction with new nonce",
log.Warn(
"Nonce is incorrect, retry sending the transaction with new nonce",
"tx_id", tx.ID,
"nonce", tx.CurrentTx.Nonce(),
"hash", rawTx.Hash(),
Expand All @@ -330,15 +333,17 @@ func (s *Sender) send(tx *TxToConfirm, resetNonce bool) error {
}
if strings.Contains(err.Error(), "replacement transaction underpriced") {
s.adjustGas(originalTx)
log.Warn("Replacement transaction underpriced",
log.Warn(
"Replacement transaction underpriced",
"tx_id", tx.ID,
"nonce", tx.CurrentTx.Nonce(),
"hash", rawTx.Hash(),
"err", err,
)
continue
}
log.Error("Failed to send transaction",
log.Error(
"Failed to send transaction",
"tx_id", tx.ID,
"nonce", tx.CurrentTx.Nonce(),
"hash", rawTx.Hash(),
Expand Down
20 changes: 10 additions & 10 deletions internal/testutils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ func (s *ClientTestSuite) ProposeAndInsertEmptyBlocks(
s.Nil(err)
s.Greater(newL1Head.Number.Uint64(), l1Head.Number.Uint64())

syncProgress, err := s.RPCClient.L2.SyncProgress(context.Background())
s.Nil(err)
s.Nil(syncProgress)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s.Nil(calldataSyncer.ProcessL1Blocks(ctx, newL1Head))
s.Nil(backoff.Retry(func() error {
return calldataSyncer.ProcessL1Blocks(ctx, newL1Head)
}, backoff.NewExponentialBackOff()))

s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background()))

return events
}
Expand Down Expand Up @@ -146,13 +146,14 @@ func (s *ClientTestSuite) ProposeAndInsertValidBlock(
s.Nil(err)
s.Greater(newL1Head.Number.Uint64(), l1Head.Number.Uint64())

_, err = s.RPCClient.L2.SyncProgress(context.Background())
s.Nil(err)

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

s.Nil(calldataSyncer.ProcessL1Blocks(ctx, newL1Head))
s.Nil(backoff.Retry(func() error {
return calldataSyncer.ProcessL1Blocks(ctx, newL1Head)
}, backoff.NewExponentialBackOff()))

s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background()))

_, err = s.RPCClient.L2.HeaderByNumber(context.Background(), nil)
s.Nil(err)
Expand Down Expand Up @@ -180,7 +181,6 @@ func (s *ClientTestSuite) NewTestProverServer(
RPC: s.RPCClient,
ProtocolConfigs: &protocolConfig,
LivenessBond: protocolConfig.LivenessBond,
IsGuardian: true,
})
s.Nil(err)

Expand Down
2 changes: 2 additions & 0 deletions internal/testutils/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ func (s *ClientTestSuite) SetupTest() {
s.Nil(err)
s.RPCClient = rpcCli

s.Nil(s.RPCClient.WaitTillL2ExecutionEngineSynced(context.Background()))

l1ProverPrivKey, err := crypto.ToECDSA(common.FromHex(os.Getenv("L1_PROVER_PRIVATE_KEY")))
s.Nil(err)

Expand Down
Loading

0 comments on commit 077ccf2

Please sign in to comment.