Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ancient block pruning #1216

Merged
merged 60 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
c9ef24f
core/state: typo
jsvisa Feb 10, 2023
a4b592c
core/rawdb: backport from https://github.com/bnb-chain/bsc/pull/543
jsvisa Feb 17, 2023
8c35ece
eth,ethdb,node,core/state: backport from https://github.com/bnb-chain…
jsvisa Feb 17, 2023
2b8ca73
eth,core: backport from https://github.com/bnb-chain/bsc/pull/543
jsvisa Feb 17, 2023
4b96e5c
cmd: open db with freeze disabled
jsvisa Feb 17, 2023
4e25407
cli: snapshot prune-block
jsvisa Feb 17, 2023
8bd357f
fix typo
jsvisa Feb 20, 2023
b34d1a5
cli/snapshot: fix the issue of dup open db error
jsvisa Feb 20, 2023
b5f9741
cli/snapshot: resolve datadir and ancient before backup
jsvisa Feb 20, 2023
b4ad6c0
core: more prune-block log
jsvisa Feb 20, 2023
cf25261
core: truncatetail missing f.offset
jsvisa Feb 20, 2023
7397e5a
core/rawdb: indextx adjust offset of pruned block
jsvisa Feb 20, 2023
f7833b7
core/rawdb: freezer batch should implement the offset commit, ref htt…
jsvisa Feb 21, 2023
786c95c
core: check of ancientdb, backport https://github.com/bnb-chain/bsc/p…
jsvisa Feb 21, 2023
ec275a9
core/state: read raw borReceipt to backup
jsvisa Feb 23, 2023
afdeeec
core/rawdb: bor receipt maybe in []Receipt or Receipt RLP format
jsvisa Mar 1, 2023
f91030b
core/state: typo and error msg
jsvisa Mar 1, 2023
727656c
core/rawdb: offSet -> offset
jsvisa Mar 1, 2023
13292b0
cli/snapshot: comment
jsvisa Mar 1, 2023
59e6752
cli/snapshot: add prune-block doc
jsvisa Mar 1, 2023
1b0abf0
docs: add prune-block document
jsvisa Mar 1, 2023
f9ae714
core/rawdb: print wrong bor-receipt length
jsvisa Mar 1, 2023
a3ed4ea
internal/cli: add snapshot prune block tests (referenced from bsc's PR)
manav2401 May 13, 2023
c314ed8
improve errors
manav2401 May 15, 2023
383508a
Merge branch 'develop' into block-pruner
manav2401 May 15, 2023
98c020c
cmd, core, eth, internal: fix lint
manav2401 May 15, 2023
3d71ce5
internal/cli: refactor snapshot prune block test
manav2401 May 16, 2023
f195feb
fix linters in tests
manav2401 May 16, 2023
7104f95
internal/cli: add inspect-ancient-db command, update docs
manav2401 May 17, 2023
54d5a5e
pruner: use a generic function for simplification
manav2401 May 19, 2023
98149be
internal/cli: fixes for inspect-db command
manav2401 May 19, 2023
7c0baba
internal/cli: improve pruning tests
manav2401 May 19, 2023
6276de6
core/rawdb: update end block calculation logic in inspect command
manav2401 May 21, 2023
e21c500
core/rawdb: improve checks db initialisation
manav2401 May 21, 2023
35a95b2
core/rawdb: remove offset check
manav2401 May 21, 2023
495e5cc
update mocks for span, ethdb and add command in makefile
manav2401 May 22, 2023
eda6bac
Merge branch 'develop' into block-pruner
manav2401 May 23, 2023
25a775b
docs/cli: update docs with inspect command
manav2401 May 23, 2023
8cc56a4
Merge branch 'develop' into block-pruner
manav2401 Jun 8, 2023
e813a95
Merge branch 'develop' into block-pruner
manav2401 Jun 20, 2023
7082cb9
Merge remote-tracking branch 'origin/develop' into HEAD
manav2401 Apr 10, 2024
b39c7ba
go mod tidy
manav2401 Apr 11, 2024
afc10b6
refactor and resolve conflicts
manav2401 Apr 11, 2024
b35fea7
resolve more conflicts
manav2401 Apr 14, 2024
968a348
refactor
manav2401 Apr 14, 2024
8dbde87
explicitly read node for hash scheme
manav2401 Apr 14, 2024
ac681be
add check for hash scheme, fix tests
manav2401 Apr 14, 2024
34933fb
Merge branch 'develop' into manav/ancient-pruner-test-1
manav2401 Apr 14, 2024
99f684f
fix typo
manav2401 Apr 14, 2024
a6aa79a
update docs and add warning
manav2401 Apr 15, 2024
0f1b9e2
raise error if pbss is enabled
manav2401 Apr 16, 2024
aba6091
revert read raw bor receipt change
manav2401 Apr 16, 2024
15c806e
consensus/bor: handle nil header case in get root hash
manav2401 Apr 22, 2024
0c49dfc
address comments
manav2401 Apr 22, 2024
3c38710
core/rawdb: check chain continuity by matching parent hash
manav2401 Apr 23, 2024
8e4b0bd
core/rawdb: account for pruned ancient blocks
manav2401 Apr 23, 2024
fbc283c
Merge branch 'develop' into manav/ancient-pruner-test-1
manav2401 May 8, 2024
dbe3449
go mod tidy
manav2401 May 8, 2024
55b5357
fix tests
manav2401 May 8, 2024
d098c59
fix tests
manav2401 May 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protoc:

generate-mocks:
go generate mockgen -destination=./tests/bor/mocks/IHeimdallClient.go -package=mocks ./consensus/bor IHeimdallClient
go generate mockgen -destination=./eth/filters/IDatabase.go -package=filters ./ethdb Database
go generate mockgen -destination=./eth/filters/IBackend.go -package=filters ./eth/filters Backend
go generate mockgen -destination=../eth/filters/IDatabase.go -package=filters ./ethdb Database

Expand Down
9 changes: 4 additions & 5 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func initGenesis(ctx *cli.Context) error {
overrides.OverrideVerkle = new(big.Int).SetInt64(v)
}
for _, name := range []string{"chaindata", "lightchaindata"} {
chaindb, err := stack.OpenDatabaseWithFreezer(name, 0, 0, ctx.String(utils.AncientFlag.Name), "", false)
chaindb, err := stack.OpenDatabaseWithFreezer(name, 0, 0, ctx.String(utils.AncientFlag.Name), "", false, false, false)
if err != nil {
utils.Fatalf("Failed to open database: %v", err)
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func importPreimages(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false)
db := utils.MakeChainDatabase(ctx, stack, false, false)
defer db.Close()
start := time.Now()

Expand All @@ -438,7 +438,7 @@ func exportPreimages(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()
start := time.Now()

Expand All @@ -452,9 +452,8 @@ func exportPreimages(ctx *cli.Context) error {
}

func parseDumpConfig(ctx *cli.Context, stack *node.Node) (*state.DumpConfig, ethdb.Database, common.Hash, error) {
db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()

var header *types.Header

if ctx.NArg() > 1 {
Expand Down
25 changes: 11 additions & 14 deletions cmd/geth/dbcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func inspect(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()

return rawdb.InspectDatabase(db, prefix, start)
Expand All @@ -317,7 +317,7 @@ func checkStateContent(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()

var (
Expand Down Expand Up @@ -381,7 +381,7 @@ func dbStats(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()

showLeveldbStats(db)
Expand All @@ -393,7 +393,7 @@ func dbCompact(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false)
db := utils.MakeChainDatabase(ctx, stack, false, false)
defer db.Close()

log.Info("Stats before compaction")
Expand Down Expand Up @@ -421,7 +421,7 @@ func dbGet(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()

key, err := common.ParseHexOrString(ctx.Args().Get(0))
Expand Down Expand Up @@ -450,7 +450,7 @@ func dbDelete(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false)
db := utils.MakeChainDatabase(ctx, stack, false, false)
defer db.Close()

key, err := common.ParseHexOrString(ctx.Args().Get(0))
Expand Down Expand Up @@ -481,7 +481,7 @@ func dbPut(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, false)
db := utils.MakeChainDatabase(ctx, stack, false, false)
defer db.Close()

var (
Expand Down Expand Up @@ -520,7 +520,7 @@ func dbDumpTrie(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()

triedb := utils.MakeTrieDatabase(ctx, db, false, true)
Expand Down Expand Up @@ -658,7 +658,7 @@ func importLDBdata(ctx *cli.Context) error {
close(stop)
}()

db := utils.MakeChainDatabase(ctx, stack, false)
db := utils.MakeChainDatabase(ctx, stack, false, false)
defer db.Close()
return utils.ImportLDBData(db, fName, int64(start), stop)
}
Expand Down Expand Up @@ -767,17 +767,15 @@ func exportChaindata(ctx *cli.Context) error {
close(stop)
}()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()
return utils.ExportChaindata(ctx.Args().Get(1), kind, exporter(db), stop)
}

func showMetaData(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()
db := utils.MakeChainDatabase(ctx, stack, true)
defer db.Close()

db := utils.MakeChainDatabase(ctx, stack, true, false)
ancients, err := db.Ancients()
if err != nil {
fmt.Fprintf(os.Stderr, "Error accessing ancients: %v", err)
Expand All @@ -803,6 +801,5 @@ func showMetaData(ctx *cli.Context) error {
table.SetHeader([]string{"Field", "Value"})
table.AppendBulk(data)
table.Render()

return nil
}
13 changes: 6 additions & 7 deletions cmd/geth/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func pruneState(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, false)
chaindb := utils.MakeChainDatabase(ctx, stack, false, false)
defer chaindb.Close()

if rawdb.ReadStateScheme(chaindb) != rawdb.HashScheme {
Expand Down Expand Up @@ -203,9 +203,8 @@ func verifyState(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
chaindb := utils.MakeChainDatabase(ctx, stack, true, false)
defer chaindb.Close()

headBlock := rawdb.ReadHeadBlock(chaindb)
if headBlock == nil {
log.Error("Failed to load head block")
Expand Down Expand Up @@ -256,7 +255,7 @@ func checkDanglingStorage(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

db := utils.MakeChainDatabase(ctx, stack, true)
db := utils.MakeChainDatabase(ctx, stack, true, false)
defer db.Close()
return snapshot.CheckDanglingStorage(db)
}
Expand All @@ -268,7 +267,7 @@ func traverseState(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
chaindb := utils.MakeChainDatabase(ctx, stack, true, false)
defer chaindb.Close()

triedb := utils.MakeTrieDatabase(ctx, chaindb, false, true)
Expand Down Expand Up @@ -392,7 +391,7 @@ func traverseRawState(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
chaindb := utils.MakeChainDatabase(ctx, stack, true, false)
defer chaindb.Close()

triedb := utils.MakeTrieDatabase(ctx, chaindb, false, true)
Expand Down Expand Up @@ -689,7 +688,7 @@ func checkAccount(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
chaindb := utils.MakeChainDatabase(ctx, stack, true, false)
defer chaindb.Close()

start := time.Now()
Expand Down
4 changes: 2 additions & 2 deletions cmd/geth/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func verifyVerkle(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
chaindb := utils.MakeChainDatabase(ctx, stack, true, false)
defer chaindb.Close()
headBlock := rawdb.ReadHeadBlock(chaindb)

Expand Down Expand Up @@ -174,7 +174,7 @@ func expandVerkle(ctx *cli.Context) error {
stack, _ := makeConfigNode(ctx)
defer stack.Close()

chaindb := utils.MakeChainDatabase(ctx, stack, true)
chaindb := utils.MakeChainDatabase(ctx, stack, true, false)
defer chaindb.Close()
var (
rootC common.Hash
Expand Down
10 changes: 5 additions & 5 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2183,8 +2183,8 @@ func SplitTagsFlag(tagsFlag string) map[string]string {
return tagsMap
}

// MakeChainDatabase opens a database using the flags passed to the client and will hard crash if it fails.
func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly bool) ethdb.Database {
// MakeChainDatabase opens a LevelDB using the flags passed to the client and will hard crash if it fails.
func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly, disableFreeze bool) ethdb.Database {
var (
cache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheDatabaseFlag.Name) / 100
handles = MakeDatabaseHandles(ctx.Int(FDLimitFlag.Name))
Expand All @@ -2205,7 +2205,7 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly bool) ethdb.
case ctx.String(SyncModeFlag.Name) == "light":
chainDb, err = stack.OpenDatabase("lightchaindata", cache, handles, "", readonly)
default:
chainDb, err = stack.OpenDatabaseWithFreezer("chaindata", cache, handles, ctx.String(AncientFlag.Name), "", readonly)
chainDb, err = stack.OpenDatabaseWithFreezer("chaindata", cache, handles, ctx.String(AncientFlag.Name), "", readonly, disableFreeze, false)
}

if err != nil {
Expand All @@ -2224,7 +2224,7 @@ func tryMakeReadOnlyDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database
if rawdb.PreexistingDatabase(stack.ResolvePath("chaindata")) == "" {
readonly = false
}
return MakeChainDatabase(ctx, stack, readonly)
return MakeChainDatabase(ctx, stack, readonly, false)
}

func IsNetworkPreset(ctx *cli.Context) bool {
Expand Down Expand Up @@ -2289,7 +2289,7 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
func MakeChain(ctx *cli.Context, stack *node.Node, readonly bool) (*core.BlockChain, ethdb.Database) {
var (
gspec = MakeGenesis(ctx)
chainDb = MakeChainDatabase(ctx, stack, readonly)
chainDb = MakeChainDatabase(ctx, stack, readonly, false)
)

config, err := core.LoadChainConfig(chainDb, gspec)
Expand Down
4 changes: 4 additions & 0 deletions consensus/bor/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func (api *API) GetRootHash(start uint64, end uint64) (string, error) {

for i := 0; i < len(blockHeaders); i++ {
blockHeader := blockHeaders[i]
// Handle no header case, which is possible if ancient pruning was done
if blockHeader == nil {
return "", errUnknownBlock
}
header := crypto.Keccak256(appendBytes32(
blockHeader.Number.Bytes(),
new(big.Int).SetUint64(blockHeader.Time).Bytes(),
Expand Down
7 changes: 6 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,12 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
}
}
// Ensure that a previous crash in SetHead doesn't leave extra ancients
if frozen, err := bc.db.Ancients(); err == nil && frozen > 0 {
//nolint:nestif
if frozen, err := bc.db.ItemAmountInAncient(); err == nil && frozen > 0 {
frozen, err = bc.db.Ancients()
if err != nil {
return nil, err
}
var (
needRewind bool
low uint64
Expand Down
8 changes: 8 additions & 0 deletions core/blockchain_repair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,8 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
Directory: datadir,
AncientsDirectory: ancient,
Ephemeral: true,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to create persistent database: %v", err)
Expand Down Expand Up @@ -1849,6 +1851,8 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s
Directory: datadir,
AncientsDirectory: ancient,
Ephemeral: true,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to reopen persistent database: %v", err)
Expand Down Expand Up @@ -1913,6 +1917,8 @@ func testIssue23496(t *testing.T, scheme string) {
db, err := rawdb.Open(rawdb.OpenOptions{
Directory: datadir,
AncientsDirectory: ancient,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to create persistent database: %v", err)
Expand Down Expand Up @@ -1971,6 +1977,8 @@ func testIssue23496(t *testing.T, scheme string) {
Directory: datadir,
AncientsDirectory: ancient,
Ephemeral: true,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to reopen persistent database: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions core/blockchain_sethead_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,8 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme
Directory: datadir,
AncientsDirectory: ancient,
Ephemeral: true,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to create persistent database: %v", err)
Expand Down
4 changes: 4 additions & 0 deletions core/blockchain_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (basic *snapshotTestBasic) prepare(t *testing.T) (*BlockChain, []*types.Blo
Directory: datadir,
AncientsDirectory: ancient,
Ephemeral: true,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to create persistent database: %v", err)
Expand Down Expand Up @@ -262,6 +264,8 @@ func (snaptest *crashSnapshotTest) test(t *testing.T) {
Directory: snaptest.datadir,
AncientsDirectory: snaptest.ancient,
Ephemeral: true,
IsLastOffset: false,
DisableFreeze: false,
})
if err != nil {
t.Fatalf("Failed to reopen persistent database: %v", err)
Expand Down
Loading
Loading