Skip to content

Commit

Permalink
Add CLI for aggOracle
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar committed Jul 26, 2024
1 parent 78ad2dc commit 164a04e
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 372 deletions.
21 changes: 16 additions & 5 deletions aggoracle/chaingersender/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/0xPolygon/cdk-contracts-tooling/contracts/manual/pessimisticglobalexitroot"
cfgTypes "github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager"
"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -39,22 +40,32 @@ type EVMChainGERSender struct {
waitPeriodMonitorTx time.Duration
}

type EVMConfig struct {
GlobalExitRootL2 common.Address `mapstructure:"GlobalExitRootL2"`
URLRPCL2 string `mapstructure:"URLRPCL2"`
ChainIDL2 uint64 `mapstructure:"ChainIDL2"`
GasOffset uint64 `mapstructure:"GasOffset"`
WaitPeriodMonitorTx cfgTypes.Duration `mapstructure:"WaitPeriodMonitorTx"`
SenderAddr common.Address `mapstructure:"SenderAddr"`
EthTxManager ethtxmanager.Config `mapstructure:"EthTxManager"`
}

func NewEVMChainGERSender(
globalExitRoot, sender common.Address,
client EthClienter,
l2GlobalExitRoot, sender common.Address,
l2Client EthClienter,
ethTxMan EthTxManager,
gasOffset uint64,
waitPeriodMonitorTx time.Duration,
) (*EVMChainGERSender, error) {
gerContract, err := pessimisticglobalexitroot.NewPessimisticglobalexitroot(globalExitRoot, client)
gerContract, err := pessimisticglobalexitroot.NewPessimisticglobalexitroot(l2GlobalExitRoot, l2Client)
if err != nil {
return nil, err
}
return &EVMChainGERSender{
gerContract: gerContract,
gerAddr: globalExitRoot,
gerAddr: l2GlobalExitRoot,
sender: sender,
client: client,
client: l2Client,
ethTxMan: ethTxMan,
gasOffset: gasOffset,
waitPeriodMonitorTx: waitPeriodMonitorTx,
Expand Down
24 changes: 24 additions & 0 deletions aggoracle/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package aggoracle

import (
"github.com/0xPolygon/cdk/aggoracle/chaingersender"
"github.com/0xPolygon/cdk/config/types"
)

type TargetChainType string

const (
EVMChain TargetChainType = "EVM"
)

var (
SupportedChainTypes = []TargetChainType{EVMChain}
)

type Config struct {
TargetChainType TargetChainType `mapstructure:"TargetChainType"`
EVMSender chaingersender.EVMConfig `mapstructure:"EVMSender"`
URLRPCL1 string `mapstructure:"URLRPCL1"`
BlockFinality string `jsonschema:"enum=latest,enum=safe, enum=pending, enum=finalized" mapstructure:"BlockFinality"`
WaitPeriodNextGER types.Duration `mapstructure:"WaitPeriodNextGER"`
}
2 changes: 1 addition & 1 deletion aggoracle/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func commonSetup(t *testing.T) (
require.NoError(t, err)
// Syncer
dbPathSyncer := t.TempDir()
syncer, err := l1infotreesync.New(ctx, dbPathSyncer, gerL1Addr, 10, etherman.LatestBlock, reorg, l1Client.Client(), 32, time.Millisecond, 0)
syncer, err := l1infotreesync.New(ctx, dbPathSyncer, gerL1Addr, 10, etherman.LatestBlock, reorg, l1Client.Client(), time.Millisecond, 0)
require.NoError(t, err)
go syncer.Start(ctx)

Expand Down
4 changes: 3 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
SEQUENCE_SENDER = "sequence-sender"
// AGGREGATOR name to identify the aggregator component
AGGREGATOR = "aggregator"
// AGGORACLE name to identify the aggoracle component
AGGORACLE = "aggoracle"
)

const (
Expand Down Expand Up @@ -47,7 +49,7 @@ var (
Aliases: []string{"co"},
Usage: "List of components to run",
Required: false,
Value: cli.NewStringSlice(SEQUENCE_SENDER, AGGREGATOR),
Value: cli.NewStringSlice(SEQUENCE_SENDER, AGGREGATOR, AGGORACLE),
}
)

Expand Down
97 changes: 97 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ import (

zkevm "github.com/0xPolygon/cdk"
dataCommitteeClient "github.com/0xPolygon/cdk-data-availability/client"
"github.com/0xPolygon/cdk/aggoracle"
"github.com/0xPolygon/cdk/aggoracle/chaingersender"
"github.com/0xPolygon/cdk/aggregator"
"github.com/0xPolygon/cdk/aggregator/db"
"github.com/0xPolygon/cdk/config"
"github.com/0xPolygon/cdk/dataavailability"
"github.com/0xPolygon/cdk/dataavailability/datacommittee"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/reorgdetector"
"github.com/0xPolygon/cdk/sequencesender"
"github.com/0xPolygon/cdk/state"
"github.com/0xPolygon/cdk/state/pgstatestorage"
ethtxman "github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/etherman/etherscan"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager"
ethtxlog "github.com/0xPolygonHermez/zkevm-ethtx-manager/log"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -59,6 +66,16 @@ func start(cliCtx *cli.Context) error {
log.Fatal(err)
}
}()
case AGGORACLE:
l1Client, err := ethclient.Dial(c.AggOracle.URLRPCL1)
if err != nil {
log.Fatal(err)
}
reorgDetector := newReorgDetectorL1(cliCtx.Context, *c, l1Client)
syncer := newL1InfoTreeSyncer(cliCtx.Context, *c, l1Client, reorgDetector)
go syncer.Start(cliCtx.Context)
aggOracle := createAggoracle(*c, l1Client, syncer)
go aggOracle.Start(cliCtx.Context)
}
}

Expand Down Expand Up @@ -147,6 +164,51 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender {
return seqSender
}

func createAggoracle(cfg config.Config, l1Client *ethclient.Client, syncer *l1infotreesync.L1InfoTreeSync) *aggoracle.AggOracle {
var sender aggoracle.ChainSender
switch cfg.AggOracle.TargetChainType {
case aggoracle.EVMChain:
cfg.AggOracle.EVMSender.EthTxManager.Log = ethtxlog.Config{
Environment: ethtxlog.LogEnvironment(cfg.Log.Environment),
Level: cfg.Log.Level,
Outputs: cfg.Log.Outputs,
}
ethTxManager, err := ethtxmanager.New(cfg.AggOracle.EVMSender.EthTxManager)
l2CLient, err := ethclient.Dial(cfg.AggOracle.EVMSender.URLRPCL2)
if err != nil {
log.Fatal(err)
}
sender, err = chaingersender.NewEVMChainGERSender(
cfg.AggOracle.EVMSender.GlobalExitRootL2,
cfg.AggOracle.EVMSender.SenderAddr,
l2CLient,
ethTxManager,
cfg.AggOracle.EVMSender.GasOffset,
cfg.AggOracle.EVMSender.WaitPeriodMonitorTx.Duration,
)
if err != nil {
log.Fatal(err)
}
default:
log.Fatalf(
"Unsupported chaintype %s. Supported values: %v",
cfg.AggOracle.TargetChainType, aggoracle.SupportedChainTypes,
)
}
aggOracle, err := aggoracle.New(
sender,
l1Client,
syncer,
etherman.BlockNumberFinality(cfg.AggOracle.BlockFinality),
cfg.AggOracle.WaitPeriodNextGER.Duration,
)
if err != nil {
log.Fatal(err)
}

return aggOracle
}

func newDataAvailability(c config.Config, etherman *etherman.Client) (*dataavailability.DataAvailability, error) {
if !c.SequenceSender.IsValidiumMode {
return nil, nil
Expand Down Expand Up @@ -255,3 +317,38 @@ func newState(c *config.Config, l2ChainID uint64, sqlDB *pgxpool.Pool) *state.St
st := state.NewState(stateCfg, stateDb)
return st
}

func newReorgDetectorL1(
ctx context.Context,
cfg config.Config,
l1Client *ethclient.Client,
) *reorgdetector.ReorgDetector {
rd, err := reorgdetector.New(ctx, l1Client, cfg.ReorgDetectorL1.DBPath)
if err != nil {
log.Fatal(err)
}
return rd
}

func newL1InfoTreeSyncer(
ctx context.Context,
cfg config.Config,
l1Client *ethclient.Client,
reorgDetector *reorgdetector.ReorgDetector,
) *l1infotreesync.L1InfoTreeSync {
syncer, err := l1infotreesync.New(
ctx,
cfg.L1InfoTreeSync.DBPath,
cfg.L1InfoTreeSync.GlobalExitRootAddr,
cfg.L1InfoTreeSync.SyncBlockChunkSize,
etherman.BlockNumberFinality(cfg.L1InfoTreeSync.BlockFinality),
reorgDetector,
l1Client,
cfg.L1InfoTreeSync.WaitForNewBlocksPeriod.Duration,
cfg.L1InfoTreeSync.InitialBlock,
)
if err != nil {
log.Fatal(err)
}
return syncer
}
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"path/filepath"
"strings"

"github.com/0xPolygon/cdk/aggoracle"
"github.com/0xPolygon/cdk/aggregator"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/reorgdetector"
"github.com/0xPolygon/cdk/sequencesender"
"github.com/0xPolygonHermez/zkevm-ethtx-manager/ethtxmanager"
"github.com/mitchellh/mapstructure"
Expand Down Expand Up @@ -66,6 +69,12 @@ type Config struct {
NetworkConfig NetworkConfig
// Configuration of the sequence sender service
SequenceSender sequencesender.Config
// Configuration of the reorg detector service to be used for the L1
ReorgDetectorL1 reorgdetector.Config
// Configuration of the aggOracle service
AggOracle aggoracle.Config
// Configuration of the L1 Info Treee Sync service
L1InfoTreeSync l1infotreesync.Config
}

// Default parses the default configuration values.
Expand Down
2 changes: 1 addition & 1 deletion l1infotreesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestE2E(t *testing.T) {
rdm.On("AddBlockToTrack", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
client, gerAddr, gerSc, err := newSimulatedClient(auth)
require.NoError(t, err)
syncer, err := New(ctx, dbPath, gerAddr, 10, etherman.LatestBlock, rdm, client.Client(), 32, time.Millisecond, 0)
syncer, err := New(ctx, dbPath, gerAddr, 10, etherman.LatestBlock, rdm, client.Client(), time.Millisecond, 0)
require.NoError(t, err)
go syncer.Start(ctx)

Expand Down
14 changes: 12 additions & 2 deletions l1infotreesync/l1infotreesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"time"

"github.com/0xPolygon/cdk/config/types"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/sync"
"github.com/ethereum/go-ethereum/common"
Expand All @@ -19,6 +20,16 @@ var (
maxRetryAttemptsAfterError = 5
)

type Config struct {
DBPath string `mapstructure:"DBPath"`
GlobalExitRootAddr common.Address `mapstructure:"GlobalExitRootAddr"`
SyncBlockChunkSize uint64 `mapstructure:"SyncBlockChunkSize"`
BlockFinality string `jsonschema:"enum=latest,enum=safe, enum=pending, enum=finalized" mapstructure:"BlockFinality"`
URLRPCL1 string `mapstructure:"URLRPCL1"`
WaitForNewBlocksPeriod types.Duration `mapstructure:"WaitForNewBlocksPeriod"`
InitialBlock uint64 `mapstructure:"InitialBlock"`
}

type L1InfoTreeSync struct {
processor *processor
driver *sync.EVMDriver
Expand All @@ -32,11 +43,10 @@ func New(
blockFinalityType etherman.BlockNumberFinality,
rd sync.ReorgDetector,
l1Client EthClienter,
treeHeight uint8,
waitForNewBlocksPeriod time.Duration,
initialBlock uint64,
) (*L1InfoTreeSync, error) {
processor, err := newProcessor(ctx, dbPath, treeHeight)
processor, err := newProcessor(ctx, dbPath)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
infoTable = "l1infotreesync-info"
blockTable = "l1infotreesync-block"
lastBlockTable = "l1infotreesync-lastBlock"

treeHeight uint8 = 32
)

var (
Expand Down Expand Up @@ -88,7 +90,7 @@ func tableCfgFunc(defaultBuckets kv.TableCfg) kv.TableCfg {
}
}

func newProcessor(ctx context.Context, dbPath string, treeHeight uint8) (*processor, error) {
func newProcessor(ctx context.Context, dbPath string) (*processor, error) {
db, err := mdbx.NewMDBX(nil).
Path(dbPath).
WithTableCfg(tableCfgFunc).
Expand Down
Loading

0 comments on commit 164a04e

Please sign in to comment.