-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Implement initialization of fault detector
- Loading branch information
Showing
7 changed files
with
249 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.1.0", | ||
"configurations": [ | ||
{ | ||
"name": "Run Fault Detector", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceRoot}/cmd", | ||
"args": ["--config", "./config.yaml"], | ||
"showLog": true | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Package faultdetector implements Optimism fault detector | ||
package faultdetector | ||
|
||
import ( | ||
"context" | ||
"math/big" | ||
"sync" | ||
"time" | ||
|
||
"github.com/LiskHQ/op-fault-detector/pkg/chain" | ||
"github.com/LiskHQ/op-fault-detector/pkg/config" | ||
"github.com/LiskHQ/op-fault-detector/pkg/log" | ||
) | ||
|
||
const ( | ||
SERVICE_INTERVAL_IN_SECS = 5 | ||
) | ||
|
||
// FaultDetector contains all the RPC providers/contract accessors and holds state information. | ||
type FaultDetector struct { | ||
ctx context.Context | ||
logger log.Logger | ||
errorChan chan error | ||
wg *sync.WaitGroup | ||
l1RpcApi *chain.ChainAPIClient | ||
l2RpcApi *chain.ChainAPIClient | ||
oracleContractAccessor *chain.OracleAccessor | ||
faultProofWindow *big.Int | ||
currentOutputIndex *big.Int | ||
diverged bool | ||
ticker *time.Ticker | ||
} | ||
|
||
// NewFaultDetector will return [FaultDetector] with the initialized providers and configuration. | ||
func NewFaultDetector(ctx context.Context, logger log.Logger, errorChan chan error, wg *sync.WaitGroup, config *config.Config) (*FaultDetector, error) { | ||
faultDetectorConfig := config.FaultDetector | ||
|
||
// Initialize API Providers | ||
l1RpcApi, err := chain.GetAPIClient(ctx, config.FaultDetector.L1RPCEndpoint, logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l2RpcApi, err := chain.GetAPIClient(ctx, config.FaultDetector.L2RPCEndpoint, logger) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
l2ChainID, err := l2RpcApi.GetChainID(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Initialize Oracle contract accessor | ||
chainConfig := &chain.ConfigOptions{ | ||
L1RPCEndpoint: faultDetectorConfig.L1RPCEndpoint, | ||
ChainID: l2ChainID.Uint64(), | ||
L2OutputOracleContractAddress: faultDetectorConfig.L2OutputOracleContractAddress, | ||
} | ||
|
||
oracleContractAccessor, err := chain.NewOracleAccessor(ctx, chainConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// TODO: Calculate from findFirstUnfinalizedOutputIndex(context, OracleContractAccessor, L1Provider, faultProofWindow, logger) | ||
|
||
faultDetector := &FaultDetector{ | ||
ctx: ctx, | ||
logger: logger, | ||
errorChan: errorChan, | ||
wg: wg, | ||
l1RpcApi: l1RpcApi, | ||
l2RpcApi: l2RpcApi, | ||
oracleContractAccessor: oracleContractAccessor, | ||
faultProofWindow: big.NewInt(int64(2)), // TODO | ||
currentOutputIndex: big.NewInt(int64(2)), // TODO | ||
diverged: false, | ||
} | ||
|
||
return faultDetector, nil | ||
} | ||
|
||
// Start will start the fault detector service by invoking the service every given interval. | ||
func (fd *FaultDetector) Start() { | ||
fd.logger.Infof("Started fault detector, checking for state root every %d.", SERVICE_INTERVAL_IN_SECS) | ||
defer fd.wg.Done() | ||
fd.ticker = time.NewTicker(SERVICE_INTERVAL_IN_SECS * time.Second) | ||
quit := make(chan struct{}) | ||
go func() { | ||
for { | ||
select { | ||
case <-fd.ticker.C: | ||
fd.service() | ||
case <-quit: | ||
fd.ticker.Stop() | ||
fd.logger.Infof("Stopped fault detector service.") | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// Stop will stop the ticker. | ||
func (fd *FaultDetector) Stop() { | ||
fd.ticker.Stop() | ||
fd.logger.Infof("Successfully stopped fault detector service.") | ||
} | ||
|
||
// TODO: Implement service to check for faults | ||
func (fd *FaultDetector) service() { | ||
l1ChainID, err := fd.l1RpcApi.GetChainID(fd.ctx) | ||
if err != nil { | ||
fd.errorChan <- err | ||
} | ||
l2ChainID, err := fd.l2RpcApi.GetChainID(fd.ctx) | ||
if err != nil { | ||
fd.errorChan <- err | ||
} | ||
|
||
fd.logger.Infof("Connected to L1 chain with chainID: %d and L2 chain with chainID: %d", l1ChainID.Int64(), l2ChainID.Int64()) | ||
} |