Skip to content

Commit 4b3d77c

Browse files
committed
Create peer interface. Add mock peer node.
1 parent 2171f15 commit 4b3d77c

23 files changed

+309
-189
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
bin
1717
multi.json
1818
multi.ignore
19+
coverage.txt

cmd/multi/daemon.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func daemonAction(c *cli.Context) error {
7373
go rpc.ListenAndServe(node)
7474

7575
fmt.Printf(daemonBanner)
76-
fmt.Printf("Peer ID: %s\n", node.PeerID().Pretty())
76+
fmt.Printf("Peer ID: %s\n", node.ID().Pretty())
7777
fmt.Printf("Web URL: %s\n", web.BindAddr)
7878

7979
quit := make(chan os.Signal, 1)

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/ipfs/go-ds-badger2 v0.1.0
1414
github.com/ipfs/go-ipfs-blockstore v0.1.4
1515
github.com/ipfs/go-ipfs-chunker v0.0.5
16-
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
1716
github.com/ipfs/go-ipfs-files v0.0.8 // indirect
1817
github.com/ipfs/go-ipfs-provider v0.4.3
1918
github.com/ipfs/go-ipld-cbor v0.0.3
@@ -23,13 +22,15 @@ require (
2322
github.com/ipfs/go-unixfs v0.2.4
2423
github.com/julienschmidt/httprouter v1.3.0
2524
github.com/libp2p/go-libp2p v0.12.0
25+
github.com/libp2p/go-libp2p-blankhost v0.2.0
2626
github.com/libp2p/go-libp2p-connmgr v0.2.4
2727
github.com/libp2p/go-libp2p-core v0.7.0
2828
github.com/libp2p/go-libp2p-kad-dht v0.11.0
2929
github.com/libp2p/go-libp2p-noise v0.1.1
3030
github.com/libp2p/go-libp2p-pubsub v0.4.1
3131
github.com/libp2p/go-libp2p-pubsub-router v0.4.0
3232
github.com/libp2p/go-libp2p-record v0.1.3
33+
github.com/libp2p/go-libp2p-swarm v0.3.1
3334
github.com/libp2p/go-libp2p-tls v0.1.3
3435
github.com/multiformats/go-multihash v0.0.14
3536
github.com/nasdf/diff3 v0.0.1

peer/authors.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import (
1010
"github.com/multiverse-vcs/go-multiverse/p2p"
1111
)
1212

13-
type authors Node
13+
// AuthorsAPI implements methods to manage authors.
14+
type AuthorsAPI struct {
15+
Peer
16+
}
1417

1518
// Publish advertises the local author.
16-
func (a *authors) Publish(ctx context.Context) error {
17-
key, err := p2p.DecodeKey(a.config.PrivateKey)
19+
func (a *AuthorsAPI) Publish(ctx context.Context) error {
20+
config := a.Config()
21+
22+
key, err := p2p.DecodeKey(config.PrivateKey)
1823
if err != nil {
1924
return err
2025
}
@@ -24,7 +29,7 @@ func (a *authors) Publish(ctx context.Context) error {
2429
return err
2530
}
2631

27-
payload, err := cbornode.DumpObject(a.config.Author)
32+
payload, err := cbornode.DumpObject(config.Author)
2833
if err != nil {
2934
return err
3035
}
@@ -34,19 +39,19 @@ func (a *authors) Publish(ctx context.Context) error {
3439
return err
3540
}
3641

37-
rec := data.NewRecord(payload, a.config.Sequence, signature)
42+
rec := data.NewRecord(payload, config.Sequence, signature)
3843

3944
val, err := cbornode.DumpObject(rec)
4045
if err != nil {
4146
return err
4247
}
4348

44-
return a.namesys.PutValue(ctx, p2p.TopicForPeerID(id), val)
49+
return a.Namesys().PutValue(ctx, p2p.TopicForPeerID(id), val)
4550
}
4651

4752
// Search returns the author published under the given peer id.
48-
func (a *authors) Search(ctx context.Context, id peer.ID) (*data.Author, error) {
49-
out, err := a.namesys.SearchValue(ctx, p2p.TopicForPeerID(id))
53+
func (a *AuthorsAPI) Search(ctx context.Context, id peer.ID) (*data.Author, error) {
54+
out, err := a.Namesys().SearchValue(ctx, p2p.TopicForPeerID(id))
5055
if err != nil {
5156
return nil, err
5257
}

peer/mock.go

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package peer
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
ipld "github.com/ipfs/go-ipld-format"
8+
"github.com/ipfs/go-merkledag/dagutils"
9+
path "github.com/ipfs/go-path"
10+
"github.com/ipfs/go-path/resolver"
11+
bhost "github.com/libp2p/go-libp2p-blankhost"
12+
"github.com/libp2p/go-libp2p-core/host"
13+
"github.com/libp2p/go-libp2p-core/peer"
14+
"github.com/libp2p/go-libp2p-core/routing"
15+
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
16+
"github.com/multiverse-vcs/go-multiverse/p2p"
17+
)
18+
19+
// Mock implements the peer interface.
20+
type Mock struct {
21+
dag ipld.DAGService
22+
host host.Host
23+
config *Config
24+
resolv *resolver.Resolver
25+
namesys routing.ValueStore
26+
}
27+
28+
// NewMock returns a new mock node.
29+
func NewMock(t *testing.T, ctx context.Context) *Mock {
30+
net := swarmt.GenSwarm(t, ctx)
31+
host := bhost.NewBlankHost(net)
32+
dag := dagutils.NewMemoryDagService()
33+
resolv := resolver.NewBasicResolver(dag)
34+
35+
config, err := NewConfig("")
36+
if err != nil {
37+
t.Fatal("failed to create peer config")
38+
}
39+
40+
namesys, err := p2p.NewNamesys(ctx, host)
41+
if err != nil {
42+
t.Fatal("failed to create peer namesys")
43+
}
44+
45+
return &Mock{
46+
dag: dag,
47+
host: host,
48+
config: config,
49+
resolv: resolv,
50+
namesys: namesys,
51+
}
52+
}
53+
54+
// Authors returns the authors api.
55+
func (n *Mock) Authors() *AuthorsAPI {
56+
return &AuthorsAPI{n}
57+
}
58+
59+
// Config returns the peer config.
60+
func (n *Mock) Config() *Config {
61+
return n.config
62+
}
63+
64+
// Dag returns the merkledag api.
65+
func (n *Mock) Dag() ipld.DAGService {
66+
return n.dag
67+
}
68+
69+
// ID returns the peer ID of the node.
70+
func (n *Mock) ID() peer.ID {
71+
return n.host.ID()
72+
}
73+
74+
// Namesys returns the name system.
75+
func (n *Mock) Namesys() routing.ValueStore {
76+
return n.namesys
77+
}
78+
79+
// ResolvePath resolves the node from the given path.
80+
func (n *Mock) ResolvePath(ctx context.Context, p path.Path) (ipld.Node, error) {
81+
return n.resolv.ResolvePath(ctx, p)
82+
}

peer/node.go

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package peer
2+
3+
import (
4+
"context"
5+
6+
bitswap "github.com/ipfs/go-bitswap"
7+
bsnet "github.com/ipfs/go-bitswap/network"
8+
blockservice "github.com/ipfs/go-blockservice"
9+
datastore "github.com/ipfs/go-datastore"
10+
blockstore "github.com/ipfs/go-ipfs-blockstore"
11+
provider "github.com/ipfs/go-ipfs-provider"
12+
ipld "github.com/ipfs/go-ipld-format"
13+
merkledag "github.com/ipfs/go-merkledag"
14+
path "github.com/ipfs/go-path"
15+
"github.com/ipfs/go-path/resolver"
16+
"github.com/libp2p/go-libp2p-core/host"
17+
"github.com/libp2p/go-libp2p-core/peer"
18+
"github.com/libp2p/go-libp2p-core/routing"
19+
"github.com/libp2p/go-libp2p-kad-dht"
20+
"github.com/multiverse-vcs/go-multiverse/p2p"
21+
)
22+
23+
var _ Peer = (*Node)(nil)
24+
25+
// Node implements the peer interface
26+
type Node struct {
27+
dag ipld.DAGService
28+
host host.Host
29+
config *Config
30+
bstore blockstore.Blockstore
31+
dstore datastore.Batching
32+
namesys routing.ValueStore
33+
provsys provider.System
34+
resolv *resolver.Resolver
35+
router routing.Routing
36+
}
37+
38+
// New returns a peer with a p2p host and persistent storage.
39+
func New(ctx context.Context, dstore datastore.Batching, config *Config) (*Node, error) {
40+
priv, err := p2p.DecodeKey(config.PrivateKey)
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
host, router, err := p2p.NewHost(ctx, priv)
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
namesys, err := p2p.NewNamesys(ctx, host)
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
bstore := blockstore.NewBlockstore(dstore)
56+
net := bsnet.NewFromIpfsHost(host, router)
57+
exc := bitswap.New(ctx, net, bstore)
58+
bserv := blockservice.New(bstore, exc)
59+
dag := merkledag.NewDAGService(bserv)
60+
resolv := resolver.NewBasicResolver(dag)
61+
62+
provsys, err := p2p.NewProvider(ctx, dstore, bstore, router)
63+
if err != nil {
64+
return nil, err
65+
}
66+
provsys.Run()
67+
68+
for _, info := range dht.GetDefaultBootstrapPeerAddrInfos() {
69+
go host.Connect(ctx, info)
70+
}
71+
72+
if err := p2p.Discovery(ctx, host); err != nil {
73+
return nil, err
74+
}
75+
76+
return &Node{
77+
dag: dag,
78+
host: host,
79+
config: config,
80+
bstore: bstore,
81+
dstore: dstore,
82+
resolv: resolv,
83+
router: router,
84+
provsys: provsys,
85+
namesys: namesys,
86+
}, nil
87+
}
88+
89+
// Authors returns the authors api.
90+
func (n *Node) Authors() *AuthorsAPI {
91+
return &AuthorsAPI{n}
92+
}
93+
94+
// Config returns the peer config.
95+
func (n *Node) Config() *Config {
96+
return n.config
97+
}
98+
99+
// Dag returns the merkledag api.
100+
func (n *Node) Dag() ipld.DAGService {
101+
return n.dag
102+
}
103+
104+
// ID returns the peer ID of the node.
105+
func (n *Node) ID() peer.ID {
106+
return n.host.ID()
107+
}
108+
109+
// Namesys returns the name system.
110+
func (n *Node) Namesys() routing.ValueStore {
111+
return n.namesys
112+
}
113+
114+
// ResolvePath resolves the node from the given path.
115+
func (n *Node) ResolvePath(ctx context.Context, p path.Path) (ipld.Node, error) {
116+
return n.resolv.ResolvePath(ctx, p)
117+
}

0 commit comments

Comments
 (0)