[#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 <a.vanin@yadro.com>
This commit is contained in:
Alexey Vanin 2023-10-19 17:20:45 +03:00
parent db7464d2d3
commit 4fc7478e1e
5 changed files with 16 additions and 4 deletions

View file

@ -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)
}

View file

@ -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) {

View file

@ -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 {

View file

@ -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)
}

View file

@ -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)
}