-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
134 lines (121 loc) · 3.89 KB
/
db.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
package db
import (
"context"
"os"
"path/filepath"
logging "github.com/ipfs/go-log/v2"
orbitdb "berty.tech/go-orbit-db"
ds "github.com/ipfs/go-datastore"
dsync "github.com/ipfs/go-datastore/sync"
cfg "github.com/ipfs/go-ipfs/config"
ipfsCore "github.com/ipfs/go-ipfs/core"
coreapi "github.com/ipfs/go-ipfs/core/coreapi"
libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
repo "github.com/ipfs/go-ipfs/repo"
iface "github.com/ipfs/interface-go-ipfs-core"
"github.com/allisterb/citizen5/crypto"
"github.com/allisterb/citizen5/util"
)
type DataStores struct {
DB orbitdb.OrbitDB
Reports orbitdb.DocumentStore
}
var log = logging.Logger("citizen5/db")
func initIPFSRepo(ctx context.Context, privkey string, pubkey string) repo.Repo {
pid := crypto.GetIdentity(pubkey)
c := cfg.Config{}
c.Pubsub.Enabled = cfg.True
c.Bootstrap = []string{
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
}
c.Addresses.Swarm = []string{"/ip4/127.0.0.1/tcp/4001", "/ip4/127.0.0.1/udp/4001/quic"}
c.Identity.PeerID = pid.Pretty()
c.Identity.PrivKey = privkey
return &repo.Mock{
D: dsync.MutexWrap(ds.NewMapDatastore()),
C: c,
}
}
func InitIPFSApi(ctx context.Context, privkey string, pubkey string) (iface.CoreAPI, func(), error) {
log.Infof("IPFS node %s starting...", crypto.GetIdentity(pubkey).Pretty())
node, err := ipfsCore.NewNode(ctx, &ipfsCore.BuildCfg{
Online: true,
Routing: libp2p.DHTOption,
Repo: initIPFSRepo(ctx, privkey, pubkey),
ExtraOpts: map[string]bool{
"pubsub": true,
},
})
if err != nil {
return nil, nil, err
}
log.Infof("IPFS node %s started", node.Identity.Pretty())
c, e := coreapi.NewCoreAPI(node)
if e != nil {
return nil, nil, e
} else {
clean := func() {
log.Infof("IPFS node %s shutting down...", node.Identity.Pretty())
node.Close()
log.Infof("IPFS node %s shutdown completed", node.Identity.Pretty())
}
return c, clean, e
}
}
func CreateDB(ctx context.Context, privkey string, pubkey string) error {
h := util.GetUserHomeDir()
datapath := filepath.Join(h, ".citizen5")
dbpath := filepath.Join(datapath, "db")
os.MkdirAll(datapath, os.ModePerm)
ipfs, cleanup, err := InitIPFSApi(ctx, privkey, pubkey)
if err != nil {
return err
}
log.Infof("creating OrbitDB database at local path %s...", dbpath)
d, e := orbitdb.NewOrbitDB(ctx, ipfs, &orbitdb.NewOrbitDBOptions{
//DirectChannelFactory: directchannel.InitDirectChannelFactory(zap.NewNop(), node1.PeerHost),
Directory: &dbpath,
Logger: log.Desugar(),
})
if e != nil {
return err
}
docs, err := d.Docs(ctx, "reports", nil)
if err != nil {
log.Errorf("could not create OrbitDB document database: %v", err)
return err
} else {
log.Infof("created OrbitDB document database 'reports' at IPFS address %s", docs.Address().String())
}
d.Close()
cleanup()
return nil
}
func OpenDB(ctx context.Context, privkey string, pubkey string) (orbitdb.OrbitDB, func(), error) {
log.Infof("opening OrbitDB database at local path %s...", util.DbDir)
ipfs, cleanup, err := InitIPFSApi(ctx, privkey, pubkey)
if err != nil {
return nil, nil, err
}
d, e := orbitdb.NewOrbitDB(ctx, ipfs, &orbitdb.NewOrbitDBOptions{
Directory: &util.DbDir,
Logger: log.Desugar(),
})
if e != nil {
return nil, nil, err
}
return d, cleanup, nil
}
func OpenDocStore(ctx context.Context, db orbitdb.OrbitDB, name string) (orbitdb.DocumentStore, error) {
docs, err := db.Docs(ctx, name, nil)
if err != nil {
log.Errorf("could not open OrbitDB document store %s: %v", name, err)
return nil, err
} else {
log.Infof("opened OrbitDB document store '%s' at IPFS address %s", name, docs.Address().String())
}
return docs, nil
}