Skip to content

Commit

Permalink
fix: resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
rachit77 committed Sep 20, 2024
2 parents 2b19636 + a68d900 commit 70181e5
Show file tree
Hide file tree
Showing 58 changed files with 3,488 additions and 1,206 deletions.
3 changes: 2 additions & 1 deletion aggoracle/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"time"

"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/etherman"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/log"
Expand Down Expand Up @@ -70,7 +71,7 @@ func (a *AggOracle) Start(ctx context.Context) {
case errors.Is(err, l1infotreesync.ErrBlockNotProcessed):
a.logger.Debugf("syncer is not ready for the block %d", blockNumToFetch)

case errors.Is(err, l1infotreesync.ErrNotFound):
case errors.Is(err, db.ErrNotFound):
blockNumToFetch = 0
a.logger.Debugf("syncer has not found any GER until block %d", blockNumToFetch)

Expand Down
15 changes: 10 additions & 5 deletions bridgesync/bridgesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (s *BridgeSync) GetLastProcessedBlock(ctx context.Context) (uint64, error)
return s.processor.GetLastProcessedBlock(ctx)
}

func (s *BridgeSync) GetBridgeRootByHash(ctx context.Context, root common.Hash) (tree.Root, error) {
func (s *BridgeSync) GetBridgeRootByHash(ctx context.Context, root common.Hash) (*tree.Root, error) {
return s.processor.exitTree.GetRootByHash(ctx, root)
}

Expand All @@ -172,10 +172,7 @@ func (s *BridgeSync) GetBridges(ctx context.Context, fromBlock, toBlock uint64)
return s.processor.GetBridges(ctx, fromBlock, toBlock)
}

// GetProof retrieves the Merkle proof for the given deposit count and exit root.
func (s *BridgeSync) GetProof(
ctx context.Context, depositCount uint32, localExitRoot common.Hash,
) ([32]common.Hash, error) {
func (s *BridgeSync) GetProof(ctx context.Context, depositCount uint32, localExitRoot common.Hash) (tree.Proof, error) {
return s.processor.exitTree.GetProof(ctx, depositCount, localExitRoot)
}

Expand All @@ -186,3 +183,11 @@ func (p *processor) GetBlockByLER(ctx context.Context, ler common.Hash) (uint64,
}
return root.BlockNum, nil
}

func (s *BridgeSync) GetRootByLER(ctx context.Context, ler common.Hash) (*tree.Root, error) {
root, err := s.processor.exitTree.GetRootByHash(ctx, ler)
if err != nil {
return root, err
}
return root, nil
}
5 changes: 3 additions & 2 deletions bridgesync/claimcalldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/test/contracts/claimmock"
"github.com/0xPolygon/cdk/test/contracts/claimmockcaller"
tree "github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -52,11 +53,11 @@ func TestClaimCalldata(t *testing.T) {
require.NoError(t, err)

proofLocal := [32][32]byte{}
proofLocalH := [32]common.Hash{}
proofLocalH := tree.Proof{}
proofLocal[5] = common.HexToHash("beef")
proofLocalH[5] = common.HexToHash("beef")
proofRollup := [32][32]byte{}
proofRollupH := [32]common.Hash{}
proofRollupH := tree.Proof{}
proofRollup[4] = common.HexToHash("a1fa")
proofRollupH[4] = common.HexToHash("a1fa")
expectedClaim := Claim{
Expand Down
3 changes: 2 additions & 1 deletion bridgesync/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridge"
"github.com/0xPolygon/cdk-contracts-tooling/contracts/etrog/polygonzkevmbridgev2"
rpcTypes "github.com/0xPolygon/cdk-rpc/types"
"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/sync"
tree "github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -181,7 +182,7 @@ func setClaimCalldata(client EthClienter, bridge common.Address, txHash common.H
callStack.Push(c)
}
}
return ErrNotFound
return db.ErrNotFound
}

func setClaimIfFoundOnInput(input []byte, claim *Claim) (bool, error) {
Expand Down
3 changes: 1 addition & 2 deletions bridgesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
var (
// ErrBlockNotProcessed indicates that the given block(s) have not been processed yet.
ErrBlockNotProcessed = errors.New("given block(s) have not been processed yet")
ErrNotFound = errors.New("not found")
)

// Bridge is the representation of a bridge event
Expand Down Expand Up @@ -184,7 +183,7 @@ func (p *processor) queryBlockRange(tx db.Querier, fromBlock, toBlock uint64, ta
`, table), fromBlock, toBlock)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
return nil, db.ErrNotFound
}
return nil, err
}
Expand Down
19 changes: 10 additions & 9 deletions claimsponsor/claimsponsor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"time"

dbCommon "github.com/0xPolygon/cdk/common"
"github.com/0xPolygon/cdk/db"
"github.com/0xPolygon/cdk/log"
"github.com/0xPolygon/cdk/sync"
tree "github.com/0xPolygon/cdk/tree/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/iter"
Expand All @@ -31,14 +33,13 @@ const (

var (
ErrInvalidClaim = errors.New("invalid claim")
ErrNotFound = errors.New("not found")
)

// Claim representation of a claim event
type Claim struct {
LeafType uint8
ProofLocalExitRoot [32]common.Hash
ProofRollupExitRoot [32]common.Hash
ProofLocalExitRoot tree.Proof
ProofRollupExitRoot tree.Proof
GlobalIndex *big.Int
MainnetExitRoot common.Hash
RollupExitRoot common.Hash
Expand Down Expand Up @@ -131,7 +132,7 @@ func (c *ClaimSponsor) Start(ctx context.Context) {
if err2 != nil {
err = err2
tx.Rollback()
if errors.Is(err, ErrNotFound) {
if errors.Is(err, db.ErrNotFound) {
c.logger.Debugf("queue is empty")
err = nil
time.Sleep(c.waitOnEmptyQueue)
Expand Down Expand Up @@ -242,7 +243,7 @@ func (c *ClaimSponsor) AddClaimToQueue(ctx context.Context, claim *Claim) error
}

_, err = getClaim(tx, claim.GlobalIndex)
if !errors.Is(err, ErrNotFound) {
if !errors.Is(err, db.ErrNotFound) {
if err != nil {
tx.Rollback()

Expand All @@ -264,7 +265,7 @@ func (c *ClaimSponsor) AddClaimToQueue(ctx context.Context, claim *Claim) error
var queuePosition uint64
lastQueuePosition, _, err := getLastQueueIndex(tx)
switch {
case errors.Is(err, ErrNotFound):
case errors.Is(err, db.ErrNotFound):
queuePosition = 0

case err != nil:
Expand Down Expand Up @@ -307,7 +308,7 @@ func (c *ClaimSponsor) getClaimByQueueIndex(ctx context.Context, queueIndex uint
return nil, err
}
if globalIndexBytes == nil {
return nil, ErrNotFound
return nil, db.ErrNotFound
}

return getClaim(tx, new(big.Int).SetBytes(globalIndexBytes))
Expand Down Expand Up @@ -345,7 +346,7 @@ func getIndex(iter iter.KV) (uint64, *big.Int, error) {
return 0, nil, err
}
if k == nil {
return 0, nil, ErrNotFound
return 0, nil, db.ErrNotFound
}
globalIndex := new(big.Int).SetBytes(v)

Expand All @@ -368,7 +369,7 @@ func getClaim(tx kv.Tx, globalIndex *big.Int) (*Claim, error) {
return nil, err
}
if claimBytes == nil {
return nil, ErrNotFound
return nil, db.ErrNotFound
}
claim := &Claim{}
err = json.Unmarshal(claimBytes, claim)
Expand Down
36 changes: 0 additions & 36 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/0xPolygon/cdk/etherman"
ethermanconfig "github.com/0xPolygon/cdk/etherman/config"
"github.com/0xPolygon/cdk/etherman/contracts"
"github.com/0xPolygon/cdk/l1bridge2infoindexsync"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/lastgersync"
"github.com/0xPolygon/cdk/log"
Expand Down Expand Up @@ -81,10 +80,6 @@ func start(cliCtx *cli.Context) error {
claimSponsor := runClaimSponsorIfNeeded(cliCtx.Context, components, l2Client, c.ClaimSponsor)
l1BridgeSync := runBridgeSyncL1IfNeeded(cliCtx.Context, components, c.BridgeL1Sync, reorgDetectorL1, l1Client)
l2BridgeSync := runBridgeSyncL2IfNeeded(cliCtx.Context, components, c.BridgeL2Sync, reorgDetectorL2, l2Client)
l1Bridge2InfoIndexSync := runL1Bridge2InfoIndexSyncIfNeeded(
cliCtx.Context, components, c.L1Bridge2InfoIndexSync,
l1BridgeSync, l1InfoTreeSync, l1Client,
)
lastGERSync := runLastGERSyncIfNeeded(
cliCtx.Context, components, c.LastGERSync, reorgDetectorL2, l2Client, l1InfoTreeSync,
)
Expand Down Expand Up @@ -115,7 +110,6 @@ func start(cliCtx *cli.Context) error {
c.Common.NetworkID,
claimSponsor,
l1InfoTreeSync,
l1Bridge2InfoIndexSync,
lastGERSync,
l1BridgeSync,
l2BridgeSync,
Expand Down Expand Up @@ -623,34 +617,6 @@ func runClaimSponsorIfNeeded(
return cs
}

func runL1Bridge2InfoIndexSyncIfNeeded(
ctx context.Context,
components []string,
cfg l1bridge2infoindexsync.Config,
l1BridgeSync *bridgesync.BridgeSync,
l1InfoTreeSync *l1infotreesync.L1InfoTreeSync,
l1Client *ethclient.Client,
) *l1bridge2infoindexsync.L1Bridge2InfoIndexSync {
if !isNeeded([]string{cdkcommon.RPC}, components) {
return nil
}
l1Bridge2InfoIndexSync, err := l1bridge2infoindexsync.New(
cfg.DBPath,
l1BridgeSync,
l1InfoTreeSync,
l1Client,
cfg.RetryAfterErrorPeriod.Duration,
cfg.MaxRetryAttemptsAfterError,
cfg.WaitForSyncersPeriod.Duration,
)
if err != nil {
log.Fatalf("error creating l1Bridge2InfoIndexSync: %s", err)
}
go l1Bridge2InfoIndexSync.Start(ctx)

return l1Bridge2InfoIndexSync
}

func runLastGERSyncIfNeeded(
ctx context.Context,
components []string,
Expand Down Expand Up @@ -751,7 +717,6 @@ func createRPC(
cdkNetworkID uint32,
sponsor *claimsponsor.ClaimSponsor,
l1InfoTree *l1infotreesync.L1InfoTreeSync,
l1Bridge2Index *l1bridge2infoindexsync.L1Bridge2InfoIndexSync,
injectedGERs *lastgersync.LastGERSync,
bridgeL1 *bridgesync.BridgeSync,
bridgeL2 *bridgesync.BridgeSync,
Expand All @@ -767,7 +732,6 @@ func createRPC(
cdkNetworkID,
sponsor,
l1InfoTree,
l1Bridge2Index,
injectedGERs,
bridgeL1,
bridgeL2,
Expand Down
73 changes: 61 additions & 12 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"bytes"
"errors"
"fmt"
"path/filepath"
"strings"

Expand All @@ -14,7 +13,6 @@ import (
"github.com/0xPolygon/cdk/claimsponsor"
"github.com/0xPolygon/cdk/common"
ethermanconfig "github.com/0xPolygon/cdk/etherman/config"
"github.com/0xPolygon/cdk/l1bridge2infoindexsync"
"github.com/0xPolygon/cdk/l1infotreesync"
"github.com/0xPolygon/cdk/lastgersync"
"github.com/0xPolygon/cdk/log"
Expand Down Expand Up @@ -52,6 +50,22 @@ const (
FlagOutputFile = "output"
// FlagMaxAmount is the flag to avoid to use the flag FlagAmount
FlagMaxAmount = "max-amount"

deprecatedFieldSyncDB = "Aggregator.Synchronizer.DB is deprecated use Aggregator.Synchronizer.SQLDB instead"
)

type ForbiddenField struct {
FieldName string
Reason string
}

var (
forbiddenFieldsOnConfig = []ForbiddenField{
{
FieldName: "aggregator.synchronizer.db.",
Reason: deprecatedFieldSyncDB,
},
}
)

/*
Expand Down Expand Up @@ -96,10 +110,6 @@ type Config struct {
// ClaimSponsor is the config for the claim sponsor
ClaimSponsor claimsponsor.EVMClaimSponsorConfig

// L1Bridge2InfoIndexSync is the config for the synchronizers that maintains the relation of
// bridge from L1 --> L1 Info tree index. Needed for the bridge service (RPC)
L1Bridge2InfoIndexSync l1bridge2infoindexsync.Config

// BridgeL1Sync is the configuration for the synchronizer of the bridge of the L1
BridgeL1Sync bridgesync.Config

Expand Down Expand Up @@ -128,15 +138,18 @@ func Default() (*Config, error) {

return &cfg, nil
}
func Load(ctx *cli.Context) (*Config, error) {
configFilePath := ctx.String(FlagCfg)
return LoadFile(configFilePath)
}

// Load loads the configuration
func Load(ctx *cli.Context) (*Config, error) {
func LoadFile(configFilePath string) (*Config, error) {
cfg, err := Default()
if err != nil {
return nil, err
}

configFilePath := ctx.String(FlagCfg)
expectedKeys := viper.AllKeys()
if configFilePath != "" {
dirName, fileName := filepath.Split(configFilePath)

Expand All @@ -160,7 +173,6 @@ func Load(ctx *cli.Context) (*Config, error) {
log.Error("config file not found")
} else {
log.Errorf("error reading config file: ", err)

return nil, err
}
}
Expand All @@ -179,8 +191,45 @@ func Load(ctx *cli.Context) (*Config, error) {
if err != nil {
return nil, err
}
if expectedKeys != nil {
configKeys := viper.AllKeys()
unexpectedFields := getUnexpectedFields(configKeys, expectedKeys)
for _, field := range unexpectedFields {
forbbidenInfo := getForbiddenField(field)
if forbbidenInfo != nil {
log.Warnf("forbidden field %s in config file: %s", field, forbbidenInfo.Reason)
} else {
log.Debugf("field %s in config file doesnt have a default value", field)
}
}
}
return cfg, nil
}

func getForbiddenField(fieldName string) *ForbiddenField {
for _, forbiddenField := range forbiddenFieldsOnConfig {
if forbiddenField.FieldName == fieldName || strings.HasPrefix(fieldName, forbiddenField.FieldName) {
return &forbiddenField
}
}
return nil
}

fmt.Println("cfg", cfg.NetworkConfig.L1Config)
func getUnexpectedFields(keysOnFile, expectedConfigKeys []string) []string {
wrongFields := make([]string, 0)
for _, key := range keysOnFile {
if !contains(expectedConfigKeys, key) {
wrongFields = append(wrongFields, key)
}
}
return wrongFields
}

return cfg, nil
func contains(keys []string, key string) bool {
for _, k := range keys {
if k == key {
return true
}
}
return false
}
Loading

0 comments on commit 70181e5

Please sign in to comment.