feat(lotus-sim): NewNode to construct a node without a repo
This commit is contained in:
parent
b5f9148748
commit
80eba1069a
@ -2,7 +2,6 @@ package simulation
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"go.uber.org/multierr"
|
"go.uber.org/multierr"
|
||||||
@ -23,55 +22,63 @@ import (
|
|||||||
|
|
||||||
// Node represents the local lotus node, or at least the part of it we care about.
|
// Node represents the local lotus node, or at least the part of it we care about.
|
||||||
type Node struct {
|
type Node struct {
|
||||||
Repo repo.LockedRepo
|
repo repo.LockedRepo
|
||||||
Blockstore blockstore.Blockstore
|
Blockstore blockstore.Blockstore
|
||||||
MetadataDS datastore.Batching
|
MetadataDS datastore.Batching
|
||||||
Chainstore *store.ChainStore
|
Chainstore *store.ChainStore
|
||||||
}
|
}
|
||||||
|
|
||||||
// OpenNode opens the local lotus node for writing. This will fail if the node is online.
|
// OpenNode opens the local lotus node for writing. This will fail if the node is online.
|
||||||
func OpenNode(ctx context.Context, path string) (*Node, error) {
|
func OpenNode(ctx context.Context, path string) (node *Node, _err error) {
|
||||||
var node Node
|
|
||||||
r, err := repo.NewFS(path)
|
r, err := repo.NewFS(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
node.Repo, err = r.Lock(repo.FullNode)
|
lr, err := r.Lock(repo.FullNode)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer func() {
|
||||||
|
if _err != nil {
|
||||||
|
lr.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
bs, err := lr.Blockstore(ctx, repo.UniversalBlockstore)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = node.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
node.Blockstore, err = node.Repo.Blockstore(ctx, repo.UniversalBlockstore)
|
ds, err := lr.Datastore(ctx, "/metadata")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = node.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
node.MetadataDS, err = node.Repo.Datastore(ctx, "/metadata")
|
node = NewNode(bs, ds)
|
||||||
if err != nil {
|
node.repo = lr
|
||||||
_ = node.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
node.Chainstore = store.NewChainStore(node.Blockstore, node.Blockstore, node.MetadataDS, vm.Syscalls(mock.Verifier), nil)
|
return node, nil
|
||||||
return &node, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close cleanly close the node. Please call this on shutdown to make sure everything is flushed.
|
// NewNode will construct a new simulation node from a datastore (used to store simulation
|
||||||
|
// configuration) and a blockstore (chain + state).
|
||||||
|
func NewNode(bs blockstore.Blockstore, ds datastore.Batching) *Node {
|
||||||
|
return &Node{
|
||||||
|
Chainstore: store.NewChainStore(bs, bs, ds, vm.Syscalls(mock.Verifier), nil),
|
||||||
|
MetadataDS: ds,
|
||||||
|
Blockstore: bs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close cleanly close the repo. Please call this on shutdown to make sure everything is flushed.
|
||||||
|
//
|
||||||
|
// This function is a no-op when the node is manually constructed with `NewNode`.
|
||||||
func (nd *Node) Close() error {
|
func (nd *Node) Close() error {
|
||||||
var err error
|
if nd.repo != nil {
|
||||||
if closer, ok := nd.Blockstore.(io.Closer); ok && closer != nil {
|
return nd.repo.Close()
|
||||||
err = multierr.Append(err, closer.Close())
|
|
||||||
}
|
}
|
||||||
if nd.MetadataDS != nil {
|
return nil
|
||||||
err = multierr.Append(err, nd.MetadataDS.Close())
|
|
||||||
}
|
|
||||||
if nd.Repo != nil {
|
|
||||||
err = multierr.Append(err, nd.Repo.Close())
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadSim loads
|
// LoadSim loads
|
||||||
|
Loading…
Reference in New Issue
Block a user