Skip to content

Commit

Permalink
refactor: lite-mode - simplify organization of dep injection
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Oct 9, 2020
1 parent b2834ba commit d69e4c7
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 102 deletions.
20 changes: 15 additions & 5 deletions api/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,27 @@ var PresealGenesis = -1

const GenesisPreseals = 2

// Options for setting up a mock storage miner
type StorageMiner struct {
Full int
Preseal int
}

type OptionGenerator func([]TestNode) node.Option

// Options for setting up a mock full node
type FullNodeOpts struct {
Lite bool // run node in "lite" mode
Opts OptionGenerator // generate dependency injection options
}

// APIBuilder is a function which is invoked in test suite to provide
// test nodes and networks
//
// fullOpts array defines options for each full node
// storage array defines storage nodes, numbers in the array specify full node
// index the storage node 'belongs' to
type APIBuilder func(t *testing.T, full []OptionGenerator, storage []StorageMiner, opts ...node.Option) ([]TestNode, []TestStorageNode)
type APIBuilder func(t *testing.T, full []FullNodeOpts, storage []StorageMiner, opts ...node.Option) ([]TestNode, []TestStorageNode)
type testSuite struct {
makeNodes APIBuilder
}
Expand All @@ -65,11 +73,13 @@ func TestApis(t *testing.T, b APIBuilder) {
t.Run("testMiningReal", ts.testMiningReal)
}

func DefaultFullOpts(nFull int) []OptionGenerator {
full := make([]OptionGenerator, nFull)
func DefaultFullOpts(nFull int) []FullNodeOpts {
full := make([]FullNodeOpts, nFull)
for i := range full {
full[i] = func(nodes []TestNode) node.Option {
return node.Options()
full[i] = FullNodeOpts{
Opts: func(nodes []TestNode) node.Option {
return node.Options()
},
}
}
return full
Expand Down
4 changes: 2 additions & 2 deletions chain/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (tu *syncTestUtil) addSourceNode(gen int) {
var out api.FullNode

stop, err := node.New(tu.ctx,
node.FullAPI(&out),
node.FullAPI(&out, false),
node.Online(),
node.Repo(sourceRepo),
node.MockHost(tu.mn),
Expand Down Expand Up @@ -254,7 +254,7 @@ func (tu *syncTestUtil) addClientNode() int {
var out api.FullNode

stop, err := node.New(tu.ctx,
node.FullAPI(&out),
node.FullAPI(&out, false),
node.Online(),
node.Repo(repo.NewMemory(nil)),
node.MockHost(tu.mn),
Expand Down
54 changes: 24 additions & 30 deletions cmd/lotus-gateway/endtoend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,23 @@ import (
"testing"
"time"

"github.com/filecoin-project/specs-actors/actors/builtin/multisig"

init0 "github.com/filecoin-project/specs-actors/actors/builtin/init"
"github.com/filecoin-project/specs-actors/actors/builtin/multisig"

"github.com/stretchr/testify/require"
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"

"github.com/filecoin-project/lotus/api"

"github.com/filecoin-project/lotus/node"

"github.com/filecoin-project/lotus/api/client"

"github.com/filecoin-project/go-jsonrpc"

"github.com/filecoin-project/lotus/chain/wallet"

"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-state-types/abi"
builder "github.com/filecoin-project/lotus/node/test"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/api/test"
"github.com/filecoin-project/lotus/chain/actors/policy"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/node"
builder "github.com/filecoin-project/lotus/node/test"
)

func init() {
Expand Down Expand Up @@ -179,20 +170,23 @@ func startNodes(ctx context.Context, t *testing.T, blocktime time.Duration) (tes
// Full node
test.OneFull,
// Lite node
func(nodes []test.TestNode) node.Option {
fullNode := nodes[0]

// Create a gateway server in front of the full node
_, addr, err := builder.CreateRPCServer(&GatewayAPI{api: fullNode})
require.NoError(t, err)

// Create a gateway client API that connects to the gateway server
var gapi api.GatewayAPI
gapi, closer, err = client.NewGatewayRPC(ctx, addr, nil)
require.NoError(t, err)

// Override this node with lite-mode options
return node.LiteModeOverrides(gapi)
test.FullNodeOpts{
Lite: true,
Opts: func(nodes []test.TestNode) node.Option {
fullNode := nodes[0]

// Create a gateway server in front of the full node
_, addr, err := builder.CreateRPCServer(&GatewayAPI{api: fullNode})
require.NoError(t, err)

// Create a gateway client API that connects to the gateway server
var gapi api.GatewayAPI
gapi, closer, err = client.NewGatewayRPC(ctx, addr, nil)
require.NoError(t, err)

// Provide the gateway API to dependency injection
return node.Override(new(api.GatewayAPI), gapi)
},
},
)
n, sn := builder.RPCMockSbBuilder(t, opts, test.OneMiner)
Expand Down
21 changes: 12 additions & 9 deletions cmd/lotus/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ var DaemonCmd = &cli.Command{
},
},
Action: func(cctx *cli.Context) error {
isLite := cctx.Bool("lite")

err := runmetrics.Enable(runmetrics.RunMetricOptions{
EnableCPU: true,
EnableMemory: true,
Expand Down Expand Up @@ -195,8 +197,10 @@ var DaemonCmd = &cli.Command{
return xerrors.Errorf("repo init error: %w", err)
}

if err := paramfetch.GetParams(lcli.ReqContext(cctx), build.ParametersJSON(), 0); err != nil {
return xerrors.Errorf("fetching proof parameters: %w", err)
if !isLite {
if err := paramfetch.GetParams(lcli.ReqContext(cctx), build.ParametersJSON(), 0); err != nil {
return xerrors.Errorf("fetching proof parameters: %w", err)
}
}

var genBytes []byte
Expand Down Expand Up @@ -243,32 +247,31 @@ var DaemonCmd = &cli.Command{

shutdownChan := make(chan struct{})

// If the daemon is started in "lite mode", replace key APIs
// with a thin client to a gateway server
liteMode := node.Options()
isLite := cctx.Bool("lite")
// If the daemon is started in "lite mode", provide a GatewayAPI
// for RPC calls
liteModeDeps := node.Options()
if isLite {
gapi, closer, err := lcli.GetGatewayAPI(cctx)
if err != nil {
return err
}

defer closer()
liteMode = node.LiteModeOverrides(gapi)
liteModeDeps = node.Override(new(api.GatewayAPI), gapi)
}

var api api.FullNode

stop, err := node.New(ctx,
node.FullAPI(&api),
node.FullAPI(&api, isLite),

node.Override(new(dtypes.Bootstrapper), isBootstrapper),
node.Override(new(dtypes.ShutdownChan), shutdownChan),
node.Online(),
node.Repo(r),

genesis,
liteMode,
liteModeDeps,

node.ApplyIf(func(s *node.Settings) bool { return cctx.IsSet("api") },
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
Expand Down
87 changes: 43 additions & 44 deletions node/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (
"os"
"time"

"github.com/filecoin-project/lotus/chain"
"github.com/filecoin-project/lotus/chain/exchange"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/filecoin-project/lotus/node/hello"

logging "github.com/ipfs/go-log"
ci "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
Expand All @@ -29,20 +36,15 @@ import (
storage2 "github.com/filecoin-project/specs-storage/storage"

"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain"
"github.com/filecoin-project/lotus/chain/beacon"
"github.com/filecoin-project/lotus/chain/exchange"
"github.com/filecoin-project/lotus/chain/gen"
"github.com/filecoin-project/lotus/chain/gen/slashfilter"
"github.com/filecoin-project/lotus/chain/market"
"github.com/filecoin-project/lotus/chain/messagepool"
"github.com/filecoin-project/lotus/chain/messagesigner"
"github.com/filecoin-project/lotus/chain/metrics"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/store"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/vm"
"github.com/filecoin-project/lotus/chain/wallet"
sectorstorage "github.com/filecoin-project/lotus/extern/sector-storage"
"github.com/filecoin-project/lotus/extern/sector-storage/ffiwrapper"
"github.com/filecoin-project/lotus/extern/sector-storage/stores"
Expand All @@ -56,7 +58,6 @@ import (
"github.com/filecoin-project/lotus/markets/storageadapter"
"github.com/filecoin-project/lotus/miner"
"github.com/filecoin-project/lotus/node/config"
"github.com/filecoin-project/lotus/node/hello"
"github.com/filecoin-project/lotus/node/impl"
"github.com/filecoin-project/lotus/node/impl/common"
"github.com/filecoin-project/lotus/node/impl/full"
Expand Down Expand Up @@ -159,7 +160,7 @@ type Settings struct {

Online bool // Online option applied
Config bool // Config option applied

Lite bool // Start node in "lite" mode
}

func defaults() []Option {
Expand Down Expand Up @@ -233,6 +234,10 @@ func isType(t repo.RepoType) func(s *Settings) bool {

// Online sets up basic libp2p node
func Online() Option {
isFullOrLiteNode := func(s *Settings) bool { return s.nodeType == repo.FullNode }
isFullNode := func(s *Settings) bool { return s.nodeType == repo.FullNode && !s.Lite }
isLiteNode := func(s *Settings) bool { return s.nodeType == repo.FullNode && s.Lite }

return Options(
// make sure that online is applied before Config.
// This is important because Config overrides some of Online units
Expand All @@ -246,33 +251,23 @@ func Online() Option {
// common
Override(new(*slashfilter.SlashFilter), modules.NewSlashFilter),

// Full node

ApplyIf(isType(repo.FullNode),
// Full node or lite node
ApplyIf(isFullOrLiteNode,
// TODO: Fix offline mode

Override(new(dtypes.BootstrapPeers), modules.BuiltinBootstrap),
Override(new(dtypes.DrandBootstrap), modules.DrandBootstrap),
Override(new(dtypes.DrandSchedule), modules.BuiltinDrandConfig),

Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),

Override(new(ffiwrapper.Verifier), ffiwrapper.ProofVerifier),
Override(new(vm.SyscallBuilder), vm.Syscalls),
Override(new(*store.ChainStore), modules.ChainStore),
Override(new(stmgr.UpgradeSchedule), stmgr.DefaultUpgradeSchedule()),
Override(new(*stmgr.StateManager), stmgr.NewStateManagerWithUpgradeSchedule),
Override(new(stmgr.StateManagerAPI), From(new(*stmgr.StateManager))),
Override(new(*wallet.Wallet), wallet.NewWallet),
Override(new(messagesigner.MpoolNonceAPI), From(new(*messagepool.MessagePool))),
Override(new(*messagesigner.MessageSigner), messagesigner.NewMessageSigner),

Override(new(full.ChainModuleAPI), From(new(full.ChainModule))),
Override(new(full.GasModuleAPI), From(new(full.GasModule))),
Override(new(full.MpoolModuleAPI), From(new(full.MpoolModule))),
Override(new(full.StateModuleAPI), From(new(full.StateModule))),
Override(new(stmgr.StateManagerAPI), From(new(*stmgr.StateManager))),

Override(new(dtypes.ChainGCLocker), blockstore.NewGCLocker),
Override(new(dtypes.ChainGCBlockstore), modules.ChainGCBlockstore),
Override(new(dtypes.ChainBitswap), modules.ChainBitswap),
Expand All @@ -297,12 +292,6 @@ func Online() Option {

Override(new(dtypes.Graphsync), modules.Graphsync),
Override(new(*dtypes.MpoolLocker), new(dtypes.MpoolLocker)),

Override(RunHelloKey, modules.RunHello),
Override(RunChainExchangeKey, modules.RunChainExchange),
Override(RunPeerMgrKey, modules.RunPeerMgr),
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),

Override(new(*discoveryimpl.Local), modules.NewLocalDiscovery),
Override(new(discovery.PeerResolver), modules.RetrievalResolver),

Expand All @@ -321,8 +310,34 @@ func Online() Option {
Override(SettlePaymentChannelsKey, settler.SettlePaymentChannels),
),

// Lite node
ApplyIf(isLiteNode,
Override(new(messagesigner.MpoolNonceAPI), From(new(modules.MpoolNonceAPI))),
Override(new(full.ChainModuleAPI), From(new(api.GatewayAPI))),
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),
),

// Full node
ApplyIf(isFullNode,
Override(new(messagesigner.MpoolNonceAPI), From(new(*messagepool.MessagePool))),
Override(new(full.ChainModuleAPI), From(new(full.ChainModule))),
Override(new(full.GasModuleAPI), From(new(full.GasModule))),
Override(new(full.MpoolModuleAPI), From(new(full.MpoolModule))),
Override(new(full.StateModuleAPI), From(new(full.StateModule))),
Override(new(stmgr.StateManagerAPI), From(new(*stmgr.StateManager))),

Override(RunHelloKey, modules.RunHello),
Override(RunChainExchangeKey, modules.RunChainExchange),
Override(RunPeerMgrKey, modules.RunPeerMgr),
Override(HandleIncomingMessagesKey, modules.HandleIncomingMessages),
Override(HandleIncomingBlocksKey, modules.HandleIncomingBlocks),
),

// miner
ApplyIf(func(s *Settings) bool { return s.nodeType == repo.StorageMiner },
ApplyIf(isType(repo.StorageMiner),
Override(new(api.Common), From(new(common.CommonAPI))),
Override(new(sectorstorage.StorageAuth), modules.StorageAuth),

Expand Down Expand Up @@ -402,23 +417,6 @@ func StorageMiner(out *api.StorageMiner) Option {
)
}

func LiteModeOverrides(gapi api.GatewayAPI) Option {
return Options(
Override(new(messagesigner.MpoolNonceAPI), From(new(modules.MpoolNonceAPI))),
Override(new(api.GatewayAPI), gapi),
Override(new(full.ChainModuleAPI), From(new(api.GatewayAPI))),
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),
Unset(RunHelloKey),
Unset(RunChainExchangeKey),
Unset(RunPeerMgrKey),
Unset(HandleIncomingBlocksKey),
Unset(HandleIncomingMessagesKey),
)
}

// Config sets up constructors based on the provided Config
func ConfigCommon(cfg *config.Common) Option {
return Options(
Expand Down Expand Up @@ -533,10 +531,11 @@ func Repo(r repo.Repo) Option {
}
}

func FullAPI(out *api.FullNode) Option {
func FullAPI(out *api.FullNode, lite bool) Option {
return Options(
func(s *Settings) error {
s.nodeType = repo.FullNode
s.Lite = lite
return nil
},
func(s *Settings) error {
Expand Down
Loading

0 comments on commit d69e4c7

Please sign in to comment.