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

l2geth: optimize loops #1027

Merged
merged 4 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-numbers-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@eth-optimism/l2geth': patch
---

Optimize main polling loops
13 changes: 7 additions & 6 deletions l2geth/rollup/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ func NewSyncService(ctx context.Context, cfg Config, txpool *core.TxPool, bc *co
}

// Wait until the remote service is done syncing
for {
t := time.NewTicker(10 * time.Second)
for ; true; <-t.C {
Copy link
Contributor

@smartcontracts smartcontracts Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility that the ticker goes off while the system is in the middle of syncing? Do we get two threads simultaneously trying to sync? If so, is there a potential problem with both threads trying to sync at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a possibility that the ticker goes off while the system is in the middle of syncing?

If the processing that occurs after the tick takes longer than the tick time, then the next tick will be dropped. If the processing takes less time then the code will block on <-t.C until the next tick, see: https://golang.org/src/time/tick.go

	// Give the channel a 1-element time buffer.
	// If the client falls behind while reading, we drop ticks
	// on the floor until the client catches up.

Do we get two threads simultaneously trying to sync? If so, is there a potential problem with both threads trying to sync at the same time?

No, this code is not creating new goroutines and is not used concurrently

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of the for ; true; <-.t.C syntax over for range t.C is that with the second syntax it will not run the first iteration of the loop until after the first tick. We currently run with a 10 second or so tick which is too long to wait for on boot

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the context. LGTM.

status, err := service.client.SyncStatus(service.backend)
if err != nil {
log.Error("Cannot get sync status")
continue
}
if !status.Syncing {
t.Stop()
break
}
log.Info("Still syncing", "index", status.CurrentTransactionIndex, "tip", status.HighestKnownTransactionIndex)
time.Sleep(10 * time.Second)
}

// Initialize the latest L1 data here to make sure that
Expand Down Expand Up @@ -320,7 +321,8 @@ func (s *SyncService) Stop() error {
// VerifierLoop is the main loop for Verifier mode
func (s *SyncService) VerifierLoop() {
log.Info("Starting Verifier Loop", "poll-interval", s.pollInterval, "timestamp-refresh-threshold", s.timestampRefreshThreshold)
for {
t := time.NewTicker(s.pollInterval)
for ; true; <-t.C {
if err := s.updateL1GasPrice(); err != nil {
log.Error("Cannot update L1 gas price", "msg", err)
}
Expand All @@ -330,7 +332,6 @@ func (s *SyncService) VerifierLoop() {
if err := s.updateL2GasPrice(nil); err != nil {
log.Error("Cannot update L2 gas price", "msg", err)
}
time.Sleep(s.pollInterval)
}
}

Expand All @@ -354,7 +355,8 @@ func (s *SyncService) verify() error {
// transactions and then updates the EthContext.
func (s *SyncService) SequencerLoop() {
log.Info("Starting Sequencer Loop", "poll-interval", s.pollInterval, "timestamp-refresh-threshold", s.timestampRefreshThreshold)
for {
t := time.NewTicker(s.pollInterval)
for ; true; <-t.C {
if err := s.updateL1GasPrice(); err != nil {
log.Error("Cannot update L1 gas price", "msg", err)
}
Expand All @@ -370,7 +372,6 @@ func (s *SyncService) SequencerLoop() {
if err := s.updateContext(); err != nil {
log.Error("Could not update execution context", "error", err)
}
time.Sleep(s.pollInterval)
}
}

Expand Down