From 1d1f468c98bfe2e713cd973aaeedaf2fc588a33a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Wed, 23 Oct 2019 13:02:00 +0200 Subject: [PATCH] peer manager: Disable in tests --- chain/sync_test.go | 2 ++ node/builder.go | 33 +++------------------------------ node/hello/hello.go | 19 ++++++++++++++++--- node/node_test.go | 2 ++ node/options.go | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 62 insertions(+), 33 deletions(-) diff --git a/chain/sync_test.go b/chain/sync_test.go index 81bcbf387..24b8fbd10 100644 --- a/chain/sync_test.go +++ b/chain/sync_test.go @@ -211,6 +211,7 @@ func (tu *syncTestUtil) addSourceNode(gen int) { node.Online(), node.Repo(sourceRepo), node.MockHost(tu.mn), + node.Test(), node.Override(new(modules.Genesis), modules.LoadGenesis(genesis)), ) @@ -242,6 +243,7 @@ func (tu *syncTestUtil) addClientNode() int { node.Online(), node.Repo(repo.NewMemory(nil)), node.MockHost(tu.mn), + node.Test(), node.Override(new(modules.Genesis), modules.LoadGenesis(tu.genesis)), ) diff --git a/node/builder.go b/node/builder.go index df49c11b1..f0bc09348 100644 --- a/node/builder.go +++ b/node/builder.go @@ -3,7 +3,6 @@ package node import ( "context" "errors" - "reflect" "time" blockstore "github.com/ipfs/go-ipfs-blockstore" @@ -124,26 +123,6 @@ type Settings struct { Config bool // Config option applied } -// Override option changes constructor for a given type -func Override(typ, constructor interface{}) Option { - return func(s *Settings) error { - if i, ok := typ.(invoke); ok { - s.invokes[i] = fx.Invoke(constructor) - return nil - } - - if c, ok := typ.(special); ok { - s.modules[c] = fx.Provide(constructor) - return nil - } - ctor := as(constructor, typ) - rt := reflect.TypeOf(typ).Elem() - - s.modules[rt] = fx.Provide(ctor) - return nil - } -} - var defConf = config.Default() func defaults() []Option { @@ -399,15 +378,9 @@ func New(ctx context.Context, opts ...Option) (StopFunc, error) { // In-memory / testing -func randomIdentity() Option { - sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) - if err != nil { - return Error(err) - } - +func Test() Option { return Options( - Override(new(ci.PrivKey), sk), - Override(new(ci.PubKey), pk), - Override(new(peer.ID), peer.IDFromPublicKey), + Unset(RunPeerMgrKey), + Unset(new(*peermgr.PeerMgr)), ) } diff --git a/node/hello/hello.go b/node/hello/hello.go index 53e32af52..d512d1d27 100644 --- a/node/hello/hello.go +++ b/node/hello/hello.go @@ -3,6 +3,7 @@ package hello import ( "context" "fmt" + "go.uber.org/fx" "github.com/ipfs/go-cid" cbor "github.com/ipfs/go-ipld-cbor" @@ -40,13 +41,23 @@ type Service struct { pmgr *peermgr.PeerMgr } -func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pmgr *peermgr.PeerMgr) *Service { +type MaybePeerMgr struct { + fx.In + + Mgr *peermgr.PeerMgr `optional:"true"` +} + +func NewHelloService(h host.Host, cs *store.ChainStore, syncer *chain.Syncer, pmgr MaybePeerMgr) *Service { + if pmgr.Mgr == nil { + log.Warn("running without peer manager") + } + return &Service{ newStream: h.NewStream, cs: cs, syncer: syncer, - pmgr: pmgr, + pmgr: pmgr.Mgr, } } @@ -77,7 +88,9 @@ func (hs *Service) HandleStream(s inet.Stream) { log.Infof("Got new tipset through Hello: %s from %s", ts.Cids(), s.Conn().RemotePeer()) hs.syncer.InformNewHead(s.Conn().RemotePeer(), ts) - hs.pmgr.AddFilecoinPeer(s.Conn().RemotePeer()) + if hs.pmgr != nil { + hs.pmgr.AddFilecoinPeer(s.Conn().RemotePeer()) + } } func (hs *Service) SayHello(ctx context.Context, pid peer.ID) error { diff --git a/node/node_test.go b/node/node_test.go index e3ad41ea9..06f1fc1c6 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -91,6 +91,7 @@ func testStorageNode(ctx context.Context, t *testing.T, waddr address.Address, a node.StorageMiner(&minerapi), node.Online(), node.Repo(r), + node.Test(), node.Override(new(*sectorbuilder.SectorBuilderConfig), modules.SectorBuilderConfig(secbpath)), node.Override(new(api.FullNode), tnd), @@ -133,6 +134,7 @@ func builder(t *testing.T, nFull int, storage []int) ([]test.TestNode, []test.Te node.Online(), node.Repo(repo.NewMemory(nil)), node.MockHost(mn), + node.Test(), node.Override(new(*miner.Miner), miner.NewTestMiner(mineBlock)), diff --git a/node/options.go b/node/options.go index 4d2ed9ced..58576a950 100644 --- a/node/options.go +++ b/node/options.go @@ -1,6 +1,7 @@ package node import ( + "go.uber.org/fx" "reflect" ) @@ -38,6 +39,44 @@ func ApplyIf(check func(s *Settings) bool, opts ...Option) Option { } } +// Override option changes constructor for a given type +func Override(typ, constructor interface{}) Option { + return func(s *Settings) error { + if i, ok := typ.(invoke); ok { + s.invokes[i] = fx.Invoke(constructor) + return nil + } + + if c, ok := typ.(special); ok { + s.modules[c] = fx.Provide(constructor) + return nil + } + ctor := as(constructor, typ) + rt := reflect.TypeOf(typ).Elem() + + s.modules[rt] = fx.Provide(ctor) + return nil + } +} + +func Unset(typ interface{}) Option { + return func(s *Settings) error { + if i, ok := typ.(invoke); ok { + s.invokes[i] = nil + return nil + } + + if c, ok := typ.(special); ok { + delete(s.modules, c) + return nil + } + rt := reflect.TypeOf(typ).Elem() + + delete(s.modules, rt) + return nil + } +} + // from go-ipfs // as casts input constructor to a given interface (if a value is given, it // wraps it into a constructor).