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 <vector>
39
40namespace ripple {
41
42namespace Attestations {
43
45{
46 // Account associated with the public key
48 // Public key from the witness server attesting to the event
50 // Signature from the witness server attesting to the event
52 // Account on the sending chain that triggered the event (sent the
53 // transaction)
55 // Amount transfered on the sending chain
57 // Account on the destination chain that collects a share of the attestation
58 // reward
60 // Amount was transfered on the locking chain
62
63 explicit AttestationBase(
64 AccountID attestationSignerAccount_,
65 PublicKey const& publicKey_,
66 Buffer signature_,
67 AccountID const& sendingAccount_,
68 STAmount const& sendingAmount_,
69 AccountID const& rewardAccount_,
70 bool wasLockingChainSend_);
71
73
74 virtual ~AttestationBase() = default;
75
77 operator=(AttestationBase const&) = default;
78
79 // verify that the signature attests to the data.
80 bool
81 verify(STXChainBridge const& bridge) const;
82
83protected:
84 explicit AttestationBase(STObject const& o);
85 explicit AttestationBase(Json::Value const& v);
86
87 [[nodiscard]] static bool
88 equalHelper(AttestationBase const& lhs, AttestationBase const& rhs);
89
90 [[nodiscard]] static bool
91 sameEventHelper(AttestationBase const& lhs, AttestationBase const& rhs);
92
93 void
94 addHelper(STObject& o) const;
95
96private:
97 [[nodiscard]] virtual std::vector<std::uint8_t>
98 message(STXChainBridge const& bridge) const = 0;
99};
100
101// Attest to a regular cross-chain transfer
103{
106
107 explicit AttestationClaim(
108 AccountID attestationSignerAccount_,
109 PublicKey const& publicKey_,
110 Buffer signature_,
111 AccountID const& sendingAccount_,
112 STAmount const& sendingAmount_,
113 AccountID const& rewardAccount_,
114 bool wasLockingChainSend_,
115 std::uint64_t claimID_,
116 std::optional<AccountID> const& dst_);
117
118 explicit AttestationClaim(
119 STXChainBridge const& bridge,
120 AccountID attestationSignerAccount_,
121 PublicKey const& publicKey_,
122 SecretKey const& secretKey_,
123 AccountID const& sendingAccount_,
124 STAmount const& sendingAmount_,
125 AccountID const& rewardAccount_,
126 bool wasLockingChainSend_,
127 std::uint64_t claimID_,
128 std::optional<AccountID> const& dst_);
129
130 explicit AttestationClaim(STObject const& o);
131 explicit AttestationClaim(Json::Value const& v);
132
133 [[nodiscard]] STObject
134 toSTObject() const;
135
136 // return true if the two attestations attest to the same thing
137 [[nodiscard]] bool
138 sameEvent(AttestationClaim const& rhs) const;
139
140 [[nodiscard]] static std::vector<std::uint8_t>
141 message(
142 STXChainBridge const& bridge,
144 STAmount const& sendingAmount,
149
150 [[nodiscard]] bool
151 validAmounts() const;
152
153private:
154 [[nodiscard]] std::vector<std::uint8_t>
155 message(STXChainBridge const& bridge) const override;
156
157 friend bool
158 operator==(AttestationClaim const& lhs, AttestationClaim const& rhs);
159};
160
162{
163 bool
164 operator()(AttestationClaim const& lhs, AttestationClaim const& rhs) const
165 {
166 return lhs.claimID < rhs.claimID;
167 }
168};
169
170// Attest to a cross-chain transfer that creates an account
172{
173 // createCount on the sending chain. This is the value of the `CreateCount`
174 // field of the bridge on the sending chain when the transaction was
175 // executed.
177 // Account to create on the destination chain
179 // Total amount of the reward pool
181
182 explicit AttestationCreateAccount(STObject const& o);
183
184 explicit AttestationCreateAccount(Json::Value const& v);
185
187 AccountID attestationSignerAccount_,
188 PublicKey const& publicKey_,
189 Buffer signature_,
190 AccountID const& sendingAccount_,
191 STAmount const& sendingAmount_,
192 STAmount const& rewardAmount_,
193 AccountID const& rewardAccount_,
194 bool wasLockingChainSend_,
195 std::uint64_t createCount_,
196 AccountID const& toCreate_);
197
199 STXChainBridge const& bridge,
200 AccountID attestationSignerAccount_,
201 PublicKey const& publicKey_,
202 SecretKey const& secretKey_,
203 AccountID const& sendingAccount_,
204 STAmount const& sendingAmount_,
205 STAmount const& rewardAmount_,
206 AccountID const& rewardAccount_,
207 bool wasLockingChainSend_,
208 std::uint64_t createCount_,
209 AccountID const& toCreate_);
210
211 [[nodiscard]] STObject
212 toSTObject() const;
213
214 // return true if the two attestations attest to the same thing
215 [[nodiscard]] bool
216 sameEvent(AttestationCreateAccount const& rhs) const;
217
218 friend bool
220 AttestationCreateAccount const& lhs,
221 AttestationCreateAccount const& rhs);
222
223 [[nodiscard]] static std::vector<std::uint8_t>
224 message(
225 STXChainBridge const& bridge,
227 STAmount const& sendingAmount,
228 STAmount const& rewardAmount,
232 AccountID const& dst);
233
234 [[nodiscard]] bool
235 validAmounts() const;
236
237private:
238 [[nodiscard]] std::vector<std::uint8_t>
239 message(STXChainBridge const& bridge) const override;
240};
241
243{
244 bool
246 AttestationCreateAccount const& lhs,
247 AttestationCreateAccount const& rhs) const
248 {
249 return lhs.createCount < rhs.createCount;
250 }
251};
252
253}; // namespace Attestations
254
255// Result when checking when two attestation match.
257 // One of the fields doesn't match, and it isn't the dst field
259 // all of the fields match, except the dst field
261 // all of the fields match
262 match
263};
264
266{
268 static SField const& ArrayFieldName;
269
276
278 {
284 STAmount const& a,
285 bool b,
287 : amount{a}, wasLockingChainSend{b}, dst{d}
288 {
289 }
290 };
291
292 explicit XChainClaimAttestation(
293 AccountID const& keyAccount_,
294 PublicKey const& publicKey_,
295 STAmount const& amount_,
296 AccountID const& rewardAccount_,
297 bool wasLockingChainSend_,
299
300 explicit XChainClaimAttestation(
301 STAccount const& keyAccount_,
302 PublicKey const& publicKey_,
303 STAmount const& amount_,
304 STAccount const& rewardAccount_,
305 bool wasLockingChainSend_,
307
308 explicit XChainClaimAttestation(TSignedAttestation const& claimAtt);
309
310 explicit XChainClaimAttestation(STObject const& o);
311
312 explicit XChainClaimAttestation(Json::Value const& v);
313
315 match(MatchFields const& rhs) const;
316
317 [[nodiscard]] STObject
318 toSTObject() const;
319
320 friend bool
322 XChainClaimAttestation const& lhs,
323 XChainClaimAttestation const& rhs);
324};
325
327{
329 static SField const& ArrayFieldName;
330
338
348
350 AccountID const& keyAccount_,
351 PublicKey const& publicKey_,
352 STAmount const& amount_,
353 STAmount const& rewardAmount_,
354 AccountID const& rewardAccount_,
355 bool wasLockingChainSend_,
356 AccountID const& dst_);
357
358 explicit XChainCreateAccountAttestation(TSignedAttestation const& claimAtt);
359
360 explicit XChainCreateAccountAttestation(STObject const& o);
361
363
364 [[nodiscard]] STObject
365 toSTObject() const;
366
368 match(MatchFields const& rhs) const;
369
370 friend bool
374};
375
376// Attestations from witness servers for a particular claimid and bridge.
377// Only one attestation per signature is allowed.
378template <class TAttestation>
380{
381public:
383
384private:
385 // Set a max number of allowed attestations to limit the amount of memory
386 // allocated and processing time. This number is much larger than the actual
387 // number of attestation a server would ever expect.
388 static constexpr std::uint32_t maxAttestations = 256;
390
391protected:
392 // Prevent slicing to the base class
394
395public:
399 operator=(XChainAttestationsBase const& rhs) = default;
400
402
403 explicit XChainAttestationsBase(Json::Value const& v);
404
405 explicit XChainAttestationsBase(STArray const& arr);
406
407 [[nodiscard]] STArray
408 toSTArray() const;
409
410 typename AttCollection::const_iterator
411 begin() const;
412
413 typename AttCollection::const_iterator
414 end() const;
415
416 typename AttCollection::iterator
417 begin();
418
419 typename AttCollection::iterator
420 end();
421
422 template <class F>
424 erase_if(F&& f);
425
427 size() const;
428
429 bool
430 empty() const;
431
432 AttCollection const&
434
435 template <class T>
436 void
437 emplace_back(T&& att);
438};
439
440template <class TAttestation>
441[[nodiscard]] inline bool
445{
446 return lhs.attestations() == rhs.attestations();
447}
448
449template <class TAttestation>
452{
453 return attestations_;
454};
455
456template <class TAttestation>
457template <class T>
458inline void
463
464template <class TAttestation>
465template <class F>
466inline std::size_t
468{
469 return std::erase_if(attestations_, std::forward<F>(f));
470}
471
472template <class TAttestation>
473inline std::size_t
475{
476 return attestations_.size();
477}
478
479template <class TAttestation>
480inline bool
482{
483 return attestations_.empty();
484}
485
487 : public XChainAttestationsBase<XChainClaimAttestation>
488{
490 using TBase::TBase;
491};
492
494 : public XChainAttestationsBase<XChainCreateAccountAttestation>
495{
497 using TBase::TBase;
498};
499
500} // namespace ripple
501
502#endif // STXCHAINATTESTATIONS_H_
Represents a JSON value.
Definition json_value.h:149
Like std::vector<char> but better.
Definition Buffer.h:36
A public key.
Definition PublicKey.h:62
Identifies fields.
Definition SField.h:146
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
T emplace_back(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
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)