rippled
Loading...
Searching...
No Matches
STAccount.cpp
1#include <xrpl/basics/Buffer.h>
2#include <xrpl/basics/base_uint.h>
3#include <xrpl/basics/contract.h>
4#include <xrpl/beast/utility/Zero.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/protocol/AccountID.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STAccount.h>
9#include <xrpl/protocol/STBase.h>
10#include <xrpl/protocol/Serializer.h>
11
12#include <cstring>
13#include <stdexcept>
14#include <string>
15#include <utility>
16
17namespace ripple {
18
19STAccount::STAccount() : STBase(), value_(beast::zero), default_(true)
20{
21}
22
24 : STBase(n), value_(beast::zero), 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::bytes)
39 Throw<std::runtime_error>("Invalid STAccount size");
40
41 default_ = false;
42 memcpy(value_.begin(), v.data(), uint160::bytes);
43}
44
46 : STAccount(name, sit.getVLBuffer())
47{
48}
49
51 : STBase(n), value_(v), default_(false)
52{
53}
54
55STBase*
56STAccount::copy(std::size_t n, void* buf) const
57{
58 return emplace(n, buf, *this);
59}
60
61STBase*
63{
64 return emplace(n, buf, std::move(*this));
65}
66
69{
70 return STI_ACCOUNT;
71}
72
73void
75{
76 XRPL_ASSERT(
77 getFName().isBinary(), "ripple::STAccount::add : field is binary");
78 XRPL_ASSERT(
79 getFName().fieldType == STI_ACCOUNT,
80 "ripple::STAccount::add : valid field type");
81
82 // Preserve the serialization behavior of an STBlob:
83 // o If we are default (all zeros) serialize as an empty blob.
84 // o Otherwise serialize 160 bits.
85 int const size = isDefault() ? 0 : uint160::bytes;
86 s.addVL(value_.data(), size);
87}
88
89bool
91{
92 auto const* const tPtr = dynamic_cast<STAccount const*>(&t);
93 return tPtr && (default_ == tPtr->default_) && (value_ == tPtr->value_);
94}
95
96bool
98{
99 return default_;
100}
101
104{
105 if (isDefault())
106 return "";
107 return toBase58(value());
108}
109
110} // namespace ripple
Like std::vector<char> but better.
Definition Buffer.h:17
Identifies fields.
Definition SField.h:127
void add(Serializer &s) const override
Definition STAccount.cpp:74
AccountID const & value() const noexcept
Definition STAccount.h:73
STBase * copy(std::size_t n, void *buf) const override
Definition STAccount.cpp:56
SerializedTypeID getSType() const override
Definition STAccount.cpp:68
STBase * move(std::size_t n, void *buf) override
Definition STAccount.cpp:62
bool isDefault() const override
Definition STAccount.cpp:97
std::string getText() const override
AccountID value_
Definition STAccount.h:19
bool isEquivalent(STBase const &t) const override
Definition STAccount.cpp:90
A type which can be exported to a well known binary format.
Definition STBase.h:116
SField const & getFName() const
Definition STBase.cpp:124
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:214
int addVL(Blob const &vector)
static std::size_t constexpr bytes
Definition base_uint.h:89
iterator begin()
Definition base_uint.h:117
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
SerializedTypeID
Definition SField.h:91