19 #ifndef RIPPLE_TEST_CSF_PEER_H_INCLUDED
20 #define RIPPLE_TEST_CSF_PEER_H_INCLUDED
22 #include <ripple/beast/utility/WrappedSink.h>
23 #include <ripple/consensus/Consensus.h>
24 #include <ripple/consensus/Validations.h>
25 #include <ripple/protocol/PublicKey.h>
26 #include <boost/container/flat_map.hpp>
27 #include <boost/container/flat_set.hpp>
29 #include <test/csf/CollectorRef.h>
30 #include <test/csf/Scheduler.h>
31 #include <test/csf/TrustGraph.h>
32 #include <test/csf/Tx.h>
33 #include <test/csf/Validation.h>
34 #include <test/csf/events.h>
35 #include <test/csf/ledgers.h>
41 namespace bc = boost::container;
300 using namespace std::chrono_literals;
347 for (
auto const p :
trustGraph.trustedPeers(
this))
365 return net.connect(
this, &o, dur);
378 return net.disconnect(
this, &o);
390 return &(it->second);
394 if (
net.links(
this).empty())
405 using namespace std::chrono_literals;
407 for (
auto const link :
net.links(
this))
409 minDuration =
std::min(minDuration, link.data.delay);
413 this, link.target, [to = link.target, from =
this, ledgerID]() {
414 if (auto it = to->ledgers.find(ledgerID);
415 it != to->ledgers.end())
420 to->net.send(to, from, [from, ledger = it->second]() {
421 from->acquiringLedgers.erase(ledger.id());
422 from->ledgers.emplace(ledger.id(), ledger);
437 return &(it->second);
441 if (
net.links(
this).empty())
452 using namespace std::chrono_literals;
454 for (
auto const link :
net.links(
this))
456 minDuration =
std::min(minDuration, link.data.delay);
459 this, link.target, [to = link.target, from =
this, setId]() {
460 if (auto it = to->txSets.find(setId);
461 it != to->txSets.end())
466 to->net.send(to, from, [from, txSet = it->second]() {
467 from->acquiringTxSets.erase(txSet.id());
486 return validations.numTrustedForLedger(prevLedger);
529 std::move(consensusJson));
542 const bool proposing = mode == ConsensusMode::proposing;
543 const bool consensusFail = result.state == ConsensusState::MovedOn;
545 TxSet const acceptedTxs = injectTxs(prevLedger, result.txns);
546 Ledger const newLedger = oracle.accept(
550 result.position.closeTime());
551 ledgers[newLedger.id()] = newLedger;
553 issue(AcceptLedger{newLedger, lastClosedLedger});
560 return acceptedTxs.exists(tx.id());
566 bool const isCompatible =
625 if (netLgr != ledgerID)
744 for (
auto const link :
net.links(
this))
746 if (link.target->id != from && link.target->id != bm.
origin)
750 if (link.target->router.lastObservedSeq[bm.
origin] < bm.
seq)
756 [to = link.target, bm,
id = this->id] {
792 if (
std::find(dest.begin(), dest.end(), p) != dest.end())
804 bool const inserted =
817 if (lastClosedTxs.find(tx) != lastClosedTxs.end())
821 return openTxs.insert(tx).second;
851 for (
auto const p :
trustGraph.trustedPeers(
this))
935 using namespace std::chrono_literals;
972 res.insert(it->second);
bc::flat_map< Ledger::ID, SimTime > acquiringLedgers
bool haveValidated() const
std::size_t prevProposers
SimDuration onReceive(M const &) const
bool handle(TxSet const &txs)
void propose(Proposal const &pos)
Json::Value getJson() const
std::chrono::seconds clockSkew
Skew of time relative to the common scheduler clock.
@ proposing
We are normal participant in consensus and propose our position.
static ID calcID(TxSetType const &txs)
boost::container::flat_set< Tx > TxSetType
Stream trace() const
Severity stream access functions.
hash_set< NodeKey_t > trustedKeys
Ledger::Seq getValidLedgerIndex() const
bc::flat_map< PeerID, std::size_t > lastObservedSeq
tagged_integer< std::uint32_t, PeerIDTag > PeerID
hash_map< Ledger::Seq, Tx > txInjections
Generic Validations adaptor that simply ignores recently stale validations.
void share(Position const &p)
TxSet is a set of transactions to consider including in the ledger.
Decorator for streaming out compact json.
bool connect(Peer &o, SimDuration dur)
Create network connection.
Json::Value getJson() const
Get JSON representation for debugging.
PeerKey key
Current signing key.
void onAccept(Result const &result, Ledger const &prevLedger, NetClock::duration const &closeResolution, ConsensusCloseTimes const &rawCloseTimes, ConsensusMode const &mode, Json::Value &&consensusJson)
bool trusts(PeerID const &oId)
void updateOperatingMode(std::size_t const positions) const
Generic implementation of consensus algorithm.
Simulated discrete-event scheduler.
PeerID const & nodeID() const
Peer fully validated a new ledger.
Ledger::ID getPrevLedger(Ledger::ID const &ledgerID, Ledger const &ledger, ConsensusMode mode)
TxSetType const & txs() const
ValStatus
Status of validation we received.
ConsensusResult< Peer > Result
beast::WrappedSink sink
Logging support that prefixes messages with the peer ID.
bc::flat_map< TxSet::ID, TxSet > txSets
TxSet associated with a TxSet::ID.
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Ledger lastClosedLedger
The last ledger closed by this node.
Simulated delays in internal peer processing.
Validations< ValAdaptor > validations
Validations from trusted nodes.
Encapsulates the result of consensus.
bool handle(Tx const &tx)
bool handle(Proposal const &p)
TxSetType const & txs() const
TxSet const * acquireTxSet(TxSet::ID const &setId)
std::chrono::milliseconds recvValidation
Delay in processing validations from remote peers.
bool disconnect(Peer &o)
Remove a network connection.
Ledger::Seq earliestAllowedSeq() const
LedgerID_t const & prevLedger() const
Get the prior accepted ledger this position is based on.
beast::uhash<>::result_type ID
CollectorRefs & collectors
The collectors to report events to.
int completedLedgers
The number of ledgers this peer has completed.
LedgerOracle & oracle
The oracle that manages unique ledgers.
cancel_token in(duration const &delay, Function &&f)
Schedule an event after a specified duration passes.
Scheduler & scheduler
Scheduler of events.
bool handle(Validation const &v)
std::chrono::milliseconds read() const
Ledger::ID prevLedgerID() const
Ledger fullyValidatedLedger
The most recent ledger that has been fully validated by the network from the perspective of this Peer...
Position(Proposal const &p)
Ledger const * acquireLedger(Ledger::ID const &ledgerID)
void onForceAccept(Result const &result, Ledger const &prevLedger, NetClock::duration const &closeResolution, ConsensusCloseTimes const &rawCloseTimes, ConsensusMode const &mode, Json::Value &&consensusJson)
std::size_t laggards(Ledger::Seq const seq, hash_set< NodeKey_t > &trusted)
Oracle maintaining unique ledgers for a simulation.
NetClock::time_point now() const
bool runAsValidator
Whether to simulate running as validator or a tracking node.
Peer closed the open ledger.
std::pair< PeerID, std::uint32_t > PeerKey
The current key of a peer.
std::pair< std::size_t, hash_set< NodeKey_t > > getQuorumKeys()
@ stale
Not current or was older than current from this node.
void schedule(std::chrono::nanoseconds when, T &&what)
Schedule the provided callback in when duration, but if when is 0, call immediately.
A generic endpoint for log messages.
ConsensusParms const & parms() const
void checkFullyValidated(Ledger const &ledger)
Check if a new ledger can be deemed fully validated.
ConsensusProposal< PeerID, Ledger::ID, TxSet::ID > Proposal
Proposal is a position taken in the consensus process and is represented directly from the generic ty...
A value relayed to another peer as part of flooding.
std::size_t proposersValidated(Ledger::ID const &prevLedger)
void send(BroadcastMesg< M > const &bm, PeerID from)
bc::flat_map< TxSet::ID, SimTime > acquiringTxSets
void submit(Tx const &tx)
TxSet injectTxs(Ledger prevLedger, TxSet const &src)
Inject non-consensus Tx.
A value received from another peer as part of flooding.
A single peer in the simulation.
Set the sequence number on a JTx.
NodeID_t const & nodeID() const
Identifying which peer took this position.
bool addTrustedValidation(Validation v)
Add a trusted validation and return true if it is worth forwarding.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
void onModeChange(ConsensusMode, ConsensusMode)
std::chrono::milliseconds ledgerAccept
Delay in consensus calling doAccept to accepting and issuing validation TODO: This should be a functi...
ConsensusMode
Represents how a node currently participates in Consensus.
Peer detected a wrong prior ledger during consensus.
Consensus algorithm parameters.
TxSetType openTxs
openTxs that haven't been closed in a ledger yet
std::chrono::milliseconds prevRoundTime
Validation of a specific ledger by a specific Peer.
void setSeen(NetClock::time_point seen)
bc::flat_map< Ledger::ID, std::vector< Proposal > > peerPositions
Map from Ledger::ID to vector of Positions with that ledger as the prior ledger.
void timerEntry()
Heartbeat timer call.
Proposal const & proposal() const
bool isAncestor(Ledger const &ancestor) const
Determine whether ancestor is really an ancestor of this ledger.
static const std::uint32_t seqJoin
tagged_integer< std::uint32_t, SeqTag > Seq
SimDuration onReceive(Validation const &) const
std::optional< Ledger > acquire(Ledger::ID const &lId)
NetClock::time_point now() const
Wraps a Journal::Sink to prefix its output with a string.
BasicNetwork< Peer * > & net
Handle to network for sending messages.
Ledger::ID ledgerID() const
A container of CollectorRefs.
ProcessingDelays delays
Simulated delays to use for internal processing.
Maintains current and recent ledger validations.
TrustGraph< Peer * > & trustGraph
Handle to Trust graph of network.
A value to be flooded to all other peers starting from this peer.
Peer starts a new consensus round.
void receive(BroadcastMesg< M > const &bm, PeerID from)
int targetLedgers
The number of ledgers this peer should complete before stopping to run.
typename SimClock::duration SimDuration
std::size_t proposersFinished(Ledger const &prevLedger, Ledger::ID const &prevLedgerID)
void on(PeerID node, SimTime when, E const &e)
time_point now() const
Return the current network time.
bool hasOpenTransactions() const
hash_map< Ledger::ID, Ledger > ledgers
Ledgers this node has closed or loaded from the network.
A transaction submitted to a peer.
std::string to_string(TxSetType const &txs)
std::chrono::time_point< NetClock > time_point
Peer to peer network simulator.
Result onClose(Ledger const &prevLedger, NetClock::time_point closeTime, ConsensusMode mode)
Stores the set of initial close times.
Consensus< Peer > consensus
Generic consensus.
Peer(PeerID i, Scheduler &s, LedgerOracle &o, BasicNetwork< Peer * > &n, TrustGraph< Peer * > &tg, CollectorRefs &c, beast::Journal jIn)
Constructor.
void issue(E const &event)
Basic wrapper of a proposed position taken by a peer.
ConsensusParms consensusParms