forked from XRPLF/rippled
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Peer.h
915 lines (774 loc) · 26.7 KB
/
Peer.h
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
//------------------------------------------------------------------------------
/*
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.
*/
//==============================================================================
#ifndef RIPPLE_TEST_CSF_PEER_H_INCLUDED
#define RIPPLE_TEST_CSF_PEER_H_INCLUDED
#include <ripple/beast/utility/WrappedSink.h>
#include <ripple/consensus/Consensus.h>
#include <ripple/consensus/Validations.h>
#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include <algorithm>
#include <test/csf/CollectorRef.h>
#include <test/csf/Scheduler.h>
#include <test/csf/TrustGraph.h>
#include <test/csf/Tx.h>
#include <test/csf/Validation.h>
#include <test/csf/events.h>
#include <test/csf/ledgers.h>
namespace ripple {
namespace test {
namespace csf {
namespace bc = boost::container;
/** A single peer in the simulation.
This is the main work-horse of the consensus simulation framework and is
where many other components are integrated. The peer
- Implements the Callbacks required by Consensus
- Manages trust & network connections with other peers
- Issues events back to the simulation based on its actions for analysis
by Collectors
- Exposes most internal state for forcibly simulating arbitrary scenarios
*/
struct Peer
{
/** Basic wrapper of a proposed position taken by a peer.
For real consensus, this would add additional data for serialization
and signing. For simulation, nothing extra is needed.
*/
class Position
{
public:
Position(Proposal const& p) : proposal_(p)
{
}
Proposal const&
proposal() const
{
return proposal_;
}
Json::Value
getJson() const
{
return proposal_.getJson();
}
private:
Proposal proposal_;
};
/** Simulated delays in internal peer processing.
*/
struct ProcessingDelays
{
//! Delay in consensus calling doAccept to accepting and issuing
//! validation
//! TODO: This should be a function of the number of transactions
std::chrono::milliseconds ledgerAccept{0};
//! Delay in processing validations from remote peers
std::chrono::milliseconds recvValidation{0};
// Return the receive delay for message type M, default is no delay
// Received delay is the time from receiving the message to actually
// handling it.
template <class M>
SimDuration
onReceive(M const&) const
{
return SimDuration{};
}
SimDuration
onReceive(Validation const&) const
{
return recvValidation;
}
};
/** Generic Validations policy that simply ignores recently stale validations
*/
class StalePolicy
{
Peer& p_;
public:
StalePolicy(Peer& p) : p_{p}
{
}
NetClock::time_point
now() const
{
return p_.now();
}
void
onStale(Validation&& v)
{
}
void
flush(hash_map<PeerKey, Validation>&& remaining)
{
}
};
/** Non-locking mutex to avoid locks in generic Validations
*/
struct NotAMutex
{
void
lock()
{
}
void
unlock()
{
}
};
//! Type definitions for generic consensus
using Ledger_t = Ledger;
using NodeID_t = PeerID;
using TxSet_t = TxSet;
using PeerPosition_t = Position;
using Result = ConsensusResult<Peer>;
//! Logging support that prefixes messages with the peer ID
beast::WrappedSink sink;
beast::Journal j;
//! Generic consensus
Consensus<Peer> consensus;
//! Our unique ID
PeerID id;
//! Current signing key
PeerKey key;
//! The oracle that manages unique ledgers
LedgerOracle& oracle;
//! Scheduler of events
Scheduler& scheduler;
//! Handle to network for sending messages
BasicNetwork<Peer*>& net;
//! Handle to Trust graph of network
TrustGraph<Peer*>& trustGraph;
//! openTxs that haven't been closed in a ledger yet
TxSetType openTxs;
//! The last ledger closed by this node
Ledger lastClosedLedger;
//! Ledgers this node has closed or loaded from the network
hash_map<Ledger::ID, Ledger> ledgers;
//! Validations from trusted nodes
Validations<StalePolicy, Validation, NotAMutex> validations;
using AddOutcome =
Validations<StalePolicy, Validation, NotAMutex>::AddOutcome;
//! The most recent ledger that has been fully validated by the network from
//! the perspective of this Peer
Ledger fullyValidatedLedger;
//-------------------------------------------------------------------------
// 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
bc::flat_map<Ledger::ID, std::vector<Proposal>> peerPositions;
//! TxSet associated with a TxSet::ID
bc::flat_map<TxSet::ID, TxSet> txSets;
// Ledgers and txSets that we have already attempted to acquire
bc::flat_set<Ledger::ID> acquiringLedgers;
bc::flat_set<TxSet::ID> acquiringTxSets;
//! The number of ledgers this peer has completed
int completedLedgers = 0;
//! The number of ledgers this peer should complete before stopping to run
int targetLedgers = std::numeric_limits<int>::max();
//! Skew of time relative to the common scheduler clock
std::chrono::seconds clockSkew{0};
//! Simulated delays to use for internal processing
ProcessingDelays delays;
//! Whether to simulate running as validator or a tracking node
bool runAsValidator = true;
//TODO: Consider removing these two, they are only a convenience for tests
// Number of proposers in the prior round
std::size_t prevProposers = 0;
// Duration of prior round
std::chrono::milliseconds prevRoundTime;
// Quorum of validations needed for a ledger to be fully validated
// TODO: Use the logic in ValidatorList to set this dynamically
std::size_t quorum = 0;
// Simulation parameters
ConsensusParms consensusParms;
//! The collectors to report events to
CollectorRefs & collectors;
/** Constructor
@param i Unique PeerID
@param s Simulation Scheduler
@param o Simulation Oracle
@param n Simulation network
@param tg Simulation trust graph
@param c Simulation collectors
@param jIn Simulation journal
*/
Peer(
PeerID i,
Scheduler& s,
LedgerOracle& o,
BasicNetwork<Peer*>& n,
TrustGraph<Peer*> & tg,
CollectorRefs & c,
beast::Journal jIn)
: sink(jIn, "Peer " + to_string(i) + ": ")
, j(sink)
, consensus(s.clock(), *this, j)
, id{i}
, key{id, 0}
, oracle{o}
, scheduler{s}
, net{n}
, trustGraph(tg)
, validations{ValidationParms{}, s.clock(), j, *this}
, collectors{c}
{
// All peers start from the default constructed genesis ledger
ledgers[lastClosedLedger.id()] = lastClosedLedger;
// nodes always trust themselves . . SHOULD THEY?
trustGraph.trust(this, this);
}
/** Schedule the provided callback in `when` duration, but if
`when` is 0, call immediately
*/
template <class T>
void
schedule(std::chrono::nanoseconds when, T&& what)
{
using namespace std::chrono_literals;
if (when == 0ns)
what();
else
scheduler.in(when, std::forward<T>(what));
}
// Issue a new event to the collectors
template <class E>
void
issue(E const & event)
{
// Use the scheduler time and not the peer's (skewed) local time
collectors.on(id, scheduler.now(), event);
}
//--------------------------------------------------------------------------
// Trust and Network members
// Methods for modifying and querying the network and trust graphs from
// the perspective of this Peer
//< Extend trust to a peer
void
trust(Peer & o)
{
trustGraph.trust(this, &o);
}
//< Revoke trust from a peer
void
untrust(Peer & o)
{
trustGraph.untrust(this, &o);
}
//< Check whether we trust a peer
bool
trusts(Peer & o)
{
return trustGraph.trusts(this, &o);
}
//< Check whether we trust a peer based on its ID
bool
trusts(PeerID const & oId)
{
for(auto const & p : trustGraph.trustedPeers(this))
if(p->id == oId)
return true;
return false;
}
/** Create network connection
Creates a new outbound connection to another Peer if none exists
@param o The peer with the inbound connection
@param dur The fixed delay for messages between the two Peers
@return Whether the connection was created.
*/
bool
connect(Peer & o, SimDuration dur)
{
return net.connect(this, &o, dur);
}
/** Remove a network connection
Removes a connection between peers if one exists
@param o The peer we disconnect from
@return Whether the connection was removed
*/
bool
disconnect(Peer & o)
{
return net.disconnect(this, &o);
}
//--------------------------------------------------------------------------
// Generic Consensus members
// Attempt to acquire the Ledger associated with the given ID
Ledger const*
acquireLedger(Ledger::ID const& ledgerID)
{
auto it = ledgers.find(ledgerID);
if (it != ledgers.end())
return &(it->second);
// Don't retry if we already are acquiring it
if(!acquiringLedgers.emplace(ledgerID).second)
return nullptr;
for (auto const& link : net.links(this))
{
// Send a messsage to neighbors to find the ledger
net.send(
this, link.target, [ to = link.target, from = this, ledgerID ]() {
auto it = to->ledgers.find(ledgerID);
if (it != to->ledgers.end())
{
// if the ledger is found, send it back to the original
// requesting peer where it is added to the available
// ledgers
to->net.send(to, from, [ from, ledger = it->second ]() {
from->ledgers.emplace(ledger.id(), ledger);
});
}
});
}
return nullptr;
}
// Attempt to acquire the TxSet associated with the given ID
TxSet const*
acquireTxSet(TxSet::ID const& setId)
{
auto it = txSets.find(setId);
if (it != txSets.end())
return &(it->second);
// Don't retry if we already are acquiring it
if(!acquiringTxSets.emplace(setId).second)
return nullptr;
for (auto const& link : net.links(this))
{
// Send a message to neighbors to find the tx set
net.send(
this, link.target, [ to = link.target, from = this, setId ]() {
auto it = to->txSets.find(setId);
if (it != to->txSets.end())
{
// If the txSet is found, send it back to the original
// requesting peer, where it is handled like a TxSet
// that was broadcast over the network
to->net.send(to, from, [ from, txSet = it->second ]() {
from->handle(txSet);
});
}
});
}
return nullptr;
}
bool
hasOpenTransactions() const
{
return !openTxs.empty();
}
std::size_t
proposersValidated(Ledger::ID const& prevLedger)
{
return validations.numTrustedForLedger(prevLedger);
}
std::size_t
proposersFinished(Ledger::ID const& prevLedger)
{
return validations.getNodesAfter(prevLedger);
}
Result
onClose(
Ledger const& prevLedger,
NetClock::time_point closeTime,
ConsensusMode mode)
{
issue(CloseLedger{prevLedger, openTxs});
return Result(
TxSet{openTxs},
Proposal(
prevLedger.id(),
Proposal::seqJoin,
TxSet::calcID(openTxs),
closeTime,
now(),
id));
}
void
onForceAccept(
Result const& result,
Ledger const& prevLedger,
NetClock::duration const& closeResolution,
ConsensusCloseTimes const& rawCloseTimes,
ConsensusMode const& mode,
Json::Value&& consensusJson)
{
onAccept(
result,
prevLedger,
closeResolution,
rawCloseTimes,
mode,
std::move(consensusJson));
}
void
onAccept(
Result const& result,
Ledger const& prevLedger,
NetClock::duration const& closeResolution,
ConsensusCloseTimes const& rawCloseTimes,
ConsensusMode const& mode,
Json::Value&& consensusJson)
{
schedule(delays.ledgerAccept, [&]() {
TxSet const acceptedTxs = injectTxs(prevLedger, result.set);
Ledger const newLedger = oracle.accept(
prevLedger,
acceptedTxs.txs(),
closeResolution,
result.position.closeTime());
ledgers[newLedger.id()] = newLedger;
issue(AcceptLedger{newLedger, lastClosedLedger});
prevProposers = result.proposers;
prevRoundTime = result.roundTime.read();
lastClosedLedger = newLedger;
auto const it = std::remove_if(
openTxs.begin(), openTxs.end(), [&](Tx const& tx) {
return acceptedTxs.exists(tx.id());
});
openTxs.erase(it, openTxs.end());
// Only send validation if the new ledger is compatible with our
// fully validated ledger
bool const isCompatible =
oracle.isAncestor(fullyValidatedLedger, newLedger);
if (runAsValidator && isCompatible)
{
Validation v{newLedger.id(),
newLedger.seq(),
now(),
now(),
key,
id,
false};
// share is not trusted
share(v);
// we trust ourselves
addTrustedValidation(v);
}
checkFullyValidated(newLedger);
// kick off the next round...
// in the actual implementation, this passes back through
// network ops
++completedLedgers;
// startRound sets the LCL state, so we need to call it once after
// the last requested round completes
if (completedLedgers <= targetLedgers)
{
startRound();
}
});
}
// Earliest allowed sequence number when checking for ledgers with more
// validations than our current ledger
Ledger::Seq
earliestAllowedSeq() const
{
if (lastClosedLedger.seq() > Ledger::Seq{20})
return lastClosedLedger.seq() - Ledger::Seq{20};
return Ledger::Seq{0};
}
Ledger::ID
getPrevLedger(
Ledger::ID const& ledgerID,
Ledger const& ledger,
ConsensusMode mode)
{
// only do if we are past the genesis ledger
if (ledger.seq() == Ledger::Seq{0})
return ledgerID;
Ledger::ID parentID{0};
// Only set the parent ID if we believe ledger is the right ledger
if (mode != ConsensusMode::wrongLedger)
parentID = ledger.parentID();
// Get validators that are on our ledger, or "close" to being on
// our ledger.
auto const ledgerCounts = validations.currentTrustedDistribution(
ledgerID, parentID, earliestAllowedSeq());
Ledger::ID const netLgr = getPreferredLedger(ledgerID, ledgerCounts);
if (netLgr != ledgerID)
{
issue(WrongPrevLedger{ledgerID, netLgr});
}
return netLgr;
}
void
propose(Proposal const& pos)
{
share(pos);
}
ConsensusParms const&
parms() const
{
return consensusParms;
}
// Not interested in tracking consensus mode changes for now
void
onModeChange(ConsensusMode, ConsensusMode)
{
}
// Share a message by broadcasting to all connected peers
template <class M>
void
share(M const& m)
{
issue(Share<M>{m});
send(BroadcastMesg<M>{m,router.nextSeq++, this->id}, this->id);
}
// Unwrap the Position and share the raw proposal
void
share(Position const & p)
{
share(p.proposal());
}
//--------------------------------------------------------------------------
// Validation members
/** Add a trusted validation and return true if it is worth forwarding */
bool
addTrustedValidation(Validation v)
{
v.setTrusted();
v.setSeen(now());
AddOutcome const res = validations.add(v.key(), v);
if(res == AddOutcome::stale || res == AddOutcome::repeat)
return false;
// Acquire will try to get from network if not already local
if (Ledger const* lgr = acquireLedger(v.ledgerID()))
checkFullyValidated(*lgr);
return true;
}
/** Check if a new ledger can be deemed fully validated */
void
checkFullyValidated(Ledger const& ledger)
{
// Only consider ledgers newer than our last fully validated ledger
if (ledger.seq() <= fullyValidatedLedger.seq())
return;
std::size_t const count = validations.numTrustedForLedger(ledger.id());
std::size_t const numTrustedPeers = trustGraph.graph().outDegree(this);
quorum = static_cast<std::size_t>(std::ceil(numTrustedPeers * 0.8));
if (count >= quorum)
{
issue(FullyValidateLedger{ledger, fullyValidatedLedger});
fullyValidatedLedger = ledger;
}
}
//-------------------------------------------------------------------------
// Peer messaging members
// Basic Sequence number router
// A message that will be flooded across the network is tagged with a
// seqeuence number by the origin node in a BroadcastMesg. Receivers will
// ignore a message as stale if they've already processed a newer sequence
// number, or will process and potentially relay the message along.
//
// The various bool handle(MessageType) members do the actual processing
// and should return true if the message should continue to be sent to peers.
//
// WARN: This assumes messages are received and processed in the order they
// are sent, so that a peer receives a message with seq 1 from node 0
// before seq 2 from node 0, etc.
// TODO: Break this out into a class and identify type interface to allow
// alternate routing strategies
template <class M>
struct BroadcastMesg
{
M mesg;
std::size_t seq;
PeerID origin;
};
struct Router
{
std::size_t nextSeq = 1;
bc::flat_map<PeerID, std::size_t> lastObservedSeq;
};
Router router;
// Send a broadcast message to all peers
template <class M>
void
send(BroadcastMesg<M> const& bm, PeerID from)
{
for (auto const& link : net.links(this))
{
if (link.target->id != from && link.target->id != bm.origin)
{
// cheat and don't bother sending if we know it has already been
// used on the other end
if (link.target->router.lastObservedSeq[bm.origin] < bm.seq)
{
issue(Relay<M>{link.target->id, bm.mesg});
net.send(
this, link.target, [to = link.target, bm, id = this->id ] {
to->receive(bm, id);
});
}
}
}
}
// Receive a shared message, process it and consider continuing to relay it
template <class M>
void
receive(BroadcastMesg<M> const& bm, PeerID from)
{
issue(Receive<M>{from, bm.mesg});
if (router.lastObservedSeq[bm.origin] < bm.seq)
{
router.lastObservedSeq[bm.origin] = bm.seq;
schedule(delays.onReceive(bm.mesg), [this, bm, from]
{
if (handle(bm.mesg))
send(bm, from);
});
}
}
// Type specific receive handlers, return true if the message should
// continue to be broadcast to peers
bool
handle(Proposal const& p)
{
// Only relay untrusted proposals on the same ledger
if(!trusts(p.nodeID()))
return p.prevLedger() == lastClosedLedger.id();
// TODO: This always suppresses relay of peer positions already seen
// Should it allow forwarding if for a recent ledger ?
auto& dest = peerPositions[p.prevLedger()];
if (std::find(dest.begin(), dest.end(), p) != dest.end())
return false;
dest.push_back(p);
// Rely on consensus to decide whether to relay
return consensus.peerProposal(now(), Position{p});
}
bool
handle(TxSet const& txs)
{
auto const it = txSets.insert(std::make_pair(txs.id(), txs));
if (it.second)
consensus.gotTxSet(now(), txs);
// relay only if new
return it.second;
}
bool
handle(Tx const& tx)
{
// Ignore and suppress relay of transactions already in last ledger
TxSetType const& lastClosedTxs = lastClosedLedger.txs();
if (lastClosedTxs.find(tx) != lastClosedTxs.end())
return false;
// only relay if it was new to our open ledger
return openTxs.insert(tx).second;
}
bool
handle(Validation const& v)
{
// TODO: This is not relaying untrusted validations
if (!trusts(v.nodeID()))
return false;
// Will only relay if current
return addTrustedValidation(v);
}
//--------------------------------------------------------------------------
// A locally submitted transaction
void
submit(Tx const& tx)
{
issue(SubmitTx{tx});
if(handle(tx))
share(tx);
}
//--------------------------------------------------------------------------
// Simulation "driver" members
//! Heartbeat timer call
void
timerEntry()
{
consensus.timerEntry(now());
// only reschedule if not completed
if (completedLedgers < targetLedgers)
scheduler.in(parms().ledgerGRANULARITY, [this]() { timerEntry(); });
}
// Called to begin the next round
void
startRound()
{
auto const valDistribution = validations.currentTrustedDistribution(
lastClosedLedger.id(),
lastClosedLedger.parentID(),
earliestAllowedSeq());
// Between rounds, we take the majority ledger and use the
Ledger::ID const bestLCL =
getPreferredLedger(lastClosedLedger.id(), valDistribution);
issue(StartRound{bestLCL, lastClosedLedger});
// TODO:
// - Get dominant peer ledger if no validated available?
// - Check that we are switching to something compatible with our
// (network) validated history of ledgers?
consensus.startRound(
now(), bestLCL, lastClosedLedger, runAsValidator);
}
// Start the consensus process assuming it is not yet running
// This runs forever unless targetLedgers is specified
void
start()
{
// TODO: Expire validations less frequently?
validations.expire();
scheduler.in(parms().ledgerGRANULARITY, [&]() { timerEntry(); });
startRound();
}
NetClock::time_point
now() const
{
// We don't care about the actual epochs, but do want the
// generated NetClock time to be well past its epoch to ensure
// any subtractions of two NetClock::time_point in the consensu
// code are positive. (e.g. proposeFRESHNESS)
using namespace std::chrono;
using namespace std::chrono_literals;
return NetClock::time_point(duration_cast<NetClock::duration>(
scheduler.now().time_since_epoch() + 86400s + clockSkew));
}
Ledger::ID
prevLedgerID() const
{
return consensus.prevLedgerID();
}
//-------------------------------------------------------------------------
// Injects a specific transaction when generating the ledger following
// the provided sequence. This allows simulating a byzantine failure in
// which a node generates the wrong ledger, even when consensus worked
// properly.
// TODO: Make this more robust
hash_map<Ledger::Seq, Tx> txInjections;
/** Inject non-consensus Tx
Injects a transactionsinto the ledger following prevLedger's sequence
number.
@param prevLedger The ledger we are building the new ledger on top of
@param src The Consensus TxSet
@return Consensus TxSet with inject transactions added if prevLedger.seq
matches a previously registered Tx.
*/
TxSet
injectTxs(Ledger prevLedger, TxSet const & src)
{
auto const it = txInjections.find(prevLedger.seq());
if(it == txInjections.end())
return src;
TxSetType res{src.txs()};
res.insert(it->second);
return TxSet{res};
}
};
} // namespace csf
} // namespace test
} // namespace ripple
#endif