2019-07-09 17:50:48 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"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"
|
2019-07-09 18:11:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2019-07-09 17:50:48 +00:00
|
|
|
"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-07-18 14:57:49 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-09 18:11:32 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/node/config"
|
2019-07-09 17:50:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemRepo struct {
|
|
|
|
api struct {
|
|
|
|
sync.Mutex
|
|
|
|
ma multiaddr.Multiaddr
|
|
|
|
}
|
|
|
|
|
|
|
|
repoLock chan struct{}
|
|
|
|
token *byte
|
|
|
|
|
|
|
|
datastore datastore.Datastore
|
|
|
|
configF func() *config.Root
|
|
|
|
libp2pKey crypto.PrivKey
|
2019-07-18 14:57:49 +00:00
|
|
|
keystore map[string]types.KeyInfo
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lockedMemRepo struct {
|
|
|
|
mem *MemRepo
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
token *byte
|
|
|
|
}
|
|
|
|
|
2019-07-12 09:59:18 +00:00
|
|
|
func (lmem *lockedMemRepo) Path() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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-07-09 19:38:05 +00:00
|
|
|
Ds datastore.Datastore
|
|
|
|
ConfigF func() *config.Root
|
|
|
|
Libp2pKey crypto.PrivKey
|
2019-07-18 14:57:49 +00:00
|
|
|
KeyStore map[string]types.KeyInfo
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 13:23:29 +00:00
|
|
|
func genLibp2pKey() (crypto.PrivKey, error) {
|
|
|
|
pk, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
opts.ConfigF = config.Default
|
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-09 19:38:05 +00:00
|
|
|
if opts.Libp2pKey == nil {
|
2019-07-10 13:23:29 +00:00
|
|
|
pk, err := genLibp2pKey()
|
2019-07-09 17:50:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-07-09 19:38:05 +00:00
|
|
|
opts.Libp2pKey = pk
|
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{
|
|
|
|
repoLock: make(chan struct{}, 1),
|
|
|
|
|
2019-07-09 19:38:05 +00:00
|
|
|
datastore: opts.Ds,
|
|
|
|
configF: opts.ConfigF,
|
|
|
|
libp2pKey: opts.Libp2pKey,
|
2019-07-18 14:01:39 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mem *MemRepo) Lock() (LockedRepo, error) {
|
|
|
|
select {
|
|
|
|
case mem.repoLock <- struct{}{}:
|
|
|
|
default:
|
|
|
|
return nil, ErrRepoAlreadyLocked
|
|
|
|
}
|
|
|
|
mem.token = new(byte)
|
|
|
|
|
|
|
|
return &lockedMemRepo{
|
|
|
|
mem: mem,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:38:35 +00:00
|
|
|
func (lmem *lockedMemRepo) Datastore(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
|
|
|
}
|
|
|
|
|
|
|
|
func (lmem *lockedMemRepo) Config() (*config.Root, error) {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return lmem.mem.configF(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lmem *lockedMemRepo) Libp2pIdentity() (crypto.PrivKey, error) {
|
|
|
|
if err := lmem.checkToken(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return lmem.mem.libp2pKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-07-18 14:57:49 +00:00
|
|
|
return types.KeyInfo{}, xerrors.Errorf("getting key '%s': %w", name, ErrKeyNotFound)
|
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 {
|
|
|
|
return xerrors.Errorf("putting key '%s': %w", name, ErrKeyExists)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return xerrors.Errorf("deleting key '%s': %w", name, ErrKeyNotFound)
|
|
|
|
}
|
|
|
|
delete(lmem.mem.keystore, name)
|
|
|
|
return nil
|
2019-07-09 17:50:48 +00:00
|
|
|
}
|