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