From 772dd6c549617d6c1fdb86fbb040c2b343649b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 24 Jul 2019 02:58:31 +0200 Subject: [PATCH] Implement storage miner API in node --- api/struct.go | 60 ++++++++++++------------ cmd/lotus-storage-miner/run.go | 8 ++-- node/builder.go | 9 +++- node/commonapi.go | 83 ++++++++++++++++++++++++++++++++++ node/{api.go => fullapi.go} | 69 +--------------------------- node/storminerapi.go | 11 +++++ 6 files changed, 139 insertions(+), 101 deletions(-) create mode 100644 node/commonapi.go rename node/{api.go => fullapi.go} (69%) create mode 100644 node/storminerapi.go diff --git a/api/struct.go b/api/struct.go index dc3153e30..08f03e9f1 100644 --- a/api/struct.go +++ b/api/struct.go @@ -28,36 +28,6 @@ type CommonStruct struct { } } -func (c *CommonStruct) AuthVerify(ctx context.Context, token string) ([]string, error) { - return c.Internal.AuthVerify(ctx, token) -} - -func (c *CommonStruct) AuthNew(ctx context.Context, perms []string) ([]byte, error) { - return c.Internal.AuthNew(ctx, perms) -} - -func (c *CommonStruct) NetPeers(ctx context.Context) ([]peer.AddrInfo, error) { - return c.Internal.NetPeers(ctx) -} - -func (c *CommonStruct) NetConnect(ctx context.Context, p peer.AddrInfo) error { - return c.Internal.NetConnect(ctx, p) -} - -func (c *CommonStruct) NetAddrsListen(ctx context.Context) (peer.AddrInfo, error) { - return c.Internal.NetAddrsListen(ctx) -} - -// ID implements API.ID -func (c *CommonStruct) ID(ctx context.Context) (peer.ID, error) { - return c.Internal.ID(ctx) -} - -// Version implements API.Version -func (c *CommonStruct) Version(ctx context.Context) (Version, error) { - return c.Internal.Version(ctx) -} - // FullNodeStruct implements API passing calls to user-provided function values. type FullNodeStruct struct { CommonStruct @@ -96,6 +66,36 @@ type StorageMinerStruct struct { } } +func (c *CommonStruct) AuthVerify(ctx context.Context, token string) ([]string, error) { + return c.Internal.AuthVerify(ctx, token) +} + +func (c *CommonStruct) AuthNew(ctx context.Context, perms []string) ([]byte, error) { + return c.Internal.AuthNew(ctx, perms) +} + +func (c *CommonStruct) NetPeers(ctx context.Context) ([]peer.AddrInfo, error) { + return c.Internal.NetPeers(ctx) +} + +func (c *CommonStruct) NetConnect(ctx context.Context, p peer.AddrInfo) error { + return c.Internal.NetConnect(ctx, p) +} + +func (c *CommonStruct) NetAddrsListen(ctx context.Context) (peer.AddrInfo, error) { + return c.Internal.NetAddrsListen(ctx) +} + +// ID implements API.ID +func (c *CommonStruct) ID(ctx context.Context) (peer.ID, error) { + return c.Internal.ID(ctx) +} + +// Version implements API.Version +func (c *CommonStruct) Version(ctx context.Context) (Version, error) { + return c.Internal.Version(ctx) +} + func (c *FullNodeStruct) ClientListImports(ctx context.Context) ([]Import, error) { return c.Internal.ClientListImports(ctx) } diff --git a/cmd/lotus-storage-miner/run.go b/cmd/lotus-storage-miner/run.go index 5b25642c7..29396b7d8 100644 --- a/cmd/lotus-storage-miner/run.go +++ b/cmd/lotus-storage-miner/run.go @@ -7,6 +7,7 @@ import ( "golang.org/x/xerrors" "gopkg.in/urfave/cli.v2" + "github.com/filecoin-project/go-lotus/api" lcli "github.com/filecoin-project/go-lotus/cli" "github.com/filecoin-project/go-lotus/lib/jsonrpc" "github.com/filecoin-project/go-lotus/node" @@ -23,13 +24,13 @@ var runCmd = &cli.Command{ }, }, Action: func(cctx *cli.Context) error { - api, err := lcli.GetAPI(cctx) + nodeApi, err := lcli.GetAPI(cctx) if err != nil { return err } ctx := lcli.ReqContext(cctx) - v, err := api.Version(ctx) + v, err := nodeApi.Version(ctx) r, err := repo.NewFS(cctx.String(FlagStorageRepo)) if err != nil { @@ -44,8 +45,9 @@ var runCmd = &cli.Command{ return xerrors.Errorf("repo at '%s' is not initialized, run 'lotus-storage-miner init' to set it up", cctx.String(FlagStorageRepo)) } + var minerapi api.StorageMiner err = node.New(ctx, - node.StorageMiner(), + node.StorageMiner(&minerapi), node.Online(), node.Repo(r), diff --git a/node/builder.go b/node/builder.go index 2637a1536..2813a55d1 100644 --- a/node/builder.go +++ b/node/builder.go @@ -207,7 +207,7 @@ func Online() Option { ) } -func StorageMiner() Option { +func StorageMiner(out *api.StorageMiner) Option { return Options( ApplyIf(func(s *Settings) bool { return s.Config }, Error(errors.New("the StorageMiner option must be set before Config option")), @@ -220,6 +220,13 @@ func StorageMiner() Option { s.nodeType = nodeStorageMiner return nil }, + + func(s *Settings) error { + resAPI := &StorageMinerAPI{} + s.invokes[ExtractApiKey] = fx.Extract(resAPI) + *out = resAPI + return nil + }, ) } diff --git a/node/commonapi.go b/node/commonapi.go new file mode 100644 index 000000000..5e3f2ac89 --- /dev/null +++ b/node/commonapi.go @@ -0,0 +1,83 @@ +package node + +import ( + "context" + + "github.com/gbrlsnchs/jwt/v3" + "github.com/libp2p/go-libp2p-core/host" + "github.com/libp2p/go-libp2p-core/peer" + ma "github.com/multiformats/go-multiaddr" + "go.uber.org/fx" + "golang.org/x/xerrors" + + "github.com/filecoin-project/go-lotus/api" + "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/node/modules" +) + +type CommonAPI struct { + fx.In + + APISecret *modules.APIAlg + Host host.Host +} + +type jwtPayload struct { + Allow []string +} + +func (a *CommonAPI) AuthVerify(ctx context.Context, token string) ([]string, error) { + var payload jwtPayload + if _, err := jwt.Verify([]byte(token), (*jwt.HMACSHA)(a.APISecret), &payload); err != nil { + return nil, xerrors.Errorf("JWT Verification failed: %w", err) + } + + return payload.Allow, nil +} + +func (a *CommonAPI) AuthNew(ctx context.Context, perms []string) ([]byte, error) { + p := jwtPayload{ + Allow: perms, // TODO: consider checking validity + } + + return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret)) +} + +func (a *CommonAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) { + conns := a.Host.Network().Conns() + out := make([]peer.AddrInfo, len(conns)) + + for i, conn := range conns { + out[i] = peer.AddrInfo{ + ID: conn.RemotePeer(), + Addrs: []ma.Multiaddr{ + conn.RemoteMultiaddr(), + }, + } + } + + return out, nil +} + +func (a *CommonAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error { + return a.Host.Connect(ctx, p) +} + +func (a *CommonAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) { + return peer.AddrInfo{ + ID: a.Host.ID(), + Addrs: a.Host.Addrs(), + }, nil +} + +func (a *CommonAPI) ID(context.Context) (peer.ID, error) { + return a.Host.ID(), nil +} + +func (a *CommonAPI) Version(context.Context) (api.Version, error) { + return api.Version{ + Version: build.Version, + }, nil +} + +var _ api.Common = &CommonAPI{} diff --git a/node/api.go b/node/fullapi.go similarity index 69% rename from node/api.go rename to node/fullapi.go index 3b5d85525..fea0f3689 100644 --- a/node/api.go +++ b/node/fullapi.go @@ -4,22 +4,15 @@ import ( "context" "github.com/filecoin-project/go-lotus/api" - "github.com/filecoin-project/go-lotus/build" "github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/miner" "github.com/filecoin-project/go-lotus/node/client" - "github.com/filecoin-project/go-lotus/node/modules" - "github.com/gbrlsnchs/jwt/v3" "github.com/ipfs/go-cid" logging "github.com/ipfs/go-log" - "github.com/libp2p/go-libp2p-core/host" - "github.com/libp2p/go-libp2p-core/peer" pubsub "github.com/libp2p/go-libp2p-pubsub" - ma "github.com/multiformats/go-multiaddr" - "golang.org/x/xerrors" ) var log = logging.Logger("node") @@ -27,33 +20,12 @@ var log = logging.Logger("node") type FullNodeAPI struct { client.LocalStorage - Host host.Host + CommonAPI + Chain *chain.ChainStore PubSub *pubsub.PubSub Mpool *chain.MessagePool Wallet *chain.Wallet - APISecret *modules.APIAlg -} - -type jwtPayload struct { - Allow []string -} - -func (a *FullNodeAPI) AuthVerify(ctx context.Context, token string) ([]string, error) { - var payload jwtPayload - if _, err := jwt.Verify([]byte(token), (*jwt.HMACSHA)(a.APISecret), &payload); err != nil { - return nil, xerrors.Errorf("JWT Verification failed: %w", err) - } - - return payload.Allow, nil -} - -func (a *FullNodeAPI) AuthNew(ctx context.Context, perms []string) ([]byte, error) { - p := jwtPayload{ - Allow: perms, // TODO: consider checking validity - } - - return jwt.Sign(&p, (*jwt.HMACSHA)(a.APISecret)) } func (a *FullNodeAPI) ChainSubmitBlock(ctx context.Context, blk *chain.BlockMsg) error { @@ -96,16 +68,6 @@ func (a *FullNodeAPI) ChainGetBlockMessages(ctx context.Context, msg cid.Cid) ([ return a.Chain.MessagesForBlock(b) } -func (a *FullNodeAPI) ID(context.Context) (peer.ID, error) { - return a.Host.ID(), nil -} - -func (a *FullNodeAPI) Version(context.Context) (api.Version, error) { - return api.Version{ - Version: build.Version, - }, nil -} - func (a *FullNodeAPI) MpoolPending(ctx context.Context, ts *chain.TipSet) ([]*chain.SignedMessage, error) { // TODO: need to make sure we don't return messages that were already included in the referenced chain // also need to accept ts == nil just fine, assume nil == chain.Head() @@ -149,22 +111,6 @@ func (a *FullNodeAPI) MinerCreateBlock(ctx context.Context, addr address.Address return &out, nil } -func (a *FullNodeAPI) NetPeers(context.Context) ([]peer.AddrInfo, error) { - conns := a.Host.Network().Conns() - out := make([]peer.AddrInfo, len(conns)) - - for i, conn := range conns { - out[i] = peer.AddrInfo{ - ID: conn.RemotePeer(), - Addrs: []ma.Multiaddr{ - conn.RemoteMultiaddr(), - }, - } - } - - return out, nil -} - func (a *FullNodeAPI) WalletNew(ctx context.Context, typ string) (address.Address, error) { return a.Wallet.GenerateKey(typ) } @@ -191,15 +137,4 @@ func (a *FullNodeAPI) WalletDefaultAddress(ctx context.Context) (address.Address return addrs[0], nil } -func (a *FullNodeAPI) NetConnect(ctx context.Context, p peer.AddrInfo) error { - return a.Host.Connect(ctx, p) -} - -func (a *FullNodeAPI) NetAddrsListen(context.Context) (peer.AddrInfo, error) { - return peer.AddrInfo{ - ID: a.Host.ID(), - Addrs: a.Host.Addrs(), - }, nil -} - var _ api.FullNode = &FullNodeAPI{} diff --git a/node/storminerapi.go b/node/storminerapi.go new file mode 100644 index 000000000..4108a3e73 --- /dev/null +++ b/node/storminerapi.go @@ -0,0 +1,11 @@ +package node + +import ( + "github.com/filecoin-project/go-lotus/api" +) + +type StorageMinerAPI struct { + CommonAPI +} + +var _ api.StorageMiner = &StorageMinerAPI{}