xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
include
xrpl
consensus
ConsensusProposal.h
1
#pragma once
2
3
#include <xrpl/basics/base_uint.h>
4
#include <xrpl/basics/chrono.h>
5
#include <xrpl/json/json_value.h>
6
#include <xrpl/protocol/HashPrefix.h>
7
#include <xrpl/protocol/digest.h>
8
#include <xrpl/protocol/jss.h>
9
10
#include <
cstdint
>
11
#include <
optional
>
12
#include <
sstream
>
13
#include <
string
>
14
15
namespace
xrpl
{
40
template
<
class
NodeId,
class
LedgerId,
class
Position>
41
class
ConsensusProposal
42
{
43
public
:
44
using
NodeID
= NodeId;
45
46
//< Sequence value when a peer initially joins consensus
47
static
std::uint32_t
const
kSeqJoin
= 0;
48
49
//< Sequence number when a peer wants to bow out and leave consensus
50
static
std::uint32_t
const
kSeqLeave
= 0xffffffff;
51
62
ConsensusProposal
(
63
LedgerId
const
&
prevLedger
,
64
std::uint32_t
seq,
65
Position
const
&
position
,
66
NetClock::time_point
closeTime
,
67
NetClock::time_point
now,
68
NodeId
const
&
nodeID
)
69
:
previousLedger_
(
prevLedger
)
70
,
position_
(
position
)
71
,
closeTime_
(
closeTime
)
72
,
time_
(now)
73
,
proposeSeq_
(seq)
74
,
nodeID_
(
nodeID
)
75
{
76
}
77
81
NodeId
const
&
82
nodeID
()
const
83
{
84
return
nodeID_
;
85
}
86
90
Position
const
&
91
position
()
const
92
{
93
return
position_
;
94
}
95
99
LedgerId
const
&
100
prevLedger
()
const
101
{
102
return
previousLedger_
;
103
}
104
113
std::uint32_t
114
proposeSeq
()
const
115
{
116
return
proposeSeq_
;
117
}
118
122
NetClock::time_point
const
&
123
closeTime
()
const
124
{
125
return
closeTime_
;
126
}
127
131
NetClock::time_point
const
&
132
seenTime
()
const
133
{
134
return
time_
;
135
}
136
141
bool
142
isInitial
()
const
143
{
144
return
proposeSeq_
==
kSeqJoin
;
145
}
146
150
bool
151
isBowOut
()
const
152
{
153
return
proposeSeq_
==
kSeqLeave
;
154
}
155
159
bool
160
isStale
(
NetClock::time_point
cutoff)
const
161
{
162
return
time_
<= cutoff;
163
}
164
173
void
174
changePosition
(
175
Position
const
& newPosition,
176
NetClock::time_point
newCloseTime,
177
NetClock::time_point
now)
178
{
179
signingHash_
.reset();
180
position_
= newPosition;
181
closeTime_
= newCloseTime;
182
time_
= now;
183
if
(
proposeSeq_
!=
kSeqLeave
)
184
++
proposeSeq_
;
185
}
186
194
void
195
bowOut
(
NetClock::time_point
now)
196
{
197
signingHash_
.reset();
198
time_
= now;
199
proposeSeq_
=
kSeqLeave
;
200
}
201
202
std::string
203
render
()
const
204
{
205
std::stringstream
ss;
206
ss <<
"proposal: previous_ledger: "
<<
previousLedger_
<<
" proposal_seq: "
<<
proposeSeq_
207
<<
" position: "
<<
position_
<<
" close_time: "
<<
to_string
(
closeTime_
)
208
<<
" now: "
<<
to_string
(
time_
) <<
" is_bow_out:"
<<
isBowOut
()
209
<<
" node_id: "
<<
nodeID_
;
210
return
ss.
str
();
211
}
212
216
json::Value
217
getJson
()
const
218
{
219
using
std::to_string
;
220
221
json::Value
ret =
json::ValueType::Object
;
222
ret[jss::previous_ledger] =
to_string
(
prevLedger
());
223
224
if
(!
isBowOut
())
225
{
226
ret[jss::transaction_hash] =
to_string
(
position
());
227
ret[jss::propose_seq] =
proposeSeq
();
228
}
229
230
ret[jss::close_time] =
to_string
(
closeTime
().time_since_epoch().count());
231
232
return
ret;
233
}
234
238
UInt256
const
&
239
signingHash
()
const
240
{
241
if
(!
signingHash_
)
242
{
243
signingHash_
=
sha512Half
(
244
HashPrefix::Proposal
,
245
std::uint32_t
(
proposeSeq
()),
246
closeTime
().time_since_epoch().count(),
247
prevLedger
(),
248
position
());
249
}
250
251
return
signingHash_
.value();
252
}
253
254
private
:
258
LedgerId
previousLedger_
;
259
263
Position
position_
;
264
268
NetClock::time_point
closeTime_
;
269
270
// !The time this position was last updated
271
NetClock::time_point
time_
;
272
276
std::uint32_t
proposeSeq_
;
277
281
NodeId
nodeID_
;
282
286
mutable
std::optional<UInt256>
signingHash_
;
287
};
288
289
template
<
class
NodeId,
class
LedgerId,
class
Position>
290
bool
291
operator==
(
292
ConsensusProposal<NodeId, LedgerId, Position>
const
& a,
293
ConsensusProposal<NodeId, LedgerId, Position>
const
& b)
294
{
295
return
a.
nodeID
() == b.
nodeID
() && a.
proposeSeq
() == b.
proposeSeq
() &&
296
a.
prevLedger
() == b.
prevLedger
() && a.
position
() == b.
position
() &&
297
a.
closeTime
() == b.
closeTime
() && a.
seenTime
() == b.
seenTime
();
298
}
299
}
// namespace xrpl
std::string
std::stringstream
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::ConsensusProposal
Represents a proposed position taken during a round of consensus.
Definition
ConsensusProposal.h:42
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::proposeSeq_
std::uint32_t proposeSeq_
Definition
ConsensusProposal.h:276
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::signingHash_
std::optional< UInt256 > signingHash_
Definition
ConsensusProposal.h:286
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::time_
NetClock::time_point time_
Definition
ConsensusProposal.h:271
xrpl::ConsensusProposal::getJson
json::Value getJson() const
Get JSON representation for debugging.
Definition
ConsensusProposal.h:217
xrpl::ConsensusProposal::isInitial
bool isInitial() const
Whether this is the first position taken during the current consensus round.
Definition
ConsensusProposal.h:142
xrpl::ConsensusProposal::signingHash
UInt256 const & signingHash() const
The digest for this proposal, used for signing purposes.
Definition
ConsensusProposal.h:239
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::kSeqLeave
static std::uint32_t const kSeqLeave
Definition
ConsensusProposal.h:50
xrpl::ConsensusProposal::seenTime
NetClock::time_point const & seenTime() const
Get when this position was taken.
Definition
ConsensusProposal.h:132
xrpl::ConsensusProposal::proposeSeq
std::uint32_t proposeSeq() const
Get the sequence number of this proposal.
Definition
ConsensusProposal.h:114
xrpl::ConsensusProposal::render
std::string render() const
Definition
ConsensusProposal.h:203
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::kSeqJoin
static std::uint32_t const kSeqJoin
Definition
ConsensusProposal.h:47
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::position
typename TxSetT::ID const & position() const
Definition
ConsensusProposal.h:91
xrpl::ConsensusProposal::ConsensusProposal
ConsensusProposal(LedgerId const &prevLedger, std::uint32_t seq, Position const &position, NetClock::time_point closeTime, NetClock::time_point now, NodeId const &nodeID)
Constructor.
Definition
ConsensusProposal.h:62
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::nodeID_
NodeIDT nodeID_
Definition
ConsensusProposal.h:281
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::prevLedger
typename LedgerT::ID const & prevLedger() const
Definition
ConsensusProposal.h:100
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::previousLedger_
typename LedgerT::ID previousLedger_
Definition
ConsensusProposal.h:258
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::nodeID
NodeIDT const & nodeID() const
Definition
ConsensusProposal.h:82
xrpl::ConsensusProposal::isBowOut
bool isBowOut() const
Get whether this node left the consensus process.
Definition
ConsensusProposal.h:151
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::NodeID
NodeIDT NodeID
Definition
ConsensusProposal.h:44
xrpl::ConsensusProposal::bowOut
void bowOut(NetClock::time_point now)
Leave consensus.
Definition
ConsensusProposal.h:195
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::position_
typename TxSetT::ID position_
Definition
ConsensusProposal.h:263
xrpl::ConsensusProposal::changePosition
void changePosition(Position const &newPosition, NetClock::time_point newCloseTime, NetClock::time_point now)
Update the position during the consensus process.
Definition
ConsensusProposal.h:174
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::closeTime
NetClock::time_point const & closeTime() const
Definition
ConsensusProposal.h:123
xrpl::ConsensusProposal< NodeIDT, typename LedgerT::ID, typename TxSetT::ID >::closeTime_
NetClock::time_point closeTime_
Definition
ConsensusProposal.h:268
xrpl::ConsensusProposal::isStale
bool isStale(NetClock::time_point cutoff) const
Get whether this position is stale relative to the provided cutoff.
Definition
ConsensusProposal.h:160
xrpl::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition
chrono.h:48
cstdint
std::uint32_t
json::ValueType::Object
@ Object
object value (collection of name/value pairs).
Definition
json_value.h:29
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::operator==
constexpr bool operator==(BaseUInt< Bits, Tag > const &lhs, BaseUInt< Bits, Tag > const &rhs)
Definition
base_uint.h:612
xrpl::to_string
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition
base_uint.h:657
xrpl::UInt256
BaseUInt< 256 > UInt256
Definition
base_uint.h:580
xrpl::HashPrefix::Proposal
@ Proposal
proposal for signing
Definition
HashPrefix.h:79
xrpl::sha512Half
Sha512HalfHasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
Definition
digest.h:215
optional
sstream
std::stringstream::str
T str(T... args)
string
std::to_string
T to_string(T... args)
Generated by
1.17.0