Skip to content

Commit

Permalink
initialize the start block for the L1 synchronizer task
Browse files Browse the repository at this point in the history
  • Loading branch information
christophercampbell committed Oct 23, 2023
1 parent a06b523 commit a03037d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func start(cliCtx *cli.Context) error {
// derive address
selfAddr := crypto.PubkeyToAddress(pk.PublicKey)

// ensure synchro/reorg start block is set
err = synchronizer.InitStartBlock(storage, c.L1)
if err != nil {
log.Fatal(err)
}

var cancelFuncs []context.CancelFunc

sequencerTracker, err := synchronizer.NewSequencerTracker(c.L1, etherman)
Expand Down
75 changes: 75 additions & 0 deletions synchronizer/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package synchronizer

import (
"context"
"math/big"
"time"

"github.com/0xPolygon/cdk-data-availability/config"
"github.com/0xPolygon/cdk-data-availability/db"
"github.com/0xPolygon/cdk-data-availability/log"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)

// InitStartBlock initializes the L1 sync task by finding the inception block for the CDKValidium contract
func InitStartBlock(db *db.DB, l1 config.L1Config) error {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)

Check failure on line 17 in synchronizer/init.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 15, in <argument> detected (gomnd)
defer cancel()

current, err := getStartBlock(db)
if err != nil {
return err
}
if current > 0 {
// no need to resolve start block, it's already been set
return nil
}
log.Info("starting search for start block of contract", l1.CDKValidiumAddress)
startBlock, err := findContractDeploymentBlock(ctx, l1.RpcURL, common.HexToAddress(l1.CDKValidiumAddress))
if err != nil {
return err
}
err = setStartBlock(db, startBlock.Uint64())
if err != nil {
return err
}
return nil
}

func findContractDeploymentBlock(ctx context.Context, url string, contract common.Address) (*big.Int, error) {
eth, err := ethclient.DialContext(ctx, url)
if err != nil {
return nil, err
}
latestBlock, err := eth.BlockByNumber(ctx, nil)
if err != nil {
return nil, err
}
b, err := findCode(ctx, eth, contract, 0, latestBlock.Number().Int64())
if err != nil {
return nil, err
}
return big.NewInt(b), nil
}

// findCode is an O(log(n)) search for the inception block of a contract at the given address
func findCode(ctx context.Context, eth *ethclient.Client, address common.Address, startBlock, endBlock int64) (int64, error) {
if startBlock == endBlock {
return startBlock, nil
}
midBlock := (startBlock + endBlock) / 2

Check failure on line 61 in synchronizer/init.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 2, in <operation> detected (gomnd)
if codeLen := codeLen(ctx, eth, address, midBlock); codeLen > 2 {

Check failure on line 62 in synchronizer/init.go

View workflow job for this annotation

GitHub Actions / lint

mnd: Magic number: 2, in <condition> detected (gomnd)
return findCode(ctx, eth, address, startBlock, midBlock)
} else {
return findCode(ctx, eth, address, midBlock+1, endBlock)
}
}

func codeLen(ctx context.Context, eth *ethclient.Client, address common.Address, blockNumber int64) int64 {
data, err := eth.CodeAt(ctx, address, big.NewInt(blockNumber))
if err != nil {
return 0
}
return int64(len(data))
}
9 changes: 9 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ run-network: ## Runs the l1 network
stop-network: ## Stops the l1 network
docker compose stop l1 && docker compose rm -f l1

.PHONY: run-local
run-local: run-network ## Run network and db for local instance
docker compose up -d cdk-data-availability-db

.PHONY: stop-local
stop-local: stop-network ## Run network and db for local instance
docker compose stop cdk-data-availability-db && docker compose rm -f cdk-data-availability-db


0 comments on commit a03037d

Please sign in to comment.