[#6] Multiple cache files support #7
1 changed files with 18 additions and 2 deletions
|
@ -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,12 @@ 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)
|
||||
}
|
||||
|
||||
dbPath := path.Join(dir, validationBlock.Hash().StringLE()+".db")
|
||||
|
||||
if rewrite {
|
||||
_ = os.Remove(dbPath) // ignore error if file does not exist, etc.
|
||||
}
|
||||
fyrchik
commented
It is better to use It is better to use `StringLE()` methods explicitly, because in neo-go LE is the method of presentation and it won't change from one version to another.
|
||||
|
@ -60,6 +64,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
|
||||
fyrchik
commented
How about having multiple caches? How about having multiple caches?
The name of the cache will be the hash.
This way there is no need in additional check, just create a file and clean the cache with an explicit command.
nzinkevich
commented
Yeah, it's better to do this way. Rewrote logic without removing and hash comparison Yeah, it's better to do this way. Rewrote logic without removing and hash comparison
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue
This branch seems impossible to reach, because we check for the error above.