-
Notifications
You must be signed in to change notification settings - Fork 66
/
ipfs.go
164 lines (139 loc) · 4 KB
/
ipfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package p2ptest
import (
"context"
"encoding/base64"
"fmt"
datastore "github.com/ipfs/go-datastore"
syncds "github.com/ipfs/go-datastore/sync"
config "github.com/ipfs/go-ipfs-config"
core "github.com/ipfs/go-ipfs/core"
corebs "github.com/ipfs/go-ipfs/core/bootstrap"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
mock "github.com/ipfs/go-ipfs/core/mock"
keystore "github.com/ipfs/go-ipfs/keystore"
repo "github.com/ipfs/go-ipfs/repo"
coreiface "github.com/ipfs/interface-go-ipfs-core"
peer "github.com/libp2p/go-libp2p-core/peer"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
qfs "github.com/qri-io/qfs"
"github.com/qri-io/qfs/localfs"
"github.com/qri-io/qfs/muxfs"
qipfs "github.com/qri-io/qfs/qipfs"
testkeys "github.com/qri-io/qri/auth/key/test"
"github.com/qri-io/qri/event"
profile "github.com/qri-io/qri/profile"
qrirepo "github.com/qri-io/qri/repo"
)
// MakeRepoFromIPFSNode wraps an ipfs node with a mock qri repo
func MakeRepoFromIPFSNode(ctx context.Context, node *core.IpfsNode, username string, bus event.Bus) (qrirepo.Repo, error) {
p := &profile.Profile{
ID: profile.IDFromPeerID(node.Identity),
Peername: username,
PrivKey: node.PrivateKey,
}
// TODO (b5) - we can't supply the usual {Type: "mem"} {Type: "local"}, configuration options here
// b/c we want ipfs to be the "DefaultWriteFS", and muxFS doesn't give us an explicit API for setting
// the write filesystem
mux, err := muxfs.New(ctx, []qfs.Config{})
if err != nil {
return nil, err
}
ipfs, err := qipfs.NewFilesystemFromNode(ctx, node)
if err != nil {
return nil, err
}
if err := mux.SetFilesystem(ipfs); err != nil {
return nil, err
}
localFS, err := localfs.NewFilesystem(ctx, nil)
if err != nil {
return nil, err
}
if err := mux.SetFilesystem(localFS); err != nil {
return nil, err
}
if err := mux.SetFilesystem(qfs.NewMemFS()); err != nil {
return nil, err
}
return qrirepo.NewMemRepoWithProfile(ctx, p, mux, bus)
}
// MakeIPFSNode creates a single mock IPFS Node
func MakeIPFSNode(ctx context.Context) (*core.IpfsNode, coreiface.CoreAPI, error) {
nd, api, err := MakeIPFSSwarm(ctx, true, 1)
if err != nil {
return nil, nil, err
}
return nd[0], api[0], nil
}
const testPeerID = "QmTFauExutTsy4XP6JbMFcw2Wa9645HJt2bTqL6qYDCKfe"
// MakeIPFSSwarm creates and connects n number of mock IPFS Nodes
func MakeIPFSSwarm(ctx context.Context, fullIdentity bool, n int) ([]*core.IpfsNode, []coreiface.CoreAPI, error) {
if n > 10 {
return nil, nil, fmt.Errorf("cannot generate a network of more than 10 peers")
}
mn := mocknet.New(ctx)
nodes := make([]*core.IpfsNode, n)
apis := make([]coreiface.CoreAPI, n)
for i := 0; i < n; i++ {
var ident config.Identity
if fullIdentity {
kd := testkeys.GetKeyData(i)
sk, pk := kd.PrivKey, kd.PrivKey.GetPublic()
id, err := peer.IDFromPublicKey(pk)
if err != nil {
return nil, nil, err
}
kbytes, err := sk.Bytes()
if err != nil {
return nil, nil, err
}
ident = config.Identity{
PeerID: id.Pretty(),
PrivKey: base64.StdEncoding.EncodeToString(kbytes),
}
} else {
ident = config.Identity{
PeerID: testPeerID,
}
}
c := config.Config{}
c.Addresses.Swarm = []string{fmt.Sprintf("/ip4/127.0.%d.1/tcp/4001", i)}
c.Identity = ident
r := &repo.Mock{
C: c,
D: syncds.MutexWrap(datastore.NewMapDatastore()),
K: keystore.NewMemKeystore(),
}
node, err := core.NewNode(ctx, &core.BuildCfg{
Repo: r,
Host: mock.MockHostOption(mn),
Online: fullIdentity,
ExtraOpts: map[string]bool{
"pubsub": true,
},
})
if err != nil {
return nil, nil, err
}
nodes[i] = node
apis[i], err = coreapi.NewCoreAPI(node)
if err != nil {
return nil, nil, err
}
}
err := mn.LinkAll()
if err != nil {
return nil, nil, err
}
bsinf := corebs.BootstrapConfigWithPeers(
[]peer.AddrInfo{
nodes[0].Peerstore.PeerInfo(nodes[0].Identity),
},
)
for _, n := range nodes[1:] {
if err := n.Bootstrap(bsinf); err != nil {
return nil, nil, err
}
}
return nodes, apis, nil
}