@cryptotaxi247 / kubo / commits / 6241c2598

make contructor code a little less magical

Jeromy committed Feb 14, 2015 at 07:16 UTC 6241c259840a4af7312a78b70130f50d64ae80c8
3 files changed +25 -51
core/core.go
+11 -20
@@ -3,7 +3,6 @@
3 package core
4
5 import (
6 - "errors"
6 "fmt"
7 "io"
8 "time"
@@ -11,7 +10,7 @@ import (
10 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
11 b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
12 ctxgroup "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-ctxgroup"
14 - datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
13 + ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
14 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
15
16 eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
@@ -231,12 +230,12 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
230 if err != nil {
231 return debugerror.Wrap(err)
232 }
234 - n.PeerHost = peerhost
233
236 - if err := n.startOnlineServicesWithHost(ctx, routingOption); err != nil {
234 + if err := n.startOnlineServicesWithHost(ctx, peerhost, routingOption); err != nil {
235 return err
236 }
237
238 + // Wrap standard peer host with routing system to allow unknown peer lookups
239 n.PeerHost = rhost.Wrap(peerhost, n.Routing)
240
241 // Ok, now we're ready to listen.
@@ -252,12 +251,12 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
251
252 // startOnlineServicesWithHost is the set of services which need to be
253 // initialized with the host and _before_ we start listening.
255 -func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, routingOption RoutingOption) error {
254 +func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost.Host, routingOption RoutingOption) error {
255 // setup diagnostics service
257 - n.Diagnostics = diag.NewDiagnostics(n.Identity, n.PeerHost)
256 + n.Diagnostics = diag.NewDiagnostics(n.Identity, host)
257
258 // setup routing service
260 - r, err := routingOption(ctx, n)
259 + r, err := routingOption(ctx, host, n.Repo.Datastore())
260 if err != nil {
261 return debugerror.Wrap(err)
262 }
@@ -265,7 +264,7 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, routingOptio
264
265 // setup exchange service
266 const alwaysSendToPeer = true // use YesManStrategy
268 - bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing)
267 + bitswapNetwork := bsnet.NewFromIpfsHost(host, n.Routing)
268 n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Blockstore, alwaysSendToPeer)
269
270 // setup name system
@@ -488,20 +487,12 @@ func startListening(ctx context.Context, host p2phost.Host, cfg *config.Config)
487 return nil
488 }
489
491 -func constructDHTRouting(ctx context.Context, host p2phost.Host, ds datastore.ThreadSafeDatastore) (*dht.IpfsDHT, error) {
492 - dhtRouting := dht.NewDHT(ctx, host, ds)
490 +func constructDHTRouting(ctx context.Context, host p2phost.Host, dstore ds.ThreadSafeDatastore) (routing.IpfsRouting, error) {
491 + dhtRouting := dht.NewDHT(ctx, host, dstore)
492 dhtRouting.Validator[IpnsValidatorTag] = namesys.ValidateIpnsRecord
493 return dhtRouting, nil
494 }
495
497 -type RoutingOption func(context.Context, *IpfsNode) (routing.IpfsRouting, error)
496 +type RoutingOption func(context.Context, p2phost.Host, ds.ThreadSafeDatastore) (routing.IpfsRouting, error)
497
499 -var DHTOption RoutingOption = func(ctx context.Context, n *IpfsNode) (routing.IpfsRouting, error) {
500 - if n.PeerHost == nil {
501 - return nil, errors.New("dht requires a peerhost")
502 - }
503 - if n.Repo == nil {
504 - return nil, errors.New("dht requires a datastore. (node has no Repo)")
505 - }
506 - return constructDHTRouting(ctx, n.PeerHost, n.Repo.Datastore())
507 -}
498 +var DHTOption RoutingOption = constructDHTRouting
core/corerouting/core.go
+12 -29
@@ -4,8 +4,9 @@ import (
4 "errors"
5
6 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 - datastore "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
7 + ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8 core "github.com/jbenet/go-ipfs/core"
9 + "github.com/jbenet/go-ipfs/p2p/host"
10 "github.com/jbenet/go-ipfs/p2p/peer"
11 routing "github.com/jbenet/go-ipfs/routing"
12 supernode "github.com/jbenet/go-ipfs/routing/supernode"
@@ -26,48 +27,30 @@ var (
27 // SupernodeServer returns a configuration for a routing server that stores
28 // routing records to the provided datastore. Only routing records are store in
29 // the datastore.
29 -func SupernodeServer(recordSource datastore.ThreadSafeDatastore) core.RoutingOption {
30 - return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
31 - if node.Peerstore == nil {
32 - return nil, errPeerstoreMissing
33 - }
34 - if node.PeerHost == nil {
35 - return nil, errHostMissing
36 - }
37 - if node.Identity == "" {
38 - return nil, errIdentityMissing
39 - }
40 - server, err := supernode.NewServer(recordSource, node.Peerstore, node.Identity)
30 +func SupernodeServer(recordSource ds.ThreadSafeDatastore) core.RoutingOption {
31 + return func(ctx context.Context, ph host.Host, dstore ds.ThreadSafeDatastore) (routing.IpfsRouting, error) {
32 + server, err := supernode.NewServer(recordSource, ph.Peerstore(), ph.ID())
33 if err != nil {
34 return nil, err
35 }
36 proxy := &gcproxy.Loopback{
37 Handler: server,
46 - Local: node.Identity,
38 + Local: ph.ID(),
39 }
48 - node.PeerHost.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
49 - return supernode.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
40 + ph.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
41 + return supernode.NewClient(proxy, ph, ph.Peerstore(), ph.ID())
42 }
43 }
44
45 // TODO doc
46 func SupernodeClient(remotes ...peer.PeerInfo) core.RoutingOption {
55 - return func(ctx context.Context, node *core.IpfsNode) (routing.IpfsRouting, error) {
47 + return func(ctx context.Context, ph host.Host, dstore ds.ThreadSafeDatastore) (routing.IpfsRouting, error) {
48 if len(remotes) < 1 {
49 return nil, errServersMissing
50 }
59 - if node.PeerHost == nil {
60 - return nil, errHostMissing
61 - }
62 - if node.Identity == "" {
63 - return nil, errIdentityMissing
64 - }
65 - if node.Peerstore == nil {
66 - return nil, errors.New("need peerstore")
67 - }
51
69 - proxy := gcproxy.Standard(node.PeerHost, remotes)
70 - node.PeerHost.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
71 - return supernode.NewClient(proxy, node.PeerHost, node.Peerstore, node.Identity)
52 + proxy := gcproxy.Standard(ph, remotes)
53 + ph.SetStreamHandler(gcproxy.ProtocolSNR, proxy.HandleStream)
54 + return supernode.NewClient(proxy, ph, ph.Peerstore(), ph.ID())
55 }
56 }
test/integration/core.go
+2 -2
@@ -4,7 +4,6 @@ import (
4 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
5 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
6 syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
7 - ds2 "github.com/jbenet/go-ipfs/util/datastore2"
7 blockstore "github.com/jbenet/go-ipfs/blocks/blockstore"
8 core "github.com/jbenet/go-ipfs/core"
9 bitswap "github.com/jbenet/go-ipfs/exchange/bitswap"
@@ -14,6 +13,7 @@ import (
13 "github.com/jbenet/go-ipfs/repo"
14 delay "github.com/jbenet/go-ipfs/thirdparty/delay"
15 eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
16 + ds2 "github.com/jbenet/go-ipfs/util/datastore2"
17 testutil "github.com/jbenet/go-ipfs/util/testutil"
18 )
19
@@ -35,7 +35,7 @@ func MocknetTestRepo(p peer.ID, h host.Host, conf testutil.LatencyConfig, routin
35 PeerHost: h,
36 Identity: p,
37 }
38 - dhtt, err := routing(ctx, n)
38 + dhtt, err := routing(ctx, n.PeerHost, n.Repo.Datastore())
39 if err != nil {
40 return nil, err
41 }