xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
protocol
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
18
namespace
xrpl
{
19
20
STAccount::STAccount
() :
value_
(
beast
::kZero),
default_
(true)
21
{
22
}
23
24
STAccount::STAccount
(
SField
const
& n) :
STBase
(n),
value_
(
beast
::kZero),
default_
(true)
25
{
26
}
27
28
STAccount::STAccount
(
SField
const
& n,
Buffer
const
& v) :
STAccount
(n)
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
45
STAccount::STAccount
(
SerialIter
& sit,
SField
const
& name) :
STAccount
(name, sit.getVLBuffer())
46
{
47
}
48
49
STAccount::STAccount
(
SField
const
& n,
AccountID
const
& v) :
STBase
(n),
value_
(v),
default_
(false)
50
{
51
}
52
53
STBase
*
54
STAccount::copy
(
std::size_t
n,
void
* buf)
const
55
{
56
return
emplace
(n, buf, *
this
);
57
}
58
59
STBase
*
60
STAccount::move
(
std::size_t
n,
void
* buf)
61
{
62
return
emplace
(n, buf, std::move(*
this
));
63
}
64
65
SerializedTypeID
66
STAccount::getSType
()
const
67
{
68
return
STI_ACCOUNT;
69
}
70
71
void
72
STAccount::add
(
Serializer
& s)
const
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
84
bool
85
STAccount::isEquivalent
(
STBase
const
& t)
const
86
{
87
auto
const
*
const
tPtr =
dynamic_cast<
STAccount
const*
>
(&t);
88
return
(tPtr !=
nullptr
) && (
default_
== tPtr->default_) && (
value_
== tPtr->value_);
89
}
90
91
bool
92
STAccount::isDefault
()
const
93
{
94
return
default_
;
95
}
96
97
std::string
98
STAccount::getText
()
const
99
{
100
if
(
isDefault
())
101
return
""
;
102
return
toBase58
(
value
());
103
}
104
105
}
// namespace xrpl
std::string
xrpl::BaseUInt< 160 >::kBytes
static constexpr std::size_t kBytes
Definition
base_uint.h:100
xrpl::Buffer
Like std::vector<char> but better.
Definition
Buffer.h:19
xrpl::Buffer::empty
bool empty() const noexcept
Definition
Buffer.h:129
xrpl::Buffer::size
std::size_t size() const noexcept
Returns the number of bytes in the buffer.
Definition
Buffer.h:123
xrpl::Buffer::data
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
Definition
Buffer.h:148
xrpl::SField
Identifies fields.
Definition
SField.h:132
xrpl::STAccount::isDefault
bool isDefault() const override
Definition
STAccount.cpp:92
xrpl::STAccount::isEquivalent
bool isEquivalent(STBase const &t) const override
Definition
STAccount.cpp:85
xrpl::STAccount::STAccount
STAccount()
Definition
STAccount.cpp:20
xrpl::STAccount::value_
AccountID value_
Definition
STAccount.h:22
xrpl::STAccount::add
void add(Serializer &s) const override
Definition
STAccount.cpp:72
xrpl::STAccount::move
STBase * move(std::size_t n, void *buf) override
Definition
STAccount.cpp:60
xrpl::STAccount::getSType
SerializedTypeID getSType() const override
Definition
STAccount.cpp:66
xrpl::STAccount::default_
bool default_
Definition
STAccount.h:23
xrpl::STAccount::copy
STBase * copy(std::size_t n, void *buf) const override
Definition
STAccount.cpp:54
xrpl::STAccount::getText
std::string getText() const override
Definition
STAccount.cpp:98
xrpl::STAccount::value
AccountID const & value() const noexcept
Definition
STAccount.h:76
xrpl::STBase
A type which can be exported to a well known binary format.
Definition
STBase.h:129
xrpl::STBase::getFName
SField const & getFName() const
Definition
STBase.cpp:120
xrpl::STBase::emplace
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition
STBase.h:226
xrpl::STBase::STBase
STBase()
Definition
STBase.cpp:15
xrpl::SerialIter
Definition
Serializer.h:478
xrpl::Serializer
Definition
Serializer.h:23
xrpl::Serializer::addVL
int addVL(Blob const &vector)
Definition
libxrpl/protocol/Serializer.cpp:180
cstring
beast
Definition
base_uint.h:697
xrpl
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition
algorithm.h:5
xrpl::toBase58
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition
AccountID.cpp:95
xrpl::SerializedTypeID
SerializedTypeID
Definition
SField.h:94
xrpl::AccountID
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition
AccountID.h:34
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
std::size_t
stdexcept
string
utility
Generated by
1.17.0