Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Refactors fx wiring into self-contained modules #46

Merged
merged 12 commits into from
Jan 5, 2023

Conversation

hmoniz
Copy link
Contributor

@hmoniz hmoniz commented Jan 2, 2023

This change does a few things with the ultimate goal of having a cleaner organization of the fx dependency graph:

(1) It completely strips out the functional "Options" layer that was being used on top of fx. This layer was obfuscating the real organization of the application dependencies, particularly through the abuse of the "Override" function.

(2) Creates an fxmodules package where we moved almost all of the fx providers and invokes into self-contained modules that reflect the high-level organization of the code (e.g., libp2p, node, repo, consensus, etc.).

(3) Moves some of the construction of the dependency graph that was outside of fx into the fx framework. Sadly, not all though, but it got the important pieces of the full node API and the RPC server construction.

(4) Fixes some antipatterns in the code and the usage of fx. In particular: (a) no more overrides, now the application fails to launch if a dependency is provided twice; (b) in a few places (e.g., mir consensus), providers are now returning the specific struct type and not the interface. We used fx annotations to fix this; (c) Some providers are now private in order to keep modules as self-contained as possible. We didn't go to far with this as the API construction makes it difficult, but it's a start.

Hopefully, this will serve as a starting point to make the lotus code cleaner and more modular. The fx module organization proposed here is just a starting point. There's a lot of room for further improvement.

This change does a few things with the ultimate goal of having a cleaner organization of the fx dependency graph:

(1) It completely strips out the functional "Options" layer that was being used on top of fx. This layer was obfuscating the real organization of the application dependencies, particularly through the abuse of the "Override" function.

(2) Creates an fxmodules package where we moved almost all of the fx providers and invokes into self-contained modules that reflect the high-level organization of the code (e.g., libp2p, node, repo, consensus, etc.).

(3) Moves some of the construction of the dependency graph that was outside of fx into the fx framework. Sadly, not all though, but it got the important pieces of the full node API and the RPC server construction.

(4) Fixes some antipatterns in the code and the usage of fx. In particular: (a) no more overrides, now the application fails to launch if a dependency is provided twice; (b) in a few places (e.g., mir consensus), providers are now returning the specific struct type and not the interface. We used fx annotations to fix this; (c) Some providers are now private in order to keep modules as self-contained as possible. We didn't go to far with this as the API construction makes it difficult, but it's a start.

Hopefully, this will serve as a starting point to make the lotus code cleaner and more modular. The fx module organization proposed here is just a starting point. There's a lot of room for further improvement.
Copy link
Contributor

@dnkolegov dnkolegov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I added minor comments.

cmd/lotus/eudico.go Show resolved Hide resolved
eudico/fxmodules/blockstore.go Outdated Show resolved Hide resolved
chain/consensus/tspow/consensus.go Show resolved Hide resolved
chain/consensus/tspow/pow.go Show resolved Hide resolved
eudico/fxmodules/consensus.go Outdated Show resolved Hide resolved
eudico/fxmodules/fullnode.go Show resolved Hide resolved
eudico/fxmodules/invokes.go Outdated Show resolved Hide resolved
eudico/fxmodules/libp2p.go Outdated Show resolved Hide resolved
eudico/fxmodules/blockstore.go Show resolved Hide resolved
Copy link
Contributor

@adlrocha adlrocha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing job! Apart from a few cosmetic suggestions, can we include the new fx approach also in itests? https://github.com/consensus-shipyard/lotus/blob/spacenet/itests/kit/ensemble.go#L352-L458

If needed we can also choose to create an independent ensemble for eudico as we do here: https://github.com/filecoin-project/eudico/blob/eudico/itests/kit/ensemble_eudico.go (although I would rather avoid a lot of duplicate code).

Once this is done, you can check if it works with make spacenet-test and if this is the case we are ready to merge (and I'll start migrating tspow code here) :) Thanks! 🙏

cmd/lotus/eudico.go Show resolved Hide resolved
eudico/fxmodules/blockstore.go Show resolved Hide resolved
eudico/fxmodules/blockstore.go Outdated Show resolved Hide resolved
eudico/fxmodules/blockstore.go Show resolved Hide resolved
eudico/fxmodules/fullnode.go Outdated Show resolved Hide resolved
eudico/fxmodules/fullnode.go Outdated Show resolved Hide resolved
eudico/fxmodules/fullnode.go Outdated Show resolved Hide resolved
eudico/fxmodules/fullnode.go Outdated Show resolved Hide resolved
eudico/fxmodules/invokes.go Show resolved Hide resolved
eudico/fxmodules/libp2p.go Show resolved Hide resolved
@hmoniz hmoniz merged commit 6e3d125 into spacenet Jan 5, 2023
@hmoniz hmoniz deleted the hmoniz/fx_refactoring branch January 5, 2023 13:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants