diff --git a/database/ffldb/db.go b/database/ffldb/db.go index f8ac1e07..1751c936 100644 --- a/database/ffldb/db.go +++ b/database/ffldb/db.go @@ -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 diff --git a/database/ffldb/driver_test.go b/database/ffldb/driver_test.go index 5b9a7533..794e8e19 100644 --- a/database/ffldb/driver_test.go +++ b/database/ffldb/driver_test.go @@ -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 { diff --git a/database/interface.go b/database/interface.go index 789c823a..7efc7c55 100644 --- a/database/interface.go +++ b/database/interface.go @@ -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. // ******************************************************************