@@ -2,47 +2,23 @@ package baseorbitdb
2
2
3
3
import (
4
4
"context"
5
- "encoding/json"
6
5
"fmt"
7
6
8
7
ipfslog "berty.tech/go-ipfs-log"
9
- "berty.tech/go-ipfs-log/enc "
8
+ "berty.tech/go-ipfs-log/entry "
10
9
"berty.tech/go-orbit-db/iface"
11
10
"berty.tech/go-orbit-db/stores"
12
- "go.uber.org/zap"
13
11
)
14
12
15
- func (o * orbitDB ) handleEventPubSubPayload (ctx context.Context , e * iface.EventPubSubPayload , sharedKey enc.SharedKey ) error {
16
- heads := & exchangedHeads {}
17
- payload := e .Payload
18
-
19
- if sharedKey != nil {
20
- var err error
21
-
22
- payload , err = sharedKey .Open (payload )
23
- if err != nil {
24
- return fmt .Errorf ("unable to decrypt payload: %w" , err )
25
- }
26
- }
27
-
28
- err := json .Unmarshal (payload , & heads )
29
- if err != nil {
30
- o .logger .Error ("unable to unmarshal heads" , zap .Error (err ))
31
- }
32
-
33
- o .logger .Debug (fmt .Sprintf ("%s: Received %d heads for '%s':" , o .PeerID ().String (), len (heads .Heads ), heads .Address ))
34
- store , ok := o .getStore (heads .Address )
35
-
36
- if ! ok {
37
- return fmt .Errorf ("heads from unknown store, skipping" )
13
+ func (o * orbitDB ) handleEventExchangeHeads (ctx context.Context , e * iface.MessageExchangeHeads , store iface.Store ) error {
14
+ untypedHeads := make ([]ipfslog.Entry , len (e .Heads ))
15
+ for i , h := range e .Heads {
16
+ untypedHeads [i ] = h
38
17
}
39
18
40
- if len (heads .Heads ) > 0 {
41
- untypedHeads := make ([]ipfslog.Entry , len (heads .Heads ))
42
- for i := range heads .Heads {
43
- untypedHeads [i ] = heads .Heads [i ]
44
- }
19
+ o .logger .Debug (fmt .Sprintf ("%s: Received %d heads for '%s':" , o .PeerID ().String (), len (untypedHeads ), e .Address ))
45
20
21
+ if len (untypedHeads ) > 0 {
46
22
if err := store .Sync (ctx , untypedHeads ); err != nil {
47
23
return fmt .Errorf ("unable to sync heads: %w" , err )
48
24
}
@@ -51,7 +27,7 @@ func (o *orbitDB) handleEventPubSubPayload(ctx context.Context, e *iface.EventPu
51
27
return nil
52
28
}
53
29
54
- func (o * orbitDB ) handleEventWrite (ctx context.Context , e * stores.EventWrite , store Store , topic iface.PubSubTopic ) error {
30
+ func (o * orbitDB ) handleEventWrite (ctx context.Context , e * stores.EventWrite , topic iface.PubSubTopic , store Store ) error {
55
31
o .logger .Debug ("received stores.write event" )
56
32
if len (e .Heads ) == 0 {
57
33
return fmt .Errorf ("'heads' are not defined" )
@@ -64,19 +40,26 @@ func (o *orbitDB) handleEventWrite(ctx context.Context, e *stores.EventWrite, st
64
40
}
65
41
66
42
if len (peer ) > 0 {
67
- headsBytes , err := json .Marshal (e .Heads )
68
- if err != nil {
69
- return fmt .Errorf ("unable to serialize heads %w" , err )
43
+ entries := make ([]* entry.Entry , len (e .Heads ))
44
+ for i , head := range e .Heads {
45
+ if entry , ok := head .(* entry.Entry ); ok {
46
+ entries [i ] = entry
47
+ } else {
48
+ return fmt .Errorf ("unable to unwrap entry" )
49
+ }
70
50
}
71
51
72
- if key := store .SharedKey (); key != nil {
73
- headsBytes , err = key .Seal (headsBytes )
74
- if err != nil {
75
- return fmt .Errorf ("unable to encrypt heads %w" , err )
76
- }
52
+ msg := & iface.MessageExchangeHeads {
53
+ Address : store .Address ().String (),
54
+ Heads : entries ,
55
+ }
56
+
57
+ payload , err := o .messageMarshaler .Marshal (msg )
58
+ if err != nil {
59
+ return fmt .Errorf ("unable to serialize heads %w" , err )
77
60
}
78
61
79
- err = topic .Publish (ctx , headsBytes )
62
+ err = topic .Publish (ctx , payload )
80
63
if err != nil {
81
64
return fmt .Errorf ("unable to publish message on pubsub %w" , err )
82
65
}
0 commit comments