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 xrpl {
18
19STAccount::STAccount() : STBase(), value_(beast::zero), default_(true)
20{
21}
22
23STAccount::STAccount(SField const& n) : STBase(n), value_(beast::zero), default_(true)
24{
25}
26
28{
29 if (v.empty())
30 return; // Zero is a valid size for a defaulted STAccount.
31
32 // Is it safe to throw from this constructor? Today (November 2015)
33 // the only place that calls this constructor is
34 // STVar::STVar (SerialIter&, SField const&)
35 // which throws. If STVar can throw in its constructor, then so can
36 // STAccount.
37 if (v.size() != uint160::bytes)
38 Throw<std::runtime_error>("Invalid STAccount size");
39
40 default_ = false;
41 memcpy(value_.begin(), v.data(), uint160::bytes);
42}
43
44STAccount::STAccount(SerialIter& sit, SField const& name) : STAccount(name, sit.getVLBuffer())
45{
46}
47
48STAccount::STAccount(SField const& n, AccountID const& v) : STBase(n), value_(v), default_(false)
49{
50}
51
52STBase*
53STAccount::copy(std::size_t n, void* buf) const
54{
55 return emplace(n, buf, *this);
56}
57
58STBase*
60{
61 return emplace(n, buf, std::move(*this));
62}
63
66{
67 return STI_ACCOUNT;
68}
69
70void
72{
73 XRPL_ASSERT(getFName().isBinary(), "xrpl::STAccount::add : field is binary");
74 XRPL_ASSERT(getFName().fieldType == STI_ACCOUNT, "xrpl::STAccount::add : valid field type");
75
76 // Preserve the serialization behavior of an STBlob:
77 // o If we are default (all zeros) serialize as an empty blob.
78 // o Otherwise serialize 160 bits.
79 int const size = isDefault() ? 0 : uint160::bytes;
80 s.addVL(value_.data(), size);
81}
82
83bool
85{
86 auto const* const tPtr = dynamic_cast<STAccount const*>(&t);
87 return tPtr && (default_ == tPtr->default_) && (value_ == tPtr->value_);
88}
89
90bool
92{
93 return default_;
94}
95
98{
99 if (isDefault())
100 return "";
101 return toBase58(value());
102}
103
104} // namespace xrpl
Like std::vector<char> but better.
Definition Buffer.h:17
Identifies fields.
Definition SField.h:127
bool isDefault() const override
Definition STAccount.cpp:91
bool isEquivalent(STBase const &t) const override
Definition STAccount.cpp:84
AccountID value_
Definition STAccount.h:19
void add(Serializer &s) const override
Definition STAccount.cpp:71
STBase * move(std::size_t n, void *buf) override
Definition STAccount.cpp:59
SerializedTypeID getSType() const override
Definition STAccount.cpp:65
STBase * copy(std::size_t n, void *buf) const override
Definition STAccount.cpp:53
std::string getText() const override
Definition STAccount.cpp:97
AccountID const & value() const noexcept
Definition STAccount.h:73
A type which can be exported to a well known binary format.
Definition STBase.h:116
SField const & getFName() const
Definition STBase.cpp:122
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:214
int addVL(Blob const &vector)
iterator begin()
Definition base_uint.h:113
pointer data()
Definition base_uint.h:102
static std::size_t constexpr bytes
Definition base_uint.h:85
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:92
SerializedTypeID
Definition SField.h:91