From f33aa47969699795630ddab9ac3700becd91b3d6 Mon Sep 17 00:00:00 2001 From: Nikita Zinkevich Date: Wed, 14 Aug 2024 15:53:53 +0300 Subject: [PATCH] [#6] Add caching from multiple environments --- internal/chain/chain.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/internal/chain/chain.go b/internal/chain/chain.go index 8294b03..71c9a32 100644 --- a/internal/chain/chain.go +++ b/internal/chain/chain.go @@ -7,7 +7,6 @@ import ( "fmt" "os" "path" - "strconv" "git.frostfs.info/TrueCloudLab/monza/internal/bytecode" "github.com/nspcc-dev/neo-go/pkg/core/block" @@ -47,7 +46,15 @@ func Open(ctx context.Context, dir, endpoint string, rewrite bool) (*Chain, erro return nil, fmt.Errorf("rpc get version: %w", err) } - dbPath := path.Join(dir, strconv.Itoa(int(v.Protocol.Network))+".db") + validationBlock, err := getValidationBlock(cli) + if err != nil { + return nil, fmt.Errorf("error get validation block: %w", err) + } + if validationBlock == nil { + return nil, fmt.Errorf("validation block does not exist: %w", err) + } + + dbPath := path.Join(dir, validationBlock.Hash().String()+".db") if rewrite { _ = os.Remove(dbPath) // ignore error if file does not exist, etc. } @@ -60,6 +67,18 @@ func Open(ctx context.Context, dir, endpoint string, rewrite bool) (*Chain, erro return &Chain{db, v.Protocol.StateRootInHeader, cli}, nil } +func getValidationBlock(cli *rpcclient.Client) (*block.Block, error) { + // not 0, because it always has the same hash + const validationBlockIndex = 1 + var err error + validBlock, err := cli.GetBlockByIndex(validationBlockIndex) + if err != nil { + return nil, fmt.Errorf("error sending request for block: %w", err) + } + + return validBlock, nil +} + func (d *Chain) Block(i uint32) (res *block.Block, err error) { cached, err := d.block(i) if err != nil {