Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 276
refactor(cache): save caches to disk#2282
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -4,9 +4,11 @@ import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/binary" | ||
| "encoding/gob" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "path/filepath" | ||
| "sync" | ||
| "sync/atomic" | ||
| "time" | ||
| @@ -276,8 +278,7 @@ func NewManager( | ||
| } | ||
| // set block height in store | ||
| err = store.SetHeight(ctx, s.LastBlockHeight) | ||
| if err != nil { | ||
| if err = store.SetHeight(ctx, s.LastBlockHeight); err != nil { | ||
| return nil, err | ||
| } | ||
| @@ -286,22 +287,22 @@ func NewManager( | ||
| } | ||
| if config.DA.BlockTime.Duration == 0 { | ||
| logger.Info("Using default DA block time", "DABlockTime", defaultDABlockTime) | ||
| logger.Info("using default DA block time", "DABlockTime", defaultDABlockTime) | ||
| config.DA.BlockTime.Duration = defaultDABlockTime | ||
| } | ||
| if config.Node.BlockTime.Duration == 0 { | ||
| logger.Info("Using default block time", "BlockTime", defaultBlockTime) | ||
| logger.Info("using default block time", "BlockTime", defaultBlockTime) | ||
| config.Node.BlockTime.Duration = defaultBlockTime | ||
| } | ||
| if config.Node.LazyBlockInterval.Duration == 0 { | ||
| logger.Info("Using default lazy block time", "LazyBlockTime", defaultLazyBlockTime) | ||
| logger.Info("using default lazy block time", "LazyBlockTime", defaultLazyBlockTime) | ||
| config.Node.LazyBlockInterval.Duration = defaultLazyBlockTime | ||
| } | ||
| if config.DA.MempoolTTL == 0 { | ||
| logger.Info("Using default mempool ttl", "MempoolTTL", defaultMempoolTTL) | ||
| logger.Info("using default mempool ttl", "MempoolTTL", defaultMempoolTTL) | ||
| config.DA.MempoolTTL = defaultMempoolTTL | ||
| } | ||
| @@ -312,7 +313,7 @@ func NewManager( | ||
| // If lastBatchHash is not set, retrieve the last batch hash from store | ||
| lastBatchDataBytes, err := store.GetMetadata(ctx, LastBatchDataKey) | ||
| if err != nil { | ||
| if err != nil && s.LastBlockHeight > 0 { | ||
| logger.Error("error while retrieving last batch hash", "error", err) | ||
| } | ||
| @@ -324,7 +325,7 @@ func NewManager( | ||
| daH := atomic.Uint64{} | ||
| daH.Store(s.DAHeight) | ||
| agg := &Manager{ | ||
| m := &Manager{ | ||
| signer: signer, | ||
| config: config, | ||
| genesis: genesis, | ||
| @@ -358,16 +359,26 @@ func NewManager( | ||
| txNotifyCh: make(chan struct{}, 1), // Non-blocking channel | ||
| batchSubmissionChan: make(chan coresequencer.Batch, eventInChLength), | ||
| } | ||
| agg.init(ctx) | ||
| // initialize da included height | ||
| if height, err := m.store.GetMetadata(ctx, DAIncludedHeightKey); err == nil && len(height) == 8 { | ||
| m.daIncludedHeight.Store(binary.LittleEndian.Uint64(height)) | ||
| } | ||
Comment on lines
+363
to
+367
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing error-handling for unexpected metadata format
ifheight, err:=m.store.GetMetadata(ctx, DAIncludedHeightKey); err==nil {
iflen(height) !=8 {
returnnil, fmt.Errorf("corrupted DAIncludedHeight metadata, length=%d", len(height))
}
m.daIncludedHeight.Store(binary.LittleEndian.Uint64(height))
}This protects later logic from a poisoned 🤖 Prompt for AI Agents | ||
| // Set the default publishBlock implementation | ||
| agg.publishBlock = agg.publishBlockInternal | ||
| if s, ok := agg.sequencer.(interface { | ||
| m.publishBlock = m.publishBlockInternal | ||
| if s, ok := m.sequencer.(interface { | ||
| SetBatchSubmissionChan(chan coresequencer.Batch) | ||
| }); ok { | ||
| s.SetBatchSubmissionChan(agg.batchSubmissionChan) | ||
| s.SetBatchSubmissionChan(m.batchSubmissionChan) | ||
| } | ||
| return agg, nil | ||
| // fetch caches from disks | ||
| if err := m.LoadCache(); err != nil { | ||
| return nil, fmt.Errorf("failed to load cache: %w", err) | ||
| } | ||
| return m, nil | ||
| } | ||
| // PendingHeaders returns the pending headers. | ||
| @@ -387,13 +398,6 @@ func (m *Manager) GetLastState() types.State { | ||
| return m.lastState | ||
| } | ||
| func (m *Manager) init(ctx context.Context) { | ||
| // initialize da included height | ||
| if height, err := m.store.GetMetadata(ctx, DAIncludedHeightKey); err == nil && len(height) == 8 { | ||
| m.daIncludedHeight.Store(binary.LittleEndian.Uint64(height)) | ||
| } | ||
| } | ||
| // GetDAIncludedHeight returns the rollup height at which all blocks have been | ||
| // included in the DA | ||
| func (m *Manager) GetDAIncludedHeight() uint64 { | ||
| @@ -925,6 +929,12 @@ func (m *Manager) NotifyNewTransactions() { | ||
| } | ||
| } | ||
| var ( | ||
| cacheDir = "cache" | ||
| headerCacheDir = filepath.Join(cacheDir, "header") | ||
| dataCacheDir = filepath.Join(cacheDir, "data") | ||
| ) | ||
| // HeaderCache returns the headerCache used by the manager. | ||
| func (m *Manager) HeaderCache() *cache.Cache[types.SignedHeader] { | ||
| return m.headerCache | ||
| @@ -934,3 +944,33 @@ func (m *Manager) HeaderCache() *cache.Cache[types.SignedHeader] { | ||
| func (m *Manager) DataCache() *cache.Cache[types.Data] { | ||
| return m.dataCache | ||
| } | ||
| // LoadCache loads the header and data caches from disk. | ||
| func (m *Manager) LoadCache() error { | ||
| gob.Register(&types.SignedHeader{}) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to use gob here? seems like an extra dependency which could be avoided | ||
| gob.Register(&types.Data{}) | ||
| cfgDir := filepath.Dir(m.config.ConfigPath()) | ||
| if err := m.headerCache.LoadFromDisk(filepath.Join(cfgDir, headerCacheDir)); err != nil { | ||
| return fmt.Errorf("failed to load header cache from disk: %w", err) | ||
| } | ||
| if err := m.dataCache.LoadFromDisk(filepath.Join(cfgDir, dataCacheDir)); err != nil { | ||
| return fmt.Errorf("failed to load data cache from disk: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
| // SaveCache saves the header and data caches to disk. | ||
| func (m *Manager) SaveCache() error { | ||
| if err := m.headerCache.SaveToDisk(filepath.Join(filepath.Dir(m.config.ConfigPath()), headerCacheDir)); err != nil { | ||
| return fmt.Errorf("failed to save header cache to disk: %w", err) | ||
| } | ||
| if err := m.dataCache.SaveToDisk(filepath.Join(filepath.Dir(m.config.ConfigPath()), dataCacheDir)); err != nil { | ||
| return fmt.Errorf("failed to save data cache to disk: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.