-
Notifications
You must be signed in to change notification settings - Fork 4
/
node.go
386 lines (339 loc) · 8.77 KB
/
node.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//go:generate go run genset.go
package scp
import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"log"
"math/big"
"math/rand"
"time"
"github.com/davecgh/go-xdr/xdr"
)
type NodeID string
func (n NodeID) Less(other NodeID) bool { return n < other }
// Node is the type of a participating SCP node.
type Node struct {
ID NodeID
// Q is the node's set of quorum slices.
// For compactness,
// this representation does not include the node itself,
// though the node is understood to be in every slice.
Q QSet
// FP/FQ is a rational giving the odds that a call to Handle will fail (drop the incoming message).
// Self messages (from the node to itself) are never dropped.
// FQ==0 is treated as 0/1.
FP, FQ int
// mu sync.Mutex
// pending holds Slot objects during nomination and balloting.
pending map[SlotID]*Slot
// ext holds externalized values for slots that have completed
// balloting.
ext map[SlotID]*ExtTopic
cmds *cmdChan
send chan<- *Msg
}
// NewNode produces a new node.
func NewNode(id NodeID, q QSet, ch chan<- *Msg, ext map[SlotID]*ExtTopic) *Node {
if ext == nil {
ext = make(map[SlotID]*ExtTopic)
}
return &Node{
ID: id,
Q: q,
pending: make(map[SlotID]*Slot),
ext: ext,
cmds: newCmdChan(),
send: ch,
}
}
// Run processes incoming events for the node. It returns only when
// its context is canceled and should be launched as a goroutine.
func (n *Node) Run(ctx context.Context) {
var delayUntil *time.Time
for {
cmd, ok := n.cmds.read(ctx)
if !ok {
if ctx.Err() != nil {
n.Logf("context canceled, Run exiting")
} else {
n.Logf("unknown command-reading error, Run exiting")
}
return
}
switch cmd := cmd.(type) {
case *msgCmd:
func() {
if delayUntil != nil {
dur := time.Until(*delayUntil)
if dur > 0 {
time.Sleep(dur)
}
delayUntil = nil
}
err := n.handle(cmd.msg)
if err != nil {
n.Logf("ERROR %s", err)
}
}()
case *delayCmd:
delayUntil = new(time.Time)
*delayUntil = time.Now().Add(time.Duration(cmd.ms * int(time.Millisecond)))
case *deferredUpdateCmd:
func() {
cmd.slot.deferredUpdate()
}()
case *newRoundCmd:
func() {
err := cmd.slot.newRound()
if err != nil {
n.Logf("ERROR %s", err)
}
}()
case *rehandleCmd:
func() {
for _, msg := range cmd.slot.M {
err := n.handle(msg)
if err != nil {
n.Logf("ERROR %s", err)
}
}
}()
}
}
}
func (n *Node) deferredUpdate(s *Slot) {
n.cmds.write(&deferredUpdateCmd{slot: s})
}
func (n *Node) newRound(s *Slot) {
n.cmds.write(&newRoundCmd{slot: s})
}
func (n *Node) rehandle(s *Slot) {
n.cmds.write(&rehandleCmd{slot: s})
}
// Handle queues an incoming protocol message. When processed it will
// send a protocol message in response on n.send unless the incoming
// message is ignored. (A message is ignored if it's invalid,
// redundant, or older than another message already received from the
// same sender.)
func (n *Node) Handle(msg *Msg) {
if msg.V != n.ID && n.FQ > 0 && n.FP < n.FQ {
// decide whether to simulate dropping this message
if rand.Intn(n.FQ) < n.FP {
n.Logf("dropping message %s", msg)
return
}
}
n.cmds.write(&msgCmd{msg: msg})
}
// Delay simulates a network delay.
func (n *Node) Delay(ms int) {
n.cmds.write(&delayCmd{ms: ms})
}
func (n *Node) handle(msg *Msg) error {
if topic, ok := n.ext[msg.I]; ok {
// This node has already externalized a value for the given slot.
// Send an EXTERNALIZE message outbound, unless the inbound
// message is also EXTERNALIZE.
if inTopic, ok := msg.T.(*ExtTopic); ok {
// Double check that the inbound EXTERNALIZE value agrees with
// this node.
if !ValueEqual(inTopic.C.X, topic.C.X) {
n.Logf("inbound message %s disagrees with externalized value %s!", msg, topic.C.X)
panic("consensus failure")
}
} else {
n.send <- NewMsg(n.ID, msg.I, n.Q, topic)
}
return nil
}
s, ok := n.pending[msg.I]
if !ok {
var err error
s, err = newSlot(msg.I, n)
if err != nil {
panic(fmt.Sprintf("cannot create slot %d: %s", msg.I, err))
}
n.pending[msg.I] = s
}
outbound, err := s.handle(msg)
if err != nil {
// delete(n.Pending, msg.I) // xxx ?
return err
}
if outbound == nil {
return nil
}
if extTopic, ok := outbound.T.(*ExtTopic); ok {
// Handling the inbound message resulted in externalizing a value.
// We can now save the EXTERNALIZE message and get rid of the Slot
// object.
n.ext[msg.I] = extTopic
delete(n.pending, msg.I)
}
n.send <- outbound
return nil
}
func (n *Node) ping() error {
for _, s := range n.pending {
for _, msg := range s.M {
err := n.handle(msg)
if err != nil {
return err
}
}
}
return nil
}
// ErrNoPrev occurs when trying to compute a hash (with Node.G) for
// slot i before the node has externalized a value for slot i-1.
var ErrNoPrev = errors.New("no previous value")
// G produces a node- and slot-specific 32-byte hash for a given
// message m. It is an error to call this on slot i>1 before n has
// externalized a value for slot i-1.
func (n *Node) G(i SlotID, m []byte) (result [32]byte, err error) { // xxx unexport
hasher := sha256.New()
var prevValBytes []byte
if i > 1 {
topic, ok := n.ext[i-1]
if !ok {
return result, ErrNoPrev
}
prevValBytes = topic.C.X.Bytes()
}
r, _ := xdr.Marshal(i)
hasher.Write(r)
hasher.Write(prevValBytes)
hasher.Write(m)
hasher.Sum(result[:0])
return result, nil
}
// Weight returns the fraction of n's quorum slices in which id
// appears. Return value is the fraction and (as an optimization) a
// bool indicating whether it's exactly 1.
func (n *Node) Weight(id NodeID) (float64, bool) {
if id == n.ID {
return 1.0, true
}
return n.Q.weight(id)
}
// Peers returns a flattened, uniquified list of the node IDs in n's
// quorum slices, not including n's own ID.
func (n *Node) Peers() NodeIDSet {
return n.Q.Nodes()
}
// Neighbors produces a deterministic subset of a node's peers (which
// may include itself) that is specific to a given slot and
// nomination-round.
func (n *Node) Neighbors(i SlotID, num int) (NodeIDSet, error) {
peers := n.Peers()
peers = peers.Add(n.ID)
var result NodeIDSet
for _, nodeID := range peers {
weight64, is1 := n.Weight(nodeID)
var hwBytes []byte
if is1 {
hwBytes = maxUint256[:]
} else {
w := big.NewFloat(weight64)
w.Mul(w, hmax)
hwInt, _ := w.Int(nil)
hwBytes = hwInt.Bytes()
}
var hw [32]byte
copy(hw[32-len(hwBytes):], hwBytes) // hw is now a big-endian uint256
m := new(bytes.Buffer)
m.WriteByte('N')
numBytes, _ := xdr.Marshal(num)
m.Write(numBytes)
m.WriteString(string(nodeID))
g, err := n.G(i, m.Bytes())
if err != nil {
return nil, err
}
if bytes.Compare(g[:], hw[:]) < 0 {
result = result.Add(nodeID)
}
}
return result, nil
}
// Priority computes a priority for a given peer node that is specific
// to a given slot and nomination-round. The result is a big-endian
// 256-bit integer expressed as a [32]byte.
func (n *Node) Priority(i SlotID, num int, nodeID NodeID) ([32]byte, error) {
m := new(bytes.Buffer)
m.WriteByte('P')
numBytes, _ := xdr.Marshal(num)
m.Write(numBytes)
m.WriteString(string(nodeID))
return n.G(i, m.Bytes())
}
// AllKnown gives the complete set of reachable node IDs,
// excluding n.ID.
func (n *Node) AllKnown() NodeIDSet {
result := n.Peers()
for _, s := range n.pending {
for _, msg := range s.M {
result = result.Union(msg.Q.Nodes())
}
}
result = result.Remove(n.ID)
return result
}
// HighestExt returns the ID of the highest slot for which this node
// has an externalized value.
func (n *Node) HighestExt() SlotID {
var result SlotID
for slotID := range n.ext {
if slotID > result {
result = slotID
}
}
return result
}
// MsgsSince returns all this node's messages with slotID > since.
// TODO: need a better interface, this list could get hella big.
func (n *Node) MsgsSince(since SlotID) []*Msg {
var result []*Msg
for slotID, topic := range n.ext {
if slotID <= since {
continue
}
msg := &Msg{
V: n.ID,
I: slotID,
Q: n.Q,
T: topic,
}
result = append(result, msg)
}
for slotID, slot := range n.pending {
if slotID <= since {
continue
}
result = append(result, slot.Msg())
}
return result
}
// Logf produces log output prefixed with the node's identity.
func (n *Node) Logf(f string, a ...interface{}) {
f = "node %s: " + f
a = append([]interface{}{n.ID}, a...)
log.Printf(f, a...)
}
var maxUint256 = [32]byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
}
// maxuint256, as a float
var hmax *big.Float
func init() {
hmaxInt := new(big.Int)
hmaxInt.SetBytes(maxUint256[:])
hmax = new(big.Float)
hmax.SetInt(hmaxInt)
}