Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2cd6ca0

Browse files
committedAug 18, 2015
make mocknet work with node constructor better
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 63fc7bb commit 2cd6ca0

File tree

5 files changed

+37
-62
lines changed

5 files changed

+37
-62
lines changed
 

‎core/core.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ import (
3939
routing "github.com/ipfs/go-ipfs/routing"
4040
dht "github.com/ipfs/go-ipfs/routing/dht"
4141
kb "github.com/ipfs/go-ipfs/routing/kbucket"
42-
offroute "github.com/ipfs/go-ipfs/routing/offline"
4342
nilrouting "github.com/ipfs/go-ipfs/routing/none"
43+
offroute "github.com/ipfs/go-ipfs/routing/offline"
4444

4545
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
4646
bserv "github.com/ipfs/go-ipfs/blockservice"

‎core/mock/mock.go

+19-45
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@ package coremock
22

33
import (
44
"encoding/base64"
5+
"net"
56

67
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
78
syncds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
89
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
910

10-
"github.com/ipfs/go-ipfs/blocks/blockstore"
11-
blockservice "github.com/ipfs/go-ipfs/blockservice"
1211
commands "github.com/ipfs/go-ipfs/commands"
1312
core "github.com/ipfs/go-ipfs/core"
14-
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
15-
bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
16-
mdag "github.com/ipfs/go-ipfs/merkledag"
17-
nsys "github.com/ipfs/go-ipfs/namesys"
13+
metrics "github.com/ipfs/go-ipfs/metrics"
14+
host "github.com/ipfs/go-ipfs/p2p/host"
1815
mocknet "github.com/ipfs/go-ipfs/p2p/net/mock"
19-
path "github.com/ipfs/go-ipfs/path"
20-
pin "github.com/ipfs/go-ipfs/pin"
16+
peer "github.com/ipfs/go-ipfs/p2p/peer"
2117
"github.com/ipfs/go-ipfs/repo"
2218
config "github.com/ipfs/go-ipfs/repo/config"
23-
dht "github.com/ipfs/go-ipfs/routing/dht"
2419
ds2 "github.com/ipfs/go-ipfs/util/datastore2"
2520
testutil "github.com/ipfs/go-ipfs/util/testutil"
2621
)
@@ -48,55 +43,34 @@ func NewMockNodeOnNet(ctx context.Context, mn mocknet.Mocknet) (*core.IpfsNode,
4843
return nil, err
4944
}
5045

46+
a := testutil.RandLocalTCPAddress()
5147
c := config.Config{
5248
Identity: config.Identity{
53-
PeerID: p.String(),
49+
PeerID: p.Pretty(),
5450
PrivKey: base64.StdEncoding.EncodeToString(pkb),
5551
},
52+
Addresses: config.Addresses{
53+
Swarm: []string{
54+
a.String(),
55+
},
56+
},
5657
}
5758

5859
r := &repo.Mock{
5960
C: c,
6061
D: ds2.CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore())),
6162
}
62-
nd, err := core.NewNode(ctx, &core.BuildCfg{
63-
Repo: r,
63+
return core.NewNode(ctx, &core.BuildCfg{
64+
Online: true,
65+
Repo: r,
66+
Host: MockHostOption(mn),
6467
})
65-
if err != nil {
66-
return nil, err
67-
}
68+
}
6869

69-
nd.PeerHost, err = mn.AddPeer(ident.PrivateKey(), ident.Address())
70-
if err != nil {
71-
return nil, err
70+
func MockHostOption(mn mocknet.Mocknet) core.HostOption {
71+
return func(ctx context.Context, id peer.ID, ps peer.Peerstore, bwr metrics.Reporter, fs []*net.IPNet) (host.Host, error) {
72+
return mn.AddPeerWithPeerstore(id, ps)
7273
}
73-
74-
nd.Peerstore = nd.PeerHost.Peerstore()
75-
nd.Peerstore.AddPrivKey(p, ident.PrivateKey())
76-
nd.Peerstore.AddPubKey(p, ident.PublicKey())
77-
nd.Identity = p
78-
79-
// Routing
80-
nd.Routing = dht.NewDHT(ctx, nd.PeerHost, nd.Repo.Datastore())
81-
82-
// Bitswap
83-
bstore := blockstore.NewBlockstore(nd.Repo.Datastore())
84-
85-
bsn := bsnet.NewFromIpfsHost(nd.PeerHost, nd.Routing)
86-
nd.Exchange = bitswap.New(ctx, p, bsn, bstore, true)
87-
88-
bserv := blockservice.New(bstore, nd.Exchange)
89-
90-
nd.DAG = mdag.NewDAGService(bserv)
91-
nd.Pinning = pin.NewPinner(nd.Repo.Datastore(), nd.DAG)
92-
93-
// Namespace resolver
94-
nd.Namesys = nsys.NewNameSystem(nd.Routing)
95-
96-
// Path resolver
97-
nd.Resolver = &path.Resolver{DAG: nd.DAG}
98-
99-
return nd, nil
10074
}
10175

10276
func MockCmdsCtx() (commands.Context, error) {

‎p2p/net/mock/interface.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Mocknet interface {
2525
// AddPeer adds an existing peer. we need both a privkey and addr.
2626
// ID is derived from PrivKey
2727
AddPeer(ic.PrivKey, ma.Multiaddr) (host.Host, error)
28+
AddPeerWithPeerstore(peer.ID, peer.Peerstore) (host.Host, error)
2829

2930
// retrieve things (with randomized iteration order)
3031
Peers() []peer.ID

‎p2p/net/mock/mock_net.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,26 @@ func (mn *mocknet) GenPeer() (host.Host, error) {
6464
}
6565

6666
func (mn *mocknet) AddPeer(k ic.PrivKey, a ma.Multiaddr) (host.Host, error) {
67-
n, err := newPeernet(mn.ctx, mn, k, a)
67+
p, err := peer.IDFromPublicKey(k.GetPublic())
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
ps := peer.NewPeerstore()
73+
ps.AddAddr(p, a, peer.PermanentAddrTTL)
74+
ps.AddPrivKey(p, k)
75+
ps.AddPubKey(p, k.GetPublic())
76+
77+
return mn.AddPeerWithPeerstore(p, ps)
78+
}
79+
80+
func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps peer.Peerstore) (host.Host, error) {
81+
n, err := newPeernet(mn.ctx, mn, p, ps)
6882
if err != nil {
6983
return nil, err
7084
}
7185

7286
h := bhost.New(n)
73-
log.Debugf("mocknet added listen addr for peer: %s -- %s", n.LocalPeer(), a)
7487

7588
mn.proc.AddChild(n.proc)
7689

‎p2p/net/mock/mock_peernet.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"math/rand"
66
"sync"
77

8-
ic "github.com/ipfs/go-ipfs/p2p/crypto"
98
inet "github.com/ipfs/go-ipfs/p2p/net"
109
peer "github.com/ipfs/go-ipfs/p2p/peer"
1110

@@ -40,19 +39,7 @@ type peernet struct {
4039
}
4140

4241
// newPeernet constructs a new peernet
43-
func newPeernet(ctx context.Context, m *mocknet, k ic.PrivKey,
44-
a ma.Multiaddr) (*peernet, error) {
45-
46-
p, err := peer.IDFromPublicKey(k.GetPublic())
47-
if err != nil {
48-
return nil, err
49-
}
50-
51-
// create our own entirely, so that peers knowledge doesn't get shared
52-
ps := peer.NewPeerstore()
53-
ps.AddAddr(p, a, peer.PermanentAddrTTL)
54-
ps.AddPrivKey(p, k)
55-
ps.AddPubKey(p, k.GetPublic())
42+
func newPeernet(ctx context.Context, m *mocknet, p peer.ID, ps peer.Peerstore) (*peernet, error) {
5643

5744
n := &peernet{
5845
mocknet: m,

0 commit comments

Comments
 (0)
Please sign in to comment.