Skip to content

Commit

Permalink
👌 Applied suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Feb 3, 2024
1 parent 31fffb6 commit 265a6e8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
6 changes: 4 additions & 2 deletions pkg/chain/chain_constants.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package chain

const (
L2BedrockMessagePasserAddress = "0x4200000000000000000000000000000000000016"
)

// L2ChainIDs manages L2 network chainIDs.
type L2ChainIDs struct {
optimism uint64
Expand Down Expand Up @@ -31,8 +35,6 @@ var l2NetworkChainIDs = L2ChainIDs{
liskSepolia: 4202,
}

const L2BedrockMessagePasserAddress string = "0x4200000000000000000000000000000000000016"

type NetworkType = string

const (
Expand Down
1 change: 1 addition & 0 deletions pkg/encoding/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ func ComputeL2OutputRoot(stateRoot common.Hash, storageHash common.Hash, outputB
}

outputRoot := crypto.Keccak256Hash(l2Output.Marshal())

return hexutil.Encode(outputRoot[:])
}
16 changes: 9 additions & 7 deletions pkg/faultdetector/faultdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newFaultDetectorMetrics(reg prometheus.Registerer) *faultDetectorMetrics {
highestOutputIndex: prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "fault_detector_highest_output_index",
Help: "The highest current output index",
Help: "The highest current output index that is being checked for faults",
}),
stateMismatch: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "fault_detector_is_state_mismatch",
Expand Down Expand Up @@ -95,7 +95,6 @@ func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan err
}

oracleContractAccessor, err := chain.NewOracleAccessor(ctx, chainConfig)

if err != nil {
logger.Errorf("Failed to create Oracle contract accessor with chainID: %d, L1 endpoint: %s and L2OutputOracleContractAddress: %s, error: %w", l2ChainID.Int64(), faultDetectorConfig.L1RPCEndpoint, faultDetectorConfig.L2OutputOracleContractAddress, err)
return nil, err
Expand Down Expand Up @@ -183,12 +182,12 @@ func (fd *FaultDetector) Stop() {
// checkFault continuously checks for the faults at regular interval.
func (fd *FaultDetector) checkFault() {
startTime := time.Now()

fd.logger.Infof("Checking current batch with output index %d.", fd.currentOutputIndex)

nextOutputIndex, err := fd.oracleContractAccessor.GetNextOutputIndex()
if err != nil {
fd.logger.Errorf("Failed to query next output index.")
fd.metrics.apiConnectionFailure.Inc()
time.Sleep(waitTimeInFailure * time.Second)
return
}
Expand All @@ -203,13 +202,15 @@ func (fd *FaultDetector) checkFault() {
l2OutputData, err := fd.oracleContractAccessor.GetL2Output(encoding.MustConvertUint64ToBigInt(fd.currentOutputIndex))
if err != nil {
fd.logger.Errorf("Failed to fetch output associated with index %d.", fd.currentOutputIndex)
fd.metrics.apiConnectionFailure.Inc()
time.Sleep(waitTimeInFailure * time.Second)
return
}

latestBlockNumber, err := fd.l2RpcApi.GetLatestBlockNumber(fd.ctx)
if err != nil {
fd.logger.Errorf("Failed to query L2 latest block number %d", latestBlockNumber)
fd.metrics.apiConnectionFailure.Inc()
time.Sleep(waitTimeInFailure * time.Second)
return
}
Expand All @@ -225,13 +226,15 @@ func (fd *FaultDetector) checkFault() {
outputBlockHeader, err := fd.l2RpcApi.GetBlockHeaderByNumber(fd.ctx, encoding.MustConvertUint64ToBigInt(l2OutputBlockNumber))
if err != nil {
fd.logger.Errorf("Failed to fetch block header by number %d.", l2OutputBlockNumber)
fd.metrics.apiConnectionFailure.Inc()
time.Sleep(waitTimeInFailure * time.Second)
return
}

messagePasserProofResponse, err := fd.l2RpcApi.GetProof(fd.ctx, encoding.MustConvertUint64ToBigInt(l2OutputBlockNumber), common.HexToAddress(chain.L2BedrockMessagePasserAddress))
if err != nil {
fd.logger.Errorf("Failed to fetch message passer proof for the block with height %d and address %s.", l2OutputBlockNumber, chain.L2BedrockMessagePasserAddress)
fd.metrics.apiConnectionFailure.Inc()
time.Sleep(waitTimeInFailure * time.Second)
return
}
Expand All @@ -241,21 +244,20 @@ func (fd *FaultDetector) checkFault() {
messagePasserProofResponse.StorageHash,
outputBlockHeader.Hash(),
)

if calculatedOutputRoot != expectedOutputRoot {
fd.diverged = true
fd.metrics.stateMismatch.Set(1)
finalizationTime := time.Unix(int64(outputBlockHeader.Time+fd.faultProofWindow), 0)
fd.logger.Errorf("State root does not match expectedStateRoot: %s, calculatedStateRoot: %s, finalizationTime: %s.", expectedOutputRoot, calculatedOutputRoot, finalizationTime)
return
}

// TODO: Increment or set in different scenarios
fd.metrics.highestOutputIndex.Inc()
fd.metrics.stateMismatch.Set(0)
fd.metrics.highestOutputIndex.Set(float64(fd.currentOutputIndex))

// Time taken to execute each batch in milliseconds.
elapsedTime := time.Since(startTime).Milliseconds()
fd.logger.Infof("Successfully checked current batch with index %d --> ok, time taken %dms.", fd.currentOutputIndex, elapsedTime)
fd.diverged = false
fd.currentOutputIndex++
fd.metrics.stateMismatch.Set(0)
}

0 comments on commit 265a6e8

Please sign in to comment.