From a7d2b964c619405d449cfebc1a7c762c3321de20 Mon Sep 17 00:00:00 2001 From: dreacot Date: Thu, 24 Oct 2024 21:25:36 +0100 Subject: [PATCH] upgrade mobile wallets db to badgerbd --- go.mod | 3 +- go.sum | 5 ++ libwallet/assets_manager.go | 5 ++ ui/page/start_page.go | 85 ++++++++++++++++++++++++-- ui/page/wallet/wallet_settings_page.go | 19 ++++++ ui/values/localizable/en.go | 3 + ui/values/strings.go | 3 + 7 files changed, 117 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index e40d3564f..4e52ee530 100644 --- a/go.mod +++ b/go.mod @@ -194,7 +194,7 @@ require ( github.com/prometheus/procfs v0.9.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/rogpeppe/go-internal v1.9.0 // indirect - github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect + github.com/shirou/gopsutil v3.21.11+incompatible // indirect github.com/stretchr/objx v0.5.2 // indirect github.com/stretchr/testify v1.9.0 // indirect github.com/supranational/blst v0.3.11 // indirect @@ -203,6 +203,7 @@ require ( github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/yeqown/reedsolomon v1.0.0 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect github.com/zquestz/grab v0.0.0-20190224022517-abcee96e61b1 // indirect golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect golang.org/x/net v0.28.0 // indirect diff --git a/go.sum b/go.sum index e8df09f59..a4bd65324 100644 --- a/go.sum +++ b/go.sum @@ -713,6 +713,7 @@ github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM= github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= @@ -1448,6 +1449,8 @@ github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c/go.mod h1:/PevMnwAx github.com/shirou/gopsutil v0.0.0-20180427012116-c95755e4bcd7/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= +github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI= +github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/shirou/gopsutil/v3 v3.21.4/go.mod h1:ghfMypLDrFSWN2c9cDYFLHyynQ+QUht0cv/18ZqVczw= github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q1u/4XEfrquwF8Lw7D7y5cD8CuHnfIc= github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= @@ -1611,6 +1614,8 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= github.com/zquestz/grab v0.0.0-20190224022517-abcee96e61b1 h1:1qKTeMTSIEvRIjvVYzgcRp0xVp0eoiRTTiHSncb5gD8= github.com/zquestz/grab v0.0.0-20190224022517-abcee96e61b1/go.mod h1:bslhAiUxakrA6z6CHmVyvkfpnxx18RJBwVyx2TluJWw= go.etcd.io/bbolt v1.3.0/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= diff --git a/libwallet/assets_manager.go b/libwallet/assets_manager.go index c2bf1d41b..aa0043b44 100644 --- a/libwallet/assets_manager.go +++ b/libwallet/assets_manager.go @@ -396,6 +396,11 @@ func (mgr *AssetsManager) LogDir() string { return filepath.Join(mgr.params.RootDir, logFileName) } +// DBDriver returns the db driver in use +func (mgr *AssetsManager) DBDriver() string { + return mgr.params.DbDriver +} + // OpenWallets opens all wallets in the assets manager. func (mgr *AssetsManager) OpenWallets(startupPassphrase string) error { for _, wallet := range mgr.AllWallets() { diff --git a/ui/page/start_page.go b/ui/page/start_page.go index c3c02c98e..75a987fb6 100644 --- a/ui/page/start_page.go +++ b/ui/page/start_page.go @@ -2,10 +2,12 @@ package page import ( "context" + "fmt" "os" "strings" "time" + gioApp "gioui.org/app" "gioui.org/font" "gioui.org/layout" "gioui.org/text" @@ -14,6 +16,7 @@ import ( "golang.org/x/text/language" "github.com/crypto-power/cryptopower/app" + "github.com/crypto-power/cryptopower/appos" sharedW "github.com/crypto-power/cryptopower/libwallet/assets/wallet" libutils "github.com/crypto-power/cryptopower/libwallet/utils" "github.com/crypto-power/cryptopower/ui/cryptomaterial" @@ -24,6 +27,7 @@ import ( "github.com/crypto-power/cryptopower/ui/page/settings" "github.com/crypto-power/cryptopower/ui/preference" "github.com/crypto-power/cryptopower/ui/values" + "github.com/shirou/gopsutil/mem" ) const ( @@ -140,12 +144,13 @@ func (sp *startPage) OnNavigatedTo() { sp.setLanguagePref(true) // Set the log levels. sp.AssetsManager.GetLogLevels() - if sp.AssetsManager.IsStartupSecuritySet() { - sp.unlock() - } else { - sp.loading = true - go func() { _ = sp.openWalletsAndDisplayHomePage("") }() + // Mobile devices usually have very limited amount of ram available to an application + // Ensure mobile users are using badgedb + if appos.Current().IsMobile() { + sp.checkDBFile() + return } + sp.checkStartupSecurityAndStartApp() } else { sp.loading = false } @@ -729,3 +734,73 @@ func (sp *startPage) updateSettings() { sp.AssetsManager.SetHTTPAPIPrivacyMode(libutils.VspAPI, true) sp.AssetsManager.SetHTTPAPIPrivacyMode(libutils.UpdateAPI, true) } + +func (sp *startPage) checkDBFile() { + isNewDB := sp.AssetsManager.DBDriver() == "badgerdb" + numberOfRam, err := getNumberOfRam() + if err != nil { + log.Errorf("Error getting number of ram: %v", err) + return + } + fmt.Println("number of ram", numberOfRam) + + if numberOfRam < 4 && !isNewDB { + sp.showRemoveWalletWarning() + return + } + + sp.checkStartupSecurityAndStartApp() +} + +func (sp *startPage) checkStartupSecurityAndStartApp() { + if sp.AssetsManager.IsStartupSecuritySet() { + sp.unlock() + } else { + sp.loading = true + go func() { _ = sp.openWalletsAndDisplayHomePage("") }() + } +} + +func (sp *startPage) clearAppDir() { + homeDir, err := gioApp.DataDir() + if err != nil { + log.Error("unable to get home dir: %v", err) + // return nil, fmt.Errorf("unable to get android home dir: %v", err) + } + + err = os.RemoveAll(homeDir) + if err != nil { + // If an error occurs, handle it (e.g., log it or show a message) + // showErrorMessage(err) + log.Error("unable to remove home dir: %v", err) + return + } +} + +func (sp *startPage) showRemoveWalletWarning() { + warningModal := modal.NewCustomModal(sp.Load). + Title(values.String(values.StrDataFileErrorTitle)). + Body(values.String(values.StrDataFileErrorBody)). + SetNegativeButtonText(values.String(values.StrCancel)). + SetNegativeButtonCallback(func() { + sp.checkStartupSecurityAndStartApp() + }). + SetNegativeButtonText(values.String(values.StrNo)). + PositiveButtonStyle(sp.Theme.Color.Surface, sp.Theme.Color.Danger). + SetPositiveButtonText(values.String(values.StrYes)). + SetPositiveButtonCallback(func(_ bool, _ *modal.InfoModal) bool { + sp.clearAppDir() + return true + }) + sp.ParentWindow().ShowModal(warningModal) +} + +// Function to get the number of RAM in GB +func getNumberOfRam() (int, error) { + vmStat, err := mem.VirtualMemory() + if err != nil { + return 0, err + } + // Convert bytes to gigabytes + return int(vmStat.Total / (1024 * 1024 * 1024)), nil +} diff --git a/ui/page/wallet/wallet_settings_page.go b/ui/page/wallet/wallet_settings_page.go index c7455ab7d..0aa13f441 100644 --- a/ui/page/wallet/wallet_settings_page.go +++ b/ui/page/wallet/wallet_settings_page.go @@ -230,6 +230,25 @@ func (pg *SettingsPage) generalSection() layout.Widget { func (pg *SettingsPage) debug() layout.Widget { dim := func(gtx C) D { return layout.Flex{Axis: layout.Vertical}.Layout(gtx, + layout.Rigid(func(gtx C) D { + return layout.Inset{ + Bottom: values.MarginPadding24, + }.Layout(gtx, func(gtx C) D { + return layout.Flex{Axis: layout.Horizontal}.Layout(gtx, + layout.Rigid(func(gtx layout.Context) layout.Dimensions { + databaseTypeLabel := pg.Theme.Label(values.TextSizeTransform(pg.Load.IsMobileView(), values.TextSize16), values.String(values.StrDatabaseType)) + return databaseTypeLabel.Layout(gtx) + }), + layout.Flexed(1, func(gtx C) D { + return layout.E.Layout(gtx, func(gtx C) D { + dbDriverLabel := pg.Theme.Label(values.TextSizeTransform(pg.Load.IsMobileView(), values.TextSize16), pg.AssetsManager.DBDriver()) + dbDriverLabel.Color = pg.Theme.Color.GrayText2 + return dbDriverLabel.Layout(gtx) + }) + }), + ) + }) + }), layout.Rigid(pg.sectionContent(pg.rescan, values.String(values.StrRescanBlockchain))), layout.Rigid(func(gtx C) D { if pg.wallet.GetAssetType() == libutils.DCRWalletAsset { diff --git a/ui/values/localizable/en.go b/ui/values/localizable/en.go index 00617b1fc..aa91c943b 100644 --- a/ui/values/localizable/en.go +++ b/ui/values/localizable/en.go @@ -956,4 +956,7 @@ const EN = ` "privacy" = "Privacy" "removeRecipient" = "Remove recipient" "removeRecipientWarning" = "Are you sure you want to proceed with removing the recipient?" +"dataFileErrorTitle" = "Data file error" +"dataFileErrorBody" = "We have detected a problem with your data files from an older version. To fix it, you will need to re-enter your wallet seed phrase. Can you do this now?" +"databaseType" = "Database type" ` diff --git a/ui/values/strings.go b/ui/values/strings.go index 2a520e3ae..6b7bc9c3b 100644 --- a/ui/values/strings.go +++ b/ui/values/strings.go @@ -1065,4 +1065,7 @@ const ( StrPrivacy = "privacy" StrRemoveRecipient = "removeRecipient" StrRemoveRecipientWarning = "removeRecipientWarning" + StrDataFileErrorTitle = "dataFileErrorTitle" + StrDataFileErrorBody = "dataFileErrorBody" + StrDatabaseType = "databaseType" )