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

Sync Potentially Completed Challenges With a Max Lookback for Refund Purposes #705

Merged
merged 17 commits into from
Nov 26, 2024
22 changes: 8 additions & 14 deletions challenge-manager/chain-watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Watcher struct {
// Only track challenges for these parent assertion hashes.
// Track all if empty / nil.
trackChallengeParentAssertionHashes []protocol.AssertionHash
maxLookbackBlocks uint64
}

// New initializes a watcher service for frequently scanning the chain
Expand All @@ -99,6 +100,7 @@ func New(
assertionConfirmingInterval time.Duration,
averageTimeForBlockCreation time.Duration,
trackChallengeParentAssertionHashes []protocol.AssertionHash,
maxLookbackBlocks uint64,
) (*Watcher, error) {
return &Watcher{
chain: chain,
Expand All @@ -114,6 +116,7 @@ func New(
averageTimeForBlockCreation: averageTimeForBlockCreation,
evilEdgesByLevel: threadsafe.NewMap(threadsafe.MapWithMetric[protocol.ChallengeLevel, *threadsafe.Set[protocol.EdgeId]]("evilEdgesByLevel")),
trackChallengeParentAssertionHashes: trackChallengeParentAssertionHashes,
maxLookbackBlocks: maxLookbackBlocks,
}, nil
}

Expand Down Expand Up @@ -580,19 +583,8 @@ func (w *Watcher) AddEdge(ctx context.Context, edge protocol.SpecEdge) (bool, er
if err != nil {
return false, err
}
challengeComplete, err := w.chain.IsChallengeComplete(ctx, challengeParentAssertionHash)
if err != nil {
return false, errors.Wrapf(
err,
"could not check if edge with parent assertion hash %#x is part of a completed challenge",
challengeParentAssertionHash.Hash,
)
}
start, startRoot := edge.StartCommitment()
end, endRoot := edge.EndCommitment()
if challengeComplete {
return false, nil
}
chal, ok := w.challenges.TryGet(challengeParentAssertionHash)
if !ok {
tree := challengetree.New(
Expand Down Expand Up @@ -943,12 +935,14 @@ type filterRange struct {
// Gets the start and end block numbers for our filter queries, starting from
// the latest confirmed assertion's block number up to the latest block number.
func (w *Watcher) getStartEndBlockNum(ctx context.Context) (filterRange, error) {
latestConfirmed, err := w.chain.LatestConfirmed(ctx, w.chain.GetCallOptsWithDesiredRpcHeadBlockNumber(&bind.CallOpts{Context: ctx}))
latestBlock, err := w.chain.Backend().HeaderU64(ctx)
if err != nil {
return filterRange{}, err
}
firstBlock := latestConfirmed.CreatedAtBlock()
startBlock := firstBlock
startBlock := latestBlock
if w.maxLookbackBlocks < startBlock {
startBlock = startBlock - w.maxLookbackBlocks
}
headerNumber, err := w.backend.HeaderU64(ctx)
if err != nil {
return filterRange{}, err
Expand Down
2 changes: 2 additions & 0 deletions challenge-manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func setupEdgeTrackersForBisection(
confInterval,
avgBlockTime,
nil,
100,
)
require.NoError(t, err)
honestWatcher.SetEdgeManager(honestValidator)
Expand Down Expand Up @@ -218,6 +219,7 @@ func setupEdgeTrackersForBisection(
confInterval,
avgBlockTime,
nil,
100,
)
require.NoError(t, err)
evilWatcher.SetEdgeManager(evilValidator)
Expand Down
27 changes: 27 additions & 0 deletions challenge-manager/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package challengemanager
import (
"time"

"github.com/ccoveille/go-safecast"

"github.com/ethereum/go-ethereum/common"

"github.com/offchainlabs/bold/api/backend"
Expand All @@ -32,6 +34,7 @@ type stackParams struct {
headerProvider HeaderProvider
enableFastConfirmation bool
assertionManagerOverride *assertions.Manager
maxLookbackBlocks uint64
}

var defaultStackParams = stackParams{
Expand All @@ -47,6 +50,7 @@ var defaultStackParams = stackParams{
headerProvider: nil,
enableFastConfirmation: false,
assertionManagerOverride: nil,
maxLookbackBlocks: blocksPerInterval(time.Second*12, 21*24*time.Hour), // Default to 3 weeks worth of blocks.
}

// StackOpt is a functional option to configure the stack.
Expand Down Expand Up @@ -130,6 +134,19 @@ func StackWithFastConfirmationEnabled() StackOpt {
}
}

// StackWithSyncMaxLookbackBlocks specifies the number of blocks behind the latest block
// to start syncing the chain watcher from.
func StackWithSyncMaxLookbackBlocks(maxLookback uint64) StackOpt {
return func(p *stackParams) {
p.maxLookbackBlocks = maxLookback
}
}
func StackWithM() StackOpt {
return func(p *stackParams) {
p.enableFastConfirmation = true
}
}

// OverrideAssertionManger can be used in tests to override the assertion
// manager.
func OverrideAssertionManager(asm *assertions.Manager) StackOpt {
Expand Down Expand Up @@ -170,6 +187,7 @@ func NewChallengeStack(
params.confInterval,
params.avgBlockTime,
params.trackChallengeParentAssertionHashes,
params.maxLookbackBlocks,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -228,3 +246,12 @@ func NewChallengeStack(
}
return New(chain, provider, watcher, asm, cmOpts...)
}

func blocksPerInterval(avgBlockTime time.Duration, interval time.Duration) uint64 {
rauljordan marked this conversation as resolved.
Show resolved Hide resolved
// Calculate the number of blocks as an integer division
res, err := safecast.ToUint64(interval / avgBlockTime)
if err != nil {
panic(err)
}
return res
}
3 changes: 3 additions & 0 deletions testing/endtoend/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_test(
name = "endtoend_test",
size = "large",
srcs = [
"e2e_crash_test.go",
"e2e_test.go",
"helpers_test.go",
],
Expand All @@ -26,7 +27,9 @@ go_test(
"//chain-abstraction/sol-implementation",
"//challenge-manager",
"//challenge-manager/types",
"//runtime",
"//solgen/go/bridgegen",
"//solgen/go/challengeV2gen",
"//solgen/go/mocksgen",
"//solgen/go/rollupgen",
"//testing",
Expand Down
Loading
Loading