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

Fix waitToPostIfNeeded to regularly check if the target block number is reached #712

Merged
merged 1 commit into from
Dec 6, 2024
Merged
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
44 changes: 16 additions & 28 deletions assertions/poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"time"

"github.com/ccoveille/go-safecast"
"github.com/pkg/errors"

"github.com/ethereum/go-ethereum/log"
Expand Down Expand Up @@ -204,37 +203,26 @@ func (m *Manager) waitToPostIfNeeded(
ctx context.Context,
parentCreationInfo *protocol.AssertionCreatedInfo,
) error {
latestBlockNumber, err := m.backend.HeaderU64(ctx)
if err != nil {
return err
}
blocksSinceLast := uint64(0)
if parentCreationInfo.CreationBlock < latestBlockNumber {
blocksSinceLast = latestBlockNumber - parentCreationInfo.CreationBlock
}
minPeriodBlocks := m.chain.MinAssertionPeriodBlocks()
canPostNow := blocksSinceLast >= minPeriodBlocks

// If we cannot post just yet, we can wait.
if !canPostNow {
var blocksLeftForConfirmation int64
if minPeriodBlocks > blocksSinceLast {
blocksLeftForConfirmation = 0
} else {
blocksLeftForConfirmation, err = safecast.ToInt64(minPeriodBlocks - blocksSinceLast)
if err != nil {
return errors.Wrap(err, "could not convert blocks left for confirmation to int64")
}
for {
latestBlockNumber, err := m.backend.HeaderU64(ctx)
if err != nil {
return err
}
blocksSinceLast := uint64(0)
if parentCreationInfo.CreationBlock < latestBlockNumber {
blocksSinceLast = latestBlockNumber - parentCreationInfo.CreationBlock
}
if blocksSinceLast >= minPeriodBlocks {
return nil
}
timeToWait := m.times.avgBlockTime * time.Duration(blocksLeftForConfirmation)
// If we cannot post just yet, we can wait.
log.Info(
fmt.Sprintf(
"Need to wait %d blocks before posting next assertion, waiting for %v",
blocksLeftForConfirmation,
timeToWait,
fmt.Sprintf("Need to wait %d blocks before posting next assertion. Current block number: %d",
minPeriodBlocks-blocksSinceLast,
latestBlockNumber,
),
)
<-time.After(timeToWait)
<-time.After(m.times.avgBlockTime)
}
return nil
}
Loading