-
Notifications
You must be signed in to change notification settings - Fork 1
/
peer.go
546 lines (469 loc) · 16 KB
/
peer.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright (c) 2018 Aidos Developer
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// This is partially from https://github.com/ripple/rippled/src/test/csf
// covered by:
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012-2017 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
package consensus
import (
"context"
"encoding/hex"
"log"
"math"
"sync"
"time"
)
//PeerInterface is the funcs for intererctint Peer struct.
//don't call funcs in peer struct in these callbacks or will cause deadlock.
type PeerInterface interface {
// Attempt to acquire a specific ledger.
AcquireLedger(LedgerID) (*Ledger, error)
// Handle a newly stale validation, this should do minimal work since
// it is called by Validations while it may be iterating Validations
// under lock
OnStale(*Validation)
// Flush the remaining validations (typically done on shutdown)
Flush(remaining map[NodeID]*Validation)
// Acquire the transaction set associated with a proposed position.
AcquireTxSet(TxSetID) ([]TxT, error)
// Whether any transactions are in the open ledger
HasOpenTransactions() bool
// Called whenever consensus operating mode changes
OnModeChange(Mode, Mode)
// Called when ledger closes
OnClose(*Ledger, time.Time, Mode) TxSet
// Called when ledger is accepted by consensus with fully validaded ledger
// (not accepted ledger)
OnAccept(newledger, fullyValidatedLedger *Ledger)
// Propose the position to Peers.
Propose(*Proposal)
// Share a received Peer proposal with other Peer's.
SharePosition(*Proposal)
// Share a disputed transaction with Peers
ShareTx(TxT)
// Share given transaction set with Peers
ShareTxset(TxSet)
// Share my validation
ShareValidaton(*Validation)
ShouldAccept(*Result) bool
//UpdateOurProposal updates our proposal from otherss proposals
UpdateOurProposal(props map[NodeID]*Proposal, ourSet, newSet TxSet) TxSet
}
type realclock struct{}
func (c realclock) Now() time.Time {
return time.Now()
}
type valAdaptor struct {
p *Peer
}
func (a *valAdaptor) AcquireLedger(id LedgerID) (*Ledger, error) {
//ValidationAdaptor
return a.p.AcquireLedger(id)
}
// Handle a newly stale validation, this should do minimal work since
// it is called by Validations while it may be iterating Validations
// under lock
func (a *valAdaptor) OnStale(v *Validation) {
//ValidationAdaptor
a.p.adaptor.OnStale(v)
}
// Flush the remaining validations (typically done on shutdown)
func (a *valAdaptor) Flush(remaining map[NodeID]*Validation) {
//ValidationAdaptor
a.p.adaptor.Flush(remaining)
}
// Return the current network time (used to determine staleness)
func (a *valAdaptor) Now() time.Time {
//ValidationAdaptor
return time.Now()
}
//Peer is the API interface for consensus.
type Peer struct {
sync.RWMutex
//! Generic consensus
consensus *Consensus
//! Our unique ID
id NodeID
unl []NodeID
//! The last ledger closed by this node
lastClosedLedger *Ledger
//! Validations from trusted nodes
validations *Validations
//! The most recent ledger that has been fully validated by the network from
//! the perspective of this Peer
fullyValidatedLedger *Ledger
//-------------------------------------------------------------------------
// Store most network messages; these could be purged if memory use ever
// becomes problematic
//! Map from Ledger::ID to vector of Positions with that ledger
//! as the prior ledger
PeerPositions map[LedgerID][]*Proposal
//! Enforce invariants on validation sequence numbers
seqEnforcer SeqEnforcer
adaptor PeerInterface
//! Whether to simulate running as validator or a tracking node
runAsValidator bool
lastValidation *Validation
lastProposal *Proposal
}
//NewPeer returns a peer object.
func NewPeer(adaptor PeerInterface, i NodeID, unl []NodeID, runAsValidator bool, lastLedger *Ledger) *Peer {
p := &Peer{
adaptor: adaptor,
id: i,
unl: unl,
lastClosedLedger: lastLedger,
fullyValidatedLedger: Genesis,
PeerPositions: make(map[LedgerID][]*Proposal),
runAsValidator: runAsValidator,
}
c := &realclock{}
p.consensus = NewConsensus(c, p)
p.validations = NewValidations(&valAdaptor{
p: p,
}, c)
return p
}
//AcquireLedger gets the ledger whose ID is ledgerID.
func (p *Peer) AcquireLedger(ledgerID LedgerID) (*Ledger, error) {
//Adaptor
return p.adaptor.AcquireLedger(ledgerID)
}
//ShouldAccept returns true if the result should be accepted.
func (p *Peer) ShouldAccept(result *Result) bool {
//Adaptor
return p.adaptor.ShouldAccept(result)
}
// AcquireTxSet Attempt to acquire the TxSet associated with the given ID
func (p *Peer) AcquireTxSet(setID TxSetID) (TxSet, error) {
//Adaptor
ts, err := p.adaptor.AcquireTxSet(setID)
if err != nil {
return nil, err
}
txset := make(TxSet)
for _, t := range ts {
txset[t.ID()] = t
}
return txset, nil
}
//HasOpenTransactions returns true if having txs that should be approved.
func (p *Peer) HasOpenTransactions() bool {
//Adaptor
return p.adaptor.HasOpenTransactions()
}
// ProposersValidated is the number of proposers that have validated the given ledger
func (p *Peer) ProposersValidated(prevLedger LedgerID) uint {
//Adaptor
//This funcs is used for determining if the node closes right now.
//But it causes consensus too fast, so for now we prevent radically faster consensus.
return 0
// return p.validations.NumTrustedForLedger(prevLedger)
}
// ProposersFinished is the number of proposers that have validated a ledger descended from the
// given ledger; if prevLedger.id() != prevLedgerID, use prevLedgerID
// for the determination
func (p *Peer) ProposersFinished(prevLedger *Ledger, prevLedgerID LedgerID) uint {
//Adaptor
return uint(p.validations.GetNodesAfter(prevLedger, prevLedgerID))
}
// GetPrevLedger returns the ID of the last closed (and validated) ledger that the
// application thinks consensus should use as the prior ledger.
func (p *Peer) GetPrevLedger(ledgerID LedgerID, ledger *Ledger, mode Mode) LedgerID {
//Adaptor
// only do if we are past the genesis ledger
if ledger.Seq == 0 {
return ledgerID
}
netLgr := p.validations.GetPreferred2(ledger, p.earliestAllowedSeq())
if netLgr != ledgerID {
log.Println("Now we are having wrong latest ledger", hex.EncodeToString(ledgerID[:4]))
log.Println("Most prefferd is", hex.EncodeToString(netLgr[:4]))
}
return netLgr
}
// Propose the position to peers.
func (p *Peer) Propose(pos *Proposal) {
//Adaptor
if pos.NodeID == p.id {
p.lastProposal = pos
}
p.adaptor.Propose(pos)
}
// OnModeChange is Called whenever consensus operating mode changes
func (p *Peer) OnModeChange(from, to Mode) {
//Adaptor
p.adaptor.OnModeChange(from, to)
}
// SharePosition share a received peer proposal with other peer's.
func (p *Peer) SharePosition(pos *Proposal) {
//Adaptor
p.adaptor.SharePosition(pos)
}
// ShareTx shares a received peer proposal with other peer's.
func (p *Peer) ShareTx(tx TxT) {
//Adaptor
p.adaptor.ShareTx(tx)
}
// ShareTxset shares given transaction set with peers
func (p *Peer) ShareTxset(ts TxSet) {
//Adaptor
p.adaptor.ShareTxset(ts)
}
// PrevLedgerID gets the previous ledger ID.
// The previous ledger is the last ledger seen by the consensus code and should correspond to the most recent validated ledger seen by this peer.
// @return ID of previous ledger
func (p *Peer) PrevLedgerID() LedgerID {
p.RLock()
defer p.RUnlock()
return p.consensus.PrevLedgerID()
}
//AddProposal adds a proposal
func (p *Peer) AddProposal(prop *Proposal) {
p.Lock()
defer p.Unlock()
p.addProposal(prop)
}
func (p *Peer) addProposal(prop *Proposal) {
if prop.NodeID == p.id {
return
}
ok := false
for _, u := range p.unl {
if u == prop.NodeID {
ok = true
}
}
if !ok {
return
}
// TODO: This always suppresses relay of peer positions already seen
// Should it allow forwarding if for a recent ledger ?
dest := p.PeerPositions[prop.PreviousLedger]
for _, d := range dest {
if d.ID() == prop.ID() {
return
}
}
p.PeerPositions[prop.PreviousLedger] = append(p.PeerPositions[prop.PreviousLedger], prop)
p.consensus.PeerProposal(time.Now(), prop)
}
func (p *Peer) earliestAllowedSeq() Seq {
return p.fullyValidatedLedger.Seq
}
var resendValidationWaitTime = time.Minute
//Start starts the consensus.
func (p *Peer) Start(ctx context.Context) {
p.startRound()
p.validations.Expire()
go func() {
ctx2, cancel2 := context.WithCancel(ctx)
defer cancel2()
for {
select {
case <-ctx2.Done():
return
case <-time.After(LedgerGranularity):
p.Lock()
p.consensus.TimerEntry(time.Now())
p.Unlock()
}
}
}()
go func() {
ctx2, cancel2 := context.WithCancel(ctx)
defer cancel2()
for {
select {
case <-ctx2.Done():
return
case <-time.After(resendValidationWaitTime):
p.Lock()
if p.lastValidation != nil {
p.lastValidation.SeenTime = time.Now()
p.lastValidation.SignTime = time.Now()
p.adaptor.ShareValidaton(p.lastValidation)
}
p.Unlock()
}
}
}()
}
//AddValidation adds a trusted validation and return true if it is worth forwarding
func (p *Peer) AddValidation(v *Validation) bool {
p.Lock()
defer p.Unlock()
return p.addValidation(v)
}
func (p *Peer) addValidation(v *Validation) bool {
v.Trusted = true
v.SeenTime = time.Now()
res := p.validations.Add(v.NodeID, v)
log.Println("validatoin status:", res)
if res == VstatStale {
return false
}
// Acquire will try to get from network if not already local
if lgr, err := p.AcquireLedger(v.LedgerID); err == nil {
p.checkFullyValidated(lgr)
}
return true
}
func (p *Peer) startRound() {
// Between rounds, we take the majority ledger
// In the future, consider taking Peer dominant ledger if no validations
// yet
bestLCL :=
p.validations.GetPreferred2(p.lastClosedLedger, p.earliestAllowedSeq())
if bestLCL == Genesis.ID() {
bestLCL = p.lastClosedLedger.ID()
}
pid := p.lastClosedLedger.ID()
log.Println("Starting a round")
log.Println("\tbest ledger", bestLCL)
log.Println("\tprevLedger", pid)
log.Println("\tfully validated", p.fullyValidatedLedger.ID())
// Not yet modeling dynamic UNL.
nowUntrusted := make(map[NodeID]struct{})
p.consensus.StartRound(
time.Now(), bestLCL, p.lastClosedLedger, nowUntrusted, p.runAsValidator)
}
/** Check if a new ledger can be deemed fully validated */
func (p *Peer) checkFullyValidated(ledger *Ledger) {
// Only consider ledgers newer than our last fully validated ledger
if ledger.Seq <= p.fullyValidatedLedger.Seq {
return
}
count := p.validations.NumTrustedForLedger(ledger.ID())
quorum := int(math.Ceil(float64(len(p.unl)+1) * 0.8))
if count >= uint(quorum) && ledger.IsDescendantOf(p.fullyValidatedLedger) {
log.Println("fully validated", ledger.ID())
p.fullyValidatedLedger = ledger
}
}
// OnClose is Called when ledger closes
func (p *Peer) OnClose(prevLedger *Ledger, closeTime time.Time, mode Mode) *Result {
txns := p.adaptor.OnClose(prevLedger, closeTime, mode)
id := txns.ID()
pid := prevLedger.ID()
log.Println("closing prevledger", hex.EncodeToString(pid[:4]), "txnsid", hex.EncodeToString(id[:4]), "time", closeTime)
return NewResult(txns,
&Proposal{
PreviousLedger: prevLedger.ID(),
Position: txns.ID(),
CloseTime: closeTime,
Time: time.Now(),
NodeID: p.id,
ProposeSeq: 0,
})
}
//IndexOfFunc returns the IndexOf func for the ledger l.
func IndexOfFunc(l *Ledger, acquireLedger func(LedgerID) (*Ledger, error)) func(s Seq) LedgerID {
return func(s Seq) LedgerID {
if s > l.Seq {
panic("not found")
}
if s == 0 {
return Genesis.ID()
}
var ll *Ledger
for ll = l; ll.Seq != s && ll.Seq > 0; {
var err error
ll, err = acquireLedger(ll.ParentID)
if err != nil {
panic(err)
}
}
return ll.ID()
}
}
// OnAccept is called when ledger is accepted by consensus
func (p *Peer) OnAccept(result *Result, prevLedger *Ledger,
closeResolution time.Duration, rawCloseTime *CloseTimes, mode Mode) {
newLedger := &Ledger{
ParentID: prevLedger.ID(),
Seq: prevLedger.Seq + 1,
Txs: result.Txns.Clone(),
CloseTimeResolution: closeResolution,
ParentCloseTime: prevLedger.CloseTime,
CloseTimeAgree: !result.Position.CloseTime.IsZero(),
}
newLedger.IndexOf = IndexOfFunc(newLedger, p.adaptor.AcquireLedger)
if newLedger.CloseTimeAgree {
newLedger.CloseTime = EffCloseTime(result.Position.CloseTime, closeResolution, prevLedger.CloseTime)
} else {
newLedger.CloseTime = prevLedger.CloseTime.Add(time.Second)
}
p.checkFullyValidated(newLedger)
p.adaptor.OnAccept(newLedger, p.fullyValidatedLedger)
nid := newLedger.ID()
pid := prevLedger.ID()
log.Println("onaccept txset", hex.EncodeToString(result.Position.Position[:4]), "prev ledger", hex.EncodeToString(pid[:4]),
"closetime", rawCloseTime.Self, "new ledger", hex.EncodeToString(nid[:4]), "seq", newLedger.Seq)
log.Println("proposers", result.Proposers, "round time", result.RoundTime.Dur)
p.PeerPositions = make(map[LedgerID][]*Proposal)
p.lastClosedLedger = newLedger
// Only send validation if the new ledger is compatible with our
// fully validated ledger
isCompatible := newLedger.IsDescendantOf(p.fullyValidatedLedger)
// Can only send one validated ledger per seq
consensusFail := result.State == StateMovedOn
if p.runAsValidator && isCompatible && !consensusFail {
enf := p.seqEnforcer.Try(time.Now(), newLedger.Seq)
if enf {
isFull := mode == ModeProposing
v := Validation{
LedgerID: newLedger.ID(),
Seq: newLedger.Seq,
SignTime: time.Now(),
SeenTime: time.Now(),
NodeID: p.id,
Full: isFull,
}
// share the new validation; it is trusted by the receiver
p.adaptor.ShareValidaton(&v)
// we trust ourselves
p.addValidation(&v)
p.lastValidation = &v
p.lastProposal = nil
}
}
// kick off the next round...
// in the actual implementation, this passes back through
// network ops
// startRound sets the LCL state, so we need to call it once after
// the last requested round completes
p.startRound()
}
//UpdateOurProposal updates our proposal from otherss proposals
func (p *Peer) UpdateOurProposal(positions map[NodeID]*Proposal, ourSet, newSet TxSet) TxSet {
return p.adaptor.UpdateOurProposal(positions, ourSet, newSet)
}