forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
217 lines (182 loc) · 5.4 KB
/
service.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
package bertyprotocol
import (
"context"
"path/filepath"
"sync"
"sync/atomic"
"time"
datastore "github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
ipfs_core "github.com/ipfs/go-ipfs/core"
ipfs_interface "github.com/ipfs/interface-go-ipfs-core"
"github.com/libp2p/go-libp2p-core/host"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"go.uber.org/zap"
"berty.tech/berty/v2/go/internal/ipfsutil"
"berty.tech/berty/v2/go/internal/tinder"
"berty.tech/berty/v2/go/pkg/bertyversion"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
"berty.tech/go-orbit-db/baseorbitdb"
"berty.tech/go-orbit-db/iface"
)
var _ Service = (*service)(nil)
// Service is the main Berty Protocol interface
type Service interface {
protocoltypes.ProtocolServiceServer
Close() error
Status() Status
IpfsCoreAPI() ipfs_interface.CoreAPI
}
type service struct {
// variables
ctx context.Context
logger *zap.Logger
ipfsCoreAPI ipfsutil.ExtendedCoreAPI
odb *BertyOrbitDB
accountGroup *groupContext
deviceKeystore DeviceKeystore
openedGroups map[string]*groupContext
groups map[string]*protocoltypes.Group
lock sync.RWMutex
authSession atomic.Value
close func() error
startedAt time.Time
host host.Host
}
// Opts contains optional configuration flags for building a new Client
type Opts struct {
Logger *zap.Logger
IpfsCoreAPI ipfsutil.ExtendedCoreAPI
DeviceKeystore DeviceKeystore
DatastoreDir string
RootDatastore datastore.Batching
OrbitDB *BertyOrbitDB
TinderDriver tinder.Driver
RendezvousRotationBase time.Duration
Host host.Host
PubSub *pubsub.PubSub
LocalOnly bool
close func() error
}
func (opts *Opts) applyDefaults(ctx context.Context) error {
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
if opts.RootDatastore == nil {
if opts.DatastoreDir == "" || opts.DatastoreDir == InMemoryDirectory {
opts.RootDatastore = ds_sync.MutexWrap(datastore.NewMapDatastore())
} else {
opts.RootDatastore = nil
}
}
if opts.DeviceKeystore == nil {
ks := ipfsutil.NewDatastoreKeystore(ipfsutil.NewNamespacedDatastore(opts.RootDatastore, datastore.NewKey(NamespaceDeviceKeystore)))
opts.DeviceKeystore = NewDeviceKeystore(ks)
}
if opts.RendezvousRotationBase.Nanoseconds() <= 0 {
opts.RendezvousRotationBase = time.Hour * 24
}
if opts.IpfsCoreAPI == nil {
var err error
var createdIPFSNode *ipfs_core.IpfsNode
opts.IpfsCoreAPI, createdIPFSNode, err = ipfsutil.NewCoreAPI(ctx, &ipfsutil.CoreAPIConfig{})
if err != nil {
return errcode.TODO.Wrap(err)
}
opts.Host = createdIPFSNode.PeerHost
oldClose := opts.close
opts.close = func() error {
if oldClose != nil {
_ = oldClose()
}
return createdIPFSNode.Close()
}
}
if opts.OrbitDB == nil {
orbitDirectory := InMemoryDirectory
if opts.DatastoreDir != InMemoryDirectory {
orbitDirectory = filepath.Join(opts.DatastoreDir, NamespaceOrbitDBDirectory)
}
odbOpts := &NewOrbitDBOptions{
NewOrbitDBOptions: baseorbitdb.NewOrbitDBOptions{
Directory: &orbitDirectory,
Logger: opts.Logger,
},
Datastore: ipfsutil.NewNamespacedDatastore(opts.RootDatastore, datastore.NewKey(NamespaceOrbitDBDatastore)),
DeviceKeystore: opts.DeviceKeystore,
}
odb, err := NewBertyOrbitDB(ctx, opts.IpfsCoreAPI, odbOpts)
if err != nil {
return nil
}
oldClose := opts.close
opts.close = func() error {
if oldClose != nil {
_ = oldClose()
}
return odb.Close()
}
opts.OrbitDB = odb
}
return nil
}
// New initializes a new Service
func New(ctx context.Context, opts Opts) (Service, error) {
if err := opts.applyDefaults(ctx); err != nil {
return nil, errcode.TODO.Wrap(err)
}
opts.Logger = opts.Logger.Named("pt")
opts.Logger.Debug("initializing protocol", zap.String("version", bertyversion.Version))
dbOpts := &iface.CreateDBOptions{LocalOnly: &opts.LocalOnly}
acc, err := opts.OrbitDB.openAccountGroup(ctx, dbOpts, opts.IpfsCoreAPI)
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
if opts.TinderDriver != nil {
s := NewSwiper(opts.Logger, opts.PubSub, opts.RendezvousRotationBase)
opts.Logger.Debug("tinder swiper is enabled")
if err := initContactRequestsManager(ctx, s, acc.metadataStore, opts.IpfsCoreAPI, opts.Logger); err != nil {
return nil, errcode.TODO.Wrap(err)
}
} else {
opts.Logger.Warn("no tinder driver provided, incoming and outgoing contact requests won't be enabled")
}
return &service{
ctx: ctx,
host: opts.Host,
ipfsCoreAPI: opts.IpfsCoreAPI,
logger: opts.Logger,
odb: opts.OrbitDB,
deviceKeystore: opts.DeviceKeystore,
close: opts.close,
accountGroup: acc,
startedAt: time.Now(),
groups: map[string]*protocoltypes.Group{
string(acc.Group().PublicKey): acc.Group(),
},
openedGroups: map[string]*groupContext{
string(acc.Group().PublicKey): acc,
},
}, nil
}
func (s *service) IpfsCoreAPI() ipfs_interface.CoreAPI {
return s.ipfsCoreAPI
}
func (s *service) Close() error {
s.odb.Close()
if s.close != nil {
s.close()
}
return nil
}
// Status contains results of status checks
type Status struct {
DB error
Protocol error
}
func (s *service) Status() Status {
return Status{
Protocol: nil,
}
}