Repository navigation
Expand file tree
/
Copy pathinterop_test.go
More file actions
344 lines (304 loc) · 11.4 KB
/
Copy pathinterop_test.go
File metadata and controls
344 lines (304 loc) · 11.4 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//go:build interop
// Package interop runs go-orbitdb against a live @orbitdb/core 4.0.0 peer
// (Helia over TCP) and checks that databases replicate both ways.
//
// cd interop/js-peer && npm ci && cd ../..
// go test -tags interop ./interop/
package interop
import (
"context"
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/require"
orbitdb "github.com/orbitdb/go-orbitdb"
"github.com/orbitdb/go-orbitdb/accesscontrollers"
"github.com/orbitdb/go-orbitdb/databases"
"github.com/orbitdb/go-orbitdb/encryption"
"github.com/orbitdb/go-orbitdb/ipfs"
"github.com/orbitdb/go-orbitdb/oplog"
)
// goPeer is a go-orbitdb node connected to a JS peer.
func goPeer(t *testing.T, js *jsPeer) *orbitdb.OrbitDB {
t.Helper()
ctx := context.Background()
node, err := ipfs.New(ctx, ipfs.Options{})
require.NoError(t, err)
t.Cleanup(func() { _ = node.Close() })
odb, err := orbitdb.New(ctx, orbitdb.Options{IPFS: node, ID: "go-peer", Directory: t.TempDir()})
require.NoError(t, err)
t.Cleanup(func() { _ = odb.Stop() })
require.NoError(t, node.Connect(ctx, js.addrInfo()))
return odb
}
var anyone = orbitdb.WithAccessController(accesscontrollers.IPFS(accesscontrollers.IPFSOptions{Write: []string{"*"}}))
type jsOpened struct {
Address string `json:"address"`
Type string `json:"type"`
Name string `json:"name"`
}
type jsKV struct {
Key string `json:"key"`
Value any `json:"value"`
Hash string `json:"hash"`
}
type jsValue struct {
Hash string `json:"hash"`
Payload any `json:"payload"`
}
// requireSameLog checks that both peers hold the same entries in the same
// order.
func requireSameLog(t *testing.T, js *jsPeer, db databases.Store, address string) {
t.Helper()
ctx := context.Background()
var jsValues []jsValue
js.call("values", map[string]any{"address": address}, &jsValues)
goValues, err := db.Log().Values(ctx)
require.NoError(t, err)
require.Len(t, goValues, len(jsValues))
for i := range goValues {
require.Equal(t, jsValues[i].Hash, goValues[i].Hash, "entry %d", i)
}
}
func TestJSCreatesGoReplicates(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
var opened jsOpened
js.call("open", map[string]any{"address": "js-kv", "type": "keyvalue", "write": []string{"*"}}, &opened)
addr := map[string]any{"address": opened.Address}
js.call("put", merge(addr, map[string]any{"key": "string", "value": "hello"}), nil)
js.call("put", merge(addr, map[string]any{"key": "nested", "value": map[string]any{"list": []any{1, 2.5, "x"}, "flag": true, "nothing": nil}}), nil)
js.call("put", merge(addr, map[string]any{"key": "number", "value": 42}), nil)
js.call("put", merge(addr, map[string]any{"key": "gone", "value": "soon deleted"}), nil)
js.call("del", merge(addr, map[string]any{"key": "gone"}), nil)
// Go knows only the address: manifest, access controller, heads,
// ancestors and the writer's identity all come from the JS peer.
db, err := odb.OpenKeyValue(ctx, opened.Address)
require.NoError(t, err)
require.Equal(t, "js-kv", db.Name())
waitFor(t, "the JS entries", func() bool {
all, err := db.All(ctx)
return err == nil && len(all) == 3
})
v, err := db.Get(ctx, "nested")
require.NoError(t, err)
require.Equal(t, map[string]any{"list": []any{int64(1), 2.5, "x"}, "flag": true, "nothing": nil}, v)
v, err = db.Get(ctx, "number")
require.NoError(t, err)
require.Equal(t, int64(42), v)
_, err = db.Get(ctx, "gone")
require.ErrorIs(t, err, databases.ErrNotFound)
_, err = db.Put(ctx, "from-go", map[string]any{"lang": "go", "n": 1.5})
require.NoError(t, err)
var jsAll []jsKV
js.call("waitAll", merge(addr, map[string]any{"count": 4}), &jsAll)
var fromGo any
js.call("get", merge(addr, map[string]any{"key": "from-go"}), &fromGo)
require.Equal(t, map[string]any{"lang": "go", "n": 1.5}, fromGo)
requireSameLog(t, js, db, opened.Address)
js.requireNoErrors()
}
func TestGoCreatesJSReplicates(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
db, err := odb.OpenEvents(ctx, "go-events", anyone)
require.NoError(t, err)
for i := range 10 {
_, err := db.Add(ctx, fmt.Sprint("event ", i))
require.NoError(t, err)
}
var opened jsOpened
js.call("open", map[string]any{"address": db.Address()}, &opened)
require.Equal(t, db.Address(), opened.Address)
require.Equal(t, "events", opened.Type)
require.Equal(t, "go-events", opened.Name)
addr := map[string]any{"address": db.Address()}
var jsAll []struct {
Hash string `json:"hash"`
Value string `json:"value"`
}
js.call("waitAll", merge(addr, map[string]any{"count": 10}), &jsAll)
goAll, err := db.All(ctx)
require.NoError(t, err)
require.Len(t, jsAll, 10)
for i := range goAll {
require.Equal(t, goAll[i].Hash, jsAll[i].Hash)
require.Equal(t, goAll[i].Value, jsAll[i].Value)
}
js.call("add", merge(addr, map[string]any{"value": "from js"}), nil)
waitFor(t, "the JS event", func() bool {
all, err := db.All(ctx)
return err == nil && len(all) == 11 && all[10].Value == "from js"
})
requireSameLog(t, js, db, db.Address())
js.requireNoErrors()
}
func TestConcurrentWritesConverge(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
db, err := odb.OpenKeyValue(ctx, "concurrent", anyone)
require.NoError(t, err)
_, err = db.Put(ctx, "start", true)
require.NoError(t, err)
js.call("open", map[string]any{"address": db.Address()}, nil)
addr := map[string]any{"address": db.Address()}
js.call("waitAll", merge(addr, map[string]any{"count": 1}), nil)
const n = 25
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for i := range n {
_, err := db.Put(ctx, fmt.Sprint("go-", i), i)
require.NoError(t, err)
// Overwrite a shared key so last-write-wins has work to do.
_, err = db.Put(ctx, "shared", fmt.Sprint("go-", i))
require.NoError(t, err)
}
}()
for i := range n {
js.call("put", merge(addr, map[string]any{"key": fmt.Sprint("js-", i), "value": i}), nil)
js.call("put", merge(addr, map[string]any{"key": "shared", "value": fmt.Sprint("js-", i)}), nil)
}
wg.Wait()
total := 2*n + 2 // go-*, js-*, start, shared
js.call("waitAll", merge(addr, map[string]any{"count": total}), nil)
waitFor(t, "all JS writes in Go", func() bool {
all, err := db.All(ctx)
return err == nil && len(all) == total
})
// Replication is eventually consistent: wait until both sides hold
// every entry, then require the same order and the same winner.
waitFor(t, "identical logs", func() bool {
var jsValues []jsValue
js.call("values", addr, &jsValues)
goValues, err := db.Log().Values(ctx)
return err == nil && len(goValues) == len(jsValues) && len(goValues) == 4*n+1
})
requireSameLog(t, js, db, db.Address())
var jsShared any
js.call("get", merge(addr, map[string]any{"key": "shared"}), &jsShared)
goShared, err := db.Get(ctx, "shared")
require.NoError(t, err)
require.Equal(t, jsShared, goShared, "both peers pick the same last write")
js.requireNoErrors()
}
func TestDocumentsBothWays(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
var opened jsOpened
js.call("open", map[string]any{"address": "js-docs", "type": "documents", "write": []string{"*"}}, &opened)
addr := map[string]any{"address": opened.Address}
js.call("putDoc", merge(addr, map[string]any{"doc": map[string]any{"_id": "a", "title": "from js", "tags": []string{"x"}}}), nil)
db, err := odb.OpenDocuments(ctx, opened.Address)
require.NoError(t, err)
waitFor(t, "the JS document", func() bool {
doc, err := db.Get(ctx, "a")
return err == nil && doc.Value["title"] == "from js"
})
_, err = db.Put(ctx, map[string]any{"_id": "b", "title": "from go"})
require.NoError(t, err)
_, err = db.Del(ctx, "a")
require.NoError(t, err)
waitFor(t, "the Go changes in JS", func() bool {
var docs []map[string]any
js.call("all", addr, &docs)
return len(docs) == 1 && docs[0]["key"] == "b"
})
requireSameLog(t, js, db, opened.Address)
js.requireNoErrors()
}
func TestWriteAccessIsShared(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
// The JS default: only the creator may write.
var opened jsOpened
js.call("open", map[string]any{"address": "js-only", "type": "keyvalue"}, &opened)
js.call("put", map[string]any{"address": opened.Address, "key": "k", "value": "v"}, nil)
db, err := odb.OpenKeyValue(ctx, opened.Address)
require.NoError(t, err)
waitFor(t, "the JS entry", func() bool {
v, err := db.Get(ctx, "k")
return err == nil && v == "v"
})
_, err = db.Put(ctx, "k", "go was here")
require.ErrorContains(t, err, "not allowed to write", "Go enforces the JS access controller")
js.requireNoErrors()
}
func TestOrbitDBAccessControllerGrant(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
var opened jsOpened
js.call("open", map[string]any{"address": "managed", "type": "events", "access": "orbitdb"}, &opened)
addr := map[string]any{"address": opened.Address}
js.call("add", merge(addr, map[string]any{"value": "first"}), nil)
db, err := odb.OpenEvents(ctx, opened.Address)
require.NoError(t, err)
ac, ok := db.AccessController().(*accesscontrollers.OrbitDBAccessController)
require.True(t, ok, "Go loads the JS orbitdb access controller from the manifest")
waitFor(t, "the first event", func() bool {
all, err := db.All(ctx)
return err == nil && len(all) == 1
})
_, err = db.Add(ctx, "denied")
require.ErrorContains(t, err, "not allowed to write")
js.call("grant", merge(addr, map[string]any{"capability": "write", "id": odb.Identity().ID}), nil)
waitFor(t, "the grant to replicate", func() bool {
ok, err := ac.HasCapability(ctx, accesscontrollers.CapabilityWrite, odb.Identity().ID)
return err == nil && ok
})
_, err = db.Add(ctx, "granted")
require.NoError(t, err)
var jsAll []map[string]any
js.call("waitAll", merge(addr, map[string]any{"count": 2}), &jsAll)
require.Equal(t, "granted", jsAll[1]["value"])
js.requireNoErrors()
}
func TestEncryptedDatabase(t *testing.T) {
ctx := context.Background()
js := startJSPeer(t)
odb := goPeer(t, js)
const password = "correct horse battery staple"
var opened jsOpened
js.call("open", map[string]any{"address": "secret", "type": "keyvalue", "write": []string{"*"}, "password": password}, &opened)
addr := map[string]any{"address": opened.Address}
js.call("put", merge(addr, map[string]any{"key": "js", "value": "encrypted by js"}), nil)
data, err := encryption.NewSimple([]byte(password))
require.NoError(t, err)
replication, err := encryption.NewSimple([]byte(password))
require.NoError(t, err)
db, err := odb.OpenKeyValue(ctx, opened.Address, orbitdb.WithEncryption(oplog.Encryption{Data: data, Replication: replication}))
require.NoError(t, err)
waitFor(t, "the encrypted JS entry", func() bool {
v, err := db.Get(ctx, "js")
return err == nil && v == "encrypted by js"
})
hash, err := db.Put(ctx, "go", "encrypted by go")
require.NoError(t, err)
raw, err := db.Log().Storage().Get(ctx, hash)
require.NoError(t, err)
require.NotContains(t, string(raw), "encrypted by go")
var jsAll []jsKV
js.call("waitAll", merge(addr, map[string]any{"count": 2}), &jsAll)
var fromGo any
js.call("get", merge(addr, map[string]any{"key": "go"}), &fromGo)
require.Equal(t, "encrypted by go", fromGo)
requireSameLog(t, js, db, opened.Address)
js.requireNoErrors()
}
func merge(a, b map[string]any) map[string]any {
out := make(map[string]any, len(a)+len(b))
for k, v := range a {
out[k] = v
}
for k, v := range b {
out[k] = v
}
return out
}