From 1b89104101227a660bd9db44a4d75b8482419e7a Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Wed, 25 Mar 2020 17:53:47 +0100 Subject: [PATCH] clean up blockchain.db if it grows too big --- util/util.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/util/util.go b/util/util.go index 16f8fb4..e6a80d4 100644 --- a/util/util.go +++ b/util/util.go @@ -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 }