This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
/
roundsubscriber.go
293 lines (244 loc) · 8.28 KB
/
roundsubscriber.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
package client
import (
"context"
"fmt"
"sync"
"time"
"github.com/AsynkronIT/protoactor-go/eventstream"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-hamt-ipld"
cbornode "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log"
"github.com/quorumcontrol/chaintree/nodestore"
"github.com/quorumcontrol/messages/v2/build/go/gossip"
"github.com/quorumcontrol/messages/v2/build/go/services"
"github.com/quorumcontrol/messages/v2/build/go/signatures"
"github.com/quorumcontrol/tupelo-go-sdk/bls"
"github.com/quorumcontrol/tupelo-go-sdk/gossip/client/pubsubinterfaces"
"github.com/quorumcontrol/tupelo-go-sdk/gossip/hamtwrapper"
"github.com/quorumcontrol/tupelo-go-sdk/gossip/types"
sigfuncs "github.com/quorumcontrol/tupelo-go-sdk/signatures"
)
type ValidationNotification struct {
Accepted []cid.Cid
Checkpoint *types.CheckpointWrapper
RoundConfirmation *types.RoundConfirmationWrapper
CompletedRound *types.RoundWrapper
}
func (vn *ValidationNotification) includes(id cid.Cid) bool {
for _, abrCid := range vn.Accepted {
if abrCid.Equals(id) {
return true
}
}
return false
}
type subscription *eventstream.Subscription
type roundConflictSet map[cid.Cid]*gossip.RoundConfirmation
func isQuorum(group *types.NotaryGroup, sig *signatures.Signature) bool {
return uint64(sigfuncs.SignerCount(sig)) >= group.QuorumCount()
}
func (rcs roundConflictSet) add(group *types.NotaryGroup, confirmation *gossip.RoundConfirmation) (makesQuorum bool, updated *gossip.RoundConfirmation, err error) {
ctx := context.TODO()
cid, err := cid.Cast(confirmation.RoundCid)
if err != nil {
return false, nil, fmt.Errorf("error casting round cid: %w", err)
}
existing, ok := rcs[cid]
if !ok {
// this is the first time we're seeing the completed round,
// just add it to the conflict set and move on
rcs[cid] = confirmation
return isQuorum(group, confirmation.Signature), confirmation, nil
}
// otherwise we've already seen a confirmation for this, let's combine the signatures
newSig, err := sigfuncs.AggregateBLSSignatures(ctx, []*signatures.Signature{existing.Signature, confirmation.Signature})
if err != nil {
return false, nil, err
}
existing.Signature = newSig
rcs[cid] = existing
return isQuorum(group, existing.Signature), existing, nil
}
type conflictSetHolder map[uint64]roundConflictSet
type roundSubscriber struct {
sync.RWMutex
pubsub pubsubinterfaces.Pubsubber
dagStore nodestore.DagStore
hamtStore *hamt.CborIpldStore
group *types.NotaryGroup
logger logging.EventLogger
inflight conflictSetHolder
current *types.RoundConfirmationWrapper
stream *eventstream.EventStream
}
func newRoundSubscriber(logger logging.EventLogger, group *types.NotaryGroup, pubsub pubsubinterfaces.Pubsubber, store nodestore.DagStore) *roundSubscriber {
hamtStore := hamtwrapper.DagStoreToCborIpld(store)
return &roundSubscriber{
pubsub: pubsub,
dagStore: store,
hamtStore: hamtStore,
group: group,
inflight: make(conflictSetHolder),
logger: logger,
stream: &eventstream.EventStream{},
}
}
func (rs *roundSubscriber) Current() *types.RoundConfirmationWrapper {
rs.RLock()
defer rs.RUnlock()
return rs.current
}
func (rs *roundSubscriber) start(ctx context.Context) error {
sub, err := rs.pubsub.Subscribe(rs.group.ID)
if err != nil {
return fmt.Errorf("error subscribing %w", err)
}
go func() {
for {
msg, err := sub.Next(ctx)
if err != nil {
rs.logger.Warningf("error getting sub message: %v", err)
return
}
if err := rs.handleMessage(ctx, msg); err != nil {
rs.logger.Warningf("error handling pubsub message: %v", err)
}
}
}()
return nil
}
func (rs *roundSubscriber) subscribe(ctx context.Context, abr *services.AddBlockRequest, ch chan *gossip.Proof) (subscription, error) {
rs.Lock()
defer rs.Unlock()
isDone := false
doneCh := ctx.Done()
// put the abr into the hamtstore because if we're subscribing, we'll probably want that later
abrCid, err := rs.hamtStore.Put(ctx, abr)
if err != nil {
rs.logger.Errorf("error decoding add block request: %v", err)
return nil, fmt.Errorf("error decoding add block request: %w", err)
}
rs.logger.Debugf("subscribing: %s", abrCid.String())
return rs.stream.Subscribe(func(evt interface{}) {
if isDone {
return // we're already done and we don't want to do another channel op below
}
select {
case <-doneCh:
isDone = true
return // we're already done
default:
// continue on, people still care
}
if noti := evt.(*ValidationNotification); noti.includes(abrCid) {
proof := &gossip.Proof{
ObjectId: abr.ObjectId,
Tip: abr.NewTip,
AddBlockRequest: abr,
Checkpoint: noti.Checkpoint.Value(),
Round: noti.CompletedRound.Value(),
RoundConfirmation: noti.RoundConfirmation.Value(),
}
isDone = true
ch <- proof
return
}
}), nil
}
func (rs *roundSubscriber) unsubscribe(sub subscription) {
rs.Lock()
defer rs.Unlock()
rs.stream.Unsubscribe(sub)
}
func (rs *roundSubscriber) pubsubMessageToRoundConfirmation(ctx context.Context, msg pubsubinterfaces.Message) (*gossip.RoundConfirmation, error) {
bits := msg.GetData()
confirmation := &gossip.RoundConfirmation{}
err := cbornode.DecodeInto(bits, confirmation)
return confirmation, err
}
// TODO: we can cache this
func (rs *roundSubscriber) verKeys() []*bls.VerKey {
keys := make([]*bls.VerKey, len(rs.group.AllSigners()))
for i, s := range rs.group.AllSigners() {
keys[i] = s.VerKey
}
return keys
}
func (rs *roundSubscriber) handleMessage(ctx context.Context, msg pubsubinterfaces.Message) error {
rs.logger.Debugf("handling message")
confirmation, err := rs.pubsubMessageToRoundConfirmation(ctx, msg)
if err != nil {
return fmt.Errorf("error unmarshaling: %w", err)
}
if rs.current != nil && confirmation.Height <= rs.current.Height() {
return fmt.Errorf("confirmation of height %d is less than current %d", confirmation.Height, rs.current.Height())
}
err = sigfuncs.RestoreBLSPublicKey(ctx, confirmation.Signature, rs.verKeys())
if err != nil {
return fmt.Errorf("error restoring BLS key: %w", err)
}
verified, err := sigfuncs.Valid(ctx, confirmation.Signature, confirmation.RoundCid, nil)
if !verified || err != nil {
return fmt.Errorf("signature invalid with error: %w", err)
}
rs.Lock()
defer rs.Unlock()
conflictSet, ok := rs.inflight[confirmation.Height]
if !ok {
conflictSet = make(roundConflictSet)
}
rs.logger.Debugf("checking quorum: %v", confirmation)
madeQuorum, updated, err := conflictSet.add(rs.group, confirmation)
if err != nil {
return fmt.Errorf("error adding to conflictset: %w", err)
}
rs.inflight[confirmation.Height] = conflictSet
if madeQuorum {
if err := rs.handleQuorum(ctx, updated); err != nil {
return fmt.Errorf("error handling quorum: %w", err)
}
}
return nil
}
func (rs *roundSubscriber) handleQuorum(ctx context.Context, confirmation *gossip.RoundConfirmation) error {
// handleQuorum expects that it's already in a lock on the roundSubscriber
rs.logger.Debugf("hande Quorum: %v", confirmation)
wrappedConfirmation := types.WrapRoundConfirmation(confirmation)
wrappedConfirmation.SetStore(rs.dagStore)
rs.current = wrappedConfirmation
for key := range rs.inflight {
if key <= confirmation.Height {
delete(rs.inflight, key)
}
}
ctxWithTimeout, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
rs.logger.Debugf("getting completed round")
completedRound, err := wrappedConfirmation.FetchCompletedRound(ctxWithTimeout)
if err != nil {
return fmt.Errorf("error fetching completed round: %w", err)
}
rs.logger.Debugf("getting checkpoint")
wrappedCheckpoint, err := completedRound.FetchCheckpoint(ctxWithTimeout)
if err != nil {
return fmt.Errorf("error fetching checkpoint: %w", err)
}
cidsAsBytes := wrappedCheckpoint.AddBlockRequests()
accepted := make([]cid.Cid, len(cidsAsBytes))
for i, cidBytes := range cidsAsBytes {
abrCid, err := cid.Cast(cidBytes)
if err != nil {
return fmt.Errorf("error casting cid: %w", err)
}
accepted[i] = abrCid
}
rs.logger.Debugf("validationNotification publish %d", wrappedConfirmation.Height())
rs.stream.Publish(&ValidationNotification{
Accepted: accepted,
Checkpoint: wrappedCheckpoint,
RoundConfirmation: wrappedConfirmation,
CompletedRound: completedRound,
})
return nil
}