-
Notifications
You must be signed in to change notification settings - Fork 43
/
globaldb.go
323 lines (285 loc) · 6.5 KB
/
globaldb.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package main
// This is a CLI that lets you join a global permissionless CRDT-based
// database using CRDTs and IPFS.
import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
ds "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/query"
badger "github.com/ipfs/go-ds-badger"
crdt "github.com/ipfs/go-ds-crdt"
logging "github.com/ipfs/go-log/v2"
pubsub "github.com/libp2p/go-libp2p-pubsub"
crypto "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/mitchellh/go-homedir"
multiaddr "github.com/multiformats/go-multiaddr"
)
var (
logger = logging.Logger("globaldb")
listen, _ = multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/33123")
topicName = "globaldb-example"
netTopic = "globaldb-example-net"
config = "globaldb-example"
)
func main() {
// Bootstrappers are using 1024 keys. See:
// https://github.com/ipfs/infra/issues/378
crypto.MinRsaKeyBits = 1024
logging.SetLogLevel("*", "error")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dir, err := homedir.Dir()
if err != nil {
logger.Fatal(err)
}
data := filepath.Join(dir, config)
store, err := badger.NewDatastore(data, &badger.DefaultOptions)
if err != nil {
logger.Fatal(err)
}
defer store.Close()
keyPath := filepath.Join(data, "key")
var priv crypto.PrivKey
_, err = os.Stat(keyPath)
if os.IsNotExist(err) {
priv, _, err = crypto.GenerateKeyPair(crypto.Ed25519, 1)
if err != nil {
logger.Fatal(err)
}
data, err := crypto.MarshalPrivateKey(priv)
if err != nil {
logger.Fatal(err)
}
err = ioutil.WriteFile(keyPath, data, 0400)
if err != nil {
logger.Fatal(err)
}
} else if err != nil {
logger.Fatal(err)
} else {
key, err := ioutil.ReadFile(keyPath)
if err != nil {
logger.Fatal(err)
}
priv, err = crypto.UnmarshalPrivateKey(key)
if err != nil {
logger.Fatal(err)
}
}
pid, err := peer.IDFromPublicKey(priv.GetPublic())
if err != nil {
logger.Fatal(err)
}
h, dht, err := ipfslite.SetupLibp2p(
ctx,
priv,
nil,
[]multiaddr.Multiaddr{listen},
nil,
ipfslite.Libp2pOptionsExtra...,
)
if err != nil {
logger.Fatal(err)
}
defer h.Close()
defer dht.Close()
psub, err := pubsub.NewGossipSub(ctx, h)
if err != nil {
logger.Fatal(err)
}
topic, err := psub.Join(netTopic)
if err != nil {
logger.Fatal(err)
}
netSubs, err := topic.Subscribe()
if err != nil {
logger.Fatal(err)
}
// Use a special pubsub topic to avoid disconnecting
// from globaldb peers.
go func() {
for {
msg, err := netSubs.Next(ctx)
if err != nil {
fmt.Println(err)
break
}
h.ConnManager().TagPeer(msg.ReceivedFrom, "keep", 100)
}
}()
go func() {
for {
select {
case <-ctx.Done():
return
default:
topic.Publish(ctx, []byte("hi!"))
time.Sleep(20 * time.Second)
}
}
}()
ipfs, err := ipfslite.New(ctx, store, nil, h, dht, nil)
if err != nil {
logger.Fatal(err)
}
psubCtx, psubCancel := context.WithCancel(ctx)
pubsubBC, err := crdt.NewPubSubBroadcaster(psubCtx, psub, topicName)
if err != nil {
logger.Fatal(err)
}
opts := crdt.DefaultOptions()
opts.Logger = logger
opts.RebroadcastInterval = 5 * time.Second
opts.PutHook = func(k ds.Key, v []byte) {
fmt.Printf("Added: [%s] -> %s\n", k, string(v))
}
opts.DeleteHook = func(k ds.Key) {
fmt.Printf("Removed: [%s]\n", k)
}
crdt, err := crdt.New(store, ds.NewKey("crdt"), ipfs, pubsubBC, opts)
if err != nil {
logger.Fatal(err)
}
defer crdt.Close()
defer psubCancel()
fmt.Println("Bootstrapping...")
bstr, _ := multiaddr.NewMultiaddr("/ip4/94.130.135.167/tcp/33123/ipfs/12D3KooWFta2AE7oiK1ioqjVAKajUJauZWfeM7R413K7ARtHRDAu")
inf, _ := peer.AddrInfoFromP2pAddr(bstr)
list := append(ipfslite.DefaultBootstrapPeers(), *inf)
ipfs.Bootstrap(list)
h.ConnManager().TagPeer(inf.ID, "keep", 100)
fmt.Printf(`
Peer ID: %s
Listen address: %s
Topic: %s
Data Folder: %s
Ready!
Commands:
> list -> list items in the store
> get <key> -> get value for a key
> put <key> <value> -> store value on a key
> exit -> quit
`,
pid, listen, topicName, data,
)
if len(os.Args) > 1 && os.Args[1] == "daemon" {
fmt.Println("Running in daemon mode")
go func() {
for {
fmt.Printf("%s - %d connected peers\n", time.Now().Format(time.Stamp), len(connectedPeers(h)))
time.Sleep(10 * time.Second)
}
}()
signalChan := make(chan os.Signal, 20)
signal.Notify(
signalChan,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGHUP,
)
<-signalChan
return
}
fmt.Printf("> ")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()
fields := strings.Fields(text)
if len(fields) == 0 {
fmt.Printf("> ")
continue
}
cmd := fields[0]
switch cmd {
case "exit", "quit":
return
case "debug":
if len(fields) < 2 {
fmt.Println("debug <on/off/peers>")
}
st := fields[1]
switch st {
case "on":
logging.SetLogLevel("globaldb", "debug")
case "off":
logging.SetLogLevel("globaldb", "error")
case "peers":
for _, p := range connectedPeers(h) {
addrs, err := peer.AddrInfoToP2pAddrs(p)
if err != nil {
logger.Warn(err)
continue
}
for _, a := range addrs {
fmt.Println(a)
}
}
}
case "list":
q := query.Query{}
results, err := crdt.Query(ctx, q)
if err != nil {
printErr(err)
}
for r := range results.Next() {
if r.Error != nil {
printErr(err)
continue
}
fmt.Printf("[%s] -> %s\n", r.Key, string(r.Value))
}
case "get":
if len(fields) < 2 {
fmt.Println("get <key>")
fmt.Println("> ")
continue
}
k := ds.NewKey(fields[1])
v, err := crdt.Get(ctx, k)
if err != nil {
printErr(err)
continue
}
fmt.Printf("[%s] -> %s\n", k, string(v))
case "put":
if len(fields) < 3 {
fmt.Println("put <key> <value>")
fmt.Println("> ")
continue
}
k := ds.NewKey(fields[1])
v := strings.Join(fields[2:], " ")
err := crdt.Put(ctx, k, []byte(v))
if err != nil {
printErr(err)
continue
}
}
fmt.Printf("> ")
}
}
func printErr(err error) {
fmt.Println("error:", err)
fmt.Println("> ")
}
func connectedPeers(h host.Host) []*peer.AddrInfo {
var pinfos []*peer.AddrInfo
for _, c := range h.Network().Conns() {
pinfos = append(pinfos, &peer.AddrInfo{
ID: c.RemotePeer(),
Addrs: []multiaddr.Multiaddr{c.RemoteMultiaddr()},
})
}
return pinfos
}