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)
25 , ledgerSeq_(ledger)
26 , nodes_(obj.getFieldArray(sfAffectedNodes))
27{
28 result_ = obj.getFieldU8(sfTransactionResult);
29 index_ = obj.getFieldU32(sfTransactionIndex);
30
31 auto affectedNodes =
32 dynamic_cast<STArray const*>(obj.peekAtPField(sfAffectedNodes));
33 XRPL_ASSERT(
34 affectedNodes, "xrpl::TxMeta::TxMeta(STObject) : type cast succeeded");
35 if (affectedNodes)
36 nodes_ = *affectedNodes;
37
39}
40
41TxMeta::TxMeta(uint256 const& txid, std::uint32_t ledger, Blob const& vec)
42 : transactionID_(txid), ledgerSeq_(ledger), nodes_(sfAffectedNodes, 32)
43{
44 SerialIter sit(makeSlice(vec));
45
46 STObject obj(sit, sfMetadata);
47 result_ = obj.getFieldU8(sfTransactionResult);
48 index_ = obj.getFieldU32(sfTransactionIndex);
49 nodes_ = obj.getFieldArray(sfAffectedNodes);
50
52}
53
55 : transactionID_(transactionID)
56 , ledgerSeq_(ledger)
57 , index_(std::numeric_limits<std::uint32_t>::max())
58 , result_(255)
59 , nodes_(sfAffectedNodes)
60{
61 nodes_.reserve(32);
62}
63
64void
66 uint256 const& node,
67 SField const& type,
68 std::uint16_t nodeType)
69{
70 // make sure the node exists and force its type
71 for (auto& n : nodes_)
72 {
73 if (n.getFieldH256(sfLedgerIndex) == node)
74 {
75 n.setFName(type);
76 n.setFieldU16(sfLedgerEntryType, nodeType);
77 return;
78 }
79 }
80
82 STObject& obj = nodes_.back();
83
84 XRPL_ASSERT(
85 obj.getFName() == type,
86 "xrpl::TxMeta::setAffectedNode : field type match");
87 obj.setFieldH256(sfLedgerIndex, node);
88 obj.setFieldU16(sfLedgerEntryType, nodeType);
89}
90
91boost::container::flat_set<AccountID>
93{
94 boost::container::flat_set<AccountID> list;
95 list.reserve(10);
96
97 // This code should match the behavior of the JS method:
98 // Meta#getAffectedAccounts
99 for (auto const& node : nodes_)
100 {
101 int index = node.getFieldIndex(
102 (node.getFName() == sfCreatedNode) ? sfNewFields : sfFinalFields);
103
104 if (index != -1)
105 {
106 auto const* inner =
107 dynamic_cast<STObject const*>(&node.peekAtIndex(index));
108 XRPL_ASSERT(
109 inner,
110 "xrpl::getAffectedAccounts : STObject type cast succeeded");
111 if (inner)
112 {
113 for (auto const& field : *inner)
114 {
115 if (auto sa = dynamic_cast<STAccount const*>(&field))
116 {
117 XRPL_ASSERT(
118 !sa->isDefault(),
119 "xrpl::getAffectedAccounts : account is set");
120 if (!sa->isDefault())
121 list.insert(sa->value());
122 }
123 else if (
124 (field.getFName() == sfLowLimit) ||
125 (field.getFName() == sfHighLimit) ||
126 (field.getFName() == sfTakerPays) ||
127 (field.getFName() == sfTakerGets))
128 {
129 auto lim = dynamic_cast<STAmount const*>(&field);
130 XRPL_ASSERT(
131 lim,
132 "xrpl::getAffectedAccounts : STAmount type cast "
133 "succeeded");
134
135 if (lim != nullptr)
136 {
137 auto issuer = lim->getIssuer();
138
139 if (issuer.isNonZero())
140 list.insert(issuer);
141 }
142 }
143 else if (field.getFName() == sfMPTokenIssuanceID)
144 {
145 auto mptID =
146 dynamic_cast<STBitString<192> const*>(&field);
147 if (mptID != nullptr)
148 {
149 auto issuer = MPTIssue(mptID->value()).getIssuer();
150
151 if (issuer.isNonZero())
152 list.insert(issuer);
153 }
154 }
155 }
156 }
157 }
158 }
159
160 return list;
161}
162
165{
166 uint256 index = node->key();
167 for (auto& n : nodes_)
168 {
169 if (n.getFieldH256(sfLedgerIndex) == index)
170 return n;
171 }
173 STObject& obj = nodes_.back();
174
175 XRPL_ASSERT(
176 obj.getFName() == type,
177 "xrpl::TxMeta::getAffectedNode(SLE::ref) : field type match");
178 obj.setFieldH256(sfLedgerIndex, index);
179 obj.setFieldU16(sfLedgerEntryType, node->getFieldU16(sfLedgerEntryType));
180
181 return obj;
182}
183
186{
187 for (auto& n : nodes_)
188 {
189 if (n.getFieldH256(sfLedgerIndex) == node)
190 return n;
191 }
192 // LCOV_EXCL_START
193 UNREACHABLE("xrpl::TxMeta::getAffectedNode(uint256) : node not found");
194 Throw<std::runtime_error>("Affected node not found");
195 return *(nodes_.begin()); // Silence compiler warning.
196 // LCOV_EXCL_STOP
197}
198
201{
202 STObject metaData(sfTransactionMetaData);
203 XRPL_ASSERT(result_ != 255, "xrpl::TxMeta::getAsObject : result_ is set");
204 metaData.setFieldU8(sfTransactionResult, result_);
205 metaData.setFieldU32(sfTransactionIndex, index_);
206 metaData.emplace_back(nodes_);
208 metaData.setFieldAmount(sfDeliveredAmount, *deliveredAmount_);
209
210 if (parentBatchID_.has_value())
211 metaData.setFieldH256(sfParentBatchID, *parentBatchID_);
212
213 return metaData;
214}
215
216void
218{
219 result_ = TERtoInt(result);
220 index_ = index;
221 XRPL_ASSERT(
222 (result_ == 0) || ((result_ > 100) && (result_ <= 255)),
223 "xrpl::TxMeta::addRaw : valid TER input");
224
225 nodes_.sort([](STObject const& o1, STObject const& o2) {
226 return o1.getFieldH256(sfLedgerIndex) < o2.getFieldH256(sfLedgerIndex);
227 });
228
229 getAsObject().add(s);
230}
231
232} // 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:193
void reserve(std::size_t n)
Definition STArray.h:247
iterator begin()
Definition STArray.h:205
void sort(bool(*compare)(STObject const &o1, STObject const &o2))
Definition STArray.cpp:178
STObject & back()
Definition STArray.h:174
SField const & getFName() const
Definition STBase.cpp:124
void setFieldU8(SField const &field, unsigned char)
Definition STObject.cpp:726
unsigned char getFieldU8(SField const &field) const
Definition STObject.cpp:584
std::uint32_t getFieldU32(SField const &field) const
Definition STObject.cpp:596
std::size_t emplace_back(Args &&... args)
Definition STObject.h:989
void setFieldU32(SField const &field, std::uint32_t)
Definition STObject.cpp:738
STArray const & getFieldArray(SField const &field) const
Definition STObject.cpp:683
void setFieldAmount(SField const &field, STAmount const &)
Definition STObject.cpp:792
void add(Serializer &s) const override
Definition STObject.cpp:122
void setFieldU16(SField const &field, std::uint16_t)
Definition STObject.cpp:732
uint256 getFieldH256(SField const &field) const
Definition STObject.cpp:626
STBase const * peekAtPField(SField const &field) const
Definition STObject.cpp:438
void setFieldH256(SField const &field, uint256 const &)
Definition STObject.cpp:756
boost::container::flat_set< AccountID > getAffectedAccounts() const
Return a list of accounts affected by this transaction.
Definition TxMeta.cpp:92
void setAffectedNode(uint256 const &, SField const &type, std::uint16_t nodeType)
Definition TxMeta.cpp:65
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:164
void addRaw(Serializer &, TER, std::uint32_t index)
Definition TxMeta.cpp:217
STObject getAsObject() const
Definition TxMeta.cpp:200
std::uint32_t index_
Definition TxMeta.h:111
TxMeta(uint256 const &transactionID, std::uint32_t ledger)
Definition TxMeta.cpp:54
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:225
T has_value(T... args)