-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
222 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package staterecovery | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core" | ||
"github.com/ethereum/go-ethereum/core/state" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
"github.com/ethereum/go-ethereum/ethdb" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/trie" | ||
"github.com/ethereum/go-ethereum/trie/triedb/hashdb" | ||
) | ||
|
||
func RecreateMissingStates(chainDb ethdb.Database, bc *core.BlockChain, cacheConfig *core.CacheConfig) error { | ||
log.Info("Recreating missing states...") | ||
start := time.Now() | ||
current := bc.Genesis().NumberU64() + 1 | ||
last := bc.CurrentBlock().Number.Uint64() | ||
|
||
previousBlock := bc.GetBlockByNumber(current - 1) | ||
if previousBlock == nil { | ||
return fmt.Errorf("genesis block is missing") | ||
} | ||
hashConfig := *hashdb.Defaults | ||
hashConfig.CleanCacheSize = cacheConfig.TrieCleanLimit | ||
trieConfig := &trie.Config{ | ||
Preimages: false, | ||
HashDB: &hashConfig, | ||
} | ||
database := state.NewDatabaseWithConfig(chainDb, trieConfig) | ||
defer database.TrieDB().Close() | ||
previousState, err := state.New(previousBlock.Root(), database, nil) | ||
if err != nil { | ||
return fmt.Errorf("genesis state is missing: %w", err) | ||
} | ||
database.TrieDB().Reference(previousBlock.Root(), common.Hash{}) | ||
logged := time.Now() | ||
recreated := 0 | ||
for current <= last { | ||
if time.Since(logged) > 1*time.Minute { | ||
log.Info("Recreating missing states", "block", current, "target", last, "remaining", last-current, "elapsed", time.Since(start), "recreated", recreated) | ||
logged = time.Now() | ||
} | ||
currentBlock := bc.GetBlockByNumber(current) | ||
if currentBlock == nil { | ||
return fmt.Errorf("missing block %d", current) | ||
} | ||
currentState, err := state.New(currentBlock.Root(), database, nil) | ||
if err != nil { | ||
_, _, _, err := bc.Processor().Process(currentBlock, previousState, vm.Config{}) | ||
if err != nil { | ||
return fmt.Errorf("processing block %d failed: %v", current, err) | ||
} | ||
root, err := previousState.Commit(current, bc.Config().IsEIP158(currentBlock.Number())) | ||
if err != nil { | ||
return fmt.Errorf("StateDB commit failed, number %d root %v: %w", current, currentBlock.Root().Hex(), err) | ||
} | ||
if root.Cmp(currentBlock.Root()) != 0 { | ||
return fmt.Errorf("reached different state root after processing block %d, want %v, have %v", current, currentBlock.Root(), root) | ||
} | ||
// commit to disk | ||
err = database.TrieDB().Commit(root, false) // TODO report = true, do we want this many logs? | ||
if err != nil { | ||
return fmt.Errorf("TrieDB commit failed, number %d root %v: %w", current, root, err) | ||
} | ||
currentState, err = state.New(currentBlock.Root(), database, nil) | ||
if err != nil { | ||
return fmt.Errorf("state reset after block %d failed: %v", current, err) | ||
} | ||
database.TrieDB().Reference(currentBlock.Root(), common.Hash{}) | ||
database.TrieDB().Dereference(previousBlock.Root()) | ||
recreated++ | ||
} | ||
current++ | ||
previousState = currentState | ||
previousBlock = currentBlock | ||
} | ||
log.Info("Finished recreating missing states", "elapsed", time.Since(start), "recreated", recreated) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package arbtest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/types" | ||
"github.com/ethereum/go-ethereum/node" | ||
"github.com/ethereum/go-ethereum/trie" | ||
"github.com/offchainlabs/nitro/cmd/staterecovery" | ||
"github.com/offchainlabs/nitro/execution/gethexec" | ||
) | ||
|
||
func TestRectreateMissingStates(t *testing.T) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
builder := NewNodeBuilder(ctx).DefaultConfig(t, true) | ||
builder.execConfig.Caching.MaxNumberOfBlocksToSkipStateSaving = 16 | ||
_ = builder.Build(t) | ||
l2cleanupDone := false | ||
defer func() { | ||
if !l2cleanupDone { | ||
builder.L2.cleanup() | ||
} | ||
builder.L1.cleanup() | ||
}() | ||
builder.L2Info.GenerateAccount("User2") | ||
var txs []*types.Transaction | ||
for i := uint64(0); i < 200; i++ { | ||
tx := builder.L2Info.PrepareTx("Owner", "User2", builder.L2Info.TransferGas, common.Big1, nil) | ||
txs = append(txs, tx) | ||
err := builder.L2.Client.SendTransaction(ctx, tx) | ||
Require(t, err) | ||
} | ||
for _, tx := range txs { | ||
_, err := builder.L2.EnsureTxSucceeded(tx) | ||
Require(t, err) | ||
} | ||
lastBlock, err := builder.L2.Client.BlockNumber(ctx) | ||
Require(t, err) | ||
l2cleanupDone = true | ||
builder.L2.cleanup() | ||
t.Log("stopped l2 node") | ||
func() { | ||
stack, err := node.New(builder.l2StackConfig) | ||
Require(t, err) | ||
defer stack.Close() | ||
chainDb, err := stack.OpenDatabase("chaindb", 0, 0, "", false) | ||
Require(t, err) | ||
defer chainDb.Close() | ||
cacheConfig := gethexec.DefaultCacheConfigFor(stack, &gethexec.DefaultCachingConfig) | ||
bc, err := gethexec.GetBlockChain(chainDb, cacheConfig, builder.chainConfig, builder.execConfig.TxLookupLimit) | ||
Require(t, err) | ||
err = staterecovery.RecreateMissingStates(chainDb, bc, cacheConfig) | ||
Require(t, err) | ||
}() | ||
|
||
testClient, cleanup := builder.Build2ndNode(t, &SecondNodeParams{stackConfig: builder.l2StackConfig}) | ||
defer cleanup() | ||
|
||
currentBlock := uint64(0) | ||
// wait for the chain to catch up | ||
for currentBlock < lastBlock { | ||
currentBlock, err = testClient.Client.BlockNumber(ctx) | ||
Require(t, err) | ||
time.Sleep(20 * time.Millisecond) | ||
} | ||
|
||
currentBlock, err = testClient.Client.BlockNumber(ctx) | ||
Require(t, err) | ||
bc := testClient.ExecNode.Backend.ArbInterface().BlockChain() | ||
triedb := bc.StateCache().TrieDB() | ||
var start uint64 | ||
if currentBlock+1 >= builder.execConfig.Caching.BlockCount { | ||
start = currentBlock + 1 - builder.execConfig.Caching.BlockCount | ||
} else { | ||
start = 0 | ||
} | ||
for i := start; i <= currentBlock; i++ { | ||
header := bc.GetHeaderByNumber(i) | ||
_, err := bc.StateAt(header.Root) | ||
Require(t, err) | ||
tr, err := trie.New(trie.TrieID(header.Root), triedb) | ||
Require(t, err) | ||
it, err := tr.NodeIterator(nil) | ||
Require(t, err) | ||
for it.Next(true) { | ||
} | ||
Require(t, it.Error()) | ||
} | ||
|
||
tx := builder.L2Info.PrepareTx("Owner", "User2", builder.L2Info.TransferGas, common.Big1, nil) | ||
err = testClient.Client.SendTransaction(ctx, tx) | ||
Require(t, err) | ||
_, err = testClient.EnsureTxSucceeded(tx) | ||
Require(t, err) | ||
} |