Skip to content

Commit 6e3816b

Browse files
Larry RuaneLarryRuane
Larry Ruane
authored andcommitted
add --nocache option to disable compact block file cache
This fixes issue #480. Enabling this option decreases the performance of gRPCs GetBlock and GetBlockRange, but conserves disk space (the size of the database is currently around 17GB). Note, specifying this option will also delete the existing cache (so if you start again without --nocache, the cache will need to be recreated).
1 parent 0bd5519 commit 6e3816b

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

cmd/root.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var rootCmd = &cobra.Command{
5555
GenCertVeryInsecure: viper.GetBool("gen-cert-very-insecure"),
5656
DataDir: viper.GetString("data-dir"),
5757
Redownload: viper.GetBool("redownload"),
58+
NoCache: viper.GetBool("nocache"),
5859
SyncFromHeight: viper.GetInt("sync-from-height"),
5960
PingEnable: viper.GetBool("ping-very-insecure"),
6061
Darkside: viper.GetBool("darkside-very-insecure"),
@@ -240,13 +241,22 @@ func startServer(opts *common.Options) error {
240241
os.Stderr.WriteString(fmt.Sprintf("\n ** Can't create db directory: %s\n\n", dbPath))
241242
os.Exit(1)
242243
}
243-
syncFromHeight := opts.SyncFromHeight
244-
if opts.Redownload {
245-
syncFromHeight = 0
244+
var cache *common.BlockCache
245+
if opts.NoCache {
246+
lengthsName, blocksName := common.DbFileNames(dbPath, chainName)
247+
os.Remove(lengthsName)
248+
os.Remove(blocksName)
249+
} else {
250+
syncFromHeight := opts.SyncFromHeight
251+
if opts.Redownload {
252+
syncFromHeight = 0
253+
}
254+
cache = common.NewBlockCache(dbPath, chainName, saplingHeight, syncFromHeight)
246255
}
247-
cache := common.NewBlockCache(dbPath, chainName, saplingHeight, syncFromHeight)
248256
if !opts.Darkside {
249-
go common.BlockIngestor(cache, 0 /*loop forever*/)
257+
if !opts.NoCache {
258+
go common.BlockIngestor(cache, 0 /*loop forever*/)
259+
}
250260
} else {
251261
// Darkside wants to control starting the block ingestor.
252262
common.DarksideInit(cache, int(opts.DarksideTimeout))
@@ -330,6 +340,7 @@ func init() {
330340
rootCmd.Flags().Bool("no-tls-very-insecure", false, "run without the required TLS certificate, only for debugging, DO NOT use in production")
331341
rootCmd.Flags().Bool("gen-cert-very-insecure", false, "run with self-signed TLS certificate, only for debugging, DO NOT use in production")
332342
rootCmd.Flags().Bool("redownload", false, "re-fetch all blocks from zcashd; reinitialize local cache files")
343+
rootCmd.Flags().Bool("nocache", false, "don't maintain a compact blocks disk cache (to reduce storage)")
333344
rootCmd.Flags().Int("sync-from-height", -1, "re-fetch blocks from zcashd start at this height")
334345
rootCmd.Flags().String("data-dir", "/var/lib/lightwalletd", "data directory (such as db)")
335346
rootCmd.Flags().Bool("ping-very-insecure", false, "allow Ping GRPC for testing")
@@ -362,6 +373,8 @@ func init() {
362373
viper.SetDefault("gen-cert-very-insecure", false)
363374
viper.BindPFlag("redownload", rootCmd.Flags().Lookup("redownload"))
364375
viper.SetDefault("redownload", false)
376+
viper.BindPFlag("nocache", rootCmd.Flags().Lookup("nocache"))
377+
viper.SetDefault("nocache", false)
365378
viper.BindPFlag("sync-from-height", rootCmd.Flags().Lookup("sync-from-height"))
366379
viper.SetDefault("sync-from-height", -1)
367380
viper.BindPFlag("data-dir", rootCmd.Flags().Lookup("data-dir"))

common/cache.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func NewBlockCache(dbPath string, chainName string, startHeight int, syncFromHei
195195
c := &BlockCache{}
196196
c.firstBlock = startHeight
197197
c.nextBlock = startHeight
198-
c.lengthsName, c.blocksName = dbFileNames(dbPath, chainName)
198+
c.lengthsName, c.blocksName = DbFileNames(dbPath, chainName)
199199
var err error
200200
if err := os.MkdirAll(filepath.Join(dbPath, chainName), 0755); err != nil {
201201
Log.Fatal("mkdir ", dbPath, " failed: ", err)
@@ -250,7 +250,7 @@ func NewBlockCache(dbPath string, chainName string, startHeight int, syncFromHei
250250
return c
251251
}
252252

253-
func dbFileNames(dbPath string, chainName string) (string, string) {
253+
func DbFileNames(dbPath string, chainName string) (string, string) {
254254
return filepath.Join(dbPath, chainName, "lengths"),
255255
filepath.Join(dbPath, chainName, "blocks")
256256
}

common/common.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Options struct {
4343
NoTLSVeryInsecure bool `json:"no_tls_very_insecure,omitempty"`
4444
GenCertVeryInsecure bool `json:"gen_cert_very_insecure,omitempty"`
4545
Redownload bool `json:"redownload"`
46+
NoCache bool `json:"nocache"`
4647
SyncFromHeight int `json:"sync_from_height"`
4748
DataDir string `json:"data_dir"`
4849
PingEnable bool `json:"ping_enable"`
@@ -476,9 +477,12 @@ func BlockIngestor(c *BlockCache, rep int) {
476477
// nil if no block exists at this height.
477478
func GetBlock(cache *BlockCache, height int) (*walletrpc.CompactBlock, error) {
478479
// First, check the cache to see if we have the block
479-
block := cache.Get(height)
480-
if block != nil {
481-
return block, nil
480+
var block *walletrpc.CompactBlock
481+
if cache != nil {
482+
block := cache.Get(height)
483+
if block != nil {
484+
return block, nil
485+
}
482486
}
483487

484488
// Not in the cache

0 commit comments

Comments
 (0)