From 0f3e3fe264f2f96833601dd12815998cf30a61aa Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 13 Aug 2015 11:44:10 -0700 Subject: [PATCH] replace nodebuilder with a nicer interface License: MIT Signed-off-by: Jeromy use NewNode instead of NewIPFSNode in most of the codebase License: MIT Signed-off-by: Jeromy make mocknet work with node constructor better License: MIT Signed-off-by: Jeromy finish cleanup of old construction method License: MIT Signed-off-by: Jeromy blockservice.New doesnt return an error anymore License: MIT Signed-off-by: Jeromy break up node construction into separate function License: MIT Signed-off-by: Jeromy add error case to default filling on node constructor License: MIT Signed-off-by: Jeromy --- net/mock/interface.go | 1 + net/mock/mock_net.go | 17 +++++++++++++++-- net/mock/mock_peernet.go | 15 +-------------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/net/mock/interface.go b/net/mock/interface.go index 28687d70..a984d865 100644 --- a/net/mock/interface.go +++ b/net/mock/interface.go @@ -25,6 +25,7 @@ type Mocknet interface { // AddPeer adds an existing peer. we need both a privkey and addr. // ID is derived from PrivKey AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error) + AddPeerWithPeerstore(peer.ID, peer.Peerstore) (host.Host, error) // retrieve things (with randomized iteration order) Peers() []peer.ID diff --git a/net/mock/mock_net.go b/net/mock/mock_net.go index f7cabf46..45e6f845 100644 --- a/net/mock/mock_net.go +++ b/net/mock/mock_net.go @@ -64,13 +64,26 @@ func (mn *mocknet) GenPeer() (host.Host, error) { } func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) { - n, err := newPeernet(mn.ctx, mn, k, a) + p, err := peer.IDFromPublicKey(k.GetPublic()) + if err != nil { + return nil, err + } + + ps := peer.NewPeerstore() + ps.AddAddr(p, a, peer.PermanentAddrTTL) + ps.AddPrivKey(p, k) + ps.AddPubKey(p, k.GetPublic()) + + return mn.AddPeerWithPeerstore(p, ps) +} + +func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps peer.Peerstore) (host.Host, error) { + n, err := newPeernet(mn.ctx, mn, p, ps) if err != nil { return nil, err } h := bhost.New(n) - log.Debugf("mocknet added listen addr for peer: %s -- %s", n.LocalPeer(), a) mn.proc.AddChild(n.proc) diff --git a/net/mock/mock_peernet.go b/net/mock/mock_peernet.go index 373953f8..6df096d1 100644 --- a/net/mock/mock_peernet.go +++ b/net/mock/mock_peernet.go @@ -5,7 +5,6 @@ import ( "math/rand" "sync" - ic "github.com/ipfs/go-ipfs/p2p/crypto" inet "github.com/ipfs/go-ipfs/p2p/net" peer "github.com/ipfs/go-ipfs/p2p/peer" @@ -40,19 +39,7 @@ type peernet struct { } // newPeernet constructs a new peernet -func newPeernet(ctx context.Context, m *mocknet, k ic.PrivKey, - a ma.Multiaddr) (*peernet, error) { - - p, err := peer.IDFromPublicKey(k.GetPublic()) - if err != nil { - return nil, err - } - - // create our own entirely, so that peers knowledge doesn't get shared - ps := peer.NewPeerstore() - ps.AddAddr(p, a, peer.PermanentAddrTTL) - ps.AddPrivKey(p, k) - ps.AddPubKey(p, k.GetPublic()) +func newPeernet(ctx context.Context, m *mocknet, p peer.ID, ps peer.Peerstore) (*peernet, error) { n := &peernet{ mocknet: m,