Skip to content

Commit

Permalink
Base the size of caches on chain configuration (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
eljobe authored Nov 26, 2024
1 parent 0efd78d commit c782ebf
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
9 changes: 7 additions & 2 deletions assertions/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"
"time"

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

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -174,6 +175,10 @@ func NewManager(
mode types.Mode,
opts ...Opt,
) (*Manager, error) {
maxAssertions, err := safecast.ToInt(chain.MaxAssertionsPerChallengePeriod())
if err != nil {
return nil, errors.Wrap(err, "could not convert max assertions to int")
}
m := &Manager{
chain: chain,
apiDB: nil,
Expand All @@ -184,12 +189,12 @@ func NewManager(
times: defaultTimings,
forksDetectedCount: 0,
assertionsProcessedCount: 0,
submittedAssertions: threadsafe.NewLruSet(1500, threadsafe.LruSetWithMetric[protocol.AssertionHash]("submittedAssertions")),
submittedAssertions: threadsafe.NewLruSet(maxAssertions, threadsafe.LruSetWithMetric[protocol.AssertionHash]("submittedAssertions")),
assertionChainData: &assertionChainData{
latestAgreedAssertion: protocol.AssertionHash{},
canonicalAssertions: make(map[protocol.AssertionHash]*protocol.AssertionCreatedInfo),
},
observedCanonicalAssertions: make(chan protocol.AssertionHash, 1000),
observedCanonicalAssertions: make(chan protocol.AssertionHash, maxAssertions),
isReadyToPost: false,
startPostingSignal: make(chan struct{}),
mode: mode,
Expand Down
5 changes: 1 addition & 4 deletions assertions/poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,7 @@ func (m *Manager) waitToPostIfNeeded(
if parentCreationInfo.CreationBlock < latestBlockNumber {
blocksSinceLast = latestBlockNumber - parentCreationInfo.CreationBlock
}
minPeriodBlocks, err := m.chain.MinAssertionPeriodBlocks(ctx)
if err != nil {
return err
}
minPeriodBlocks := m.chain.MinAssertionPeriodBlocks()
canPostNow := blocksSinceLast >= minPeriodBlocks

// If we cannot post just yet, we can wait.
Expand Down
6 changes: 5 additions & 1 deletion chain-abstraction/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ type AssertionChain interface {
) (*AssertionCreatedInfo, error)
GetCallOptsWithDesiredRpcHeadBlockNumber(opts *bind.CallOpts) *bind.CallOpts

MinAssertionPeriodBlocks(ctx context.Context) (uint64, error)
MinAssertionPeriodBlocks() uint64
AssertionUnrivaledBlocks(ctx context.Context, assertionHash AssertionHash) (uint64, error)
TopLevelAssertion(ctx context.Context, edgeId EdgeId) (AssertionHash, error)
TopLevelClaimHeights(ctx context.Context, edgeId EdgeId) (OriginHeights, error)
Expand Down Expand Up @@ -190,6 +190,10 @@ type AssertionChain interface {

// Spec-based implementation methods.
SpecChallengeManager() SpecChallengeManager

// MaxAssertionsPerChallenge period returns maximum number of assertions that
// may need to be processed during a challenge period of blocks.
MaxAssertionsPerChallengePeriod() uint64
}

// InheritedTimer for an edge from its children or claiming edges.
Expand Down
31 changes: 22 additions & 9 deletions chain-abstraction/sol-implementation/assertion_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ type AssertionChain struct {
confirmedChallengesByParentAssertionHash *threadsafe.LruSet[protocol.AssertionHash]
specChallengeManager protocol.SpecChallengeManager
averageTimeForBlockCreation time.Duration
minAssertionPeriodBlocks uint64
transactor Transactor
fastConfirmSafeAddress common.Address
fastConfirmSafe *contractsgen.Safe
Expand Down Expand Up @@ -196,6 +197,18 @@ func NewAssertionChain(
return nil, err
}
chain.rollup = coreBinding
minPeriod, err := chain.rollup.MinimumAssertionPeriod(chain.GetCallOptsWithDesiredRpcHeadBlockNumber(&bind.CallOpts{Context: ctx}))
if err != nil {
return nil, err
}
if !minPeriod.IsUint64() {
return nil, errors.New("minimum assertion period was not a uint64")
}
if minPeriod.Uint64() == 0 {
minPeriod = big.NewInt(1)
}
log.Info("Minimum assertion period", "blocks", minPeriod.Uint64())
chain.minAssertionPeriodBlocks = minPeriod.Uint64()
chain.userLogic = assertionChainBinding
specChallengeManager, err := NewSpecChallengeManager(
ctx,
Expand Down Expand Up @@ -473,15 +486,15 @@ func (a *AssertionChain) GenesisAssertionHash(ctx context.Context) (common.Hash,
return a.userLogic.GenesisAssertionHash(a.GetCallOptsWithDesiredRpcHeadBlockNumber(&bind.CallOpts{Context: ctx}))
}

func (a *AssertionChain) MinAssertionPeriodBlocks(ctx context.Context) (uint64, error) {
minPeriod, err := a.rollup.MinimumAssertionPeriod(a.GetCallOptsWithDesiredRpcHeadBlockNumber(&bind.CallOpts{Context: ctx}))
if err != nil {
return 0, err
}
if !minPeriod.IsUint64() {
return 0, errors.New("minimum assertion period was not a uint64")
}
return minPeriod.Uint64(), nil
func (a *AssertionChain) MinAssertionPeriodBlocks() uint64 {
return a.minAssertionPeriodBlocks
}

// MaxAssertionsPerChallenge period returns maximum number of assertions that
// may need to be processed during a challenge period of blocks.
func (a *AssertionChain) MaxAssertionsPerChallengePeriod() uint64 {
cb := a.SpecChallengeManager().ChallengePeriodBlocks()
return cb / a.minAssertionPeriodBlocks
}

func TryConfirmingAssertion(
Expand Down
1 change: 1 addition & 0 deletions challenge-manager/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ go_library(
"//runtime",
"//time",
"//util/stopwaiter",
"@com_github_ccoveille_go_safecast//:go-safecast",
"@com_github_ethereum_go_ethereum//accounts/abi/bind",
"@com_github_ethereum_go_ethereum//common",
"@com_github_ethereum_go_ethereum//core/types",
Expand Down
9 changes: 7 additions & 2 deletions challenge-manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"time"

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

"github.com/ethereum/go-ethereum/accounts/abi/bind"
Expand Down Expand Up @@ -120,17 +121,21 @@ func New(
assertionManager AssertionManager,
opts ...Opt,
) (*Manager, error) {
maxAssertions, err := safecast.ToInt(chain.MaxAssertionsPerChallengePeriod())
if err != nil {
return nil, errors.Wrap(err, "could not convert max assertions to int")
}
m := &Manager{
chain: chain,
stateManager: stateManager,
assertionManager: assertionManager,
watcher: watcher,
timeRef: utilTime.NewRealTimeReference(),
trackedEdgeIds: threadsafe.NewMap(threadsafe.MapWithMetric[protocol.EdgeId, *edgetracker.Tracker]("trackedEdgeIds")),
assertionMetadataCache: threadsafe.NewLruMap(1500, threadsafe.LruMapWithMetric[protocol.AssertionHash, l2stateprovider.AssociatedAssertionMetadata]("batchIndexForAssertionCache")),
assertionMetadataCache: threadsafe.NewLruMap(maxAssertions, threadsafe.LruMapWithMetric[protocol.AssertionHash, l2stateprovider.AssociatedAssertionMetadata]("batchIndexForAssertionCache")),
notifyOnNumberOfBlocks: 1,
newBlockNotifier: events.NewProducer[*gethtypes.Header](),
claimedAssertionsInChallenge: threadsafe.NewLruSet(1500, threadsafe.LruSetWithMetric[protocol.AssertionHash]("claimedAssertionsInChallenge")),
claimedAssertionsInChallenge: threadsafe.NewLruSet(maxAssertions, threadsafe.LruSetWithMetric[protocol.AssertionHash]("claimedAssertionsInChallenge")),
api: nil,
}
for _, o := range opts {
Expand Down
1 change: 1 addition & 0 deletions challenge-manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func setupValidator(ctx context.Context, t *testing.T) (*Manager, *mocks.MockPro
cm := &mocks.MockSpecChallengeManager{}
p.On("CurrentChallengeManager", ctx).Return(cm, nil)
p.On("SpecChallengeManager").Return(cm)
p.On("MaxAssertionsPerChallengePeriod").Return(uint64(100))
cm.On("NumBigSteps").Return(uint8(1))
s := &mocks.MockStateManager{}
cfg, err := setup.ChainsWithEdgeChallengeManager(setup.WithMockOneStepProver())
Expand Down
11 changes: 8 additions & 3 deletions testing/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,14 @@ func (m *MockProtocol) NumAssertions(ctx context.Context) (uint64, error) {
return args.Get(0).(uint64), args.Error(1)
}

func (m *MockProtocol) MinAssertionPeriodBlocks(ctx context.Context) (uint64, error) {
args := m.Called(ctx)
return args.Get(0).(uint64), args.Error(1)
func (m *MockProtocol) MinAssertionPeriodBlocks() uint64 {
args := m.Called()
return args.Get(0).(uint64)
}

func (m *MockProtocol) MaxAssertionsPerChallengePeriod() uint64 {
args := m.Called()
return args.Get(0).(uint64)
}

func (m *MockProtocol) GetAssertion(ctx context.Context, opts *bind.CallOpts, id protocol.AssertionHash) (protocol.Assertion, error) {
Expand Down

0 comments on commit c782ebf

Please sign in to comment.