From 959fe371b050606c7dffa52bd4b1160f13562bb8 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 7 Aug 2019 21:24:49 -0700 Subject: [PATCH 1/2] move storage miner keys to full node --- cmd/lotus-storage-miner/init.go | 15 +-------------- go.mod | 1 + node/modules/storageminer.go | 8 ++++---- storage/miner.go | 21 ++++++++++++++------- 4 files changed, 20 insertions(+), 25 deletions(-) diff --git a/cmd/lotus-storage-miner/init.go b/cmd/lotus-storage-miner/init.go index fcc55d1bf..eb135cd19 100644 --- a/cmd/lotus-storage-miner/init.go +++ b/cmd/lotus-storage-miner/init.go @@ -10,7 +10,6 @@ import ( "github.com/filecoin-project/go-lotus/chain/actors" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/types" - "github.com/filecoin-project/go-lotus/chain/wallet" lcli "github.com/filecoin-project/go-lotus/cli" "github.com/filecoin-project/go-lotus/node/repo" ) @@ -66,18 +65,6 @@ var initCmd = &cli.Command{ } defer lr.Close() - log.Info("Initializing wallet") - - ks, err := lr.KeyStore() - if err != nil { - return err - } - - wallet, err := wallet.NewWallet(ks) - if err != nil { - return err - } - log.Info("Initializing libp2p identity") p2pSk, err := lr.Libp2pIdentity() @@ -102,7 +89,7 @@ var initCmd = &cli.Command{ return err } - k, err := wallet.GenerateKey(types.KTSecp256k1) // TODO: review: is this right? + k, err := api.WalletNew(ctx, types.KTSecp256k1) if err != nil { return err } diff --git a/go.mod b/go.mod index 01fa1e6c3..b0e39b63f 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,7 @@ require ( github.com/polydawn/refmt v0.0.0-20190731040541-eff0b363297a github.com/smartystreets/assertions v1.0.1 // indirect github.com/smartystreets/goconvey v0.0.0-20190710185942-9d28bd7c0945 // indirect + github.com/stretchr/objx v0.1.1 // indirect github.com/stretchr/testify v1.3.0 github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 github.com/whyrusleeping/pubsub v0.0.0-20131020042734-02de8aa2db3d diff --git a/node/modules/storageminer.go b/node/modules/storageminer.go index b1ac6a17a..0cd005c2a 100644 --- a/node/modules/storageminer.go +++ b/node/modules/storageminer.go @@ -2,10 +2,11 @@ package modules import ( "context" + "path/filepath" + "github.com/ipfs/go-bitswap" "github.com/ipfs/go-bitswap/network" "github.com/libp2p/go-libp2p-core/routing" - "path/filepath" "github.com/ipfs/go-blockservice" "github.com/ipfs/go-datastore" @@ -18,7 +19,6 @@ import ( "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/deals" - "github.com/filecoin-project/go-lotus/chain/wallet" "github.com/filecoin-project/go-lotus/lib/sectorbuilder" "github.com/filecoin-project/go-lotus/node/modules/dtypes" "github.com/filecoin-project/go-lotus/node/modules/helpers" @@ -81,13 +81,13 @@ func SectorBuilder(mctx helpers.MetricsCtx, lc fx.Lifecycle, sbc *sectorbuilder. return sb, nil } -func StorageMiner(mctx helpers.MetricsCtx, lc fx.Lifecycle, api api.FullNode, h host.Host, ds dtypes.MetadataDS, sb *sectorbuilder.SectorBuilder, w *wallet.Wallet) (*storage.Miner, error) { +func StorageMiner(mctx helpers.MetricsCtx, lc fx.Lifecycle, api api.FullNode, h host.Host, ds dtypes.MetadataDS, sb *sectorbuilder.SectorBuilder) (*storage.Miner, error) { maddr, err := minerAddrFromDS(ds) if err != nil { return nil, err } - sm, err := storage.NewMiner(api, maddr, h, ds, sb, w) + sm, err := storage.NewMiner(api, maddr, h, ds, sb) if err != nil { return nil, err } diff --git a/storage/miner.go b/storage/miner.go index ee90fa3b5..a303bef11 100644 --- a/storage/miner.go +++ b/storage/miner.go @@ -9,7 +9,6 @@ import ( "github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/types" - "github.com/filecoin-project/go-lotus/chain/wallet" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log" @@ -26,8 +25,6 @@ type Miner struct { sb *sectorbuilder.SectorBuilder - w *wallet.Wallet - maddr address.Address worker address.Address @@ -49,16 +46,19 @@ type storageMinerApi interface { ChainWaitMsg(context.Context, cid.Cid) (*api.MsgWait, error) ChainNotify(context.Context) (<-chan *store.HeadChange, error) + + WalletBalance(context.Context, address.Address) (types.BigInt, error) + WalletSign(context.Context, address.Address, []byte) (*types.Signature, error) + WalletList(context.Context) ([]address.Address, error) } -func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datastore.Batching, sb *sectorbuilder.SectorBuilder, w *wallet.Wallet) (*Miner, error) { +func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datastore.Batching, sb *sectorbuilder.SectorBuilder) (*Miner, error) { return &Miner{ api: api, maddr: addr, h: h, ds: ds, sb: sb, - w: w, }, nil } @@ -138,7 +138,7 @@ func (m *Miner) commitSector(ctx context.Context, sinfo sectorbuilder.SectorSeal return errors.Wrap(err, "serializing commit sector message") } - sig, err := m.w.Sign(m.worker, data) + sig, err := m.api.WalletSign(ctx, m.worker, data) if err != nil { return errors.Wrap(err, "signing commit sector message") } @@ -173,10 +173,17 @@ func (m *Miner) runPreflightChecks(ctx context.Context) error { m.worker = worker - has, err := m.w.HasKey(worker) + addrs, err := m.api.WalletList(ctx) if err != nil { return errors.Wrap(err, "failed to check wallet for worker key") } + var has bool + for _, a := range addrs { + if a == worker { + has = true + break + } + } if !has { return errors.New("key for worker not found in local wallet") From 28ef081d0e64dbc7020d0c456e7a8293027d86a0 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 8 Aug 2019 10:29:23 -0700 Subject: [PATCH 2/2] use wallet has instead of list --- api/api.go | 1 + api/struct.go | 5 +++++ node/impl/full.go | 4 ++++ storage/miner.go | 11 ++--------- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/api/api.go b/api/api.go index 8e9bd6b29..5ca4a156b 100644 --- a/api/api.go +++ b/api/api.go @@ -107,6 +107,7 @@ type FullNode interface { // wallet WalletNew(context.Context, string) (address.Address, error) + WalletHas(context.Context, address.Address) (bool, error) WalletList(context.Context) ([]address.Address, error) WalletBalance(context.Context, address.Address) (types.BigInt, error) WalletSign(context.Context, address.Address, []byte) (*types.Signature, error) diff --git a/api/struct.go b/api/struct.go index 332a36db9..2073f144f 100644 --- a/api/struct.go +++ b/api/struct.go @@ -56,6 +56,7 @@ type FullNodeStruct struct { MinerCreateBlock func(context.Context, address.Address, *types.TipSet, []types.Ticket, types.ElectionProof, []*types.SignedMessage) (*chain.BlockMsg, error) `perm:"write"` WalletNew func(context.Context, string) (address.Address, error) `perm:"write"` + WalletHas func(context.Context, address.Address) (bool, error) `perm:"write"` WalletList func(context.Context) ([]address.Address, error) `perm:"write"` WalletBalance func(context.Context, address.Address) (types.BigInt, error) `perm:"read"` WalletSign func(context.Context, address.Address, []byte) (*types.Signature, error) `perm:"sign"` @@ -173,6 +174,10 @@ func (c *FullNodeStruct) WalletNew(ctx context.Context, typ string) (address.Add return c.Internal.WalletNew(ctx, typ) } +func (c *FullNodeStruct) WalletHas(ctx context.Context, addr address.Address) (bool, error) { + return c.Internal.WalletHas(ctx, addr) +} + func (c *FullNodeStruct) WalletList(ctx context.Context) ([]address.Address, error) { return c.Internal.WalletList(ctx) } diff --git a/node/impl/full.go b/node/impl/full.go index 3fb40eb84..aae954b0c 100644 --- a/node/impl/full.go +++ b/node/impl/full.go @@ -244,6 +244,10 @@ func (a *FullNodeAPI) WalletNew(ctx context.Context, typ string) (address.Addres return a.Wallet.GenerateKey(typ) } +func (a *FullNodeAPI) WalletHas(ctx context.Context, addr address.Address) (bool, error) { + return a.Wallet.HasKey(addr) +} + func (a *FullNodeAPI) WalletList(ctx context.Context) ([]address.Address, error) { return a.Wallet.ListAddrs() } diff --git a/storage/miner.go b/storage/miner.go index a303bef11..4c74cd7f8 100644 --- a/storage/miner.go +++ b/storage/miner.go @@ -49,7 +49,7 @@ type storageMinerApi interface { WalletBalance(context.Context, address.Address) (types.BigInt, error) WalletSign(context.Context, address.Address, []byte) (*types.Signature, error) - WalletList(context.Context) ([]address.Address, error) + WalletHas(context.Context, address.Address) (bool, error) } func NewMiner(api storageMinerApi, addr address.Address, h host.Host, ds datastore.Batching, sb *sectorbuilder.SectorBuilder) (*Miner, error) { @@ -173,17 +173,10 @@ func (m *Miner) runPreflightChecks(ctx context.Context) error { m.worker = worker - addrs, err := m.api.WalletList(ctx) + has, err := m.api.WalletHas(ctx, worker) if err != nil { return errors.Wrap(err, "failed to check wallet for worker key") } - var has bool - for _, a := range addrs { - if a == worker { - has = true - break - } - } if !has { return errors.New("key for worker not found in local wallet")