clean up blockchain.db if it grows too big

This commit is contained in:
Niko Storni 2020-03-25 17:53:47 +01:00
parent 3dc30c3b98
commit 1b89104101

View file

@ -49,6 +49,19 @@ func GetLBRYNetDir() string {
return lbrynetDir
}
func GetLbryumDir() string {
lbryumDir := os.Getenv("LBRYNET_WALLETS_DIR")
if lbryumDir == "" {
usr, err := user.Current()
if err != nil {
log.Errorln(err.Error())
return ""
}
return usr.HomeDir + "/.lbryum/"
}
return lbryumDir + "/"
}
const ALL = true
const ONLINE = false
@ -225,6 +238,30 @@ func CleanupLbrynet() error {
if err != nil {
return errors.Err(err)
}
lbryumDir := GetLbryumDir()
ledger := "lbc_mainnet"
if IsRegTest() {
ledger = "lbc_regtest"
}
db, err := os.Stat(lbryumDir + ledger + "/blockchain.db")
if err != nil {
return errors.Err(err)
}
dbSizeLimit := int64(2 * 1024 * 1024 * 1024)
if db.Size() > dbSizeLimit {
files, err := filepath.Glob(lbryumDir + "blockchain.db*")
if err != nil {
return errors.Err(err)
}
for _, f := range files {
err = os.Remove(f)
if err != nil {
return errors.Err(err)
}
}
}
return nil
}