Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fault detector helper function #21

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"math/big"

"github.com/LiskHQ/op-fault-detector/pkg/encoding"
"github.com/LiskHQ/op-fault-detector/pkg/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand Down Expand Up @@ -60,6 +61,15 @@ func (c *ChainAPIClient) GetBlockByNumber(ctx context.Context, blockNumber *big.
return c.eth.BlockByNumber(ctx, blockNumber)
}

// GetLatestBlockHeader returns latest block header from a connected node.
func (c *ChainAPIClient) GetLatestBlockHeader(ctx context.Context) (*types.Header, error) {
blockNumber, err := c.eth.BlockNumber(ctx)
if err != nil {
return nil, err
}
return c.eth.HeaderByNumber(ctx, encoding.MustConvertUint64ToBigInt(blockNumber))
}

// GetProof returns the account and storage values, including the Merkle proof, of the specified account/address.
func (c *ChainAPIClient) GetProof(ctx context.Context, blockNumber *big.Int, address common.Address) (*ProofResponse, error) {
var result ProofResponse
Expand Down
7 changes: 4 additions & 3 deletions pkg/faultdetector/faultdetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/LiskHQ/op-fault-detector/pkg/chain"
"github.com/LiskHQ/op-fault-detector/pkg/config"
"github.com/LiskHQ/op-fault-detector/pkg/encoding"
"github.com/LiskHQ/op-fault-detector/pkg/log"
)

Expand Down Expand Up @@ -48,20 +49,20 @@ func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan err

l2ChainID, err := l2RpcApi.GetChainID(ctx)
if err != nil {
logger.Errorf("Failed to get L2 provider's chainID: %d, error: %w", l2ChainID.Int64(), err)
logger.Errorf("Failed to get L2 provider's chainID: %d, error: %w", encoding.MustConvertBigIntToUint64(l2ChainID), err)
return nil, err
}

// Initialize Oracle contract accessor
chainConfig := &chain.ConfigOptions{
L1RPCEndpoint: faultDetectorConfig.L1RPCEndpoint,
ChainID: l2ChainID.Uint64(),
ChainID: encoding.MustConvertBigIntToUint64(l2ChainID),
L2OutputOracleContractAddress: faultDetectorConfig.L2OutputOracleContractAddress,
}

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)
logger.Errorf("Failed to create Oracle contract accessor with chainID: %d, L1 endpoint: %s and L2OutputOracleContractAddress: %s, error: %w", encoding.MustConvertBigIntToUint64(l2ChainID), faultDetectorConfig.L1RPCEndpoint, faultDetectorConfig.L2OutputOracleContractAddress, err)
return nil, err
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/faultdetector/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package faultdetector

import (
"context"
"fmt"

"github.com/LiskHQ/op-fault-detector/pkg/chain"
"github.com/LiskHQ/op-fault-detector/pkg/config"
"github.com/LiskHQ/op-fault-detector/pkg/encoding"
"github.com/LiskHQ/op-fault-detector/pkg/log"
)

// FindFirstUnfinalizedOutputIndex finds and returns the first L2 output index that has not yet passed the fault proof window.
func FindFirstUnfinalizedOutputIndex(ctx context.Context, logger log.Logger, fpw uint64, faultDetectorConfig *config.FaultDetectorConfig) (uint64, error) {
Incede marked this conversation as resolved.
Show resolved Hide resolved
l2RpcApi, err := chain.GetAPIClient(ctx, faultDetectorConfig.L2RPCEndpoint, logger)
if err != nil {
logger.Errorf("Failed to create API client for L2 Provider with given endpoint: %s, error: %w", faultDetectorConfig.L2RPCEndpoint, err)
return 0, err
}
l2ChainID, err := l2RpcApi.GetChainID(ctx)
if err != nil {
logger.Errorf("Failed to get L2 provider's chainID: %d, error: %w", encoding.MustConvertBigIntToUint64(l2ChainID), err)
return 0, err
}
chainConfig := &chain.ConfigOptions{
L1RPCEndpoint: faultDetectorConfig.L1RPCEndpoint,
ChainID: encoding.MustConvertBigIntToUint64(l2ChainID),
L2OutputOracleContractAddress: faultDetectorConfig.L2OutputOracleContractAddress,
}
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", encoding.MustConvertBigIntToUint64(l2ChainID), faultDetectorConfig.L1RPCEndpoint, faultDetectorConfig.L2OutputOracleContractAddress, err)
return 0, err
}
latestBlockHeader, err := l2RpcApi.GetLatestBlockHeader(ctx)
if err != nil {
logger.Errorf("Failed to get latest block header from L2 provider, error: %w", err)
return 0, err
}
totalOutputsBigInt, err := oracleContractAccessor.GetNextOutputIndex()
if err != nil {
logger.Errorf("Failed to get next output index, error: %w", err)
return 0, err
}
totalOutputs := encoding.MustConvertBigIntToUint64(totalOutputsBigInt)

// Perform a binary search to find the next batch that will pass the challenge period.
ishantiw marked this conversation as resolved.
Show resolved Hide resolved
var lo uint64 = 0
hi := totalOutputs
for lo != hi {
mid := (lo + hi) / 2
midBigInt := encoding.MustConvertUint64ToBigInt(mid)
outputData, err := oracleContractAccessor.GetL2Output(midBigInt)
if err != nil {
logger.Errorf("Failed to get L2 output for index: %d, error: %w", midBigInt, err)
return 0, err
}
if outputData.L1Timestamp+fpw < latestBlockHeader.Time {
lo = mid + 1
} else {
hi = mid
}
}

// Result will be zero if the chain is less than FPW seconds old. Only returns 0 with error Undefined in the
// case that no batches have been submitted for an entire challenge period.
if lo == totalOutputs {
logger.Errorf("No batches have been submitted for the entire challenge period and therefore first unfinalized output index is undefined")
return 0, fmt.Errorf("Undefined")
} else {
return lo, nil
}
}
Loading