From bf42a3be5e14a300312135ef69e1c3115b4f7669 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 11 Mar 2021 17:35:31 -0800 Subject: [PATCH 1/4] move DI stuff for paychmgr into modules --- node/builder.go | 6 +++--- node/modules/paych.go | 37 +++++++++++++++++++++++++++++++++++++ paychmgr/manager.go | 18 +----------------- paychmgr/store.go | 5 +---- 4 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 node/modules/paych.go diff --git a/node/builder.go b/node/builder.go index ee0a66e532..07d8294c94 100644 --- a/node/builder.go +++ b/node/builder.go @@ -307,9 +307,9 @@ var ChainNode = Options( Override(new(api.WalletAPI), From(new(wallet.MultiWallet))), // Service: Payment channels - Override(new(*paychmgr.Store), paychmgr.NewStore), - Override(new(*paychmgr.Manager), paychmgr.NewManager), - Override(HandlePaymentChannelManagerKey, paychmgr.HandleManager), + Override(new(*paychmgr.Store), modules.NewPaychStore), + Override(new(*paychmgr.Manager), modules.NewManager), + Override(HandlePaymentChannelManagerKey, modules.HandlePaychManager), Override(SettlePaymentChannelsKey, settler.SettlePaymentChannels), // Markets (common) diff --git a/node/modules/paych.go b/node/modules/paych.go new file mode 100644 index 0000000000..28b0f4898d --- /dev/null +++ b/node/modules/paych.go @@ -0,0 +1,37 @@ +package modules + +import ( + "context" + + "github.com/filecoin-project/lotus/chain/stmgr" + "github.com/filecoin-project/lotus/node/modules/dtypes" + "github.com/filecoin-project/lotus/node/modules/helpers" + "github.com/filecoin-project/lotus/paychmgr" + "github.com/ipfs/go-datastore" + "github.com/ipfs/go-datastore/namespace" + "go.uber.org/fx" +) + +func NewManager(mctx helpers.MetricsCtx, lc fx.Lifecycle, sm stmgr.StateManagerAPI, pchstore *paychmgr.Store, api paychmgr.PaychAPI) *paychmgr.Manager { + ctx := helpers.LifecycleCtx(mctx, lc) + ctx, shutdown := context.WithCancel(ctx) + + return paychmgr.NewManager(ctx, shutdown, sm, pchstore, api) +} + +func NewPaychStore(ds dtypes.MetadataDS) *paychmgr.Store { + ds = namespace.Wrap(ds, datastore.NewKey("/paych/")) + return paychmgr.NewStore(ds) +} + +// HandlePaychManager is called by dependency injection to set up hooks +func HandlePaychManager(lc fx.Lifecycle, pm *paychmgr.Manager) { + lc.Append(fx.Hook{ + OnStart: func(ctx context.Context) error { + return pm.Start() + }, + OnStop: func(context.Context) error { + return pm.Stop() + }, + }) +} diff --git a/paychmgr/manager.go b/paychmgr/manager.go index 5e0aa88cea..8484bc8b78 100644 --- a/paychmgr/manager.go +++ b/paychmgr/manager.go @@ -20,7 +20,6 @@ import ( "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/node/impl/full" - "github.com/filecoin-project/lotus/node/modules/helpers" ) var log = logging.Logger("paych") @@ -77,10 +76,7 @@ type Manager struct { channels map[string]*channelAccessor } -func NewManager(mctx helpers.MetricsCtx, lc fx.Lifecycle, sm stmgr.StateManagerAPI, pchstore *Store, api PaychAPI) *Manager { - ctx := helpers.LifecycleCtx(mctx, lc) - ctx, shutdown := context.WithCancel(ctx) - +func NewManager(ctx context.Context, shutdown func(), sm stmgr.StateManagerAPI, pchstore *Store, api PaychAPI) *Manager { impl := &managerAPIImpl{StateManagerAPI: sm, paychAPI: &api} return &Manager{ ctx: ctx, @@ -103,18 +99,6 @@ func newManager(pchstore *Store, pchapi managerAPI) (*Manager, error) { return pm, pm.Start() } -// HandleManager is called by dependency injection to set up hooks -func HandleManager(lc fx.Lifecycle, pm *Manager) { - lc.Append(fx.Hook{ - OnStart: func(ctx context.Context) error { - return pm.Start() - }, - OnStop: func(context.Context) error { - return pm.Stop() - }, - }) -} - // Start restarts tracking of any messages that were sent to chain. func (pm *Manager) Start() error { return pm.restartPending() diff --git a/paychmgr/store.go b/paychmgr/store.go index a17ad1fcd8..343149f932 100644 --- a/paychmgr/store.go +++ b/paychmgr/store.go @@ -14,14 +14,12 @@ import ( cborutil "github.com/filecoin-project/go-cbor-util" "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" - "github.com/ipfs/go-datastore/namespace" dsq "github.com/ipfs/go-datastore/query" "github.com/filecoin-project/go-address" cborrpc "github.com/filecoin-project/go-cbor-util" "github.com/filecoin-project/lotus/chain/actors/builtin/paych" - "github.com/filecoin-project/lotus/node/modules/dtypes" ) var ErrChannelNotTracked = errors.New("channel not tracked") @@ -30,8 +28,7 @@ type Store struct { ds datastore.Batching } -func NewStore(ds dtypes.MetadataDS) *Store { - ds = namespace.Wrap(ds, datastore.NewKey("/paych/")) +func NewStore(ds datastore.Batching) *Store { return &Store{ ds: ds, } From e0b650d4ec461e204571bddd23a176eb9467db2e Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Thu, 11 Mar 2021 18:21:18 -0800 Subject: [PATCH 2/4] make rpcstatemanager its own package, for easy reuse --- {node/modules => chain/stmgr/rpc}/rpcstatemanager.go | 2 +- node/builder.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) rename {node/modules => chain/stmgr/rpc}/rpcstatemanager.go (99%) diff --git a/node/modules/rpcstatemanager.go b/chain/stmgr/rpc/rpcstatemanager.go similarity index 99% rename from node/modules/rpcstatemanager.go rename to chain/stmgr/rpc/rpcstatemanager.go index b14e1dc800..98ea315db9 100644 --- a/node/modules/rpcstatemanager.go +++ b/chain/stmgr/rpc/rpcstatemanager.go @@ -1,4 +1,4 @@ -package modules +package rpcstmgr import ( "context" diff --git a/node/builder.go b/node/builder.go index 07d8294c94..a37ec944a2 100644 --- a/node/builder.go +++ b/node/builder.go @@ -11,6 +11,7 @@ import ( "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/lotus/chain" "github.com/filecoin-project/lotus/chain/exchange" + rpcstmgr "github.com/filecoin-project/lotus/chain/stmgr/rpc" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/vm" "github.com/filecoin-project/lotus/chain/wallet" @@ -334,7 +335,7 @@ var ChainNode = Options( Override(new(full.GasModuleAPI), From(new(api.GatewayAPI))), Override(new(full.MpoolModuleAPI), From(new(api.GatewayAPI))), Override(new(full.StateModuleAPI), From(new(api.GatewayAPI))), - Override(new(stmgr.StateManagerAPI), modules.NewRPCStateManager), + Override(new(stmgr.StateManagerAPI), rpcstmgr.NewRPCStateManager), ), // Full node API / service startup From cbc7f1c244da8574d6a9578a74ebf5f635d8e726 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Fri, 12 Mar 2021 00:39:20 -0800 Subject: [PATCH 3/4] fix paychmgr constructor to take an easier to implement interface --- node/builder.go | 1 + node/modules/paych.go | 8 ++++++++ paychmgr/manager.go | 18 ++++-------------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/node/builder.go b/node/builder.go index a37ec944a2..4d1a71889d 100644 --- a/node/builder.go +++ b/node/builder.go @@ -308,6 +308,7 @@ var ChainNode = Options( Override(new(api.WalletAPI), From(new(wallet.MultiWallet))), // Service: Payment channels + Override(new(paychmgr.PaychAPI), new(modules.PaychAPI)), Override(new(*paychmgr.Store), modules.NewPaychStore), Override(new(*paychmgr.Manager), modules.NewManager), Override(HandlePaymentChannelManagerKey, modules.HandlePaychManager), diff --git a/node/modules/paych.go b/node/modules/paych.go index 28b0f4898d..a9fd25a3ec 100644 --- a/node/modules/paych.go +++ b/node/modules/paych.go @@ -4,6 +4,7 @@ import ( "context" "github.com/filecoin-project/lotus/chain/stmgr" + "github.com/filecoin-project/lotus/node/impl/full" "github.com/filecoin-project/lotus/node/modules/dtypes" "github.com/filecoin-project/lotus/node/modules/helpers" "github.com/filecoin-project/lotus/paychmgr" @@ -24,6 +25,13 @@ func NewPaychStore(ds dtypes.MetadataDS) *paychmgr.Store { return paychmgr.NewStore(ds) } +type PaychAPI struct { + fx.In + + full.MpoolAPI + full.StateAPI +} + // HandlePaychManager is called by dependency injection to set up hooks func HandlePaychManager(lc fx.Lifecycle, pm *paychmgr.Manager) { lc.Append(fx.Hook{ diff --git a/paychmgr/manager.go b/paychmgr/manager.go index 8484bc8b78..e9700bc9d1 100644 --- a/paychmgr/manager.go +++ b/paychmgr/manager.go @@ -8,7 +8,6 @@ import ( "github.com/ipfs/go-cid" "github.com/ipfs/go-datastore" logging "github.com/ipfs/go-log/v2" - "go.uber.org/fx" xerrors "golang.org/x/xerrors" "github.com/filecoin-project/go-address" @@ -19,21 +18,12 @@ import ( "github.com/filecoin-project/lotus/chain/actors/builtin/paych" "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/types" - "github.com/filecoin-project/lotus/node/impl/full" ) var log = logging.Logger("paych") var errProofNotSupported = errors.New("payment channel proof parameter is not supported") -// PaychAPI is used by dependency injection to pass the consituent APIs to NewManager() -type PaychAPI struct { - fx.In - - full.MpoolAPI - full.StateAPI -} - // stateManagerAPI defines the methods needed from StateManager type stateManagerAPI interface { ResolveToKeyAddress(ctx context.Context, addr address.Address, ts *types.TipSet) (address.Address, error) @@ -42,7 +32,7 @@ type stateManagerAPI interface { } // paychAPI defines the API methods needed by the payment channel manager -type paychAPI interface { +type PaychAPI interface { StateAccountKey(context.Context, address.Address, types.TipSetKey) (address.Address, error) StateWaitMsg(ctx context.Context, msg cid.Cid, confidence uint64) (*api.MsgLookup, error) MpoolPushMessage(ctx context.Context, msg *types.Message, maxFee *api.MessageSendSpec) (*types.SignedMessage, error) @@ -54,13 +44,13 @@ type paychAPI interface { // managerAPI defines all methods needed by the manager type managerAPI interface { stateManagerAPI - paychAPI + PaychAPI } // managerAPIImpl is used to create a composite that implements managerAPI type managerAPIImpl struct { stmgr.StateManagerAPI - paychAPI + PaychAPI } type Manager struct { @@ -77,7 +67,7 @@ type Manager struct { } func NewManager(ctx context.Context, shutdown func(), sm stmgr.StateManagerAPI, pchstore *Store, api PaychAPI) *Manager { - impl := &managerAPIImpl{StateManagerAPI: sm, paychAPI: &api} + impl := &managerAPIImpl{StateManagerAPI: sm, PaychAPI: api} return &Manager{ ctx: ctx, shutdown: shutdown, From e5226412fb528027ef7eb1ab27a7ce26a95823a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Fri, 12 Mar 2021 11:27:00 +0100 Subject: [PATCH 4/4] builder: set modules.PaychAPI correctly --- node/builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/builder.go b/node/builder.go index 4d1a71889d..df183b2162 100644 --- a/node/builder.go +++ b/node/builder.go @@ -308,7 +308,7 @@ var ChainNode = Options( Override(new(api.WalletAPI), From(new(wallet.MultiWallet))), // Service: Payment channels - Override(new(paychmgr.PaychAPI), new(modules.PaychAPI)), + Override(new(paychmgr.PaychAPI), From(new(modules.PaychAPI))), Override(new(*paychmgr.Store), modules.NewPaychStore), Override(new(*paychmgr.Manager), modules.NewManager), Override(HandlePaymentChannelManagerKey, modules.HandlePaychManager),