2019-07-09 17:50:48 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2020-12-30 10:04:00 +00:00
|
|
|
"context"
|
2020-03-05 22:02:01 +00:00
|
|
|
"encoding/json"
|
2019-11-12 17:59:38 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-03-05 22:02:01 +00:00
|
|
|
"path/filepath"
|
2019-07-09 17:50:48 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-03-05 22:02:01 +00:00
|
|
|
"github.com/google/uuid"
|
2019-07-09 17:50:48 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
2019-07-10 15:38:35 +00:00
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2019-07-09 17:50:48 +00:00
|
|
|
dssync "github.com/ipfs/go-datastore/sync"
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
2019-07-18 14:01:39 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-09 18:11:32 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-11-01 13:01:26 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/fsutil"
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
|
2020-11-06 18:57:04 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/blockstore"
|
2020-03-03 22:19:22 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2019-07-09 17:50:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemRepo struct {
|
|
|
|
api struct {
|
|
|
|
sync.Mutex
|
2019-07-23 18:49:09 +00:00
|
|
|
ma multiaddr.Multiaddr
|
|
|
|
token []byte
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
repoLock chan struct{}
|
|
|
|
token *byte
|
|
|
|
|
2020-11-01 13:01:26 +00:00
|
|
|
datastore datastore.Datastore
|
|
|
|
keystore map[string]types.KeyInfo
|
|
|
|
blockstore blockstore.Blockstore
|
2020-06-09 23:37:18 +00:00
|
|
|
|
|
|
|
// given a repo type, produce the default config
|
|
|
|
configF func(t RepoType) interface{}
|
|
|
|
|
|
|
|
// holds the current config value
|
2020-06-10 16:07:47 +00:00
|
|
|
config struct {
|
|
|
|
sync.Mutex
|
|
|
|
val interface{}
|
|
|
|
}
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lockedMemRepo struct {
|
|
|
|
mem *MemRepo
|
2019-10-30 16:38:39 +00:00
|
|
|
t RepoType
|
2019-07-09 17:50:48 +00:00
|
|
|
sync.RWMutex
|
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
tempDir string
|
|
|
|
token *byte
|
2020-03-27 20:08:06 +00:00
|
|
|
sc *stores.StorageConfig
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
func (lmem *lockedMemRepo) GetStorage() (stores.StorageConfig, error) {
|
2020-06-10 16:07:47 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return stores.StorageConfig{}, err
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:21:42 +00:00
|
|
|
if lmem.sc == nil {
|
2020-03-27 20:08:06 +00:00
|
|
|
lmem.sc = &stores.StorageConfig{StoragePaths: []stores.LocalPath{
|
2020-03-09 06:21:42 +00:00
|
|
|
{Path: lmem.Path()},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *lmem.sc, nil
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
func (lmem *lockedMemRepo) SetStorage(c func(*stores.StorageConfig)) error {
|
2020-06-10 16:07:47 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-09 19:22:30 +00:00
|
|
|
_, _ = lmem.GetStorage()
|
|
|
|
|
|
|
|
c(lmem.sc)
|
2020-03-09 06:21:42 +00:00
|
|
|
return nil
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 15:23:27 +00:00
|
|
|
func (lmem *lockedMemRepo) Stat(path string) (fsutil.FsStat, error) {
|
|
|
|
return fsutil.Statfs(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lmem *lockedMemRepo) DiskUsage(path string) (int64, error) {
|
|
|
|
si, err := fsutil.FileSize(path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return si.OnDisk, nil
|
2020-05-26 08:20:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 09:59:18 +00:00
|
|
|
func (lmem *lockedMemRepo) Path() string {
|
2020-03-05 22:02:01 +00:00
|
|
|
lmem.Lock()
|
|
|
|
defer lmem.Unlock()
|
|
|
|
|
|
|
|
if lmem.tempDir != "" {
|
|
|
|
return lmem.tempDir
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
t, err := ioutil.TempDir(os.TempDir(), "lotus-memrepo-temp-")
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // only used in tests, probably fine
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:02:01 +00:00
|
|
|
if lmem.t == StorageMiner {
|
2020-03-27 20:08:06 +00:00
|
|
|
if err := config.WriteStorageFile(filepath.Join(t, fsStorageConfig), stores.StorageConfig{
|
|
|
|
StoragePaths: []stores.LocalPath{
|
2020-03-05 22:02:01 +00:00
|
|
|
{Path: t},
|
|
|
|
}}); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-03-19 15:10:19 +00:00
|
|
|
b, err := json.MarshalIndent(&stores.LocalStorageMeta{
|
2020-03-13 11:59:19 +00:00
|
|
|
ID: stores.ID(uuid.New().String()),
|
2020-03-05 22:02:01 +00:00
|
|
|
Weight: 10,
|
|
|
|
CanSeal: true,
|
|
|
|
CanStore: true,
|
|
|
|
}, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(t, "sectorstore.json"), b, 0644); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
lmem.tempDir = t
|
|
|
|
return t
|
2019-07-12 09:59:18 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 17:50:48 +00:00
|
|
|
var _ Repo = &MemRepo{}
|
|
|
|
|
2019-07-09 18:11:32 +00:00
|
|
|
// MemRepoOptions contains options for memory repo
|
2019-07-09 17:50:48 +00:00
|
|
|
type MemRepoOptions struct {
|
2019-10-03 00:02:06 +00:00
|
|
|
Ds datastore.Datastore
|
2019-10-30 16:38:39 +00:00
|
|
|
ConfigF func(RepoType) interface{}
|
2019-10-03 00:02:06 +00:00
|
|
|
KeyStore map[string]types.KeyInfo
|
2019-07-10 13:23:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 18:11:32 +00:00
|
|
|
// NewMemory creates new memory based repo with provided options.
|
2019-07-09 18:19:54 +00:00
|
|
|
// opts can be nil, it will be replaced with defaults.
|
|
|
|
// Any field in opts can be nil, they will be replaced by defaults.
|
2019-07-09 17:50:48 +00:00
|
|
|
func NewMemory(opts *MemRepoOptions) *MemRepo {
|
|
|
|
if opts == nil {
|
|
|
|
opts = &MemRepoOptions{}
|
|
|
|
}
|
2019-07-09 19:38:05 +00:00
|
|
|
if opts.ConfigF == nil {
|
2019-10-30 16:38:39 +00:00
|
|
|
opts.ConfigF = defConfForType
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
2019-07-09 19:38:05 +00:00
|
|
|
if opts.Ds == nil {
|
|
|
|
opts.Ds = dssync.MutexWrap(datastore.NewMapDatastore())
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
2019-07-18 14:01:39 +00:00
|
|
|
if opts.KeyStore == nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
opts.KeyStore = make(map[string]types.KeyInfo)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
2019-07-09 17:50:48 +00:00
|
|
|
|
|
|
|
return &MemRepo{
|
2020-11-01 13:01:26 +00:00
|
|
|
repoLock: make(chan struct{}, 1),
|
2020-11-06 19:34:25 +00:00
|
|
|
blockstore: blockstore.WrapIDStore(blockstore.NewTemporarySync()),
|
2020-11-01 13:01:26 +00:00
|
|
|
datastore: opts.Ds,
|
|
|
|
configF: opts.ConfigF,
|
|
|
|
keystore: opts.KeyStore,
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mem *MemRepo) APIEndpoint() (multiaddr.Multiaddr, error) {
|
|
|
|
mem.api.Lock()
|
|
|
|
defer mem.api.Unlock()
|
|
|
|
if mem.api.ma == nil {
|
|
|
|
return nil, ErrNoAPIEndpoint
|
|
|
|
}
|
|
|
|
return mem.api.ma, nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
func (mem *MemRepo) APIToken() ([]byte, error) {
|
|
|
|
mem.api.Lock()
|
|
|
|
defer mem.api.Unlock()
|
|
|
|
if mem.api.ma == nil {
|
|
|
|
return nil, ErrNoAPIToken
|
|
|
|
}
|
|
|
|
return mem.api.token, nil
|
|
|
|
}
|
|
|
|
|
2019-10-30 16:38:39 +00:00
|
|
|
func (mem *MemRepo) Lock(t RepoType) (LockedRepo, error) {
|
2019-07-09 17:50:48 +00:00
|
|
|
select {
|
|
|
|
case mem.repoLock <- struct{}{}:
|
|
|
|
default:
|
|
|
|
return nil, ErrRepoAlreadyLocked
|
|
|
|
}
|
|
|
|
mem.token = new(byte)
|
|
|
|
|
|
|
|
return &lockedMemRepo{
|
|
|
|
mem: mem,
|
2019-10-30 16:38:39 +00:00
|
|
|
t: t,
|
2019-07-09 17:50:48 +00:00
|
|
|
token: mem.token,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lmem *lockedMemRepo) checkToken() error {
|
|
|
|
lmem.RLock()
|
|
|
|
defer lmem.RUnlock()
|
|
|
|
if lmem.mem.token != lmem.token {
|
|
|
|
return ErrClosedRepo
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lmem *lockedMemRepo) Close() error {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lmem.Lock()
|
|
|
|
defer lmem.Unlock()
|
|
|
|
|
|
|
|
if lmem.mem.token != lmem.token {
|
|
|
|
return ErrClosedRepo
|
|
|
|
}
|
|
|
|
|
2019-11-12 17:59:38 +00:00
|
|
|
if lmem.tempDir != "" {
|
|
|
|
if err := os.RemoveAll(lmem.tempDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lmem.tempDir = ""
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:50:48 +00:00
|
|
|
lmem.mem.token = nil
|
|
|
|
lmem.mem.api.Lock()
|
|
|
|
lmem.mem.api.ma = nil
|
|
|
|
lmem.mem.api.Unlock()
|
2019-07-09 18:11:32 +00:00
|
|
|
<-lmem.mem.repoLock // unlock
|
2019-07-09 17:50:48 +00:00
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:25:34 +00:00
|
|
|
func (lmem *lockedMemRepo) Datastore(_ context.Context, ns string) (datastore.Batching, error) {
|
2019-07-09 17:50:48 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-10 15:38:35 +00:00
|
|
|
|
|
|
|
return namespace.Wrap(lmem.mem.datastore, datastore.NewKey(ns)), nil
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 10:04:00 +00:00
|
|
|
func (lmem *lockedMemRepo) Blockstore(ctx context.Context, domain BlockstoreDomain) (blockstore.Blockstore, error) {
|
2020-11-01 13:01:26 +00:00
|
|
|
if domain != BlockstoreChain {
|
|
|
|
return nil, ErrInvalidBlockstoreDomain
|
|
|
|
}
|
|
|
|
return lmem.mem.blockstore, nil
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:39:30 +00:00
|
|
|
func (lmem *lockedMemRepo) ListDatastores(ns string) ([]int64, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:03:37 +00:00
|
|
|
func (lmem *lockedMemRepo) DeleteDatastore(ns string) error {
|
|
|
|
/** poof **/
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-09 23:37:18 +00:00
|
|
|
func (lmem *lockedMemRepo) Config() (interface{}, error) {
|
2019-07-09 17:50:48 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-09 23:37:18 +00:00
|
|
|
|
2020-06-10 16:07:47 +00:00
|
|
|
lmem.mem.config.Lock()
|
|
|
|
defer lmem.mem.config.Unlock()
|
|
|
|
|
|
|
|
if lmem.mem.config.val == nil {
|
|
|
|
lmem.mem.config.val = lmem.mem.configF(lmem.t)
|
2020-06-09 23:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:07:47 +00:00
|
|
|
return lmem.mem.config.val, nil
|
2020-06-09 23:37:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 16:07:47 +00:00
|
|
|
func (lmem *lockedMemRepo) SetConfig(c func(interface{})) error {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-09 23:37:18 +00:00
|
|
|
|
2020-06-10 16:07:47 +00:00
|
|
|
lmem.mem.config.Lock()
|
|
|
|
defer lmem.mem.config.Unlock()
|
2019-07-09 17:50:48 +00:00
|
|
|
|
2020-06-10 16:07:47 +00:00
|
|
|
if lmem.mem.config.val == nil {
|
|
|
|
lmem.mem.config.val = lmem.mem.configF(lmem.t)
|
|
|
|
}
|
|
|
|
|
|
|
|
c(lmem.mem.config.val)
|
|
|
|
|
|
|
|
return nil
|
2020-03-03 22:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 17:50:48 +00:00
|
|
|
func (lmem *lockedMemRepo) SetAPIEndpoint(ma multiaddr.Multiaddr) error {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lmem.mem.api.Lock()
|
|
|
|
lmem.mem.api.ma = ma
|
|
|
|
lmem.mem.api.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
func (lmem *lockedMemRepo) SetAPIToken(token []byte) error {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lmem.mem.api.Lock()
|
|
|
|
lmem.mem.api.token = token
|
|
|
|
lmem.mem.api.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:57:49 +00:00
|
|
|
func (lmem *lockedMemRepo) KeyStore() (types.KeyStore, error) {
|
2019-07-18 14:01:39 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return lmem, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implement KeyStore on the same instance
|
|
|
|
|
|
|
|
// List lists all the keys stored in the KeyStore
|
|
|
|
func (lmem *lockedMemRepo) List() ([]string, error) {
|
2019-07-09 17:50:48 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-18 14:01:39 +00:00
|
|
|
lmem.RLock()
|
|
|
|
defer lmem.RUnlock()
|
|
|
|
|
|
|
|
res := make([]string, 0, len(lmem.mem.keystore))
|
|
|
|
for k := range lmem.mem.keystore {
|
|
|
|
res = append(res, k)
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 14:57:49 +00:00
|
|
|
// Get gets a key out of keystore and returns types.KeyInfo coresponding to named key
|
|
|
|
func (lmem *lockedMemRepo) Get(name string) (types.KeyInfo, error) {
|
2019-07-18 14:01:39 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
2019-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, err
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
lmem.RLock()
|
|
|
|
defer lmem.RUnlock()
|
|
|
|
|
|
|
|
key, ok := lmem.mem.keystore[name]
|
|
|
|
if !ok {
|
2019-10-17 10:18:40 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("getting key '%s': %w", name, types.ErrKeyInfoNotFound)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put saves key info under given name
|
2019-07-18 14:57:49 +00:00
|
|
|
func (lmem *lockedMemRepo) Put(name string, key types.KeyInfo) error {
|
2019-07-18 14:01:39 +00:00
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lmem.Lock()
|
|
|
|
defer lmem.Unlock()
|
|
|
|
|
|
|
|
_, isThere := lmem.mem.keystore[name]
|
|
|
|
if isThere {
|
2019-10-18 11:39:31 +00:00
|
|
|
return xerrors.Errorf("putting key '%s': %w", name, types.ErrKeyExists)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lmem.mem.keystore[name] = key
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lmem *lockedMemRepo) Delete(name string) error {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
lmem.Lock()
|
|
|
|
defer lmem.Unlock()
|
|
|
|
|
|
|
|
_, isThere := lmem.mem.keystore[name]
|
|
|
|
if !isThere {
|
2019-10-17 10:18:40 +00:00
|
|
|
return xerrors.Errorf("deleting key '%s': %w", name, types.ErrKeyInfoNotFound)
|
2019-07-18 14:01:39 +00:00
|
|
|
}
|
|
|
|
delete(lmem.mem.keystore, name)
|
|
|
|
return nil
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|