From 4fc7478e1e48d15bc50c087de9c76a20746c6eb2 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 19 Oct 2023 17:20:45 +0300 Subject: [PATCH] [#1] Add '-f' flag to overwrite local cache Sometimes multiple environments have blockchains with the same magic number. In this case, user should not reuse cached chain, because cache contains magic number to identify different chains. With new '-f' flag user will be able to repopulate cache with new data for the chain with the same magic number. Signed-off-by: Alex Vanin --- explorer.go | 2 +- flags.go | 7 +++++++ internal/chain/chain.go | 6 +++++- main.go | 3 ++- stutter.go | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/explorer.go b/explorer.go index fda0804..2706142 100644 --- a/explorer.go +++ b/explorer.go @@ -57,7 +57,7 @@ func explorer(c *cli.Context) (err error) { } endpoint := c.String(endpointFlagKey) - blockchain, err := chain.Open(ctx, cacheDir, endpoint) + blockchain, err := chain.Open(ctx, cacheDir, endpoint, false) if err != nil { return fmt.Errorf("cannot initialize remote blockchain client: %w", err) } diff --git a/flags.go b/flags.go index be06eaf..c122433 100644 --- a/flags.go +++ b/flags.go @@ -21,6 +21,7 @@ const ( workersFlagKey = "workers" disableProgressBarFlagKey = "disable-progress-bar" stutterThresholdFlagKey = "threshold" + forceCacheRewriteKey = "force" ) var ( @@ -78,6 +79,12 @@ var ( Usage: "duration limit between block timestamps", Value: 20 * time.Second, } + + forceCacheRewriteFlag = &cli.BoolFlag{ + Name: forceCacheRewriteKey, + Aliases: []string{"f"}, + Usage: "force blockchain cache rewrite", + } ) func parseNotifications(notifications []string, cli *rpcclient.Client) (map[string]*util.Uint160, error) { diff --git a/internal/chain/chain.go b/internal/chain/chain.go index b80a00b..b48f7f5 100644 --- a/internal/chain/chain.go +++ b/internal/chain/chain.go @@ -4,6 +4,7 @@ import ( "context" "encoding/binary" "fmt" + "os" "path" "strconv" @@ -27,7 +28,7 @@ var ( logsBucket = []byte("logs") ) -func Open(ctx context.Context, dir, endpoint string) (*Chain, error) { +func Open(ctx context.Context, dir, endpoint string, rewrite bool) (*Chain, error) { cli, err := rpcclient.New(ctx, endpoint, rpcclient.Options{}) if err != nil { return nil, fmt.Errorf("rpc connection: %w", err) @@ -44,6 +45,9 @@ func Open(ctx context.Context, dir, endpoint string) (*Chain, error) { } dbPath := path.Join(dir, strconv.Itoa(int(v.Protocol.Network))+".db") + if rewrite { + _ = os.Remove(dbPath) // ignore error if file does not exist, etc. + } db, err := bbolt.Open(dbPath, 0600, nil) if err != nil { diff --git a/main.go b/main.go index bc6b2f5..7c68fbb 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ func main() { cacheFlag, workersFlag, disableProgressBarFlag, + forceCacheRewriteFlag, }, }, { @@ -84,7 +85,7 @@ func monza(c *cli.Context) (err error) { } } - blockchain, err := chain.Open(ctx, cacheDir, c.String(endpointFlagKey)) + blockchain, err := chain.Open(ctx, cacheDir, c.String(endpointFlagKey), c.Bool(forceCacheRewriteKey)) if err != nil { return fmt.Errorf("cannot initialize remote blockchain client: %w", err) } diff --git a/stutter.go b/stutter.go index 8d0585f..14d9939 100644 --- a/stutter.go +++ b/stutter.go @@ -25,7 +25,7 @@ func stutter(c *cli.Context) (err error) { } } - blockchain, err := chain.Open(ctx, cacheDir, c.String(endpointFlagKey)) + blockchain, err := chain.Open(ctx, cacheDir, c.String(endpointFlagKey), false) if err != nil { return fmt.Errorf("cannot initialize remote blockchain client: %w", err) }