Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --nocache option to disable compact block file cache #483

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var rootCmd = &cobra.Command{
GenCertVeryInsecure: viper.GetBool("gen-cert-very-insecure"),
DataDir: viper.GetString("data-dir"),
Redownload: viper.GetBool("redownload"),
NoCache: viper.GetBool("nocache"),
SyncFromHeight: viper.GetInt("sync-from-height"),
PingEnable: viper.GetBool("ping-very-insecure"),
Darkside: viper.GetBool("darkside-very-insecure"),
Expand Down Expand Up @@ -240,13 +241,22 @@ func startServer(opts *common.Options) error {
os.Stderr.WriteString(fmt.Sprintf("\n ** Can't create db directory: %s\n\n", dbPath))
os.Exit(1)
}
syncFromHeight := opts.SyncFromHeight
if opts.Redownload {
syncFromHeight = 0
var cache *common.BlockCache
if opts.NoCache {
lengthsName, blocksName := common.DbFileNames(dbPath, chainName)
os.Remove(lengthsName)
os.Remove(blocksName)
} else {
syncFromHeight := opts.SyncFromHeight
if opts.Redownload {
syncFromHeight = 0
}
cache = common.NewBlockCache(dbPath, chainName, saplingHeight, syncFromHeight)
}
cache := common.NewBlockCache(dbPath, chainName, saplingHeight, syncFromHeight)
if !opts.Darkside {
go common.BlockIngestor(cache, 0 /*loop forever*/)
if !opts.NoCache {
go common.BlockIngestor(cache, 0 /*loop forever*/)
}
} else {
// Darkside wants to control starting the block ingestor.
common.DarksideInit(cache, int(opts.DarksideTimeout))
arya2 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -330,6 +340,7 @@ func init() {
rootCmd.Flags().Bool("no-tls-very-insecure", false, "run without the required TLS certificate, only for debugging, DO NOT use in production")
rootCmd.Flags().Bool("gen-cert-very-insecure", false, "run with self-signed TLS certificate, only for debugging, DO NOT use in production")
rootCmd.Flags().Bool("redownload", false, "re-fetch all blocks from zcashd; reinitialize local cache files")
rootCmd.Flags().Bool("nocache", false, "don't maintain a compact blocks disk cache (to reduce storage)")
rootCmd.Flags().Int("sync-from-height", -1, "re-fetch blocks from zcashd start at this height")
rootCmd.Flags().String("data-dir", "/var/lib/lightwalletd", "data directory (such as db)")
rootCmd.Flags().Bool("ping-very-insecure", false, "allow Ping GRPC for testing")
Expand Down Expand Up @@ -362,6 +373,8 @@ func init() {
viper.SetDefault("gen-cert-very-insecure", false)
viper.BindPFlag("redownload", rootCmd.Flags().Lookup("redownload"))
viper.SetDefault("redownload", false)
viper.BindPFlag("nocache", rootCmd.Flags().Lookup("nocache"))
viper.SetDefault("nocache", false)
viper.BindPFlag("sync-from-height", rootCmd.Flags().Lookup("sync-from-height"))
viper.SetDefault("sync-from-height", -1)
viper.BindPFlag("data-dir", rootCmd.Flags().Lookup("data-dir"))
Expand Down
4 changes: 2 additions & 2 deletions common/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func NewBlockCache(dbPath string, chainName string, startHeight int, syncFromHei
c := &BlockCache{}
c.firstBlock = startHeight
c.nextBlock = startHeight
c.lengthsName, c.blocksName = dbFileNames(dbPath, chainName)
c.lengthsName, c.blocksName = DbFileNames(dbPath, chainName)
var err error
if err := os.MkdirAll(filepath.Join(dbPath, chainName), 0755); err != nil {
Log.Fatal("mkdir ", dbPath, " failed: ", err)
Expand Down Expand Up @@ -250,7 +250,7 @@ func NewBlockCache(dbPath string, chainName string, startHeight int, syncFromHei
return c
}

func dbFileNames(dbPath string, chainName string) (string, string) {
func DbFileNames(dbPath string, chainName string) (string, string) {
return filepath.Join(dbPath, chainName, "lengths"),
filepath.Join(dbPath, chainName, "blocks")
}
Expand Down
10 changes: 7 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Options struct {
NoTLSVeryInsecure bool `json:"no_tls_very_insecure,omitempty"`
GenCertVeryInsecure bool `json:"gen_cert_very_insecure,omitempty"`
Redownload bool `json:"redownload"`
NoCache bool `json:"nocache"`
SyncFromHeight int `json:"sync_from_height"`
DataDir string `json:"data_dir"`
PingEnable bool `json:"ping_enable"`
Expand Down Expand Up @@ -476,9 +477,12 @@ func BlockIngestor(c *BlockCache, rep int) {
// nil if no block exists at this height.
func GetBlock(cache *BlockCache, height int) (*walletrpc.CompactBlock, error) {
// First, check the cache to see if we have the block
block := cache.Get(height)
if block != nil {
return block, nil
var block *walletrpc.CompactBlock
if cache != nil {
block := cache.Get(height)
if block != nil {
return block, nil
}
}

// Not in the cache
Expand Down
Loading