Skip to content

Commit

Permalink
Sync Potentially Completed Challenges With a Max Lookback for Refund …
Browse files Browse the repository at this point in the history
…Purposes (#705)
  • Loading branch information
rauljordan authored Nov 26, 2024
1 parent 9bc9790 commit 7f3b9eb
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 15 deletions.
24 changes: 10 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,16 @@ 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
} else {
startBlock = 0
}
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
22 changes: 22 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 int64
}

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,14 @@ func StackWithFastConfirmationEnabled() StackOpt {
}
}

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

// OverrideAssertionManger can be used in tests to override the assertion
// manager.
func OverrideAssertionManager(asm *assertions.Manager) StackOpt {
Expand Down Expand Up @@ -160,6 +172,10 @@ func NewChallengeStack(
}
provider.UpdateAPIDatabase(apiDB)
}
maxLookbackBlocks, err := safecast.ToUint64(params.maxLookbackBlocks)
if err != nil {
return nil, err
}

// Create the chain watcher.
watcher, err := watcher.New(
Expand All @@ -170,6 +186,7 @@ func NewChallengeStack(
params.confInterval,
params.avgBlockTime,
params.trackChallengeParentAssertionHashes,
maxLookbackBlocks,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -228,3 +245,8 @@ func NewChallengeStack(
}
return New(chain, provider, watcher, asm, cmOpts...)
}

func blocksPerInterval(avgBlockTime time.Duration, interval time.Duration) int64 {
// Calculate the number of blocks as an integer division
return int64(interval / avgBlockTime)
}
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

0 comments on commit 7f3b9eb

Please sign in to comment.