lotus/node/modules/core.go

219 lines
5.3 KiB
Go
Raw Normal View History

package modules
import (
2019-07-25 11:46:48 +00:00
"bytes"
2019-07-08 13:36:43 +00:00
"context"
2019-07-23 20:37:06 +00:00
"crypto/rand"
"io"
"io/ioutil"
2019-07-12 09:59:18 +00:00
"path/filepath"
2019-07-24 01:16:17 +00:00
"github.com/gbrlsnchs/jwt/v3"
2019-07-08 13:36:43 +00:00
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
2019-07-16 16:02:51 +00:00
"github.com/ipfs/go-blockservice"
2019-07-25 11:46:48 +00:00
"github.com/ipfs/go-car"
2019-07-10 15:38:35 +00:00
"github.com/ipfs/go-datastore"
2019-07-16 16:02:51 +00:00
"github.com/ipfs/go-datastore/namespace"
"github.com/ipfs/go-filestore"
2019-07-08 13:36:43 +00:00
blockstore "github.com/ipfs/go-ipfs-blockstore"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
2019-07-12 09:59:18 +00:00
offline "github.com/ipfs/go-ipfs-exchange-offline"
ipld "github.com/ipfs/go-ipld-format"
2019-07-03 17:39:07 +00:00
logging "github.com/ipfs/go-log"
2019-07-16 16:02:51 +00:00
"github.com/ipfs/go-merkledag"
2019-07-08 13:36:43 +00:00
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peerstore"
2019-07-08 13:36:43 +00:00
"github.com/libp2p/go-libp2p-core/routing"
record "github.com/libp2p/go-libp2p-record"
2019-07-08 13:36:43 +00:00
"go.uber.org/fx"
2019-07-24 01:16:17 +00:00
"golang.org/x/xerrors"
2019-07-24 01:16:17 +00:00
"github.com/filecoin-project/go-lotus/api"
2019-07-26 04:54:22 +00:00
"github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/node/modules/helpers"
2019-07-10 15:38:35 +00:00
"github.com/filecoin-project/go-lotus/node/repo"
)
2019-07-03 17:39:07 +00:00
var log = logging.Logger("modules")
type Genesis func() (*types.BlockHeader, error)
2019-07-08 13:36:43 +00:00
// RecordValidator provides namesys compatible routing record validator
func RecordValidator(ps peerstore.Peerstore) record.Validator {
return record.NamespacedValidator{
"pk": record.PublicKeyValidator{},
}
}
2019-07-08 13:36:43 +00:00
func Bitswap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return exch.Close()
},
})
return exch
}
2019-07-26 04:54:22 +00:00
func SetGenesis(cs *store.ChainStore, g Genesis) error {
2019-07-24 22:49:37 +00:00
_, err := cs.GetGenesis()
if err == nil {
return nil // already set, noop
}
if err != datastore.ErrNotFound {
return err
}
genesis, err := g()
if err != nil {
return err
}
return cs.SetGenesis(genesis)
2019-07-08 13:36:43 +00:00
}
2019-07-10 15:38:35 +00:00
func LockedRepo(lr repo.LockedRepo) func(lc fx.Lifecycle) repo.LockedRepo {
return func(lc fx.Lifecycle) repo.LockedRepo {
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
return lr.Close()
},
})
return lr
}
}
func KeyStore(lr repo.LockedRepo) (types.KeyStore, error) {
return lr.KeyStore()
}
2019-07-23 20:23:44 +00:00
const JWTSecretName = "auth-jwt-private"
type APIAlg jwt.HMACSHA
2019-07-23 20:37:06 +00:00
type jwtPayload struct {
Allow []string
}
func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) {
2019-07-23 20:23:44 +00:00
key, err := keystore.Get(JWTSecretName)
if err != nil {
2019-07-23 20:37:06 +00:00
log.Warn("Generating new API secret")
sk, err := ioutil.ReadAll(io.LimitReader(rand.Reader, 32))
if err != nil {
return nil, err
}
key = types.KeyInfo{
Type: "jwt-hmac-secret",
PrivateKey: sk,
}
if err := keystore.Put(JWTSecretName, key); err != nil {
return nil, xerrors.Errorf("writing API secret: %w", err)
}
// TODO: make this configurable
p := jwtPayload{
Allow: api.AllPermissions,
}
cliToken, err := jwt.Sign(&p, jwt.NewHS256(key.PrivateKey))
if err != nil {
return nil, err
}
if err := lr.SetAPIToken(cliToken); err != nil {
return nil, err
}
2019-07-23 20:23:44 +00:00
}
2019-07-23 20:37:06 +00:00
2019-07-23 20:23:44 +00:00
return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil
}
2019-07-10 15:38:35 +00:00
func Datastore(r repo.LockedRepo) (datastore.Batching, error) {
return r.Datastore("/metadata")
}
func Blockstore(r repo.LockedRepo) (blockstore.Blockstore, error) {
blocks, err := r.Datastore("/blocks")
if err != nil {
return nil, err
}
bs := blockstore.NewBlockstore(blocks)
return blockstore.NewIdStore(bs), nil
}
2019-07-12 09:59:18 +00:00
2019-07-12 10:44:01 +00:00
func ClientFstore(r repo.LockedRepo) (*filestore.Filestore, error) {
2019-07-12 09:59:18 +00:00
clientds, err := r.Datastore("/client")
if err != nil {
return nil, err
}
blocks := namespace.Wrap(clientds, datastore.NewKey("blocks"))
fm := filestore.NewFileManager(clientds, filepath.Dir(r.Path()))
2019-07-12 10:17:44 +00:00
fm.AllowFiles = true
// TODO: fm.AllowUrls (needs more code in client import)
2019-07-12 09:59:18 +00:00
bs := blockstore.NewBlockstore(blocks)
2019-07-12 10:44:01 +00:00
return filestore.NewFilestore(bs, fm), nil
}
func ClientDAG(lc fx.Lifecycle, fstore *filestore.Filestore) ipld.DAGService {
2019-07-12 09:59:18 +00:00
ibs := blockstore.NewIdStore(fstore)
bsvc := blockservice.New(ibs, offline.Exchange(ibs))
dag := merkledag.NewDAGService(bsvc)
lc.Append(fx.Hook{
OnStop: func(_ context.Context) error {
return bsvc.Close()
},
})
2019-07-12 10:44:01 +00:00
return dag
2019-07-12 09:59:18 +00:00
}
2019-07-24 21:10:27 +00:00
2019-07-26 04:54:22 +00:00
func ChainStore(lc fx.Lifecycle, bs blockstore.Blockstore, ds datastore.Batching) *store.ChainStore {
chain := store.NewChainStore(bs, ds)
2019-07-24 21:10:27 +00:00
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
return chain.Load()
},
})
return chain
}
2019-07-24 22:49:37 +00:00
func ErrorGenesis() Genesis {
return func() (header *types.BlockHeader, e error) {
return nil, xerrors.New("No genesis block provided, provide the file with 'lotus daemon --genesis=[genesis file]'")
2019-07-24 22:49:37 +00:00
}
}
2019-07-25 11:46:48 +00:00
func LoadGenesis(genBytes []byte) func(blockstore.Blockstore) Genesis {
return func(bs blockstore.Blockstore) Genesis {
return func() (header *types.BlockHeader, e error) {
2019-07-25 11:46:48 +00:00
c, err := car.LoadCar(bs, bytes.NewReader(genBytes))
if err != nil {
return nil, err
}
if len(c.Roots) != 1 {
return nil, xerrors.New("expected genesis file to have one root")
}
root, err := bs.Get(c.Roots[0])
if err != nil {
return &types.BlockHeader{}, err
2019-07-25 11:46:48 +00:00
}
return types.DecodeBlock(root.RawData())
2019-07-24 22:49:37 +00:00
}
}
}