fix complaint about Ethash

This commit is contained in:
Guillaume Ballet 2023-11-02 13:54:54 +01:00
parent a915bda7a5
commit 272a0cc6fa
2 changed files with 29 additions and 15 deletions

View file

@ -624,7 +624,11 @@ func snapshotExportPreimages(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx) stack, _ := makeConfigNode(ctx)
defer stack.Close() defer stack.Close()
chain, _ := utils.MakeChain(ctx, stack, true) chaindb := utils.MakeChainDatabase(ctx, stack, true)
defer chaindb.Close()
triedb := utils.MakeTrieDatabase(ctx, chaindb, false, true)
defer triedb.Close()
var root common.Hash var root common.Hash
if ctx.NArg() > 1 { if ctx.NArg() > 1 {
@ -633,10 +637,28 @@ func snapshotExportPreimages(ctx *cli.Context) error {
return fmt.Errorf("invalid hash: %s", ctx.Args().Get(1)) return fmt.Errorf("invalid hash: %s", ctx.Args().Get(1))
} }
root = common.BytesToHash(rootBytes) root = common.BytesToHash(rootBytes)
} else {
headBlock := rawdb.ReadHeadBlock(chaindb)
if headBlock == nil {
log.Error("Failed to load head block")
return errors.New("no head block")
}
root = headBlock.Root()
}
snapConfig := snapshot.Config{
CacheSize: 256,
Recovery: false,
NoBuild: true,
AsyncBuild: false,
}
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, root)
if err != nil {
return err
} }
start := time.Now() start := time.Now()
if err := utils.ExportSnapshotPreimages(chain, ctx.Args().First(), root); err != nil { if err := utils.ExportSnapshotPreimages(chaindb, snaptree, ctx.Args().First(), root); err != nil {
utils.Fatalf("Export error: %v\n", err) utils.Fatalf("Export error: %v\n", err)
} }
log.Info("Export done", "duration", time.Since(start)) log.Info("Export done", "duration", time.Since(start))

View file

@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/ethconfig" "github.com/ethereum/go-ethereum/eth/ethconfig"
@ -376,7 +377,7 @@ func ExportPreimages(db ethdb.Database, fn string) error {
// ExportSnapshotPreimages exports the preimages corresponding to the enumeration of // ExportSnapshotPreimages exports the preimages corresponding to the enumeration of
// the snapshot for a given root. // the snapshot for a given root.
func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash) error { func ExportSnapshotPreimages(chaindb ethdb.Database, snaptree *snapshot.Tree, fn string, root common.Hash) error {
log.Info("Exporting preimages", "file", fn) log.Info("Exporting preimages", "file", fn)
fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) fh, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
@ -397,15 +398,6 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
defer buf.Flush() defer buf.Flush()
writer = buf writer = buf
statedb, err := chain.State()
if err != nil {
return fmt.Errorf("failed to open statedb: %w", err)
}
if root == (common.Hash{}) {
root = chain.CurrentBlock().Root
}
type hashAndPreimageSize struct { type hashAndPreimageSize struct {
Hash common.Hash Hash common.Hash
Size int Size int
@ -414,7 +406,7 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
go func() { go func() {
defer close(hashCh) defer close(hashCh)
accIt, err := chain.Snapshots().AccountIterator(root, common.Hash{}) accIt, err := snaptree.AccountIterator(root, common.Hash{})
if err != nil { if err != nil {
log.Error("Failed to create account iterator", "error", err) log.Error("Failed to create account iterator", "error", err)
return return
@ -431,7 +423,7 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
hashCh <- hashAndPreimageSize{Hash: accIt.Hash(), Size: 20} hashCh <- hashAndPreimageSize{Hash: accIt.Hash(), Size: 20}
if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash { if acc.Root != (common.Hash{}) && acc.Root != types.EmptyRootHash {
stIt, err := chain.Snapshots().StorageIterator(root, accIt.Hash(), common.Hash{}) stIt, err := snaptree.StorageIterator(root, accIt.Hash(), common.Hash{})
if err != nil { if err != nil {
log.Error("Failed to create storage iterator", "error", err) log.Error("Failed to create storage iterator", "error", err)
return return
@ -449,7 +441,7 @@ func ExportSnapshotPreimages(chain *core.BlockChain, fn string, root common.Hash
}() }()
for item := range hashCh { for item := range hashCh {
preimage := rawdb.ReadPreimage(statedb.Database().DiskDB(), item.Hash) preimage := rawdb.ReadPreimage(chaindb, item.Hash)
if len(preimage) == 0 { if len(preimage) == 0 {
return fmt.Errorf("missing preimage for %v", item.Hash) return fmt.Errorf("missing preimage for %v", item.Hash)
} }