/
chain.go
229 lines (210 loc) 路 7.02 KB
/
chain.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
package beacon
import (
"context"
"fmt"
"github.com/drand/drand/chain"
"github.com/drand/drand/key"
"github.com/drand/drand/log"
"github.com/drand/drand/net"
"github.com/drand/drand/protobuf/drand"
)
const (
defaultPartialChanBuffer = 10
defaultNewBeaconBuffer = 100
)
// chainStore implements CallbackStore, Syncer and deals with reconstructing the
// beacons, and sync when needed. This struct is the gateway logic for beacons to
// be inserted in the database and for replying to beacon requests.
type chainStore struct {
CallbackStore
l log.Logger
conf *Config
client net.ProtocolClient
sync Syncer
crypto *cryptoStore
ticker *ticker
done chan bool
newPartials chan partialInfo
// catchupBeacons is used to notify the Handler when a node has aggregated a
// beacon.
catchupBeacons chan *chain.Beacon
// all beacons finally inserted into the store are sent over this cannel for
// the aggregation loop to know
beaconStoredAgg chan *chain.Beacon
}
func newChainStore(l log.Logger, cf *Config, cl net.ProtocolClient, c *cryptoStore, store chain.Store, t *ticker) *chainStore {
// we make sure the chain is increasing monotically
as := newAppendStore(store)
// we write some stats about the timing when new beacon is saved
ds := newDiscrepancyStore(as, l, c.GetGroup())
// we can register callbacks on it
cbs := NewCallbackStore(ds)
// we give the final append store to the syncer
syncer := NewSyncer(l, cbs, c.chain, cl)
cs := &chainStore{
CallbackStore: cbs,
l: l,
conf: cf,
client: cl,
sync: syncer,
crypto: c,
ticker: t,
done: make(chan bool, 1),
newPartials: make(chan partialInfo, defaultPartialChanBuffer),
catchupBeacons: make(chan *chain.Beacon, 1),
beaconStoredAgg: make(chan *chain.Beacon, defaultNewBeaconBuffer),
}
// we add callbacks to notify each time a final beacon is stored on the
// database so to update the latest view
cbs.AddCallback("chainstore", func(b *chain.Beacon) {
cs.beaconStoredAgg <- b
})
// TODO maybe look if it's worth having multiple workers there
go cs.runAggregator()
return cs
}
func (c *chainStore) NewValidPartial(addr string, p *drand.PartialBeaconPacket) {
c.newPartials <- partialInfo{
addr: addr,
p: p,
}
}
func (c *chainStore) Stop() {
c.CallbackStore.Close()
close(c.done)
}
// we store partials that are up to this amount of rounds more than the last
// beacon we have - it is useful to store partials that may come in advance,
// especially in case of a quick catchup.
var partialCacheStoreLimit = 3
// runAggregator runs a continuous loop that tries to aggregate partial
// signatures when it can
func (c *chainStore) runAggregator() {
lastBeacon, err := c.Last()
if err != nil {
c.l.Fatal("chain_aggregator", "loading", "last_beacon", err)
}
var cache = newPartialCache(c.l)
for {
select {
case <-c.done:
return
case lastBeacon = <-c.beaconStoredAgg:
cache.FlushRounds(lastBeacon.Round)
break
case partial := <-c.newPartials:
// look if we have info for this round first
pRound := partial.p.GetRound()
// look if we want to store ths partial anyway
isNotInPast := pRound > lastBeacon.Round
isNotTooFar := pRound <= lastBeacon.Round+uint64(partialCacheStoreLimit+1)
shouldStore := isNotInPast && isNotTooFar
// check if we can reconstruct
if !shouldStore {
c.l.Debug("ignoring_partial", partial.p.GetRound(), "last_beacon_stored", lastBeacon.Round)
break
}
// NOTE: This line means we can only verify partial signatures of
// the current group we are in as only current members should
// participate in the randomness generation. Previous beacons can be
// verified using the single distributed public key point from the
// crypto store.
thr := c.crypto.GetGroup().Threshold
n := c.crypto.GetGroup().Len()
cache.Append(partial.p)
roundCache := cache.GetRoundCache(partial.p.GetRound(), partial.p.GetPreviousSig())
if roundCache == nil {
c.l.Error("store_partial", partial.addr, "no_round_cache", partial.p.GetRound())
break
}
c.l.Debug("store_partial", partial.addr, "round", roundCache.round, "len_partials", fmt.Sprintf("%d/%d", roundCache.Len(), thr))
if roundCache.Len() < thr {
break
}
msg := roundCache.Msg()
finalSig, err := key.Scheme.Recover(c.crypto.GetPub(), msg, roundCache.Partials(), thr, n)
if err != nil {
c.l.Debug("invalid_recovery", err, "round", pRound, "got", fmt.Sprintf("%d/%d", roundCache.Len(), n))
break
}
if err := key.Scheme.VerifyRecovered(c.crypto.GetPub().Commit(), msg, finalSig); err != nil {
c.l.Error("invalid_sig", err, "round", pRound)
break
}
cache.FlushRounds(partial.p.GetRound())
newBeacon := &chain.Beacon{
Round: roundCache.round,
PreviousSig: roundCache.prev,
Signature: finalSig,
}
c.l.Info("aggregated_beacon", newBeacon.Round)
if c.tryAppend(lastBeacon, newBeacon) {
lastBeacon = newBeacon
break
}
// XXX store them for lfutur usage if it's a later round than what
// we have
c.l.Debug("new_aggregated", "not_appendable", "last", lastBeacon.String(), "new", newBeacon.String())
if c.shouldSync(lastBeacon, newBeacon) {
peers := toPeers(c.crypto.GetGroup().Nodes)
go func() {
// XXX Could do something smarter with context and cancellation
// if we got to the right round
if err := c.sync.Follow(context.Background(), newBeacon.Round, peers); err != nil {
c.l.Debug("chain_store", "unable to follow", "err", err)
}
}()
}
break
}
}
}
func (c *chainStore) tryAppend(last, newB *chain.Beacon) bool {
if last.Round+1 != newB.Round {
// quick check before trying to compare bytes
return false
}
if err := c.CallbackStore.Put(newB); err != nil {
// if round is ok but bytes are different, error will be raised
c.l.Error("chain_store", "error storing beacon", "err", err)
return false
}
select {
// only send if it's not full already
case c.catchupBeacons <- newB:
default:
}
return true
}
type likeBeacon interface {
GetRound() uint64
}
func (c *chainStore) shouldSync(last *chain.Beacon, newB likeBeacon) bool {
// we should sync if we are two blocks late
return newB.GetRound() > last.GetRound()+1
}
// RunSync will sync up with other nodes and fill the store. If upTo is equal to
// 0, then it will follow the chain indefinitely. If peers is nil, it uses the
// peers of the current group.
func (c *chainStore) RunSync(ctx context.Context, upTo uint64, peers []net.Peer) {
if peers == nil {
peers = toPeers(c.crypto.GetGroup().Nodes)
}
if err := c.sync.Follow(ctx, upTo, peers); err != nil {
c.l.Debug("chain_store", "follow_finished", "err", err)
}
}
func (c *chainStore) AppendedBeaconNoSync() chan *chain.Beacon {
return c.catchupBeacons
}
type partialInfo struct {
addr string
p *drand.PartialBeaconPacket
}
func toPeers(nodes []*key.Node) []net.Peer {
peers := make([]net.Peer, len(nodes))
for i := 0; i < len(nodes); i++ {
peers[i] = nodes[i].Identity
}
return peers
}