2018-09-24 15:57:49 +03:00
|
|
|
// Copyright 2018 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rawdb
|
|
|
|
|
|
|
|
import (
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
"bytes"
|
2019-05-16 14:30:11 +03:00
|
|
|
"errors"
|
2019-03-14 09:59:47 +03:00
|
|
|
"fmt"
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
"os"
|
2022-08-08 12:08:36 +03:00
|
|
|
"path"
|
2023-02-09 11:48:34 +03:00
|
|
|
"path/filepath"
|
2022-10-28 11:23:49 +03:00
|
|
|
"strings"
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
"time"
|
2019-03-14 09:59:47 +03:00
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-09-24 15:57:49 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb/leveldb"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb/memorydb"
|
2023-10-13 22:50:20 +03:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb/pebble"
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2024-04-15 05:47:54 +03:00
|
|
|
"github.com/olekukonko/tablewriter"
|
2018-09-24 15:57:49 +03:00
|
|
|
)
|
|
|
|
|
2023-09-29 10:52:22 +03:00
|
|
|
// freezerdb is a database wrapper that enables freezer data retrievals.
|
2019-03-08 16:56:20 +03:00
|
|
|
type freezerdb struct {
|
2022-08-08 12:08:36 +03:00
|
|
|
ancientRoot string
|
2019-03-08 16:56:20 +03:00
|
|
|
ethdb.KeyValueStore
|
2019-03-14 09:59:47 +03:00
|
|
|
ethdb.AncientStore
|
2024-03-22 17:37:47 +03:00
|
|
|
ethdb.AncientFreezer
|
2024-03-08 11:07:29 +03:00
|
|
|
diffStore ethdb.KeyValueStore
|
|
|
|
stateStore ethdb.Database
|
2024-04-18 10:12:05 +03:00
|
|
|
blockStore ethdb.Database
|
2024-03-08 11:07:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (frdb *freezerdb) StateStoreReader() ethdb.Reader {
|
|
|
|
if frdb.stateStore == nil {
|
|
|
|
return frdb
|
|
|
|
}
|
|
|
|
return frdb.stateStore
|
2019-03-14 09:59:47 +03:00
|
|
|
}
|
|
|
|
|
2024-04-18 10:12:05 +03:00
|
|
|
func (frdb *freezerdb) BlockStoreReader() ethdb.Reader {
|
|
|
|
if frdb.blockStore == nil {
|
|
|
|
return frdb
|
|
|
|
}
|
|
|
|
return frdb.blockStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (frdb *freezerdb) BlockStoreWriter() ethdb.Writer {
|
2024-07-16 17:37:03 +03:00
|
|
|
if frdb.blockStore == nil {
|
|
|
|
return frdb
|
|
|
|
}
|
|
|
|
return frdb.blockStore
|
2024-04-18 10:12:05 +03:00
|
|
|
}
|
|
|
|
|
2022-08-08 12:08:36 +03:00
|
|
|
// AncientDatadir returns the path of root ancient directory.
|
|
|
|
func (frdb *freezerdb) AncientDatadir() (string, error) {
|
|
|
|
return frdb.ancientRoot, nil
|
|
|
|
}
|
|
|
|
|
2019-03-14 09:59:47 +03:00
|
|
|
// Close implements io.Closer, closing both the fast key-value store as well as
|
|
|
|
// the slow ancient tables.
|
|
|
|
func (frdb *freezerdb) Close() error {
|
|
|
|
var errs []error
|
2020-05-11 15:11:17 +03:00
|
|
|
if err := frdb.AncientStore.Close(); err != nil {
|
2019-03-14 09:59:47 +03:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2020-05-11 15:11:17 +03:00
|
|
|
if err := frdb.KeyValueStore.Close(); err != nil {
|
2019-03-14 09:59:47 +03:00
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
if frdb.diffStore != nil {
|
|
|
|
if err := frdb.diffStore.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 11:07:29 +03:00
|
|
|
if frdb.stateStore != nil {
|
|
|
|
if err := frdb.stateStore.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 10:12:05 +03:00
|
|
|
if frdb.blockStore != nil {
|
|
|
|
if err := frdb.blockStore.Close(); err != nil {
|
|
|
|
errs = append(errs, err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 09:59:47 +03:00
|
|
|
if len(errs) != 0 {
|
|
|
|
return fmt.Errorf("%v", errs)
|
|
|
|
}
|
|
|
|
return nil
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
func (frdb *freezerdb) DiffStore() ethdb.KeyValueStore {
|
|
|
|
return frdb.diffStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (frdb *freezerdb) SetDiffStore(diff ethdb.KeyValueStore) {
|
|
|
|
if frdb.diffStore != nil {
|
|
|
|
frdb.diffStore.Close()
|
|
|
|
}
|
|
|
|
frdb.diffStore = diff
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:07:29 +03:00
|
|
|
func (frdb *freezerdb) StateStore() ethdb.Database {
|
|
|
|
return frdb.stateStore
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:37:03 +03:00
|
|
|
func (frdb *freezerdb) GetStateStore() ethdb.Database {
|
|
|
|
if frdb.stateStore != nil {
|
|
|
|
return frdb.stateStore
|
|
|
|
}
|
|
|
|
return frdb
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:07:29 +03:00
|
|
|
func (frdb *freezerdb) SetStateStore(state ethdb.Database) {
|
|
|
|
if frdb.stateStore != nil {
|
|
|
|
frdb.stateStore.Close()
|
|
|
|
}
|
|
|
|
frdb.stateStore = state
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:12:05 +03:00
|
|
|
func (frdb *freezerdb) BlockStore() ethdb.Database {
|
|
|
|
if frdb.blockStore != nil {
|
|
|
|
return frdb.blockStore
|
|
|
|
} else {
|
|
|
|
return frdb
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (frdb *freezerdb) SetBlockStore(block ethdb.Database) {
|
|
|
|
if frdb.blockStore != nil {
|
|
|
|
frdb.blockStore.Close()
|
|
|
|
}
|
|
|
|
frdb.blockStore = block
|
|
|
|
}
|
|
|
|
|
2024-05-27 10:10:59 +03:00
|
|
|
func (frdb *freezerdb) HasSeparateBlockStore() bool {
|
|
|
|
return frdb.blockStore != nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 13:01:24 +03:00
|
|
|
// Freeze is a helper method used for external testing to trigger and block until
|
|
|
|
// a freeze cycle completes, without having to sleep for a minute to trigger the
|
|
|
|
// automatic background run.
|
2024-05-29 10:11:52 +03:00
|
|
|
func (frdb *freezerdb) Freeze(threshold uint64) error {
|
2022-05-06 14:28:42 +03:00
|
|
|
if frdb.AncientStore.(*chainFreezer).readonly {
|
2021-03-22 21:06:30 +03:00
|
|
|
return errReadOnly
|
|
|
|
}
|
2024-05-29 10:11:52 +03:00
|
|
|
// Set the freezer threshold to a temporary value
|
|
|
|
defer func(old uint64) {
|
|
|
|
frdb.AncientStore.(*chainFreezer).threshold.Store(old)
|
|
|
|
}(frdb.AncientStore.(*chainFreezer).threshold.Load())
|
|
|
|
frdb.AncientStore.(*chainFreezer).threshold.Store(threshold)
|
2020-08-20 13:01:24 +03:00
|
|
|
// Trigger a freeze cycle and block until it's done
|
|
|
|
trigger := make(chan struct{}, 1)
|
2022-05-06 14:28:42 +03:00
|
|
|
frdb.AncientStore.(*chainFreezer).trigger <- trigger
|
2020-08-20 13:01:24 +03:00
|
|
|
<-trigger
|
2021-03-22 21:06:30 +03:00
|
|
|
return nil
|
2020-08-20 13:01:24 +03:00
|
|
|
}
|
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
func (frdb *freezerdb) SetupFreezerEnv(env *ethdb.FreezerEnv) error {
|
|
|
|
return frdb.AncientFreezer.SetupFreezerEnv(env)
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:56:20 +03:00
|
|
|
// nofreezedb is a database wrapper that disables freezer data retrievals.
|
|
|
|
type nofreezedb struct {
|
|
|
|
ethdb.KeyValueStore
|
2024-03-08 11:07:29 +03:00
|
|
|
diffStore ethdb.KeyValueStore
|
|
|
|
stateStore ethdb.Database
|
2024-04-18 10:12:05 +03:00
|
|
|
blockStore ethdb.Database
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// HasAncient returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) HasAncient(kind string, number uint64) (bool, error) {
|
|
|
|
return false, errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ancient returns an error as we don't have a backing chain freezer.
|
2019-03-08 16:56:20 +03:00
|
|
|
func (db *nofreezedb) Ancient(kind string, number uint64) ([]byte, error) {
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
return nil, errNotSupported
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:24:27 +03:00
|
|
|
// AncientRange returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) AncientRange(kind string, start, max, maxByteSize uint64) ([][]byte, error) {
|
2021-08-13 11:51:01 +03:00
|
|
|
return nil, errNotSupported
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// Ancients returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) Ancients() (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
2024-06-04 10:47:16 +03:00
|
|
|
// ItemAmountInAncient returns an error as we don't have a backing chain freezer.
|
2022-07-05 06:14:21 +03:00
|
|
|
func (db *nofreezedb) ItemAmountInAncient() (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
2022-03-10 11:37:23 +03:00
|
|
|
// Tail returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) Tail() (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
// AncientSize returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) AncientSize(kind string) (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
2021-09-07 13:31:17 +03:00
|
|
|
// ModifyAncients is not supported.
|
|
|
|
func (db *nofreezedb) ModifyAncients(func(ethdb.AncientWriteOp) error) (int64, error) {
|
|
|
|
return 0, errNotSupported
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
|
2022-03-10 11:37:23 +03:00
|
|
|
// TruncateHead returns an error as we don't have a backing chain freezer.
|
2023-08-01 15:17:32 +03:00
|
|
|
func (db *nofreezedb) TruncateHead(items uint64) (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
2022-03-10 11:37:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TruncateTail returns an error as we don't have a backing chain freezer.
|
2023-08-01 15:17:32 +03:00
|
|
|
func (db *nofreezedb) TruncateTail(items uint64) (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 11:35:39 +03:00
|
|
|
// TruncateTableTail will truncate certain table to new tail
|
|
|
|
func (db *nofreezedb) TruncateTableTail(kind string, tail uint64) (uint64, error) {
|
|
|
|
return 0, errNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetTable will reset certain table with new start point
|
|
|
|
func (db *nofreezedb) ResetTable(kind string, startAt uint64, onlyEmpty bool) error {
|
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 17:59:48 +03:00
|
|
|
// Sync returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) Sync() error {
|
|
|
|
return errNotSupported
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
func (db *nofreezedb) DiffStore() ethdb.KeyValueStore {
|
|
|
|
return db.diffStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *nofreezedb) SetDiffStore(diff ethdb.KeyValueStore) {
|
|
|
|
db.diffStore = diff
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:07:29 +03:00
|
|
|
func (db *nofreezedb) StateStore() ethdb.Database {
|
|
|
|
return db.stateStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *nofreezedb) SetStateStore(state ethdb.Database) {
|
|
|
|
db.stateStore = state
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:37:03 +03:00
|
|
|
func (db *nofreezedb) GetStateStore() ethdb.Database {
|
|
|
|
if db.stateStore != nil {
|
|
|
|
return db.stateStore
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:07:29 +03:00
|
|
|
func (db *nofreezedb) StateStoreReader() ethdb.Reader {
|
|
|
|
if db.stateStore != nil {
|
|
|
|
return db.stateStore
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:12:05 +03:00
|
|
|
func (db *nofreezedb) BlockStore() ethdb.Database {
|
|
|
|
if db.blockStore != nil {
|
|
|
|
return db.blockStore
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *nofreezedb) SetBlockStore(block ethdb.Database) {
|
|
|
|
db.blockStore = block
|
|
|
|
}
|
|
|
|
|
2024-05-27 10:10:59 +03:00
|
|
|
func (db *nofreezedb) HasSeparateBlockStore() bool {
|
|
|
|
return db.blockStore != nil
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:12:05 +03:00
|
|
|
func (db *nofreezedb) BlockStoreReader() ethdb.Reader {
|
|
|
|
if db.blockStore != nil {
|
|
|
|
return db.blockStore
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *nofreezedb) BlockStoreWriter() ethdb.Writer {
|
|
|
|
if db.blockStore != nil {
|
|
|
|
return db.blockStore
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:28:42 +03:00
|
|
|
func (db *nofreezedb) ReadAncients(fn func(reader ethdb.AncientReaderOp) error) (err error) {
|
2021-10-25 17:24:27 +03:00
|
|
|
// Unlike other ancient-related methods, this method does not return
|
|
|
|
// errNotSupported when invoked.
|
|
|
|
// The reason for this is that the caller might want to do several things:
|
2023-09-29 10:52:22 +03:00
|
|
|
// 1. Check if something is in the freezer,
|
2021-10-25 17:24:27 +03:00
|
|
|
// 2. If not, check leveldb.
|
|
|
|
//
|
|
|
|
// This will work, since the ancient-checks inside 'fn' will return errors,
|
|
|
|
// and the leveldb work will continue.
|
|
|
|
//
|
|
|
|
// If we instead were to return errNotSupported here, then the caller would
|
|
|
|
// have to explicitly check for that, having an extra clause to do the
|
|
|
|
// non-ancient operations.
|
|
|
|
return fn(db)
|
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
func (db *nofreezedb) AncientOffSet() uint64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-03-23 22:57:32 +03:00
|
|
|
// MigrateTable processes the entries in a given table in sequence
|
|
|
|
// converting them to a new format if they're of an old format.
|
|
|
|
func (db *nofreezedb) MigrateTable(kind string, convert convertLegacyFn) error {
|
|
|
|
return errNotSupported
|
|
|
|
}
|
|
|
|
|
2022-05-06 14:28:42 +03:00
|
|
|
// AncientDatadir returns an error as we don't have a backing chain freezer.
|
|
|
|
func (db *nofreezedb) AncientDatadir() (string, error) {
|
|
|
|
return "", errNotSupported
|
|
|
|
}
|
|
|
|
|
2024-03-22 17:37:47 +03:00
|
|
|
func (db *nofreezedb) SetupFreezerEnv(env *ethdb.FreezerEnv) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 15:57:49 +03:00
|
|
|
// NewDatabase creates a high level database on top of a given key-value data
|
|
|
|
// store without a freezer moving immutable chain segments into cold storage.
|
|
|
|
func NewDatabase(db ethdb.KeyValueStore) ethdb.Database {
|
2021-09-07 13:31:17 +03:00
|
|
|
return &nofreezedb{KeyValueStore: db}
|
2019-03-08 16:56:20 +03:00
|
|
|
}
|
|
|
|
|
2024-06-04 10:47:16 +03:00
|
|
|
type emptyfreezedb struct {
|
|
|
|
ethdb.KeyValueStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasAncient returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) HasAncient(kind string, number uint64) (bool, error) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ancient returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) Ancient(kind string, number uint64) ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AncientRange returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) AncientRange(kind string, start, max, maxByteSize uint64) ([][]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ancients returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) Ancients() (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ItemAmountInAncient returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) ItemAmountInAncient() (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tail returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) Tail() (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AncientSize returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) AncientSize(kind string) (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ModifyAncients returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) ModifyAncients(func(ethdb.AncientWriteOp) error) (int64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TruncateHead returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) TruncateHead(items uint64) (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TruncateTail returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) TruncateTail(items uint64) (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TruncateTableTail returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) TruncateTableTail(kind string, tail uint64) (uint64, error) {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetTable returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) ResetTable(kind string, startAt uint64, onlyEmpty bool) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sync returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *emptyfreezedb) DiffStore() ethdb.KeyValueStore { return db }
|
|
|
|
func (db *emptyfreezedb) SetDiffStore(diff ethdb.KeyValueStore) {}
|
|
|
|
func (db *emptyfreezedb) StateStore() ethdb.Database { return db }
|
2024-07-16 17:37:03 +03:00
|
|
|
func (db *emptyfreezedb) GetStateStore() ethdb.Database { return db }
|
2024-06-04 10:47:16 +03:00
|
|
|
func (db *emptyfreezedb) SetStateStore(state ethdb.Database) {}
|
|
|
|
func (db *emptyfreezedb) StateStoreReader() ethdb.Reader { return db }
|
|
|
|
func (db *emptyfreezedb) BlockStore() ethdb.Database { return db }
|
|
|
|
func (db *emptyfreezedb) SetBlockStore(block ethdb.Database) {}
|
|
|
|
func (db *emptyfreezedb) HasSeparateBlockStore() bool { return false }
|
|
|
|
func (db *emptyfreezedb) BlockStoreReader() ethdb.Reader { return db }
|
|
|
|
func (db *emptyfreezedb) BlockStoreWriter() ethdb.Writer { return db }
|
|
|
|
func (db *emptyfreezedb) ReadAncients(fn func(reader ethdb.AncientReaderOp) error) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (db *emptyfreezedb) AncientOffSet() uint64 { return 0 }
|
|
|
|
|
|
|
|
// MigrateTable returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) MigrateTable(kind string, convert convertLegacyFn) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AncientDatadir returns nil for pruned db that we don't have a backing chain freezer.
|
|
|
|
func (db *emptyfreezedb) AncientDatadir() (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
func (db *emptyfreezedb) SetupFreezerEnv(env *ethdb.FreezerEnv) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEmptyFreezeDB is used for CLI such as `geth db inspect` in pruned db that we don't
|
|
|
|
// have a backing chain freezer.
|
|
|
|
// WARNING: it must be only used in the above case.
|
|
|
|
func NewEmptyFreezeDB(db ethdb.KeyValueStore) ethdb.Database {
|
|
|
|
return &emptyfreezedb{KeyValueStore: db}
|
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
// NewFreezerDb only create a freezer without statedb.
|
2023-08-23 12:46:08 +03:00
|
|
|
func NewFreezerDb(db ethdb.KeyValueStore, frz, namespace string, readonly bool, newOffSet uint64) (*Freezer, error) {
|
2022-07-05 06:14:21 +03:00
|
|
|
// Create the idle freezer instance, this operation should be atomic to avoid mismatch between offset and acientDB.
|
2023-08-23 12:46:08 +03:00
|
|
|
frdb, err := NewFreezer(frz, namespace, readonly, newOffSet, freezerTableSize, chainFreezerNoSnappy)
|
2022-07-05 06:14:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return frdb, nil
|
|
|
|
}
|
|
|
|
|
2022-08-08 12:08:36 +03:00
|
|
|
// resolveChainFreezerDir is a helper function which resolves the absolute path
|
|
|
|
// of chain freezer by considering backward compatibility.
|
2024-01-15 06:09:41 +03:00
|
|
|
//
|
|
|
|
// rules:
|
|
|
|
// 1. in path mode, block data is stored in chain dir and state data is in state dir.
|
|
|
|
// 2. in hash mode, block data is stored in chain dir or ancient dir(before big merge), no state dir.
|
2022-08-08 12:08:36 +03:00
|
|
|
func resolveChainFreezerDir(ancient string) string {
|
|
|
|
// Check if the chain freezer is already present in the specified
|
|
|
|
// sub folder, if not then two possibilities:
|
|
|
|
// - chain freezer is not initialized
|
|
|
|
// - chain freezer exists in legacy location (root ancient folder)
|
2024-02-02 10:43:33 +03:00
|
|
|
chain := path.Join(ancient, ChainFreezerName)
|
|
|
|
state := path.Join(ancient, StateFreezerName)
|
2024-01-12 10:36:26 +03:00
|
|
|
if common.FileExist(chain) {
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
if common.FileExist(state) {
|
|
|
|
return chain
|
|
|
|
}
|
|
|
|
if common.FileExist(ancient) {
|
|
|
|
log.Info("Found legacy ancient chain path", "location", ancient)
|
|
|
|
chain = ancient
|
2022-08-08 12:08:36 +03:00
|
|
|
}
|
2024-01-12 10:36:26 +03:00
|
|
|
return chain
|
2022-08-08 12:08:36 +03:00
|
|
|
}
|
|
|
|
|
2019-03-08 16:56:20 +03:00
|
|
|
// NewDatabaseWithFreezer creates a high level database on top of a given key-
|
|
|
|
// value data store with a freezer moving immutable chain segments into cold
|
2022-08-08 12:08:36 +03:00
|
|
|
// storage. The passed ancient indicates the path of root ancient directory
|
|
|
|
// where the chain freezer can be opened.
|
2024-05-29 10:11:52 +03:00
|
|
|
func NewDatabaseWithFreezer(db ethdb.KeyValueStore, ancient string, namespace string, readonly, disableFreeze, isLastOffset, pruneAncientData, multiDatabase bool) (ethdb.Database, error) {
|
2022-12-20 04:40:01 +03:00
|
|
|
var offset uint64
|
|
|
|
// The offset of ancientDB should be handled differently in different scenarios.
|
|
|
|
if isLastOffset {
|
|
|
|
offset = ReadOffSetOfLastAncientFreezer(db)
|
|
|
|
} else {
|
|
|
|
offset = ReadOffSetOfCurrentAncientFreezer(db)
|
|
|
|
}
|
|
|
|
|
2024-06-04 10:47:16 +03:00
|
|
|
// This case is used for someone who wants to execute geth db inspect CLI in a pruned db
|
|
|
|
if !disableFreeze && readonly && ReadAncientType(db) == PruneFreezerType {
|
|
|
|
log.Warn("Disk db is pruned, using an empty freezer db for CLI")
|
|
|
|
return NewEmptyFreezeDB(db), nil
|
|
|
|
}
|
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
if pruneAncientData && !disableFreeze && !readonly {
|
2023-08-23 12:46:08 +03:00
|
|
|
frdb, err := newPrunedFreezer(resolveChainFreezerDir(ancient), db, offset)
|
2022-07-05 06:14:21 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go frdb.freeze()
|
2022-07-25 04:56:17 +03:00
|
|
|
if !readonly {
|
|
|
|
WriteAncientType(db, PruneFreezerType)
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
return &freezerdb{
|
2024-03-22 17:37:47 +03:00
|
|
|
ancientRoot: ancient,
|
|
|
|
KeyValueStore: db,
|
|
|
|
AncientStore: frdb,
|
|
|
|
AncientFreezer: frdb,
|
2022-07-05 06:14:21 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if pruneAncientData {
|
|
|
|
log.Error("pruneancient not take effect, disableFreezer or readonly be set")
|
|
|
|
}
|
|
|
|
|
2022-12-20 04:40:01 +03:00
|
|
|
if prunedFrozen := ReadFrozenOfAncientFreezer(db); prunedFrozen > offset {
|
|
|
|
offset = prunedFrozen
|
2022-07-05 06:14:21 +03:00
|
|
|
}
|
|
|
|
|
2022-07-22 11:11:34 +03:00
|
|
|
// Create the idle freezer instance
|
2024-05-29 10:11:52 +03:00
|
|
|
frdb, err := newChainFreezer(resolveChainFreezerDir(ancient), namespace, readonly, offset, multiDatabase)
|
2024-07-16 17:37:03 +03:00
|
|
|
|
|
|
|
// We are creating the freezerdb here because the validation logic for db and freezer below requires certain interfaces
|
|
|
|
// that need a database type. Therefore, we are pre-creating it for subsequent use.
|
|
|
|
freezerDb := &freezerdb{
|
|
|
|
ancientRoot: ancient,
|
|
|
|
KeyValueStore: db,
|
|
|
|
AncientStore: frdb,
|
|
|
|
AncientFreezer: frdb,
|
|
|
|
}
|
2022-07-22 11:11:34 +03:00
|
|
|
if err != nil {
|
2024-07-16 17:37:03 +03:00
|
|
|
printChainMetadata(freezerDb)
|
2022-07-22 11:11:34 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-05 06:14:21 +03:00
|
|
|
|
2019-05-16 14:30:11 +03:00
|
|
|
// Since the freezer can be stored separately from the user's key-value database,
|
|
|
|
// there's a fairly high probability that the user requests invalid combinations
|
|
|
|
// of the freezer and database. Ensure that we don't shoot ourselves in the foot
|
|
|
|
// by serving up conflicting data, leading to both datastores getting corrupted.
|
|
|
|
//
|
2023-09-29 10:52:22 +03:00
|
|
|
// - If both the freezer and key-value store are empty (no genesis), we just
|
2019-05-16 14:30:11 +03:00
|
|
|
// initialized a new empty freezer, so everything's fine.
|
|
|
|
// - If the key-value store is empty, but the freezer is not, we need to make
|
|
|
|
// sure the user's genesis matches the freezer. That will be checked in the
|
|
|
|
// blockchain, since we don't have the genesis block here (nor should we at
|
|
|
|
// this point care, the key-value/freezer combo is valid).
|
|
|
|
// - If neither the key-value store nor the freezer is empty, cross validate
|
|
|
|
// the genesis hashes to make sure they are compatible. If they are, also
|
2022-04-25 10:28:03 +03:00
|
|
|
// ensure that there's no gap between the freezer and subsequently leveldb.
|
2023-09-29 10:52:22 +03:00
|
|
|
// - If the key-value store is not empty, but the freezer is, we might just be
|
2019-05-16 14:30:11 +03:00
|
|
|
// upgrading to the freezer release, or we might have had a small chain and
|
|
|
|
// not frozen anything yet. Ensure that no blocks are missing yet from the
|
|
|
|
// key-value store, since that would mean we already had an old freezer.
|
|
|
|
|
|
|
|
// If the genesis hash is empty, we have a new key-value store, so nothing to
|
|
|
|
// validate in this method. If, however, the genesis hash is not nil, compare
|
|
|
|
// it to the freezer content.
|
2022-07-05 06:14:21 +03:00
|
|
|
// Only to check the followings when offset equal to 0, otherwise the block number
|
|
|
|
// in ancientdb did not start with 0, no genesis block in ancientdb as well.
|
|
|
|
|
|
|
|
if kvgenesis, _ := db.Get(headerHashKey(0)); offset == 0 && len(kvgenesis) > 0 {
|
2019-05-16 14:30:11 +03:00
|
|
|
if frozen, _ := frdb.Ancients(); frozen > 0 {
|
|
|
|
// If the freezer already contains something, ensure that the genesis blocks
|
|
|
|
// match, otherwise we might mix up freezers across chains and destroy both
|
|
|
|
// the freezer and the key-value store.
|
2023-02-21 13:17:34 +03:00
|
|
|
frgenesis, err := frdb.Ancient(ChainFreezerHashTable, 0)
|
2020-07-13 21:43:30 +03:00
|
|
|
if err != nil {
|
2024-07-16 17:37:03 +03:00
|
|
|
printChainMetadata(freezerDb)
|
2020-07-13 21:43:30 +03:00
|
|
|
return nil, fmt.Errorf("failed to retrieve genesis from ancient %v", err)
|
|
|
|
} else if !bytes.Equal(kvgenesis, frgenesis) {
|
2024-07-16 17:37:03 +03:00
|
|
|
printChainMetadata(freezerDb)
|
2019-05-16 14:30:11 +03:00
|
|
|
return nil, fmt.Errorf("genesis mismatch: %#x (leveldb) != %#x (ancients)", kvgenesis, frgenesis)
|
|
|
|
}
|
|
|
|
// Key-value store and freezer belong to the same network. Ensure that they
|
|
|
|
// are contiguous, otherwise we might end up with a non-functional freezer.
|
|
|
|
if kvhash, _ := db.Get(headerHashKey(frozen)); len(kvhash) == 0 {
|
|
|
|
// Subsequent header after the freezer limit is missing from the database.
|
2022-06-29 12:47:33 +03:00
|
|
|
// Reject startup if the database has a more recent head.
|
2024-07-16 17:37:03 +03:00
|
|
|
if head := *ReadHeaderNumber(freezerDb, ReadHeadHeaderHash(freezerDb)); head > frozen-1 {
|
2023-03-08 10:39:13 +03:00
|
|
|
// Find the smallest block stored in the key-value store
|
|
|
|
// in range of [frozen, head]
|
|
|
|
var number uint64
|
|
|
|
for number = frozen; number <= head; number++ {
|
|
|
|
if present, _ := db.Has(headerHashKey(number)); present {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 14:43:37 +03:00
|
|
|
// We are about to exit on error. Print database metadata before exiting
|
2024-07-16 17:37:03 +03:00
|
|
|
printChainMetadata(freezerDb)
|
2023-03-08 10:39:13 +03:00
|
|
|
return nil, fmt.Errorf("gap in the chain between ancients [0 - #%d] and leveldb [#%d - #%d] ",
|
|
|
|
frozen-1, number, head)
|
2019-05-16 14:30:11 +03:00
|
|
|
}
|
|
|
|
// Database contains only older data than the freezer, this happens if the
|
|
|
|
// state was wiped and reinited from an existing freezer.
|
|
|
|
}
|
2019-11-27 11:50:30 +03:00
|
|
|
// Otherwise, key-value store continues where the freezer left off, all is fine.
|
|
|
|
// We might have duplicate blocks (crash after freezer write but before key-value
|
|
|
|
// store deletion, but that's fine).
|
2019-05-16 14:30:11 +03:00
|
|
|
} else {
|
|
|
|
// If the freezer is empty, ensure nothing was moved yet from the key-value
|
|
|
|
// store, otherwise we'll end up missing data. We check block #1 to decide
|
|
|
|
// if we froze anything previously or not, but do take care of databases with
|
|
|
|
// only the genesis block.
|
2024-07-16 17:37:03 +03:00
|
|
|
if ReadHeadHeaderHash(freezerDb) != common.BytesToHash(kvgenesis) {
|
2019-05-16 14:30:11 +03:00
|
|
|
// Key-value store contains more data than the genesis block, make sure we
|
|
|
|
// didn't freeze anything yet.
|
|
|
|
if kvblob, _ := db.Get(headerHashKey(1)); len(kvblob) == 0 {
|
2024-07-16 17:37:03 +03:00
|
|
|
printChainMetadata(freezerDb)
|
2019-05-16 14:30:11 +03:00
|
|
|
return nil, errors.New("ancient chain segments already extracted, please set --datadir.ancient to the correct path")
|
|
|
|
}
|
2022-08-19 09:00:21 +03:00
|
|
|
// Block #1 is still in the database, we're allowed to init a new freezer
|
2019-05-16 14:30:11 +03:00
|
|
|
}
|
2019-11-27 11:50:30 +03:00
|
|
|
// Otherwise, the head header is still the genesis, we're allowed to init a new
|
2022-03-10 11:37:23 +03:00
|
|
|
// freezer.
|
2019-05-16 14:30:11 +03:00
|
|
|
}
|
|
|
|
}
|
2024-04-18 10:12:05 +03:00
|
|
|
|
2024-01-12 10:36:26 +03:00
|
|
|
// no prune ancient start success
|
2022-07-25 04:56:17 +03:00
|
|
|
if !readonly {
|
|
|
|
WriteAncientType(db, EntireFreezerType)
|
|
|
|
}
|
2019-05-16 14:30:11 +03:00
|
|
|
// Freezer is consistent with the key-value database, permit combining the two
|
2022-07-05 06:14:21 +03:00
|
|
|
if !disableFreeze && !frdb.readonly {
|
2021-05-18 02:30:01 +03:00
|
|
|
frdb.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
frdb.freeze(db)
|
|
|
|
frdb.wg.Done()
|
|
|
|
}()
|
2021-03-22 21:06:30 +03:00
|
|
|
}
|
2024-07-16 17:37:03 +03:00
|
|
|
return freezerDb, nil
|
2018-09-24 15:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoryDatabase creates an ephemeral in-memory key-value database without a
|
|
|
|
// freezer moving immutable chain segments into cold storage.
|
|
|
|
func NewMemoryDatabase() ethdb.Database {
|
|
|
|
return NewDatabase(memorydb.New())
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:56:20 +03:00
|
|
|
// NewMemoryDatabaseWithCap creates an ephemeral in-memory key-value database
|
|
|
|
// with an initial starting capacity, but without a freezer moving immutable
|
|
|
|
// chain segments into cold storage.
|
2018-09-24 15:57:49 +03:00
|
|
|
func NewMemoryDatabaseWithCap(size int) ethdb.Database {
|
|
|
|
return NewDatabase(memorydb.NewWithCap(size))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLevelDBDatabase creates a persistent key-value database without a freezer
|
|
|
|
// moving immutable chain segments into cold storage.
|
2021-03-22 21:06:30 +03:00
|
|
|
func NewLevelDBDatabase(file string, cache int, handles int, namespace string, readonly bool) (ethdb.Database, error) {
|
|
|
|
db, err := leveldb.New(file, cache, handles, namespace, readonly)
|
2018-09-24 15:57:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-09 11:48:34 +03:00
|
|
|
log.Info("Using LevelDB as the backing database")
|
2018-09-24 15:57:49 +03:00
|
|
|
return NewDatabase(db), nil
|
|
|
|
}
|
2019-03-08 16:56:20 +03:00
|
|
|
|
2023-10-13 22:50:20 +03:00
|
|
|
// NewPebbleDBDatabase creates a persistent key-value database without a freezer
|
|
|
|
// moving immutable chain segments into cold storage.
|
2023-10-13 22:50:20 +03:00
|
|
|
func NewPebbleDBDatabase(file string, cache int, handles int, namespace string, readonly, ephemeral bool) (ethdb.Database, error) {
|
|
|
|
db, err := pebble.New(file, cache, handles, namespace, readonly, ephemeral)
|
2023-10-13 22:50:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return NewDatabase(db), nil
|
|
|
|
}
|
|
|
|
|
2023-02-09 11:48:34 +03:00
|
|
|
const (
|
|
|
|
dbPebble = "pebble"
|
|
|
|
dbLeveldb = "leveldb"
|
|
|
|
)
|
|
|
|
|
2023-08-23 16:42:37 +03:00
|
|
|
// PreexistingDatabase checks the given data directory whether a database is already
|
2023-02-09 11:48:34 +03:00
|
|
|
// instantiated at that location, and if so, returns the type of database (or the
|
|
|
|
// empty string).
|
2023-08-23 16:42:37 +03:00
|
|
|
func PreexistingDatabase(path string) string {
|
2023-02-09 11:48:34 +03:00
|
|
|
if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil {
|
|
|
|
return "" // No pre-existing db
|
|
|
|
}
|
|
|
|
if matches, err := filepath.Glob(filepath.Join(path, "OPTIONS*")); len(matches) > 0 || err != nil {
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // only possible if the pattern is malformed
|
|
|
|
}
|
|
|
|
return dbPebble
|
|
|
|
}
|
|
|
|
return dbLeveldb
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenOptions contains the options to apply when opening a database.
|
|
|
|
// OBS: If AncientsDirectory is empty, it indicates that no freezer is to be used.
|
|
|
|
type OpenOptions struct {
|
|
|
|
Type string // "leveldb" | "pebble"
|
|
|
|
Directory string // the datadir
|
|
|
|
AncientsDirectory string // the ancients-dir
|
|
|
|
Namespace string // the namespace for database relevant metrics
|
|
|
|
Cache int // the capacity(in megabytes) of the data caching
|
|
|
|
Handles int // number of files to be open simultaneously
|
|
|
|
ReadOnly bool
|
2023-08-23 12:46:08 +03:00
|
|
|
|
|
|
|
DisableFreeze bool
|
|
|
|
IsLastOffset bool
|
|
|
|
PruneAncientData bool
|
2024-04-18 10:12:05 +03:00
|
|
|
|
2023-08-23 21:43:55 +03:00
|
|
|
// Ephemeral means that filesystem sync operations should be avoided: data integrity in the face of
|
|
|
|
// a crash is not important. This option should typically be used in tests.
|
|
|
|
Ephemeral bool
|
2024-05-29 10:11:52 +03:00
|
|
|
|
|
|
|
MultiDataBase bool
|
2023-02-09 11:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// openKeyValueDatabase opens a disk-based key-value database, e.g. leveldb or pebble.
|
|
|
|
//
|
|
|
|
// type == null type != null
|
|
|
|
// +----------------------------------------
|
2024-03-21 06:41:21 +03:00
|
|
|
// db is non-existent | pebble default | specified type
|
2023-04-21 19:24:18 +03:00
|
|
|
// db is existent | from db | specified type (if compatible)
|
2023-02-09 11:48:34 +03:00
|
|
|
func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
|
2023-04-21 19:24:18 +03:00
|
|
|
// Reject any unsupported database type
|
|
|
|
if len(o.Type) != 0 && o.Type != dbLeveldb && o.Type != dbPebble {
|
|
|
|
return nil, fmt.Errorf("unknown db.engine %v", o.Type)
|
|
|
|
}
|
|
|
|
// Retrieve any pre-existing database's type and use that or the requested one
|
|
|
|
// as long as there's no conflict between the two types
|
2023-08-23 16:42:37 +03:00
|
|
|
existingDb := PreexistingDatabase(o.Directory)
|
2023-02-09 11:48:34 +03:00
|
|
|
if len(existingDb) != 0 && len(o.Type) != 0 && o.Type != existingDb {
|
|
|
|
return nil, fmt.Errorf("db.engine choice was %v but found pre-existing %v database in specified data directory", o.Type, existingDb)
|
|
|
|
}
|
|
|
|
if o.Type == dbPebble || existingDb == dbPebble {
|
2023-10-13 22:50:20 +03:00
|
|
|
log.Info("Using pebble as the backing database")
|
2023-10-13 22:50:20 +03:00
|
|
|
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
|
2023-02-09 11:48:34 +03:00
|
|
|
}
|
2023-04-21 19:24:18 +03:00
|
|
|
if o.Type == dbLeveldb || existingDb == dbLeveldb {
|
|
|
|
log.Info("Using leveldb as the backing database")
|
|
|
|
return NewLevelDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly)
|
|
|
|
}
|
2024-03-21 06:41:21 +03:00
|
|
|
// No pre-existing database, no user-requested one either. Default to Pebble.
|
|
|
|
log.Info("Defaulting to pebble as the backing database")
|
|
|
|
return NewPebbleDBDatabase(o.Directory, o.Cache, o.Handles, o.Namespace, o.ReadOnly, o.Ephemeral)
|
2023-02-09 11:48:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens both a disk-based key-value database such as leveldb or pebble, but also
|
|
|
|
// integrates it with a freezer database -- if the AncientDir option has been
|
|
|
|
// set on the provided OpenOptions.
|
|
|
|
// The passed o.AncientDir indicates the path of root ancient directory where
|
|
|
|
// the chain freezer can be opened.
|
|
|
|
func Open(o OpenOptions) (ethdb.Database, error) {
|
|
|
|
kvdb, err := openKeyValueDatabase(o)
|
2019-03-08 16:56:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-12 10:36:26 +03:00
|
|
|
if ReadAncientType(kvdb) == PruneFreezerType {
|
|
|
|
if !o.PruneAncientData {
|
2024-06-04 10:47:16 +03:00
|
|
|
log.Warn("NOTICE: You're opening a pruned disk db!")
|
2024-01-12 10:36:26 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-09 11:48:34 +03:00
|
|
|
if len(o.AncientsDirectory) == 0 {
|
|
|
|
return kvdb, nil
|
|
|
|
}
|
2024-05-29 10:11:52 +03:00
|
|
|
frdb, err := NewDatabaseWithFreezer(kvdb, o.AncientsDirectory, o.Namespace, o.ReadOnly, o.DisableFreeze, o.IsLastOffset, o.PruneAncientData, o.MultiDataBase)
|
2019-03-08 16:56:20 +03:00
|
|
|
if err != nil {
|
|
|
|
kvdb.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return frdb, nil
|
|
|
|
}
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
|
2020-09-17 11:23:56 +03:00
|
|
|
type counter uint64
|
|
|
|
|
|
|
|
func (c counter) String() string {
|
|
|
|
return fmt.Sprintf("%d", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c counter) Percentage(current uint64) string {
|
|
|
|
return fmt.Sprintf("%d", current*100/uint64(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
// stat stores sizes and count for a parameter
|
|
|
|
type stat struct {
|
|
|
|
size common.StorageSize
|
|
|
|
count counter
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add size to the stat and increase the counter by 1
|
|
|
|
func (s *stat) Add(size common.StorageSize) {
|
|
|
|
s.size += size
|
|
|
|
s.count++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stat) Size() string {
|
|
|
|
return s.size.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stat) Count() string {
|
|
|
|
return s.count.String()
|
|
|
|
}
|
2024-01-12 10:36:26 +03:00
|
|
|
|
2022-07-05 06:14:21 +03:00
|
|
|
func AncientInspect(db ethdb.Database) error {
|
|
|
|
offset := counter(ReadOffSetOfCurrentAncientFreezer(db))
|
|
|
|
// Get number of ancient rows inside the freezer.
|
|
|
|
ancients := counter(0)
|
2024-04-18 10:12:05 +03:00
|
|
|
if count, err := db.BlockStore().ItemAmountInAncient(); err != nil {
|
2022-07-05 06:14:21 +03:00
|
|
|
log.Error("failed to get the items amount in ancientDB", "err", err)
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
ancients = counter(count)
|
|
|
|
}
|
|
|
|
var endNumber counter
|
|
|
|
if offset+ancients <= 0 {
|
|
|
|
endNumber = 0
|
|
|
|
} else {
|
|
|
|
endNumber = offset + ancients - 1
|
|
|
|
}
|
|
|
|
stats := [][]string{
|
|
|
|
{"Offset/StartBlockNumber", "Offset/StartBlockNumber of ancientDB", offset.String()},
|
|
|
|
{"Amount of remained items in AncientStore", "Remaining items of ancientDB", ancients.String()},
|
|
|
|
{"The last BlockNumber within ancientDB", "The last BlockNumber", endNumber.String()},
|
|
|
|
}
|
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
table.SetHeader([]string{"Database", "Category", "Items"})
|
|
|
|
table.SetFooter([]string{"", "AncientStore information", ""})
|
|
|
|
table.AppendBulk(stats)
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-17 11:23:56 +03:00
|
|
|
|
2023-09-25 06:16:36 +03:00
|
|
|
func PruneHashTrieNodeInDataBase(db ethdb.Database) error {
|
|
|
|
it := db.NewIterator([]byte{}, []byte{})
|
|
|
|
defer it.Release()
|
|
|
|
|
|
|
|
total_num := 0
|
|
|
|
for it.Next() {
|
|
|
|
var key = it.Key()
|
|
|
|
switch {
|
|
|
|
case IsLegacyTrieNode(key, it.Value()):
|
|
|
|
db.Delete(key)
|
|
|
|
total_num++
|
|
|
|
if total_num%100000 == 0 {
|
2023-09-27 10:01:49 +03:00
|
|
|
log.Info("Pruning hash-base state trie nodes", "Complete progress: ", total_num)
|
2023-09-25 06:16:36 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-09-27 10:01:49 +03:00
|
|
|
log.Info("Pruning hash-base state trie nodes", "Complete progress", total_num)
|
2023-09-25 06:16:36 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-04-18 10:12:05 +03:00
|
|
|
type DataType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
StateDataType DataType = iota
|
|
|
|
BlockDataType
|
|
|
|
ChainDataType
|
|
|
|
Unknown
|
|
|
|
)
|
|
|
|
|
|
|
|
func DataTypeByKey(key []byte) DataType {
|
|
|
|
switch {
|
|
|
|
// state
|
|
|
|
case IsLegacyTrieNode(key, key),
|
|
|
|
bytes.HasPrefix(key, stateIDPrefix) && len(key) == len(stateIDPrefix)+common.HashLength,
|
|
|
|
IsAccountTrieNode(key),
|
|
|
|
IsStorageTrieNode(key):
|
|
|
|
return StateDataType
|
|
|
|
|
|
|
|
// block
|
|
|
|
case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength),
|
|
|
|
bytes.HasPrefix(key, blockBodyPrefix) && len(key) == (len(blockBodyPrefix)+8+common.HashLength),
|
|
|
|
bytes.HasPrefix(key, blockReceiptsPrefix) && len(key) == (len(blockReceiptsPrefix)+8+common.HashLength),
|
|
|
|
bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerTDSuffix),
|
|
|
|
bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix),
|
|
|
|
bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
|
|
|
|
return BlockDataType
|
|
|
|
default:
|
|
|
|
for _, meta := range [][]byte{
|
|
|
|
fastTrieProgressKey, persistentStateIDKey, trieJournalKey, snapSyncStatusFlagKey} {
|
|
|
|
if bytes.Equal(key, meta) {
|
|
|
|
return StateDataType
|
|
|
|
}
|
|
|
|
}
|
2024-06-18 10:20:23 +03:00
|
|
|
for _, meta := range [][]byte{headHeaderKey, headFinalizedBlockKey, headBlockKey, headFastBlockKey} {
|
2024-04-18 10:12:05 +03:00
|
|
|
if bytes.Equal(key, meta) {
|
|
|
|
return BlockDataType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ChainDataType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
// InspectDatabase traverses the entire database and checks the size
|
|
|
|
// of all different categories of data.
|
2021-02-23 13:27:32 +03:00
|
|
|
func InspectDatabase(db ethdb.Database, keyPrefix, keyStart []byte) error {
|
|
|
|
it := db.NewIterator(keyPrefix, keyStart)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
defer it.Release()
|
|
|
|
|
2024-03-08 11:07:29 +03:00
|
|
|
var trieIter ethdb.Iterator
|
2024-04-18 10:12:05 +03:00
|
|
|
var blockIter ethdb.Iterator
|
2024-03-08 11:07:29 +03:00
|
|
|
if db.StateStore() != nil {
|
|
|
|
trieIter = db.StateStore().NewIterator(keyPrefix, nil)
|
|
|
|
defer trieIter.Release()
|
|
|
|
}
|
2024-05-27 10:10:59 +03:00
|
|
|
if db.HasSeparateBlockStore() {
|
2024-04-18 10:12:05 +03:00
|
|
|
blockIter = db.BlockStore().NewIterator(keyPrefix, nil)
|
|
|
|
defer blockIter.Release()
|
|
|
|
}
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
var (
|
|
|
|
count int64
|
|
|
|
start = time.Now()
|
|
|
|
logged = time.Now()
|
|
|
|
|
|
|
|
// Key-value store statistics
|
2020-09-17 11:23:56 +03:00
|
|
|
headers stat
|
|
|
|
bodies stat
|
|
|
|
receipts stat
|
|
|
|
tds stat
|
|
|
|
numHashPairings stat
|
|
|
|
hashNumPairings stat
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
legacyTries stat
|
|
|
|
stateLookups stat
|
|
|
|
accountTries stat
|
|
|
|
storageTries stat
|
2020-09-17 11:23:56 +03:00
|
|
|
codes stat
|
|
|
|
txLookups stat
|
|
|
|
accountSnaps stat
|
|
|
|
storageSnaps stat
|
|
|
|
preimages stat
|
|
|
|
bloomBits stat
|
|
|
|
cliqueSnaps stat
|
2021-04-16 07:45:26 +03:00
|
|
|
parliaSnaps stat
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
|
|
|
|
// Les statistic
|
2020-09-17 11:23:56 +03:00
|
|
|
chtTrieNodes stat
|
|
|
|
bloomTrieNodes stat
|
2019-05-15 14:33:33 +03:00
|
|
|
|
|
|
|
// Meta- and unaccounted data
|
2021-06-08 11:39:24 +03:00
|
|
|
metadata stat
|
|
|
|
unaccounted stat
|
2020-09-17 11:23:56 +03:00
|
|
|
|
|
|
|
// Totals
|
|
|
|
total common.StorageSize
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
)
|
|
|
|
// Inspect key-value database first.
|
|
|
|
for it.Next() {
|
|
|
|
var (
|
|
|
|
key = it.Key()
|
|
|
|
size = common.StorageSize(len(key) + len(it.Value()))
|
|
|
|
)
|
|
|
|
total += size
|
|
|
|
switch {
|
|
|
|
case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
headers.Add(size)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
case bytes.HasPrefix(key, blockBodyPrefix) && len(key) == (len(blockBodyPrefix)+8+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
bodies.Add(size)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
case bytes.HasPrefix(key, blockReceiptsPrefix) && len(key) == (len(blockReceiptsPrefix)+8+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
receipts.Add(size)
|
2024-03-08 11:07:29 +03:00
|
|
|
case IsLegacyTrieNode(key, it.Value()):
|
|
|
|
legacyTries.Add(size)
|
2020-09-17 11:23:56 +03:00
|
|
|
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerTDSuffix):
|
|
|
|
tds.Add(size)
|
|
|
|
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix):
|
|
|
|
numHashPairings.Add(size)
|
|
|
|
case bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
|
|
|
|
hashNumPairings.Add(size)
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
case bytes.HasPrefix(key, stateIDPrefix) && len(key) == len(stateIDPrefix)+common.HashLength:
|
|
|
|
stateLookups.Add(size)
|
|
|
|
case IsAccountTrieNode(key):
|
|
|
|
accountTries.Add(size)
|
|
|
|
case IsStorageTrieNode(key):
|
|
|
|
storageTries.Add(size)
|
all: bloom-filter based pruning mechanism (#21724)
* cmd, core, tests: initial state pruner
core: fix db inspector
cmd/geth: add verify-state
cmd/geth: add verification tool
core/rawdb: implement flatdb
cmd, core: fix rebase
core/state: use new contract code layout
core/state/pruner: avoid deleting genesis state
cmd/geth: add helper function
core, cmd: fix extract genesis
core: minor fixes
contracts: remove useless
core/state/snapshot: plugin stacktrie
core: polish
core/state/snapshot: iterate storage concurrently
core/state/snapshot: fix iteration
core: add comments
core/state/snapshot: polish code
core/state: polish
core/state/snapshot: rebase
core/rawdb: add comments
core/rawdb: fix tests
core/rawdb: improve tests
core/state/snapshot: fix concurrent iteration
core/state: run pruning during the recovery
core, trie: implement martin's idea
core, eth: delete flatdb and polish pruner
trie: fix import
core/state/pruner: add log
core/state/pruner: fix issues
core/state/pruner: don't read back
core/state/pruner: fix contract code write
core/state/pruner: check root node presence
cmd, core: polish log
core/state: use HEAD-127 as the target
core/state/snapshot: improve tests
cmd/geth: fix verification tool
cmd/geth: use HEAD as the verification default target
all: replace the bloomfilter with martin's fork
cmd, core: polish code
core, cmd: forcibly delete state root
core/state/pruner: add hash64
core/state/pruner: fix blacklist
core/state: remove blacklist
cmd, core: delete trie clean cache before pruning
cmd, core: fix lint
cmd, core: fix rebase
core/state: fix the special case for clique networks
core/state/snapshot: remove useless code
core/state/pruner: capping the snapshot after pruning
cmd, core, eth: fixes
core/rawdb: update db inspector
cmd/geth: polish code
core/state/pruner: fsync bloom filter
cmd, core: print warning log
core/state/pruner: adjust the parameters for bloom filter
cmd, core: create the bloom filter by size
core: polish
core/state/pruner: sanitize invalid bloomfilter size
cmd: address comments
cmd/geth: address comments
cmd/geth: address comment
core/state/pruner: address comments
core/state/pruner: rename homedir to datadir
cmd, core: address comments
core/state/pruner: address comment
core/state: address comments
core, cmd, tests: address comments
core: address comments
core/state/pruner: release the iterator after each commit
core/state/pruner: improve pruner
cmd, core: adjust bloom paramters
core/state/pruner: fix lint
core/state/pruner: fix tests
core: fix rebase
core/state/pruner: remove atomic rename
core/state/pruner: address comments
all: run go mod tidy
core/state/pruner: avoid false-positive for the middle state roots
core/state/pruner: add checks for middle roots
cmd/geth: replace crit with error
* core/state/pruner: fix lint
* core: drop legacy bloom filter
* core/state/snapshot: improve pruner
* core/state/snapshot: polish concurrent logs to report ETA vs. hashes
* core/state/pruner: add progress report for pruning and compaction too
* core: fix snapshot test API
* core/state: fix some pruning logs
* core/state/pruner: support recovering from bloom flush fail
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-02-08 14:16:30 +03:00
|
|
|
case bytes.HasPrefix(key, CodePrefix) && len(key) == len(CodePrefix)+common.HashLength:
|
2020-09-17 11:23:56 +03:00
|
|
|
codes.Add(size)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
case bytes.HasPrefix(key, txLookupPrefix) && len(key) == (len(txLookupPrefix)+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
txLookups.Add(size)
|
2019-11-26 10:48:29 +03:00
|
|
|
case bytes.HasPrefix(key, SnapshotAccountPrefix) && len(key) == (len(SnapshotAccountPrefix)+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
accountSnaps.Add(size)
|
2019-11-26 10:48:29 +03:00
|
|
|
case bytes.HasPrefix(key, SnapshotStoragePrefix) && len(key) == (len(SnapshotStoragePrefix)+2*common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
storageSnaps.Add(size)
|
2021-11-02 13:31:45 +03:00
|
|
|
case bytes.HasPrefix(key, PreimagePrefix) && len(key) == (len(PreimagePrefix)+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
preimages.Add(size)
|
2021-06-08 11:39:24 +03:00
|
|
|
case bytes.HasPrefix(key, configPrefix) && len(key) == (len(configPrefix)+common.HashLength):
|
|
|
|
metadata.Add(size)
|
2022-03-22 12:53:22 +03:00
|
|
|
case bytes.HasPrefix(key, genesisPrefix) && len(key) == (len(genesisPrefix)+common.HashLength):
|
|
|
|
metadata.Add(size)
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
case bytes.HasPrefix(key, bloomBitsPrefix) && len(key) == (len(bloomBitsPrefix)+10+common.HashLength):
|
2020-09-17 11:23:56 +03:00
|
|
|
bloomBits.Add(size)
|
all: bloom-filter based pruning mechanism (#21724)
* cmd, core, tests: initial state pruner
core: fix db inspector
cmd/geth: add verify-state
cmd/geth: add verification tool
core/rawdb: implement flatdb
cmd, core: fix rebase
core/state: use new contract code layout
core/state/pruner: avoid deleting genesis state
cmd/geth: add helper function
core, cmd: fix extract genesis
core: minor fixes
contracts: remove useless
core/state/snapshot: plugin stacktrie
core: polish
core/state/snapshot: iterate storage concurrently
core/state/snapshot: fix iteration
core: add comments
core/state/snapshot: polish code
core/state: polish
core/state/snapshot: rebase
core/rawdb: add comments
core/rawdb: fix tests
core/rawdb: improve tests
core/state/snapshot: fix concurrent iteration
core/state: run pruning during the recovery
core, trie: implement martin's idea
core, eth: delete flatdb and polish pruner
trie: fix import
core/state/pruner: add log
core/state/pruner: fix issues
core/state/pruner: don't read back
core/state/pruner: fix contract code write
core/state/pruner: check root node presence
cmd, core: polish log
core/state: use HEAD-127 as the target
core/state/snapshot: improve tests
cmd/geth: fix verification tool
cmd/geth: use HEAD as the verification default target
all: replace the bloomfilter with martin's fork
cmd, core: polish code
core, cmd: forcibly delete state root
core/state/pruner: add hash64
core/state/pruner: fix blacklist
core/state: remove blacklist
cmd, core: delete trie clean cache before pruning
cmd, core: fix lint
cmd, core: fix rebase
core/state: fix the special case for clique networks
core/state/snapshot: remove useless code
core/state/pruner: capping the snapshot after pruning
cmd, core, eth: fixes
core/rawdb: update db inspector
cmd/geth: polish code
core/state/pruner: fsync bloom filter
cmd, core: print warning log
core/state/pruner: adjust the parameters for bloom filter
cmd, core: create the bloom filter by size
core: polish
core/state/pruner: sanitize invalid bloomfilter size
cmd: address comments
cmd/geth: address comments
cmd/geth: address comment
core/state/pruner: address comments
core/state/pruner: rename homedir to datadir
cmd, core: address comments
core/state/pruner: address comment
core/state: address comments
core, cmd, tests: address comments
core: address comments
core/state/pruner: release the iterator after each commit
core/state/pruner: improve pruner
cmd, core: adjust bloom paramters
core/state/pruner: fix lint
core/state/pruner: fix tests
core: fix rebase
core/state/pruner: remove atomic rename
core/state/pruner: address comments
all: run go mod tidy
core/state/pruner: avoid false-positive for the middle state roots
core/state/pruner: add checks for middle roots
cmd/geth: replace crit with error
* core/state/pruner: fix lint
* core: drop legacy bloom filter
* core/state/snapshot: improve pruner
* core/state/snapshot: polish concurrent logs to report ETA vs. hashes
* core/state/pruner: add progress report for pruning and compaction too
* core: fix snapshot test API
* core/state: fix some pruning logs
* core/state/pruner: support recovering from bloom flush fail
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-02-08 14:16:30 +03:00
|
|
|
case bytes.HasPrefix(key, BloomBitsIndexPrefix):
|
|
|
|
bloomBits.Add(size)
|
2022-10-19 10:53:09 +03:00
|
|
|
case bytes.HasPrefix(key, CliqueSnapshotPrefix) && len(key) == 7+common.HashLength:
|
2020-09-17 11:23:56 +03:00
|
|
|
cliqueSnaps.Add(size)
|
2023-08-23 12:46:08 +03:00
|
|
|
case bytes.HasPrefix(key, ParliaSnapshotPrefix) && len(key) == 7+common.HashLength:
|
2021-04-16 07:45:26 +03:00
|
|
|
parliaSnaps.Add(size)
|
2022-10-19 10:53:09 +03:00
|
|
|
case bytes.HasPrefix(key, ChtTablePrefix) ||
|
|
|
|
bytes.HasPrefix(key, ChtIndexTablePrefix) ||
|
|
|
|
bytes.HasPrefix(key, ChtPrefix): // Canonical hash trie
|
2020-09-17 11:23:56 +03:00
|
|
|
chtTrieNodes.Add(size)
|
2022-10-19 10:53:09 +03:00
|
|
|
case bytes.HasPrefix(key, BloomTrieTablePrefix) ||
|
|
|
|
bytes.HasPrefix(key, BloomTrieIndexPrefix) ||
|
|
|
|
bytes.HasPrefix(key, BloomTriePrefix): // Bloomtrie sub
|
2020-09-17 11:23:56 +03:00
|
|
|
bloomTrieNodes.Add(size)
|
2019-05-15 14:33:33 +03:00
|
|
|
default:
|
|
|
|
var accounted bool
|
all: bloom-filter based pruning mechanism (#21724)
* cmd, core, tests: initial state pruner
core: fix db inspector
cmd/geth: add verify-state
cmd/geth: add verification tool
core/rawdb: implement flatdb
cmd, core: fix rebase
core/state: use new contract code layout
core/state/pruner: avoid deleting genesis state
cmd/geth: add helper function
core, cmd: fix extract genesis
core: minor fixes
contracts: remove useless
core/state/snapshot: plugin stacktrie
core: polish
core/state/snapshot: iterate storage concurrently
core/state/snapshot: fix iteration
core: add comments
core/state/snapshot: polish code
core/state: polish
core/state/snapshot: rebase
core/rawdb: add comments
core/rawdb: fix tests
core/rawdb: improve tests
core/state/snapshot: fix concurrent iteration
core/state: run pruning during the recovery
core, trie: implement martin's idea
core, eth: delete flatdb and polish pruner
trie: fix import
core/state/pruner: add log
core/state/pruner: fix issues
core/state/pruner: don't read back
core/state/pruner: fix contract code write
core/state/pruner: check root node presence
cmd, core: polish log
core/state: use HEAD-127 as the target
core/state/snapshot: improve tests
cmd/geth: fix verification tool
cmd/geth: use HEAD as the verification default target
all: replace the bloomfilter with martin's fork
cmd, core: polish code
core, cmd: forcibly delete state root
core/state/pruner: add hash64
core/state/pruner: fix blacklist
core/state: remove blacklist
cmd, core: delete trie clean cache before pruning
cmd, core: fix lint
cmd, core: fix rebase
core/state: fix the special case for clique networks
core/state/snapshot: remove useless code
core/state/pruner: capping the snapshot after pruning
cmd, core, eth: fixes
core/rawdb: update db inspector
cmd/geth: polish code
core/state/pruner: fsync bloom filter
cmd, core: print warning log
core/state/pruner: adjust the parameters for bloom filter
cmd, core: create the bloom filter by size
core: polish
core/state/pruner: sanitize invalid bloomfilter size
cmd: address comments
cmd/geth: address comments
cmd/geth: address comment
core/state/pruner: address comments
core/state/pruner: rename homedir to datadir
cmd, core: address comments
core/state/pruner: address comment
core/state: address comments
core, cmd, tests: address comments
core: address comments
core/state/pruner: release the iterator after each commit
core/state/pruner: improve pruner
cmd, core: adjust bloom paramters
core/state/pruner: fix lint
core/state/pruner: fix tests
core: fix rebase
core/state/pruner: remove atomic rename
core/state/pruner: address comments
all: run go mod tidy
core/state/pruner: avoid false-positive for the middle state roots
core/state/pruner: add checks for middle roots
cmd/geth: replace crit with error
* core/state/pruner: fix lint
* core: drop legacy bloom filter
* core/state/snapshot: improve pruner
* core/state/snapshot: polish concurrent logs to report ETA vs. hashes
* core/state/pruner: add progress report for pruning and compaction too
* core: fix snapshot test API
* core/state: fix some pruning logs
* core/state/pruner: support recovering from bloom flush fail
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-02-08 14:16:30 +03:00
|
|
|
for _, meta := range [][]byte{
|
2023-08-23 12:46:08 +03:00
|
|
|
databaseVersionKey, headHeaderKey, headBlockKey, headFastBlockKey,
|
2022-05-18 17:30:42 +03:00
|
|
|
lastPivotKey, fastTrieProgressKey, snapshotDisabledKey, SnapshotRootKey, snapshotJournalKey,
|
2021-04-29 17:33:45 +03:00
|
|
|
snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey,
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey,
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 10:00:53 +03:00
|
|
|
persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey,
|
all: bloom-filter based pruning mechanism (#21724)
* cmd, core, tests: initial state pruner
core: fix db inspector
cmd/geth: add verify-state
cmd/geth: add verification tool
core/rawdb: implement flatdb
cmd, core: fix rebase
core/state: use new contract code layout
core/state/pruner: avoid deleting genesis state
cmd/geth: add helper function
core, cmd: fix extract genesis
core: minor fixes
contracts: remove useless
core/state/snapshot: plugin stacktrie
core: polish
core/state/snapshot: iterate storage concurrently
core/state/snapshot: fix iteration
core: add comments
core/state/snapshot: polish code
core/state: polish
core/state/snapshot: rebase
core/rawdb: add comments
core/rawdb: fix tests
core/rawdb: improve tests
core/state/snapshot: fix concurrent iteration
core/state: run pruning during the recovery
core, trie: implement martin's idea
core, eth: delete flatdb and polish pruner
trie: fix import
core/state/pruner: add log
core/state/pruner: fix issues
core/state/pruner: don't read back
core/state/pruner: fix contract code write
core/state/pruner: check root node presence
cmd, core: polish log
core/state: use HEAD-127 as the target
core/state/snapshot: improve tests
cmd/geth: fix verification tool
cmd/geth: use HEAD as the verification default target
all: replace the bloomfilter with martin's fork
cmd, core: polish code
core, cmd: forcibly delete state root
core/state/pruner: add hash64
core/state/pruner: fix blacklist
core/state: remove blacklist
cmd, core: delete trie clean cache before pruning
cmd, core: fix lint
cmd, core: fix rebase
core/state: fix the special case for clique networks
core/state/snapshot: remove useless code
core/state/pruner: capping the snapshot after pruning
cmd, core, eth: fixes
core/rawdb: update db inspector
cmd/geth: polish code
core/state/pruner: fsync bloom filter
cmd, core: print warning log
core/state/pruner: adjust the parameters for bloom filter
cmd, core: create the bloom filter by size
core: polish
core/state/pruner: sanitize invalid bloomfilter size
cmd: address comments
cmd/geth: address comments
cmd/geth: address comment
core/state/pruner: address comments
core/state/pruner: rename homedir to datadir
cmd, core: address comments
core/state/pruner: address comment
core/state: address comments
core, cmd, tests: address comments
core: address comments
core/state/pruner: release the iterator after each commit
core/state/pruner: improve pruner
cmd, core: adjust bloom paramters
core/state/pruner: fix lint
core/state/pruner: fix tests
core: fix rebase
core/state/pruner: remove atomic rename
core/state/pruner: address comments
all: run go mod tidy
core/state/pruner: avoid false-positive for the middle state roots
core/state/pruner: add checks for middle roots
cmd/geth: replace crit with error
* core/state/pruner: fix lint
* core: drop legacy bloom filter
* core/state/snapshot: improve pruner
* core/state/snapshot: polish concurrent logs to report ETA vs. hashes
* core/state/pruner: add progress report for pruning and compaction too
* core: fix snapshot test API
* core/state: fix some pruning logs
* core/state/pruner: support recovering from bloom flush fail
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-02-08 14:16:30 +03:00
|
|
|
} {
|
2019-05-15 14:33:33 +03:00
|
|
|
if bytes.Equal(key, meta) {
|
2020-09-17 11:23:56 +03:00
|
|
|
metadata.Add(size)
|
2019-05-15 14:33:33 +03:00
|
|
|
accounted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !accounted {
|
2020-09-17 11:23:56 +03:00
|
|
|
unaccounted.Add(size)
|
2019-05-15 14:33:33 +03:00
|
|
|
}
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
}
|
2020-09-17 11:23:56 +03:00
|
|
|
count++
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
if count%1000 == 0 && time.Since(logged) > 8*time.Second {
|
|
|
|
log.Info("Inspecting database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
}
|
2024-03-08 11:07:29 +03:00
|
|
|
// inspect separate trie db
|
|
|
|
if trieIter != nil {
|
|
|
|
count = 0
|
|
|
|
logged = time.Now()
|
|
|
|
for trieIter.Next() {
|
|
|
|
var (
|
|
|
|
key = trieIter.Key()
|
|
|
|
value = trieIter.Value()
|
|
|
|
size = common.StorageSize(len(key) + len(value))
|
|
|
|
)
|
2024-04-18 10:12:05 +03:00
|
|
|
total += size
|
2024-03-08 11:07:29 +03:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case IsLegacyTrieNode(key, value):
|
|
|
|
legacyTries.Add(size)
|
|
|
|
case bytes.HasPrefix(key, stateIDPrefix) && len(key) == len(stateIDPrefix)+common.HashLength:
|
|
|
|
stateLookups.Add(size)
|
|
|
|
case IsAccountTrieNode(key):
|
|
|
|
accountTries.Add(size)
|
|
|
|
case IsStorageTrieNode(key):
|
|
|
|
storageTries.Add(size)
|
|
|
|
default:
|
|
|
|
var accounted bool
|
|
|
|
for _, meta := range [][]byte{
|
2024-04-18 10:12:05 +03:00
|
|
|
fastTrieProgressKey, persistentStateIDKey, trieJournalKey, snapSyncStatusFlagKey} {
|
2024-03-08 11:07:29 +03:00
|
|
|
if bytes.Equal(key, meta) {
|
|
|
|
metadata.Add(size)
|
2024-04-18 10:12:05 +03:00
|
|
|
accounted = true
|
2024-03-08 11:07:29 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !accounted {
|
|
|
|
unaccounted.Add(size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
if count%1000 == 0 && time.Since(logged) > 8*time.Second {
|
|
|
|
log.Info("Inspecting separate state database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
}
|
2024-04-18 10:12:05 +03:00
|
|
|
log.Info("Inspecting separate state database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
}
|
|
|
|
// inspect separate block db
|
|
|
|
if blockIter != nil {
|
|
|
|
count = 0
|
|
|
|
logged = time.Now()
|
|
|
|
|
|
|
|
for blockIter.Next() {
|
|
|
|
var (
|
|
|
|
key = blockIter.Key()
|
|
|
|
value = blockIter.Value()
|
|
|
|
size = common.StorageSize(len(key) + len(value))
|
|
|
|
)
|
|
|
|
total += size
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case bytes.HasPrefix(key, headerPrefix) && len(key) == (len(headerPrefix)+8+common.HashLength):
|
|
|
|
headers.Add(size)
|
|
|
|
case bytes.HasPrefix(key, blockBodyPrefix) && len(key) == (len(blockBodyPrefix)+8+common.HashLength):
|
|
|
|
bodies.Add(size)
|
|
|
|
case bytes.HasPrefix(key, blockReceiptsPrefix) && len(key) == (len(blockReceiptsPrefix)+8+common.HashLength):
|
|
|
|
receipts.Add(size)
|
|
|
|
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerTDSuffix):
|
|
|
|
tds.Add(size)
|
|
|
|
case bytes.HasPrefix(key, headerPrefix) && bytes.HasSuffix(key, headerHashSuffix):
|
|
|
|
numHashPairings.Add(size)
|
|
|
|
case bytes.HasPrefix(key, headerNumberPrefix) && len(key) == (len(headerNumberPrefix)+common.HashLength):
|
|
|
|
hashNumPairings.Add(size)
|
|
|
|
default:
|
|
|
|
var accounted bool
|
2024-06-18 10:20:23 +03:00
|
|
|
for _, meta := range [][]byte{headHeaderKey, headFinalizedBlockKey, headBlockKey, headFastBlockKey} {
|
2024-04-18 10:12:05 +03:00
|
|
|
if bytes.Equal(key, meta) {
|
|
|
|
metadata.Add(size)
|
|
|
|
accounted = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !accounted {
|
|
|
|
unaccounted.Add(size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
if count%1000 == 0 && time.Since(logged) > 8*time.Second {
|
|
|
|
log.Info("Inspecting separate block database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Info("Inspecting separate block database", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
2024-03-08 11:07:29 +03:00
|
|
|
}
|
2022-10-28 11:23:49 +03:00
|
|
|
// Display the database statistic of key-value store.
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
stats := [][]string{
|
2020-09-17 11:23:56 +03:00
|
|
|
{"Key-Value store", "Headers", headers.Size(), headers.Count()},
|
|
|
|
{"Key-Value store", "Bodies", bodies.Size(), bodies.Count()},
|
|
|
|
{"Key-Value store", "Receipt lists", receipts.Size(), receipts.Count()},
|
|
|
|
{"Key-Value store", "Difficulties", tds.Size(), tds.Count()},
|
|
|
|
{"Key-Value store", "Block number->hash", numHashPairings.Size(), numHashPairings.Count()},
|
|
|
|
{"Key-Value store", "Block hash->number", hashNumPairings.Size(), hashNumPairings.Count()},
|
|
|
|
{"Key-Value store", "Transaction index", txLookups.Size(), txLookups.Count()},
|
|
|
|
{"Key-Value store", "Bloombit index", bloomBits.Size(), bloomBits.Count()},
|
|
|
|
{"Key-Value store", "Contract codes", codes.Size(), codes.Count()},
|
all: activate pbss as experimental feature from eth (#26274)
* all: activate pbss
* core/rawdb: fix compilation error
* cma, core, eth, les, trie: address comments
* cmd, core, eth, trie: polish code
* core, cmd, eth: address comments
* cmd, core, eth, les, light, tests: address comment
* cmd/utils: shorten log message
* trie/triedb/pathdb: limit node buffer size to 1gb
* cmd/utils: fix opening non-existing db
* cmd/utils: rename flag name
* cmd, core: group chain history flags and fix tests
* core, eth, trie: fix memory leak in snapshot generation
* cmd, eth, internal: deprecate flags
* all: enable state tests for pathdb, fixes
* cmd, core: polish code
* trie/triedb/pathdb: limit the node buffer size to 256mb
---------
Co-authored-by: Martin Holst Swende <martin@swende.se>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2023-08-10 22:21:36 +03:00
|
|
|
{"Key-Value store", "Hash trie nodes", legacyTries.Size(), legacyTries.Count()},
|
|
|
|
{"Key-Value store", "Path trie state lookups", stateLookups.Size(), stateLookups.Count()},
|
|
|
|
{"Key-Value store", "Path trie account nodes", accountTries.Size(), accountTries.Count()},
|
|
|
|
{"Key-Value store", "Path trie storage nodes", storageTries.Size(), storageTries.Count()},
|
2020-09-17 11:23:56 +03:00
|
|
|
{"Key-Value store", "Trie preimages", preimages.Size(), preimages.Count()},
|
|
|
|
{"Key-Value store", "Account snapshot", accountSnaps.Size(), accountSnaps.Count()},
|
|
|
|
{"Key-Value store", "Storage snapshot", storageSnaps.Size(), storageSnaps.Count()},
|
|
|
|
{"Key-Value store", "Clique snapshots", cliqueSnaps.Size(), cliqueSnaps.Count()},
|
2021-04-16 07:45:26 +03:00
|
|
|
{"Key-Value store", "Parlia snapshots", parliaSnaps.Size(), parliaSnaps.Count()},
|
2020-09-17 11:23:56 +03:00
|
|
|
{"Key-Value store", "Singleton metadata", metadata.Size(), metadata.Count()},
|
|
|
|
{"Light client", "CHT trie nodes", chtTrieNodes.Size(), chtTrieNodes.Count()},
|
|
|
|
{"Light client", "Bloom trie nodes", bloomTrieNodes.Size(), bloomTrieNodes.Count()},
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
}
|
2022-10-28 11:23:49 +03:00
|
|
|
// Inspect all registered append-only file store then.
|
2024-04-18 10:12:05 +03:00
|
|
|
ancients, err := inspectFreezers(db.BlockStore())
|
2022-10-28 11:23:49 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ancient := range ancients {
|
|
|
|
for _, table := range ancient.sizes {
|
|
|
|
stats = append(stats, []string{
|
|
|
|
fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
|
|
|
|
strings.Title(table.name),
|
|
|
|
table.size.String(),
|
|
|
|
fmt.Sprintf("%d", ancient.count()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
total += ancient.size()
|
|
|
|
}
|
2024-03-08 11:07:29 +03:00
|
|
|
|
|
|
|
// inspect ancient state in separate trie db if exist
|
|
|
|
if trieIter != nil {
|
|
|
|
stateAncients, err := inspectFreezers(db.StateStore())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ancient := range stateAncients {
|
|
|
|
for _, table := range ancient.sizes {
|
|
|
|
if ancient.name == "chain" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
stats = append(stats, []string{
|
|
|
|
fmt.Sprintf("Ancient store (%s)", strings.Title(ancient.name)),
|
|
|
|
strings.Title(table.name),
|
|
|
|
table.size.String(),
|
|
|
|
fmt.Sprintf("%d", ancient.count()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
total += ancient.size()
|
|
|
|
}
|
|
|
|
}
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
table := tablewriter.NewWriter(os.Stdout)
|
2020-09-17 11:23:56 +03:00
|
|
|
table.SetHeader([]string{"Database", "Category", "Size", "Items"})
|
|
|
|
table.SetFooter([]string{"", "Total", total.String(), " "})
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
table.AppendBulk(stats)
|
|
|
|
table.Render()
|
2019-05-15 14:33:33 +03:00
|
|
|
|
2020-09-17 11:23:56 +03:00
|
|
|
if unaccounted.size > 0 {
|
|
|
|
log.Error("Database contains unaccounted data", "size", unaccounted.size, "count", unaccounted.count)
|
2019-05-15 14:33:33 +03:00
|
|
|
}
|
core, cmd, vendor: fixes and database inspection tool (#15)
* core, eth: some fixes for freezer
* vendor, core/rawdb, cmd/geth: add db inspector
* core, cmd/utils: check ancient store path forceily
* cmd/geth, common, core/rawdb: a few fixes
* cmd/geth: support windows file rename and fix rename error
* core: support ancient plugin
* core, cmd: streaming file copy
* cmd, consensus, core, tests: keep genesis in leveldb
* core: write txlookup during ancient init
* core: bump database version
2019-05-14 17:07:44 +03:00
|
|
|
return nil
|
|
|
|
}
|
2023-03-08 10:39:13 +03:00
|
|
|
|
2024-03-12 07:20:40 +03:00
|
|
|
func DeleteTrieState(db ethdb.Database) error {
|
|
|
|
var (
|
|
|
|
it ethdb.Iterator
|
|
|
|
batch = db.NewBatch()
|
|
|
|
start = time.Now()
|
|
|
|
logged = time.Now()
|
|
|
|
count int64
|
|
|
|
key []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
prefixKeys := map[string]func([]byte) bool{
|
|
|
|
string(trieNodeAccountPrefix): IsAccountTrieNode,
|
|
|
|
string(trieNodeStoragePrefix): IsStorageTrieNode,
|
|
|
|
string(stateIDPrefix): func(key []byte) bool { return len(key) == len(stateIDPrefix)+common.HashLength },
|
|
|
|
}
|
|
|
|
|
|
|
|
for prefix, isValid := range prefixKeys {
|
|
|
|
it = db.NewIterator([]byte(prefix), nil)
|
|
|
|
|
|
|
|
for it.Next() {
|
|
|
|
key = it.Key()
|
|
|
|
if !isValid(key) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Delete(it.Key())
|
|
|
|
if batch.ValueSize() > ethdb.IdealBatchSize {
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
it.Release()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
if time.Since(logged) > 8*time.Second {
|
|
|
|
log.Info("Deleting trie state", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
logged = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it.Release()
|
|
|
|
}
|
|
|
|
|
|
|
|
if batch.ValueSize() > 0 {
|
|
|
|
if err := batch.Write(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
batch.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Deleted trie state", "count", count, "elapsed", common.PrettyDuration(time.Since(start)))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-08 10:39:13 +03:00
|
|
|
// printChainMetadata prints out chain metadata to stderr.
|
2024-07-16 17:37:03 +03:00
|
|
|
func printChainMetadata(db ethdb.Reader) {
|
2023-03-08 10:39:13 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "Chain metadata\n")
|
|
|
|
for _, v := range ReadChainMetadata(db) {
|
|
|
|
fmt.Fprintf(os.Stderr, " %s\n", strings.Join(v, ": "))
|
|
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "\n\n")
|
|
|
|
}
|
|
|
|
|
2023-09-29 10:52:22 +03:00
|
|
|
// ReadChainMetadata returns a set of key/value pairs that contains information
|
2023-03-08 10:39:13 +03:00
|
|
|
// about the database chain status. This can be used for diagnostic purposes
|
|
|
|
// when investigating the state of the node.
|
2024-07-16 17:37:03 +03:00
|
|
|
func ReadChainMetadata(db ethdb.Reader) [][]string {
|
2023-03-08 10:39:13 +03:00
|
|
|
pp := func(val *uint64) string {
|
|
|
|
if val == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d (%#x)", *val, *val)
|
|
|
|
}
|
|
|
|
data := [][]string{
|
|
|
|
{"databaseVersion", pp(ReadDatabaseVersion(db))},
|
|
|
|
{"headBlockHash", fmt.Sprintf("%v", ReadHeadBlockHash(db))},
|
|
|
|
{"headFastBlockHash", fmt.Sprintf("%v", ReadHeadFastBlockHash(db))},
|
|
|
|
{"headHeaderHash", fmt.Sprintf("%v", ReadHeadHeaderHash(db))},
|
|
|
|
{"lastPivotNumber", pp(ReadLastPivotNumber(db))},
|
|
|
|
{"len(snapshotSyncStatus)", fmt.Sprintf("%d bytes", len(ReadSnapshotSyncStatus(db)))},
|
|
|
|
{"snapshotDisabled", fmt.Sprintf("%v", ReadSnapshotDisabled(db))},
|
|
|
|
{"snapshotJournal", fmt.Sprintf("%d bytes", len(ReadSnapshotJournal(db)))},
|
|
|
|
{"snapshotRecoveryNumber", pp(ReadSnapshotRecoveryNumber(db))},
|
|
|
|
{"snapshotRoot", fmt.Sprintf("%v", ReadSnapshotRoot(db))},
|
|
|
|
{"txIndexTail", pp(ReadTxIndexTail(db))},
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|