xrpld
Loading...
Searching...
No Matches
STAccount.cpp
1#include <xrpl/protocol/STAccount.h>
2
3#include <xrpl/basics/Buffer.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/basics/contract.h>
6#include <xrpl/beast/utility/Zero.h>
7#include <xrpl/beast/utility/instrumentation.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/SField.h>
10#include <xrpl/protocol/STBase.h>
11#include <xrpl/protocol/Serializer.h>
12
13#include <cstring>
14#include <stdexcept>
15#include <string>
16#include <utility>
17
18namespace xrpl {
19
21{
22}
23
24STAccount::STAccount(SField const& n) : STBase(n), value_(beast::kZero), default_(true)
25{
26}
27
29{
30 if (v.empty())
31 return; // Zero is a valid size for a defaulted STAccount.
32
33 // Is it safe to throw from this constructor? Today (November 2015)
34 // the only place that calls this constructor is
35 // STVar::STVar (SerialIter&, SField const&)
36 // which throws. If STVar can throw in its constructor, then so can
37 // STAccount.
38 if (v.size() != uint160::kBytes)
39 Throw<std::runtime_error>("Invalid STAccount size");
40
41 default_ = false;
42 memcpy(value_.begin(), v.data(), uint160::kBytes);
43}
44
45STAccount::STAccount(SerialIter& sit, SField const& name) : STAccount(name, sit.getVLBuffer())
46{
47}
48
49STAccount::STAccount(SField const& n, AccountID const& v) : STBase(n), value_(v), default_(false)
50{
51}
52
53STBase*
54STAccount::copy(std::size_t n, void* buf) const
55{
56 return emplace(n, buf, *this);
57}
58
59STBase*
61{
62 return emplace(n, buf, std::move(*this));
63}
64
67{
68 return STI_ACCOUNT;
69}
70
71void
73{
74 XRPL_ASSERT(getFName().isBinary(), "xrpl::STAccount::add : field is binary");
75 XRPL_ASSERT(getFName().fieldType == STI_ACCOUNT, "xrpl::STAccount::add : valid field type");
76
77 // Preserve the serialization behavior of an STBlob:
78 // o If we are default (all zeros) serialize as an empty blob.
79 // o Otherwise serialize 160 bits.
80 int const size = isDefault() ? 0 : uint160::kBytes;
81 s.addVL(value_.data(), size);
82}
83
84bool
86{
87 auto const* const tPtr = dynamic_cast<STAccount const*>(&t);
88 return (tPtr != nullptr) && (default_ == tPtr->default_) && (value_ == tPtr->value_);
89}
90
91bool
93{
94 return default_;
95}
96
99{
100 if (isDefault())
101 return "";
102 return toBase58(value());
103}
104
105} // namespace xrpl
static constexpr std::size_t kBytes
Definition base_uint.h:89
Like std::vector<char> but better.
Definition Buffer.h:16
bool empty() const noexcept
Definition Buffer.h:111
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition Buffer.h:105
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition Buffer.h:129
Identifies fields.
Definition SField.h:130
bool isDefault() const override
Definition STAccount.cpp:92
bool isEquivalent(STBase const &t) const override
Definition STAccount.cpp:85
AccountID value_
Definition STAccount.h:18
void add(Serializer &s) const override
Definition STAccount.cpp:72
STBase * move(std::size_t n, void *buf) override
Definition STAccount.cpp:60
SerializedTypeID getSType() const override
Definition STAccount.cpp:66
STBase * copy(std::size_t n, void *buf) const override
Definition STAccount.cpp:54
std::string getText() const override
Definition STAccount.cpp:98
AccountID const & value() const noexcept
Definition STAccount.h:72
A type which can be exported to a well known binary format.
Definition STBase.h:117
SField const & getFName() const
Definition STBase.cpp:126
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:215
int addVL(Blob const &vector)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:93
SerializedTypeID
Definition SField.h:93
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:49