rippled
Loading...
Searching...
No Matches
XChainAttestations.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2022 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_STXATTESTATIONS_H_INCLUDED
21#define RIPPLE_PROTOCOL_STXATTESTATIONS_H_INCLUDED
22
23#include <xrpl/basics/Buffer.h>
24#include <xrpl/basics/Expected.h>
25#include <xrpl/protocol/AccountID.h>
26#include <xrpl/protocol/Issue.h>
27#include <xrpl/protocol/PublicKey.h>
28#include <xrpl/protocol/SField.h>
29#include <xrpl/protocol/STBase.h>
30#include <xrpl/protocol/STXChainBridge.h>
31#include <xrpl/protocol/SecretKey.h>
32#include <xrpl/protocol/TER.h>
33
34#include <boost/container/flat_set.hpp>
35#include <boost/container/vector.hpp>
36
37#include <cstddef>
38#include <type_traits>
39#include <unordered_map>
40#include <vector>
41
42namespace ripple {
43
44namespace Attestations {
45
47{
48 // Account associated with the public key
50 // Public key from the witness server attesting to the event
52 // Signature from the witness server attesting to the event
54 // Account on the sending chain that triggered the event (sent the
55 // transaction)
57 // Amount transfered on the sending chain
59 // Account on the destination chain that collects a share of the attestation
60 // reward
62 // Amount was transfered on the locking chain
64
65 explicit AttestationBase(
66 AccountID attestationSignerAccount_,
67 PublicKey const& publicKey_,
68 Buffer signature_,
69 AccountID const& sendingAccount_,
70 STAmount const& sendingAmount_,
71 AccountID const& rewardAccount_,
72 bool wasLockingChainSend_);
73
75
76 virtual ~AttestationBase() = default;
77
79 operator=(AttestationBase const&) = default;
80
81 // verify that the signature attests to the data.
82 bool
83 verify(STXChainBridge const& bridge) const;
84
85protected:
86 explicit AttestationBase(STObject const& o);
87 explicit AttestationBase(Json::Value const& v);
88
89 [[nodiscard]] static bool
90 equalHelper(AttestationBase const& lhs, AttestationBase const& rhs);
91
92 [[nodiscard]] static bool
93 sameEventHelper(AttestationBase const& lhs, AttestationBase const& rhs);
94
95 void
96 addHelper(STObject& o) const;
97
98private:
99 [[nodiscard]] virtual std::vector<std::uint8_t>
100 message(STXChainBridge const& bridge) const = 0;
101};
102
103// Attest to a regular cross-chain transfer
105{
108
109 explicit AttestationClaim(
110 AccountID attestationSignerAccount_,
111 PublicKey const& publicKey_,
112 Buffer signature_,
113 AccountID const& sendingAccount_,
114 STAmount const& sendingAmount_,
115 AccountID const& rewardAccount_,
116 bool wasLockingChainSend_,
117 std::uint64_t claimID_,
118 std::optional<AccountID> const& dst_);
119
120 explicit AttestationClaim(
121 STXChainBridge const& bridge,
122 AccountID attestationSignerAccount_,
123 PublicKey const& publicKey_,
124 SecretKey const& secretKey_,
125 AccountID const& sendingAccount_,
126 STAmount const& sendingAmount_,
127 AccountID const& rewardAccount_,
128 bool wasLockingChainSend_,
129 std::uint64_t claimID_,
130 std::optional<AccountID> const& dst_);
131
132 explicit AttestationClaim(STObject const& o);
133 explicit AttestationClaim(Json::Value const& v);
134
135 [[nodiscard]] STObject
136 toSTObject() const;
137
138 // return true if the two attestations attest to the same thing
139 [[nodiscard]] bool
140 sameEvent(AttestationClaim const& rhs) const;
141
142 [[nodiscard]] static std::vector<std::uint8_t>
143 message(
144 STXChainBridge const& bridge,
146 STAmount const& sendingAmount,
151
152 [[nodiscard]] bool
153 validAmounts() const;
154
155private:
156 [[nodiscard]] std::vector<std::uint8_t>
157 message(STXChainBridge const& bridge) const override;
158
159 friend bool
160 operator==(AttestationClaim const& lhs, AttestationClaim const& rhs);
161};
162
164{
165 bool
166 operator()(AttestationClaim const& lhs, AttestationClaim const& rhs) const
167 {
168 return lhs.claimID < rhs.claimID;
169 }
170};
171
172// Attest to a cross-chain transfer that creates an account
174{
175 // createCount on the sending chain. This is the value of the `CreateCount`
176 // field of the bridge on the sending chain when the transaction was
177 // executed.
179 // Account to create on the destination chain
181 // Total amount of the reward pool
183
184 explicit AttestationCreateAccount(STObject const& o);
185
186 explicit AttestationCreateAccount(Json::Value const& v);
187
189 AccountID attestationSignerAccount_,
190 PublicKey const& publicKey_,
191 Buffer signature_,
192 AccountID const& sendingAccount_,
193 STAmount const& sendingAmount_,
194 STAmount const& rewardAmount_,
195 AccountID const& rewardAccount_,
196 bool wasLockingChainSend_,
197 std::uint64_t createCount_,
198 AccountID const& toCreate_);
199
201 STXChainBridge const& bridge,
202 AccountID attestationSignerAccount_,
203 PublicKey const& publicKey_,
204 SecretKey const& secretKey_,
205 AccountID const& sendingAccount_,
206 STAmount const& sendingAmount_,
207 STAmount const& rewardAmount_,
208 AccountID const& rewardAccount_,
209 bool wasLockingChainSend_,
210 std::uint64_t createCount_,
211 AccountID const& toCreate_);
212
213 [[nodiscard]] STObject
214 toSTObject() const;
215
216 // return true if the two attestations attest to the same thing
217 [[nodiscard]] bool
218 sameEvent(AttestationCreateAccount const& rhs) const;
219
220 friend bool
222 AttestationCreateAccount const& lhs,
223 AttestationCreateAccount const& rhs);
224
225 [[nodiscard]] static std::vector<std::uint8_t>
226 message(
227 STXChainBridge const& bridge,
229 STAmount const& sendingAmount,
230 STAmount const& rewardAmount,
234 AccountID const& dst);
235
236 [[nodiscard]] bool
237 validAmounts() const;
238
239private:
240 [[nodiscard]] std::vector<std::uint8_t>
241 message(STXChainBridge const& bridge) const override;
242};
243
245{
246 bool
248 AttestationCreateAccount const& lhs,
249 AttestationCreateAccount const& rhs) const
250 {
251 return lhs.createCount < rhs.createCount;
252 }
253};
254
255}; // namespace Attestations
256
257// Result when checking when two attestation match.
259 // One of the fields doesn't match, and it isn't the dst field
261 // all of the fields match, except the dst field
263 // all of the fields match
264 match
265};
266
268{
270 static SField const& ArrayFieldName;
271
278
280 {
286 STAmount const& a,
287 bool b,
289 : amount{a}, wasLockingChainSend{b}, dst{d}
290 {
291 }
292 };
293
294 explicit XChainClaimAttestation(
295 AccountID const& keyAccount_,
296 PublicKey const& publicKey_,
297 STAmount const& amount_,
298 AccountID const& rewardAccount_,
299 bool wasLockingChainSend_,
301
302 explicit XChainClaimAttestation(
303 STAccount const& keyAccount_,
304 PublicKey const& publicKey_,
305 STAmount const& amount_,
306 STAccount const& rewardAccount_,
307 bool wasLockingChainSend_,
309
310 explicit XChainClaimAttestation(TSignedAttestation const& claimAtt);
311
312 explicit XChainClaimAttestation(STObject const& o);
313
314 explicit XChainClaimAttestation(Json::Value const& v);
315
317 match(MatchFields const& rhs) const;
318
319 [[nodiscard]] STObject
320 toSTObject() const;
321
322 friend bool
324 XChainClaimAttestation const& lhs,
325 XChainClaimAttestation const& rhs);
326};
327
329{
331 static SField const& ArrayFieldName;
332
340
342 {
347
349 };
350
352 AccountID const& keyAccount_,
353 PublicKey const& publicKey_,
354 STAmount const& amount_,
355 STAmount const& rewardAmount_,
356 AccountID const& rewardAccount_,
357 bool wasLockingChainSend_,
358 AccountID const& dst_);
359
360 explicit XChainCreateAccountAttestation(TSignedAttestation const& claimAtt);
361
362 explicit XChainCreateAccountAttestation(STObject const& o);
363
365
366 [[nodiscard]] STObject
367 toSTObject() const;
368
370 match(MatchFields const& rhs) const;
371
372 friend bool
376};
377
378// Attestations from witness servers for a particular claimid and bridge.
379// Only one attestation per signature is allowed.
380template <class TAttestation>
382{
383public:
385
386private:
387 // Set a max number of allowed attestations to limit the amount of memory
388 // allocated and processing time. This number is much larger than the actual
389 // number of attestation a server would ever expect.
390 static constexpr std::uint32_t maxAttestations = 256;
392
393protected:
394 // Prevent slicing to the base class
396
397public:
401 operator=(XChainAttestationsBase const& rhs) = default;
402
404
405 explicit XChainAttestationsBase(Json::Value const& v);
406
407 explicit XChainAttestationsBase(STArray const& arr);
408
409 [[nodiscard]] STArray
410 toSTArray() const;
411
412 typename AttCollection::const_iterator
413 begin() const;
414
415 typename AttCollection::const_iterator
416 end() const;
417
418 typename AttCollection::iterator
419 begin();
420
421 typename AttCollection::iterator
422 end();
423
424 template <class F>
426 erase_if(F&& f);
427
429 size() const;
430
431 bool
432 empty() const;
433
434 AttCollection const&
436
437 template <class T>
438 void
439 emplace_back(T&& att);
440};
441
442template <class TAttestation>
443[[nodiscard]] inline bool
447{
448 return lhs.attestations() == rhs.attestations();
449}
450
451template <class TAttestation>
454{
455 return attestations_;
456};
457
458template <class TAttestation>
459template <class T>
460inline void
462{
463 attestations_.emplace_back(std::forward<T>(att));
464};
465
466template <class TAttestation>
467template <class F>
468inline std::size_t
470{
471 return std::erase_if(attestations_, std::forward<F>(f));
472}
473
474template <class TAttestation>
475inline std::size_t
477{
478 return attestations_.size();
479}
480
481template <class TAttestation>
482inline bool
484{
485 return attestations_.empty();
486}
487
489 : public XChainAttestationsBase<XChainClaimAttestation>
490{
492 using TBase::TBase;
493};
494
496 : public XChainAttestationsBase<XChainCreateAccountAttestation>
497{
499 using TBase::TBase;
500};
501
502} // namespace ripple
503
504#endif // STXCHAINATTESTATIONS_H_
Represents a JSON value.
Definition: json_value.h:150
Like std::vector<char> but better.
Definition: Buffer.h:35
A public key.
Definition: PublicKey.h:62
Identifies fields.
Definition: SField.h:143
A secret key.
Definition: SecretKey.h:38
AttCollection::const_iterator end() const
static constexpr std::uint32_t maxAttestations
XChainAttestationsBase & operator=(XChainAttestationsBase const &rhs)=default
XChainAttestationsBase(XChainAttestationsBase const &rhs)=default
AttCollection::const_iterator begin() const
XChainAttestationsBase(AttCollection &&sigs)
AttCollection const & attestations() const
std::vector< TAttestation > AttCollection
Json::Value bridge(Account const &lockingChainDoor, Issue const &lockingChainIssue, Account const &issuingChainDoor, Issue const &issuingChainIssue)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
constexpr bool operator==(base_uint< Bits, Tag > const &lhs, base_uint< Bits, Tag > const &rhs)
Definition: base_uint.h:585
AttestationBase & operator=(AttestationBase const &)=default
static bool equalHelper(AttestationBase const &lhs, AttestationBase const &rhs)
virtual std::vector< std::uint8_t > message(STXChainBridge const &bridge) const =0
static bool sameEventHelper(AttestationBase const &lhs, AttestationBase const &rhs)
bool verify(STXChainBridge const &bridge) const
AttestationBase(AttestationBase const &)=default
friend bool operator==(AttestationClaim const &lhs, AttestationClaim const &rhs)
bool sameEvent(AttestationClaim const &rhs) const
static std::vector< std::uint8_t > message(STXChainBridge const &bridge, AccountID const &sendingAccount, STAmount const &sendingAmount, AccountID const &rewardAccount, bool wasLockingChainSend, std::uint64_t claimID, std::optional< AccountID > const &dst)
static std::vector< std::uint8_t > message(STXChainBridge const &bridge, AccountID const &sendingAccount, STAmount const &sendingAmount, STAmount const &rewardAmount, AccountID const &rewardAccount, bool wasLockingChainSend, std::uint64_t createCount, AccountID const &dst)
friend bool operator==(AttestationCreateAccount const &lhs, AttestationCreateAccount const &rhs)
bool sameEvent(AttestationCreateAccount const &rhs) const
bool operator()(AttestationClaim const &lhs, AttestationClaim const &rhs) const
bool operator()(AttestationCreateAccount const &lhs, AttestationCreateAccount const &rhs) const
MatchFields(STAmount const &a, bool b, std::optional< AccountID > const &d)
Attestations::AttestationClaim TSignedAttestation
static SField const & ArrayFieldName
friend bool operator==(XChainClaimAttestation const &lhs, XChainClaimAttestation const &rhs)
std::optional< AccountID > dst
friend bool operator==(XChainCreateAccountAttestation const &lhs, XChainCreateAccountAttestation const &rhs)