From eda26faf219b939646f6ca04f3dd1c445e0e5a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 20 Aug 2019 19:19:24 +0200 Subject: [PATCH] Register miner address from storageminer process --- chain/sync_test.go | 2 +- cmd/lotus-storage-miner/init.go | 6 ++++++ miner/miner.go | 6 +++++- node/builder.go | 6 ++++-- node/impl/common.go | 10 +++++----- node/impl/full.go | 6 +++--- node/modules/core.go | 7 +++---- node/modules/dtypes/api.go | 5 +++++ node/modules/storageminer.go | 15 +++++++++++++++ 9 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 node/modules/dtypes/api.go diff --git a/chain/sync_test.go b/chain/sync_test.go index 6c2bbef3f..3d03b9f0c 100644 --- a/chain/sync_test.go +++ b/chain/sync_test.go @@ -194,7 +194,7 @@ func (tu *syncTestUtil) submitSourceBlock(to int, h int) { // -1 to match block.Height b.Header = tu.blocks[h-1].Header for _, msg := range tu.blocks[h-1].SecpkMessages { - c, err := tu.nds[to].(*impl.API).ChainAPI.Chain.PutMessage(msg) + c, err := tu.nds[to].(*impl.FullNodeAPI).ChainAPI.Chain.PutMessage(msg) require.NoError(tu.t, err) b.SecpkMessages = append(b.SecpkMessages, c) diff --git a/cmd/lotus-storage-miner/init.go b/cmd/lotus-storage-miner/init.go index 1f2235cbf..43aa3cf77 100644 --- a/cmd/lotus-storage-miner/init.go +++ b/cmd/lotus-storage-miner/init.go @@ -123,6 +123,12 @@ var initCmd = &cli.Command{ } func configureStorageMiner(ctx context.Context, api api.FullNode, addr address.Address, peerid peer.ID) error { + // We may be one of genesis miners, start mining before trying to do any chain operations + // (otherwise our messages won't be mined) + if err := api.MinerRegister(ctx, addr); err != nil { + return err + } + // This really just needs to be an api call at this point... recp, err := api.ChainCall(ctx, &types.Message{ To: addr, diff --git a/miner/miner.go b/miner/miner.go index 81d7d9925..d7bf49444 100644 --- a/miner/miner.go +++ b/miner/miner.go @@ -56,7 +56,11 @@ func (m *Miner) Register(addr address.Address) error { defer m.lk.Unlock() if len(m.addresses) > 0 { - return errors.New("mining with more than one storage miner instance not supported yet") // TODO ! + if len(m.addresses) > 1 || m.addresses[0] != addr { + return errors.New("mining with more than one storage miner instance not supported yet") // TODO ! + } + + log.Warnf("miner.Register called more than once for actor '%s'", addr) } m.addresses = append(m.addresses, addr) diff --git a/node/builder.go b/node/builder.go index f54cdfe8b..5ef4b5b3e 100644 --- a/node/builder.go +++ b/node/builder.go @@ -80,6 +80,7 @@ const ( // storage miner HandleDealsKey RunSectorServiceKey + RegisterMinerKey // daemon ExtractApiKey @@ -238,6 +239,7 @@ func Online() Option { Override(new(*deals.Handler), deals.NewHandler), Override(HandleDealsKey, modules.HandleDeals), Override(RunSectorServiceKey, modules.RunSectorService), + Override(RegisterMinerKey, modules.RegisterMiner), ), ) } @@ -306,13 +308,13 @@ func Repo(r repo.Repo) Option { Override(new(types.KeyStore), modules.KeyStore), - Override(new(*modules.APIAlg), modules.APISecret), + Override(new(*dtypes.APIAlg), modules.APISecret), ) } func FullAPI(out *api.FullNode) Option { return func(s *Settings) error { - resAPI := &impl.API{} + resAPI := &impl.FullNodeAPI{} s.invokes[ExtractApiKey] = fx.Extract(resAPI) *out = resAPI return nil diff --git a/node/impl/common.go b/node/impl/common.go index dc91f97aa..6c564f739 100644 --- a/node/impl/common.go +++ b/node/impl/common.go @@ -3,6 +3,10 @@ package impl import ( "context" + "github.com/filecoin-project/go-lotus/api" + "github.com/filecoin-project/go-lotus/build" + "github.com/filecoin-project/go-lotus/node/modules/dtypes" + "github.com/gbrlsnchs/jwt/v3" "github.com/libp2p/go-libp2p-core/host" "github.com/libp2p/go-libp2p-core/network" @@ -10,16 +14,12 @@ import ( 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 + APISecret *dtypes.APIAlg Host host.Host } diff --git a/node/impl/full.go b/node/impl/full.go index c617b57b6..0b266f6c0 100644 --- a/node/impl/full.go +++ b/node/impl/full.go @@ -12,7 +12,7 @@ import ( var log = logging.Logger("node") -type API struct { +type FullNodeAPI struct { CommonAPI full.ChainAPI full.ClientAPI @@ -24,8 +24,8 @@ type API struct { Miner *miner.Miner } -func (a *API) MinerRegister(ctx context.Context, addr address.Address) error { +func (a *FullNodeAPI) MinerRegister(ctx context.Context, addr address.Address) error { return a.Miner.Register(addr) } -var _ api.FullNode = &API{} +var _ api.FullNode = &FullNodeAPI{} diff --git a/node/modules/core.go b/node/modules/core.go index 849fce85f..547cf0fe1 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -13,6 +13,7 @@ import ( "github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/chain/types" + "github.com/filecoin-project/go-lotus/node/modules/dtypes" "github.com/filecoin-project/go-lotus/node/repo" ) @@ -29,13 +30,11 @@ func RecordValidator(ps peerstore.Peerstore) record.Validator { const JWTSecretName = "auth-jwt-private" -type APIAlg jwt.HMACSHA - type jwtPayload struct { Allow []string } -func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) { +func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*dtypes.APIAlg, error) { key, err := keystore.Get(JWTSecretName) if err != nil { log.Warn("Generating new API secret") @@ -69,5 +68,5 @@ func APISecret(keystore types.KeyStore, lr repo.LockedRepo) (*APIAlg, error) { } } - return (*APIAlg)(jwt.NewHS256(key.PrivateKey)), nil + return (*dtypes.APIAlg)(jwt.NewHS256(key.PrivateKey)), nil } diff --git a/node/modules/dtypes/api.go b/node/modules/dtypes/api.go new file mode 100644 index 000000000..c0e57d911 --- /dev/null +++ b/node/modules/dtypes/api.go @@ -0,0 +1,5 @@ +package dtypes + +import "github.com/gbrlsnchs/jwt/v3" + +type APIAlg jwt.HMACSHA diff --git a/node/modules/storageminer.go b/node/modules/storageminer.go index df068c99c..773e488cc 100644 --- a/node/modules/storageminer.go +++ b/node/modules/storageminer.go @@ -125,3 +125,18 @@ func StagingDAG(mctx helpers.MetricsCtx, lc fx.Lifecycle, r repo.LockedRepo, rt return dag, nil } + +func RegisterMiner(lc fx.Lifecycle, ds dtypes.MetadataDS, api api.FullNode) error { + minerAddr, err := minerAddrFromDS(ds) + if err != nil { + return err + } + + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + log.Infof("registering miner '%s' with full node", minerAddr) + return api.MinerRegister(ctx, minerAddr) + }, + }) + return nil +}