Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NIT-2484] snap-sync consensus 0.2: start reading parent chain from the right block #2296

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
84 changes: 73 additions & 11 deletions arbnode/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,21 @@ type Node struct {
}

type SnapSyncConfig struct {
Enabled bool
PrevBatchMessageCount uint64
PrevDelayedRead uint64
BatchCount uint64
DelayedCount uint64
Enabled bool
PrevBatchMessageCount uint64
PrevDelayedRead uint64
BatchCount uint64
DelayedCount uint64
ParentChainAssertionBlock uint64
}

var DefaultSnapSyncConfig = SnapSyncConfig{
Enabled: false,
PrevBatchMessageCount: 0,
BatchCount: 0,
DelayedCount: 0,
PrevDelayedRead: 0,
Enabled: false,
PrevBatchMessageCount: 0,
PrevDelayedRead: 0,
BatchCount: 0,
DelayedCount: 0,
ParentChainAssertionBlock: 0,
}

type ConfigFetcher interface {
Expand Down Expand Up @@ -589,7 +591,22 @@ func createNodeImpl(
if err != nil {
return nil, err
}
inboxReader, err := NewInboxReader(inboxTracker, l1client, l1Reader, new(big.Int).SetUint64(deployInfo.DeployedAt), delayedBridge, sequencerInbox, func() *InboxReaderConfig { return &configFetcher.Get().InboxReader })
firstMessageBlock := new(big.Int).SetUint64(deployInfo.DeployedAt)
if config.SnapSyncTest.Enabled {
batchCount := config.SnapSyncTest.BatchCount
// Find the first block containing the batch count.
// Subtract 1 to get the block before the needed batch count,
// this is done to fetch previous batch metadata needed for snap sync.
if batchCount > 0 {
batchCount--
}
block, err := FindBlockContainingBatchCount(ctx, deployInfo.Bridge, l1client, config.SnapSyncTest.ParentChainAssertionBlock, batchCount)
if err != nil {
return nil, err
}
firstMessageBlock.SetUint64(block)
}
inboxReader, err := NewInboxReader(inboxTracker, l1client, l1Reader, firstMessageBlock, delayedBridge, sequencerInbox, func() *InboxReaderConfig { return &configFetcher.Get().InboxReader })
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -770,6 +787,51 @@ func createNodeImpl(
}, nil
}

func FindBlockContainingBatchCount(ctx context.Context, bridgeAddress common.Address, l1Client *ethclient.Client, parentChainAssertionBlock uint64, batchCount uint64) (uint64, error) {
bridge, err := bridgegen.NewIBridge(bridgeAddress, l1Client)
if err != nil {
return 0, err
}
high := parentChainAssertionBlock
low := uint64(0)
if high > 100 {
low = high - 100
}
// Reduce high and low by 100 until lowNode.InboxMaxCount < batchCount
// This will give us a range (low to high) of blocks that contain the batch count.
for low > 0 {
lowCount, err := bridge.SequencerMessageCount(&bind.CallOpts{Context: ctx, BlockNumber: new(big.Int).SetUint64(low)})
if err != nil {
return 0, err
}
if lowCount.Uint64() > batchCount {
high = low
if low > 100 {
low = low - 100
} else {
low = 0
}
} else {
break
}
}
// Then binary search between low and high to find the block containing the batch count.
for low < high {
mid := low + (high-low)/2

midCount, err := bridge.SequencerMessageCount(&bind.CallOpts{Context: ctx, BlockNumber: new(big.Int).SetUint64(mid)})
if err != nil {
return 0, err
}
if midCount.Uint64() < batchCount {
low = mid + 1
} else {
high = mid
}
}
return low, nil
}

func (n *Node) OnConfigReload(_ *Config, _ *Config) error {
// TODO
return nil
Expand Down
Loading