database, database/ffldb: add BeenPruned() method

This change is part of the effort to add pruning support to btcd.

BeenPruned will return true if the database has ever been pruned.  This
allows for accurate prune status to be reported as well as ux
improvements by disallowing the user to accidently remove or enable
indexes.
This commit is contained in:
Calvin Kim 2023-08-22 15:49:48 +09:00
parent 57903c71c9
commit aaedc11887
3 changed files with 51 additions and 0 deletions

View File

@ -1764,6 +1764,21 @@ func (tx *transaction) PruneBlocks(targetSize uint64) ([]chainhash.Hash, error)
return deletedBlockHashes, nil
}
// BeenPruned returns if the block storage has ever been pruned.
//
// This function is part of the database.Tx interface implementation.
func (tx *transaction) BeenPruned() (bool, error) {
first, last, _, err := scanBlockFiles(tx.db.store.basePath)
if err != nil {
return false, err
}
// If the database is pruned, then the first .fdb will not be there.
// We also check that there isn't just 1 file on disk or if there are
// no files on disk by checking if first != last.
return first != 0 && (first != last), nil
}
// Commit commits all changes that have been made to the root metadata bucket
// and all of its sub-buckets to the database cache which is periodically synced
// to persistent storage. In addition, it commits all new blocks directly to

View File

@ -319,6 +319,21 @@ func TestPrune(t *testing.T) {
if err != nil {
t.Fatal(err)
}
err = db.View(func(tx database.Tx) error {
pruned, err := tx.BeenPruned()
if err != nil {
return err
}
if pruned {
err = fmt.Errorf("The database hasn't been pruned but " +
"BeenPruned returned true")
}
return err
})
if err != nil {
t.Fatal(err)
}
var deletedBlocks []chainhash.Hash
@ -339,6 +354,22 @@ func TestPrune(t *testing.T) {
3, len(files))
}
err = db.View(func(tx database.Tx) error {
pruned, err := tx.BeenPruned()
if err != nil {
return err
}
if !pruned {
err = fmt.Errorf("The database has been pruned but " +
"BeenPruned returned false")
}
return err
})
if err != nil {
t.Fatal(err)
}
// Check that all the blocks that say were deleted are deleted from the
// block index bucket as well.
err = db.View(func(tx database.Tx) error {

View File

@ -404,6 +404,11 @@ type Tx interface {
// implementations.
PruneBlocks(targetSize uint64) ([]chainhash.Hash, error)
// BeenPruned returns if the block storage has ever been pruned.
//
// Implementation specific errors are possible.
BeenPruned() (bool, error)
// ******************************************************************
// Methods related to both atomic metadata storage and block storage.
// ******************************************************************