Skip to content

Commit

Permalink
Fix/l1info (#44)
Browse files Browse the repository at this point in the history
* wip

* wip

* fix sequence sender and l1 info tree sync

* wip

* wip

* TT

* minor improvements

* add context to NewSequence

* fix UTs

* use main versions for contracts and dac deps

* apply PR requests by joanestebanr

* ignore .vscode

* rename index to counter for l1 info
  • Loading branch information
arnaubennassar authored Aug 20, 2024
1 parent 480d314 commit a46b419
Show file tree
Hide file tree
Showing 46 changed files with 735 additions and 225 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ target/
book/
index.html
tmp
.vscode
42 changes: 0 additions & 42 deletions .vscode/launch.json

This file was deleted.

16 changes: 9 additions & 7 deletions aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,14 @@ func (a *Aggregator) handleReceivedDataStream(entry *datastreamer.FileEntry, cli
a.currentStreamBatch.L1InfoRoot = a.currentStreamBatch.GlobalExitRoot
}

accInputHash, err := cdkcommon.CalculateAccInputHash(oldDBBatch.Batch.AccInputHash, a.currentStreamBatch.BatchL2Data, a.currentStreamBatch.L1InfoRoot, uint64(a.currentStreamBatch.Timestamp.Unix()), a.currentStreamBatch.Coinbase, forcedBlockhashL1)
if err != nil {
log.Errorf("Error calculating acc input hash: %v", err)
return err
}

accInputHash := cdkcommon.CalculateAccInputHash(
oldDBBatch.Batch.AccInputHash,
a.currentStreamBatch.BatchL2Data,
a.currentStreamBatch.L1InfoRoot,
uint64(a.currentStreamBatch.Timestamp.Unix()),
a.currentStreamBatch.Coinbase,
forcedBlockhashL1,
)
a.currentStreamBatch.AccInputHash = accInputHash

dbBatch := state.DBBatch{
Expand Down Expand Up @@ -409,7 +411,7 @@ func (a *Aggregator) handleReceivedDataStream(entry *datastreamer.FileEntry, cli
}

// Retrieve the witness
if dbBatch.Witness == nil || len(dbBatch.Witness) == 0 {
if len(dbBatch.Witness) == 0 {
a.witnessRetrievalChan <- dbBatch
}
}
Expand Down
135 changes: 118 additions & 17 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/ecdsa"
"fmt"
"math/big"
"os"
"os/signal"
"runtime"
Expand Down Expand Up @@ -53,12 +54,14 @@ func start(cliCtx *cli.Context) error {
}

components := cliCtx.StringSlice(config.FlagComponents)

l1Client := runL1ClientIfNeeded(components, c.SequenceSender.EthTxManager.Etherman.URL)
reorgDetectorL1 := runReorgDetectorL1IfNeeded(cliCtx.Context, components, l1Client, c.ReorgDetectorL1.DBPath)
l1InfoTreeSync := runL1InfoTreeSyncerIfNeeded(cliCtx.Context, components, *c, l1Client, reorgDetectorL1)
for _, component := range components {
switch component {
case SEQUENCE_SENDER:
c.SequenceSender.Log = c.Log
seqSender := createSequenceSender(*c)
seqSender := createSequenceSender(*c, l1Client, l1InfoTreeSync)
// start sequence sender in a goroutine, checking for errors
go seqSender.Start(cliCtx.Context)

Expand All @@ -71,15 +74,7 @@ func start(cliCtx *cli.Context) error {
}
}()
case AGGORACLE:
l1Client, err := ethclient.Dial(c.AggOracle.URLRPCL1)
if err != nil {
log.Fatal(err)
}
reorgDetector := newReorgDetectorL1(cliCtx.Context, *c, l1Client)
go reorgDetector.Start(cliCtx.Context)
syncer := newL1InfoTreeSyncer(cliCtx.Context, *c, l1Client, reorgDetector)
go syncer.Start(cliCtx.Context)
aggOracle := createAggoracle(*c, l1Client, syncer)
aggOracle := createAggoracle(*c, l1Client, l1InfoTreeSync)
go aggOracle.Start(cliCtx.Context)
}
}
Expand Down Expand Up @@ -130,7 +125,11 @@ func createAggregator(ctx context.Context, c config.Config, runMigrations bool)
return aggregator
}

func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender {
func createSequenceSender(
cfg config.Config,
l1Client *ethclient.Client,
l1InfoTreeSync *l1infotreesync.L1InfoTreeSync,
) *sequencesender.SequenceSender {
ethman, err := etherman.NewClient(ethermanconfig.Config{
EthermanConfig: ethtxman.Config{
URL: cfg.SequenceSender.EthTxManager.Etherman.URL,
Expand All @@ -152,8 +151,12 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender {
log.Fatal(err)
}
cfg.SequenceSender.SenderAddress = auth.From

txBuilder, err := newTxBuilder(cfg, ethman)
blockFialityType := etherman.BlockNumberFinality(cfg.SequenceSender.BlockFinality)
blockFinality, err := blockFialityType.ToBlockNum()
if err != nil {
log.Fatalf("Failed to create block finality. Err: %w, ", err)
}
txBuilder, err := newTxBuilder(cfg, ethman, l1Client, l1InfoTreeSync, blockFinality)
if err != nil {
log.Fatal(err)
}
Expand All @@ -165,7 +168,13 @@ func createSequenceSender(cfg config.Config) *sequencesender.SequenceSender {
return seqSender
}

func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuilder, error) {
func newTxBuilder(
cfg config.Config,
ethman *etherman.Client,
l1Client *ethclient.Client,
l1InfoTreeSync *l1infotreesync.L1InfoTreeSync,
blockFinality *big.Int,
) (txbuilder.TxBuilder, error) {
auth, _, err := ethman.LoadAuthFromKeyStore(cfg.SequenceSender.PrivateKey.Path, cfg.SequenceSender.PrivateKey.Password)
if err != nil {
log.Fatal(err)
Expand All @@ -179,9 +188,26 @@ func newTxBuilder(cfg config.Config, ethman *etherman.Client) (txbuilder.TxBuild
switch contracts.VersionType(cfg.Common.ContractVersions) {
case contracts.VersionBanana:
if cfg.Common.IsValidiumMode {
txBuilder = txbuilder.NewTxBuilderBananaValidium(ethman.Contracts.Banana.Rollup, ethman.Contracts.Banana.GlobalExitRoot, da, *auth, cfg.SequenceSender.MaxBatchesForL1)
txBuilder = txbuilder.NewTxBuilderBananaValidium(
ethman.Contracts.Banana.Rollup,
ethman.Contracts.Banana.GlobalExitRoot,
da,
*auth,
cfg.SequenceSender.MaxBatchesForL1,
l1InfoTreeSync,
l1Client,
blockFinality,
)
} else {
txBuilder = txbuilder.NewTxBuilderBananaZKEVM(ethman.Contracts.Banana.Rollup, ethman.Contracts.Banana.GlobalExitRoot, *auth, cfg.SequenceSender.MaxTxSizeForL1)
txBuilder = txbuilder.NewTxBuilderBananaZKEVM(
ethman.Contracts.Banana.Rollup,
ethman.Contracts.Banana.GlobalExitRoot,
*auth,
cfg.SequenceSender.MaxTxSizeForL1,
l1InfoTreeSync,
l1Client,
blockFinality,
)
}
case contracts.VersionElderberry:
if cfg.Common.IsValidiumMode {
Expand Down Expand Up @@ -392,3 +418,78 @@ func newL1InfoTreeSyncer(
}
return syncer
}

func isNeeded(casesWhereNeeded, actualCases []string) bool {
for _, actaulCase := range actualCases {
for _, caseWhereNeeded := range casesWhereNeeded {
if actaulCase == caseWhereNeeded {
return true
}
}
}
return false
}

func runL1InfoTreeSyncerIfNeeded(
ctx context.Context,
components []string,
cfg config.Config,
l1Client *ethclient.Client,
reorgDetector *reorgdetector.ReorgDetector,
) *l1infotreesync.L1InfoTreeSync {
if !isNeeded([]string{AGGORACLE, SEQUENCE_SENDER}, components) {
return nil
}
l1InfoTreeSync, err := l1infotreesync.New(
ctx,
cfg.L1InfoTreeSync.DBPath,
cfg.L1InfoTreeSync.GlobalExitRootAddr,
cfg.L1InfoTreeSync.RollupManagerAddr,
cfg.L1InfoTreeSync.SyncBlockChunkSize,
etherman.BlockNumberFinality(cfg.L1InfoTreeSync.BlockFinality),
reorgDetector,
l1Client,
cfg.L1InfoTreeSync.WaitForNewBlocksPeriod.Duration,
cfg.L1InfoTreeSync.InitialBlock,
cfg.L1InfoTreeSync.RetryAfterErrorPeriod.Duration,
cfg.L1InfoTreeSync.MaxRetryAttemptsAfterError,
)
if err != nil {
log.Fatal(err)
}
go l1InfoTreeSync.Start(ctx)
return l1InfoTreeSync
}

func runL1ClientIfNeeded(components []string, urlRPCL1 string) *ethclient.Client {
if !isNeeded([]string{SEQUENCE_SENDER, AGGREGATOR, AGGORACLE}, components) {
return nil
}
log.Debugf("dialing L1 client at: %s", urlRPCL1)
l1CLient, err := ethclient.Dial(urlRPCL1)
if err != nil {
log.Fatal(err)
}
return l1CLient
}

func runReorgDetectorL1IfNeeded(ctx context.Context, components []string, l1Client *ethclient.Client, dbPath string) *reorgdetector.ReorgDetector {
if !isNeeded([]string{SEQUENCE_SENDER, AGGREGATOR, AGGORACLE}, components) {
return nil
}
rd := newReorgDetector(ctx, dbPath, l1Client)
go rd.Start(ctx)
return rd
}

func newReorgDetector(
ctx context.Context,
dbPath string,
client *ethclient.Client,
) *reorgdetector.ReorgDetector {
rd, err := reorgdetector.New(ctx, client, dbPath)
if err != nil {
log.Fatal(err)
}
return rd
}
5 changes: 3 additions & 2 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func CalculateAccInputHash(
l1InfoRoot common.Hash,
timestampLimit uint64,
sequencerAddr common.Address,
forcedBlockhashL1 common.Hash) (common.Hash, error) {
forcedBlockhashL1 common.Hash,
) common.Hash {
v1 := oldAccInputHash.Bytes()
v2 := batchData
v3 := l1InfoRoot.Bytes()
Expand Down Expand Up @@ -74,5 +75,5 @@ func CalculateAccInputHash(
log.Debugf("Sequencer Address: %v", sequencerAddr)
log.Debugf("Forced BlockHashL1: %v", forcedBlockhashL1)

return common.BytesToHash(keccak256.Hash(v1, v2, v3, v4, v5, v6)), nil
return common.BytesToHash(keccak256.Hash(v1, v2, v3, v4, v5, v6))
}
2 changes: 2 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ SequencesTxFileName = "sequencesender.json"
GasOffset = 80000
WaitPeriodPurgeTxFile = "15m"
MaxPendingTx = 1
MaxBatchesForL1 = 300
BlockFinality = "FinalizedBlock"
[SequenceSender.StreamClient]
Server = "127.0.0.1:6900"
[SequenceSender.EthTxManager]
Expand Down
7 changes: 7 additions & 0 deletions dataavailability/datacommittee/datacommittee.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ func (d *Backend) PostSequenceBanana(ctx context.Context, sequence etherman.Sequ
L1InfoRoot: sequence.L1InfoRoot,
MaxSequenceTimestamp: daTypes.ArgUint64(sequence.MaxSequenceTimestamp),
}
hashToSign := common.BytesToHash(sequenceBanana.HashToSign())
if hashToSign != sequence.AccInputHash {
return nil, fmt.Errorf(
"calculated accInputHash diverges: DA = %s vs Seq = %s",
hashToSign, sequence.AccInputHash,
)
}

signature, err := sequenceBanana.Sign(d.privKey)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions etherman/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ type SequenceBanana struct {
AccInputHash common.Hash
L1InfoRoot common.Hash
MaxSequenceTimestamp uint64
IndexL1InfoRoot uint32
CounterL1InfoRoot uint32
L2Coinbase common.Address
LastVirtualBatchNumber uint64
}
Expand All @@ -191,7 +191,7 @@ func NewSequenceBanana(batches []Batch, l2Coinbase common.Address) *SequenceBana
return &SequenceBanana{
Batches: batches,
MaxSequenceTimestamp: maxSequenceTimestamp,
IndexL1InfoRoot: indexL1InfoRoot,
CounterL1InfoRoot: indexL1InfoRoot,
L2Coinbase: l2Coinbase,
}
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/0xPolygon/cdk
go 1.22.4

require (
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240726125827-301fa4c59245
github.com/0xPolygon/cdk-data-availability v0.0.8
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240819092536-5a65d4761b2f
github.com/0xPolygon/cdk-data-availability v0.0.9
github.com/0xPolygon/cdk-rpc v0.0.0-20240419104226-c0a62ba0f49d
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.3
github.com/0xPolygonHermez/zkevm-ethtx-manager v0.1.10-0.20240716105056-c051c96d0234
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240726125827-301fa4c59245 h1:BBmVd50JQID9UyUR3vWFMKr2pMHD3mrqjpuB9DDepBw=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240726125827-301fa4c59245/go.mod h1:mFlcEjsm2YBBsu8atHJ3zyVnwM+Z/fMXpVmIJge+WVU=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240816153044-04551a6747e1 h1:51BkwYm+mGPkI+M1HHPk0L/7jOeo/TAljLWffVFZDVI=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240816153044-04551a6747e1/go.mod h1:mFlcEjsm2YBBsu8atHJ3zyVnwM+Z/fMXpVmIJge+WVU=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240819092536-5a65d4761b2f h1:i9oCNDG4N7ha3fNkEKbito/HF3o4gjnW6//cpTwnp8E=
github.com/0xPolygon/cdk-contracts-tooling v0.0.0-20240819092536-5a65d4761b2f/go.mod h1:mFlcEjsm2YBBsu8atHJ3zyVnwM+Z/fMXpVmIJge+WVU=
github.com/0xPolygon/cdk-data-availability v0.0.8 h1:bMmOYZ7Ei683y80ric3KzMPXtRGmchAmfjIRzghaHb4=
github.com/0xPolygon/cdk-data-availability v0.0.8/go.mod h1:3XkZ0zn0GsvAT01MPQMmukF534CVSFmtrcoK3F/BK6Q=
github.com/0xPolygon/cdk-data-availability v0.0.9-0.20240815124433-d1436f1a29c8 h1:M4dqgOJtUjAJiQ2m+SNH1cTYnBuFsXJJpEJ3eOG/V20=
github.com/0xPolygon/cdk-data-availability v0.0.9-0.20240815124433-d1436f1a29c8/go.mod h1:5A+CU4FGeyG8zTDJc0khMzRxPzFzmpRydeEWmLztgh4=
github.com/0xPolygon/cdk-data-availability v0.0.9-0.20240815125053-230aa34389ca h1:UL6pfroUyI4HXNdZcbvBjfwrrwExoFlbFYUpQvk6u3c=
github.com/0xPolygon/cdk-data-availability v0.0.9-0.20240815125053-230aa34389ca/go.mod h1:5A+CU4FGeyG8zTDJc0khMzRxPzFzmpRydeEWmLztgh4=
github.com/0xPolygon/cdk-data-availability v0.0.9-0.20240815125241-f3d73dfba97a h1:LuPvswDi+86NGwDHITt3kroG1idrQYHLCjeFOTVGtgE=
github.com/0xPolygon/cdk-data-availability v0.0.9-0.20240815125241-f3d73dfba97a/go.mod h1:5A+CU4FGeyG8zTDJc0khMzRxPzFzmpRydeEWmLztgh4=
github.com/0xPolygon/cdk-data-availability v0.0.9 h1:KkP+hJH9nY5fljpSNtW2pfP5YQCJOsSRzfnl0yT78rI=
github.com/0xPolygon/cdk-data-availability v0.0.9/go.mod h1:5A+CU4FGeyG8zTDJc0khMzRxPzFzmpRydeEWmLztgh4=
github.com/0xPolygon/cdk-rpc v0.0.0-20240419104226-c0a62ba0f49d h1:sxh6hZ2jF/sxxj2jd5o1vuNNCZjYmn4aRG9SRlVaEFs=
github.com/0xPolygon/cdk-rpc v0.0.0-20240419104226-c0a62ba0f49d/go.mod h1:2scWqMMufrQXu7TikDgQ3BsyaKoX8qP26D6E262vSOg=
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.3 h1:zJ06KCGLMDOap4slop/QmiMUO+VPsKSS3+944SY06ww=
Expand Down
Loading

0 comments on commit a46b419

Please sign in to comment.