Skip to content

Commit

Permalink
Merge pull request #2606 from OffchainLabs/allow-forcing-rebuildLocal…
Browse files Browse the repository at this point in the history
…Wasm

Allow force rebuilding of local wasm store
  • Loading branch information
tsahee committed Aug 23, 2024
2 parents 712f9da + 25a9c56 commit 99763bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
14 changes: 11 additions & 3 deletions cmd/conf/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type InitConfig struct {
PruneThreads int `koanf:"prune-threads"`
PruneTrieCleanCache int `koanf:"prune-trie-clean-cache"`
RecreateMissingStateFrom uint64 `koanf:"recreate-missing-state-from"`
RebuildLocalWasm bool `koanf:"rebuild-local-wasm"`
RebuildLocalWasm string `koanf:"rebuild-local-wasm"`
ReorgToBatch int64 `koanf:"reorg-to-batch"`
ReorgToMessageBatch int64 `koanf:"reorg-to-message-batch"`
ReorgToBlockBatch int64 `koanf:"reorg-to-block-batch"`
Expand All @@ -56,7 +56,7 @@ var InitConfigDefault = InitConfig{
PruneThreads: runtime.NumCPU(),
PruneTrieCleanCache: 600,
RecreateMissingStateFrom: 0, // 0 = disabled
RebuildLocalWasm: true,
RebuildLocalWasm: "auto",
ReorgToBatch: -1,
ReorgToMessageBatch: -1,
ReorgToBlockBatch: -1,
Expand All @@ -82,10 +82,14 @@ func InitConfigAddOptions(prefix string, f *pflag.FlagSet) {
f.Int(prefix+".prune-threads", InitConfigDefault.PruneThreads, "the number of threads to use when pruning")
f.Int(prefix+".prune-trie-clean-cache", InitConfigDefault.PruneTrieCleanCache, "amount of memory in megabytes to cache unchanged state trie nodes with when traversing state database during pruning")
f.Uint64(prefix+".recreate-missing-state-from", InitConfigDefault.RecreateMissingStateFrom, "block number to start recreating missing states from (0 = disabled)")
f.Bool(prefix+".rebuild-local-wasm", InitConfigDefault.RebuildLocalWasm, "rebuild local wasm database on boot if needed (otherwise-will be done lazily)")
f.Int64(prefix+".reorg-to-batch", InitConfigDefault.ReorgToBatch, "rolls back the blockchain to a specified batch number")
f.Int64(prefix+".reorg-to-message-batch", InitConfigDefault.ReorgToMessageBatch, "rolls back the blockchain to the first batch at or before a given message index")
f.Int64(prefix+".reorg-to-block-batch", InitConfigDefault.ReorgToBlockBatch, "rolls back the blockchain to the first batch at or before a given block number")
f.String(prefix+".rebuild-local-wasm", InitConfigDefault.RebuildLocalWasm, "rebuild local wasm database on boot if needed (otherwise-will be done lazily). Three modes are supported \n"+
"\"auto\"- (enabled by default) if any previous rebuilding attempt was successful then rebuilding is disabled else continues to rebuild,\n"+
"\"force\"- force rebuilding which would commence rebuilding despite the status of previous attempts,\n"+
"\"false\"- do not rebuild on startup",
)
}

func (c *InitConfig) Validate() error {
Expand All @@ -110,6 +114,10 @@ func (c *InitConfig) Validate() error {
}
}
}
c.RebuildLocalWasm = strings.ToLower(c.RebuildLocalWasm)
if c.RebuildLocalWasm != "auto" && c.RebuildLocalWasm != "force" && c.RebuildLocalWasm != "false" {
return fmt.Errorf("invalid value of rebuild-local-wasm, want: auto or force or false, got: %s", c.RebuildLocalWasm)
}
return nil
}

Expand Down
16 changes: 12 additions & 4 deletions cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,13 +532,21 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
if err = gethexec.WriteToKeyValueStore(wasmDb, gethexec.RebuildingPositionKey, gethexec.RebuildingDone); err != nil {
return nil, nil, fmt.Errorf("unable to set rebuilding status of wasm store to done: %w", err)
}
} else if config.Init.RebuildLocalWasm {
position, err := gethexec.ReadFromKeyValueStore[common.Hash](wasmDb, gethexec.RebuildingPositionKey)
if err != nil {
log.Info("Unable to get codehash position in rebuilding of wasm store, its possible it isnt initialized yet, so initializing it and starting rebuilding", "err", err)
} else if config.Init.RebuildLocalWasm != "false" {
var position common.Hash
if config.Init.RebuildLocalWasm == "force" {
log.Info("Commencing force rebuilding of wasm store by setting codehash position in rebuilding to beginning")
if err := gethexec.WriteToKeyValueStore(wasmDb, gethexec.RebuildingPositionKey, common.Hash{}); err != nil {
return nil, nil, fmt.Errorf("unable to initialize codehash position in rebuilding of wasm store to beginning: %w", err)
}
} else {
position, err = gethexec.ReadFromKeyValueStore[common.Hash](wasmDb, gethexec.RebuildingPositionKey)
if err != nil {
log.Info("Unable to get codehash position in rebuilding of wasm store, its possible it isnt initialized yet, so initializing it and starting rebuilding", "err", err)
if err := gethexec.WriteToKeyValueStore(wasmDb, gethexec.RebuildingPositionKey, common.Hash{}); err != nil {
return nil, nil, fmt.Errorf("unable to initialize codehash position in rebuilding of wasm store to beginning: %w", err)
}
}
}
if position != gethexec.RebuildingDone {
startBlockHash, err := gethexec.ReadFromKeyValueStore[common.Hash](wasmDb, gethexec.RebuildingStartBlockHashKey)
Expand Down

0 comments on commit 99763bd

Please sign in to comment.