rippled
Loading...
Searching...
No Matches
STAccount.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/Buffer.h>
21#include <xrpl/basics/base_uint.h>
22#include <xrpl/basics/contract.h>
23#include <xrpl/beast/utility/Zero.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/protocol/AccountID.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STAccount.h>
28#include <xrpl/protocol/STBase.h>
29#include <xrpl/protocol/Serializer.h>
30
31#include <cstring>
32#include <stdexcept>
33#include <string>
34#include <utility>
35
36namespace ripple {
37
38STAccount::STAccount() : STBase(), value_(beast::zero), default_(true)
39{
40}
41
43 : STBase(n), value_(beast::zero), default_(true)
44{
45}
46
48{
49 if (v.empty())
50 return; // Zero is a valid size for a defaulted STAccount.
51
52 // Is it safe to throw from this constructor? Today (November 2015)
53 // the only place that calls this constructor is
54 // STVar::STVar (SerialIter&, SField const&)
55 // which throws. If STVar can throw in its constructor, then so can
56 // STAccount.
57 if (v.size() != uint160::bytes)
58 Throw<std::runtime_error>("Invalid STAccount size");
59
60 default_ = false;
61 memcpy(value_.begin(), v.data(), uint160::bytes);
62}
63
65 : STAccount(name, sit.getVLBuffer())
66{
67}
68
70 : STBase(n), value_(v), default_(false)
71{
72}
73
74STBase*
75STAccount::copy(std::size_t n, void* buf) const
76{
77 return emplace(n, buf, *this);
78}
79
80STBase*
82{
83 return emplace(n, buf, std::move(*this));
84}
85
88{
89 return STI_ACCOUNT;
90}
91
92void
94{
95 XRPL_ASSERT(
96 getFName().isBinary(), "ripple::STAccount::add : field is binary");
97 XRPL_ASSERT(
98 getFName().fieldType == STI_ACCOUNT,
99 "ripple::STAccount::add : valid field type");
100
101 // Preserve the serialization behavior of an STBlob:
102 // o If we are default (all zeros) serialize as an empty blob.
103 // o Otherwise serialize 160 bits.
104 int const size = isDefault() ? 0 : uint160::bytes;
105 s.addVL(value_.data(), size);
106}
107
108bool
110{
111 auto const* const tPtr = dynamic_cast<STAccount const*>(&t);
112 return tPtr && (default_ == tPtr->default_) && (value_ == tPtr->value_);
113}
114
115bool
117{
118 return default_;
119}
120
123{
124 if (isDefault())
125 return "";
126 return toBase58(value());
127}
128
129} // namespace ripple
Like std::vector<char> but better.
Definition Buffer.h:36
Identifies fields.
Definition SField.h:146
void add(Serializer &s) const override
Definition STAccount.cpp:93
AccountID const & value() const noexcept
Definition STAccount.h:92
STBase * copy(std::size_t n, void *buf) const override
Definition STAccount.cpp:75
SerializedTypeID getSType() const override
Definition STAccount.cpp:87
STBase * move(std::size_t n, void *buf) override
Definition STAccount.cpp:81
bool isDefault() const override
std::string getText() const override
AccountID value_
Definition STAccount.h:38
bool isEquivalent(STBase const &t) const override
A type which can be exported to a well known binary format.
Definition STBase.h:135
SField const & getFName() const
Definition STBase.cpp:143
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition STBase.h:233
int addVL(Blob const &vector)
static std::size_t constexpr bytes
Definition base_uint.h:108
iterator begin()
Definition base_uint.h:136
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
SerializedTypeID
Definition SField.h:110