rippled
Loading...
Searching...
No Matches
ledgers.cpp
1#include <test/csf/ledgers.h>
2
3#include <algorithm>
4
5namespace ripple {
6namespace test {
7namespace csf {
8
9Ledger::Instance const Ledger::genesis;
10
13{
15 res["id"] = static_cast<ID::value_type>(id());
16 res["seq"] = static_cast<Seq::value_type>(seq());
17 return res;
18}
19
20bool
21Ledger::isAncestor(Ledger const& ancestor) const
22{
23 if (ancestor.seq() < seq())
24 return operator[](ancestor.seq()) == ancestor.id();
25 return false;
26}
27
30{
31 if (s > seq())
32 return {};
33 if (s == seq())
34 return id();
35 return instance_->ancestors[static_cast<Seq::value_type>(s)];
36}
37
39mismatch(Ledger const& a, Ledger const& b)
40{
41 using Seq = Ledger::Seq;
42
43 // end is 1 past end of range
44 Seq start{0};
45 Seq end = std::min(a.seq() + Seq{1}, b.seq() + Seq{1});
46
47 // Find mismatch in [start,end)
48 // Binary search
49 Seq count = end - start;
50 while (count > Seq{0})
51 {
52 Seq step = count / Seq{2};
53 Seq curr = start + step;
54 if (a[curr] == b[curr])
55 {
56 // go to second half
57 start = ++curr;
58 count -= step + Seq{1};
59 }
60 else
61 count = step;
62 }
63 return start;
64}
65
70
73{
74 return Ledger::ID{static_cast<Ledger::ID::value_type>(instances_.size())};
75}
76
79 Ledger const& parent,
80 TxSetType const& txs,
81 NetClock::duration closeTimeResolution,
82 NetClock::time_point const& consensusCloseTime)
83{
84 using namespace std::chrono_literals;
85 Ledger::Instance next(*parent.instance_);
86 next.txs.insert(txs.begin(), txs.end());
87 next.seq = parent.seq() + Ledger::Seq{1};
88 next.closeTimeResolution = closeTimeResolution;
89 next.closeTimeAgree = consensusCloseTime != NetClock::time_point{};
90 if (next.closeTimeAgree)
91 next.closeTime = effCloseTime(
92 consensusCloseTime, closeTimeResolution, parent.closeTime());
93 else
94 next.closeTime = parent.closeTime() + 1s;
95
96 next.parentCloseTime = parent.closeTime();
97 next.parentID = parent.id();
98 next.ancestors.push_back(parent.id());
99
100 auto it = instances_.left.find(next);
101 if (it == instances_.left.end())
102 {
103 using Entry = InstanceMap::left_value_type;
104 it = instances_.left.insert(Entry{next, nextID()}).first;
105 }
106 return Ledger(it->second, &(it->first));
107}
108
111{
112 auto const it = instances_.right.find(id);
113 if (it != instances_.right.end())
114 {
115 return Ledger(it->first, &(it->second));
116 }
117 return std::nullopt;
118}
119
122{
123 // Tips always maintains the Ledgers with largest sequence number
124 // along all known chains.
126 tips.reserve(ledgers.size());
127
128 for (Ledger const& ledger : ledgers)
129 {
130 // Three options,
131 // 1. ledger is on a new branch
132 // 2. ledger is on a branch that we have seen tip for
133 // 3. ledger is the new tip for a branch
134 bool found = false;
135 for (auto idx = 0; idx < tips.size() && !found; ++idx)
136 {
137 bool const idxEarlier = tips[idx].seq() < ledger.seq();
138 Ledger const& earlier = idxEarlier ? tips[idx] : ledger;
139 Ledger const& later = idxEarlier ? ledger : tips[idx];
140 if (later.isAncestor(earlier))
141 {
142 tips[idx] = later;
143 found = true;
144 }
145 }
146
147 if (!found)
148 tips.push_back(ledger);
149 }
150 // The size of tips is the number of branches
151 return tips.size();
152}
153} // namespace csf
154} // namespace test
155} // namespace ripple
Represents a JSON value.
Definition json_value.h:130
InstanceMap::value_type InstanceEntry
Definition ledgers.h:235
Ledger::ID nextID() const
Definition ledgers.cpp:72
Ledger accept(Ledger const &curr, TxSetType const &txs, NetClock::duration closeTimeResolution, NetClock::time_point const &consensusCloseTime)
Accept the given txs and generate a new ledger.
Definition ledgers.cpp:78
std::optional< Ledger > lookup(Ledger::ID const &id) const
Find the ledger with the given ID.
Definition ledgers.cpp:110
std::size_t branches(std::set< Ledger > const &ledgers) const
Determine the number of distinct branches for the set of ledgers.
Definition ledgers.cpp:121
A ledger is a set of observed transactions and a sequence number identifying the ledger.
Definition ledgers.h:45
static Instance const genesis
Definition ledgers.h:135
NetClock::time_point closeTime() const
Definition ledgers.h:177
Instance const * instance_
Definition ledgers.h:225
Json::Value getJson() const
Definition ledgers.cpp:12
bool isAncestor(Ledger const &ancestor) const
Determine whether ancestor is really an ancestor of this ledger.
Definition ledgers.cpp:21
ID operator[](Seq seq) const
Return the id of the ancestor with the given seq (if exists/known)
Definition ledgers.cpp:29
tagged_integer< std::uint32_t, SeqTag > Seq
Definition ledgers.h:50
T is_same_v
T min(T... args)
@ objectValue
object value (collection of name/value pairs).
Definition json_value.h:26
boost::container::flat_set< Tx > TxSetType
Definition Tx.h:60
Ledger::Seq mismatch(Ledger const &a, Ledger const &b)
Definition ledgers.cpp:39
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::chrono::time_point< Clock, Duration > effCloseTime(std::chrono::time_point< Clock, Duration > closeTime, std::chrono::duration< Rep, Period > resolution, std::chrono::time_point< Clock, Duration > priorCloseTime)
Calculate the effective ledger close time.
T push_back(T... args)
T reserve(T... args)
T size(T... args)
std::vector< Ledger::ID > ancestors
IDs of this ledgers ancestors.
Definition ledgers.h:92
Set the sequence number on a JTx.
Definition seq.h:15