rippled
Loading...
Searching...
No Matches
TxMeta.cpp
1#include <xrpl/basics/Blob.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/SField.h>
7#include <xrpl/protocol/STAccount.h>
8#include <xrpl/protocol/STAmount.h>
9#include <xrpl/protocol/STLedgerEntry.h>
10#include <xrpl/protocol/STObject.h>
11#include <xrpl/protocol/Serializer.h>
12#include <xrpl/protocol/TER.h>
13#include <xrpl/protocol/TxMeta.h>
14
15#include <boost/container/flat_set.hpp>
16
17#include <cstdint>
18#include <stdexcept>
19#include <string>
20
21namespace xrpl {
22
23TxMeta::TxMeta(uint256 const& txid, std::uint32_t ledger, STObject const& obj)
24 : transactionID_(txid), ledgerSeq_(ledger), nodes_(obj.getFieldArray(sfAffectedNodes))
25{
26 result_ = obj.getFieldU8(sfTransactionResult);
27 index_ = obj.getFieldU32(sfTransactionIndex);
28
29 auto affectedNodes = dynamic_cast<STArray const*>(obj.peekAtPField(sfAffectedNodes));
30 XRPL_ASSERT(affectedNodes, "xrpl::TxMeta::TxMeta(STObject) : type cast succeeded");
31 if (affectedNodes)
32 nodes_ = *affectedNodes;
33
35}
36
37TxMeta::TxMeta(uint256 const& txid, std::uint32_t ledger, Blob const& vec)
38 : transactionID_(txid), ledgerSeq_(ledger), nodes_(sfAffectedNodes, 32)
39{
40 SerialIter sit(makeSlice(vec));
41
42 STObject obj(sit, sfMetadata);
43 result_ = obj.getFieldU8(sfTransactionResult);
44 index_ = obj.getFieldU32(sfTransactionIndex);
45 nodes_ = obj.getFieldArray(sfAffectedNodes);
46
48}
49
51 : transactionID_(transactionID)
52 , ledgerSeq_(ledger)
53 , index_(std::numeric_limits<std::uint32_t>::max())
54 , result_(255)
55 , nodes_(sfAffectedNodes)
56{
57 nodes_.reserve(32);
58}
59
60void
61TxMeta::setAffectedNode(uint256 const& node, SField const& type, std::uint16_t nodeType)
62{
63 // make sure the node exists and force its type
64 for (auto& n : nodes_)
65 {
66 if (n.getFieldH256(sfLedgerIndex) == node)
67 {
68 n.setFName(type);
69 n.setFieldU16(sfLedgerEntryType, nodeType);
70 return;
71 }
72 }
73
75 STObject& obj = nodes_.back();
76
77 XRPL_ASSERT(obj.getFName() == type, "xrpl::TxMeta::setAffectedNode : field type match");
78 obj.setFieldH256(sfLedgerIndex, node);
79 obj.setFieldU16(sfLedgerEntryType, nodeType);
80}
81
82boost::container::flat_set<AccountID>
84{
85 boost::container::flat_set<AccountID> list;
86 list.reserve(10);
87
88 // This code should match the behavior of the JS method:
89 // Meta#getAffectedAccounts
90 for (auto const& node : nodes_)
91 {
92 int index = node.getFieldIndex((node.getFName() == sfCreatedNode) ? sfNewFields : sfFinalFields);
93
94 if (index != -1)
95 {
96 auto const* inner = dynamic_cast<STObject const*>(&node.peekAtIndex(index));
97 XRPL_ASSERT(inner, "xrpl::getAffectedAccounts : STObject type cast succeeded");
98 if (inner)
99 {
100 for (auto const& field : *inner)
101 {
102 if (auto sa = dynamic_cast<STAccount const*>(&field))
103 {
104 XRPL_ASSERT(!sa->isDefault(), "xrpl::getAffectedAccounts : account is set");
105 if (!sa->isDefault())
106 list.insert(sa->value());
107 }
108 else if (
109 (field.getFName() == sfLowLimit) || (field.getFName() == sfHighLimit) ||
110 (field.getFName() == sfTakerPays) || (field.getFName() == sfTakerGets))
111 {
112 auto lim = dynamic_cast<STAmount const*>(&field);
113 XRPL_ASSERT(
114 lim,
115 "xrpl::getAffectedAccounts : STAmount type cast "
116 "succeeded");
117
118 if (lim != nullptr)
119 {
120 auto issuer = lim->getIssuer();
121
122 if (issuer.isNonZero())
123 list.insert(issuer);
124 }
125 }
126 else if (field.getFName() == sfMPTokenIssuanceID)
127 {
128 auto mptID = dynamic_cast<STBitString<192> const*>(&field);
129 if (mptID != nullptr)
130 {
131 auto issuer = MPTIssue(mptID->value()).getIssuer();
132
133 if (issuer.isNonZero())
134 list.insert(issuer);
135 }
136 }
137 }
138 }
139 }
140 }
141
142 return list;
143}
144
147{
148 uint256 index = node->key();
149 for (auto& n : nodes_)
150 {
151 if (n.getFieldH256(sfLedgerIndex) == index)
152 return n;
153 }
155 STObject& obj = nodes_.back();
156
157 XRPL_ASSERT(obj.getFName() == type, "xrpl::TxMeta::getAffectedNode(SLE::ref) : field type match");
158 obj.setFieldH256(sfLedgerIndex, index);
159 obj.setFieldU16(sfLedgerEntryType, node->getFieldU16(sfLedgerEntryType));
160
161 return obj;
162}
163
166{
167 for (auto& n : nodes_)
168 {
169 if (n.getFieldH256(sfLedgerIndex) == node)
170 return n;
171 }
172 // LCOV_EXCL_START
173 UNREACHABLE("xrpl::TxMeta::getAffectedNode(uint256) : node not found");
174 Throw<std::runtime_error>("Affected node not found");
175 return *(nodes_.begin()); // Silence compiler warning.
176 // LCOV_EXCL_STOP
177}
178
181{
182 STObject metaData(sfTransactionMetaData);
183 XRPL_ASSERT(result_ != 255, "xrpl::TxMeta::getAsObject : result_ is set");
184 metaData.setFieldU8(sfTransactionResult, result_);
185 metaData.setFieldU32(sfTransactionIndex, index_);
186 metaData.emplace_back(nodes_);
188 metaData.setFieldAmount(sfDeliveredAmount, *deliveredAmount_);
189
190 if (parentBatchID_.has_value())
191 metaData.setFieldH256(sfParentBatchID, *parentBatchID_);
192
193 return metaData;
194}
195
196void
198{
199 result_ = TERtoInt(result);
200 index_ = index;
201 XRPL_ASSERT((result_ == 0) || ((result_ > 100) && (result_ <= 255)), "xrpl::TxMeta::addRaw : valid TER input");
202
203 nodes_.sort([](STObject const& o1, STObject const& o2) {
204 return o1.getFieldH256(sfLedgerIndex) < o2.getFieldH256(sfLedgerIndex);
205 });
206
207 getAsObject().add(s);
208}
209
210} // namespace xrpl
AccountID const & getIssuer() const
Definition MPTIssue.cpp:21
Identifies fields.
Definition SField.h:127
void push_back(STObject const &object)
Definition STArray.h:188
void reserve(std::size_t n)
Definition STArray.h:242
iterator begin()
Definition STArray.h:200
void sort(bool(*compare)(STObject const &o1, STObject const &o2))
Definition STArray.cpp:175
STObject & back()
Definition STArray.h:169
SField const & getFName() const
Definition STBase.cpp:122
void setFieldU8(SField const &field, unsigned char)
Definition STObject.cpp:706
unsigned char getFieldU8(SField const &field) const
Definition STObject.cpp:564
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:576
std::size_t emplace_back(Args &&... args)
Definition STObject.h:976
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:718
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:663
void setFieldAmount(SField const &field, STAmount const &)
Definition STObject.cpp:772
void add(Serializer &s) const override
Definition STObject.cpp:117
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:712
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:606
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:412
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:736
boost::container::flat_set< AccountID > getAffectedAccounts() const
Return a list of accounts affected by this transaction.
Definition TxMeta.cpp:83
void setAffectedNode(uint256 const &, SField const &type, std::uint16_t nodeType)
Definition TxMeta.cpp:61
void setAdditionalFields(STObject const &obj)
Definition TxMeta.h:81
std::optional< uint256 > parentBatchID_
Definition TxMeta.h:115
std::optional< STAmount > deliveredAmount_
Definition TxMeta.h:114
STArray nodes_
Definition TxMeta.h:117
int result_
Definition TxMeta.h:112
STObject & getAffectedNode(SLE::ref node, SField const &type)
Definition TxMeta.cpp:146
void addRaw(Serializer &, TER, std::uint32_t index)
Definition TxMeta.cpp:197
STObject getAsObject() const
Definition TxMeta.cpp:180
std::uint32_t index_
Definition TxMeta.h:111
TxMeta(uint256 const &transactionID, std::uint32_t ledger)
Definition TxMeta.cpp:50
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ transactionID
transaction plus signature to give transaction ID
constexpr TERUnderlyingType TERtoInt(TELcodes v)
Definition TER.h:356
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition Slice.h:214
T has_value(T... args)