xrpld
Loading...
Searching...
No Matches
SHAMapLeafNode.cpp
1#include <xrpl/shamap/SHAMapLeafNode.h>
2
3#include <xrpl/basics/SHAMapHash.h>
4#include <xrpl/basics/base_uint.h>
5#include <xrpl/beast/utility/instrumentation.h>
6#include <xrpl/shamap/SHAMapItem.h>
7#include <xrpl/shamap/SHAMapNodeID.h>
8#include <xrpl/shamap/SHAMapTreeNode.h>
9
10#include <boost/smart_ptr/intrusive_ptr.hpp>
11
12#include <cstdint>
13#include <string>
14#include <utility>
15
16namespace xrpl {
17
18SHAMapLeafNode::SHAMapLeafNode(boost::intrusive_ptr<SHAMapItem const> item, std::uint32_t cowid)
19 : SHAMapTreeNode(cowid), item_(std::move(item))
20{
21 XRPL_ASSERT(
22 item_->size() >= kMinShaMapItemBytes,
23 "xrpl::SHAMapLeafNode::SHAMapLeafNode(boost::intrusive_ptr<"
24 "SHAMapItem const>, std::uint32_t) : minimum input size");
25}
26
28 boost::intrusive_ptr<SHAMapItem const> item,
30 SHAMapHash const& hash)
31 : SHAMapTreeNode(cowid, hash), item_(std::move(item))
32{
33 XRPL_ASSERT(
34 item_->size() >= kMinShaMapItemBytes,
35 "xrpl::SHAMapLeafNode::SHAMapLeafNode(boost::intrusive_ptr<"
36 "SHAMapItem const>, std::uint32_t, SHAMapHash const&) : minimum input "
37 "size");
38}
39
40boost::intrusive_ptr<SHAMapItem const> const&
42{
43 return item_;
44}
45
46bool
47SHAMapLeafNode::setItem(boost::intrusive_ptr<SHAMapItem const> item)
48{
49 XRPL_ASSERT(cowid_, "xrpl::SHAMapLeafNode::setItem : nonzero cowid");
50 item_ = std::move(item);
51
52 auto const oldHash = hash_;
53
54 updateHash();
55
56 return (oldHash != hash_);
57}
58
61{
63
64 auto const type = getType();
65
67 {
68 ret += ",txn\n";
69 }
70 else if (type == SHAMapNodeType::TnTransactionMd)
71 {
72 ret += ",txn+md\n";
73 }
74 else if (type == SHAMapNodeType::TnAccountState)
75 {
76 ret += ",as\n";
77 }
78 else
79 {
80 ret += ",leaf\n";
81 }
82
83 ret += " Tag=";
84 ret += to_string(item_->key());
85 ret += "\n Hash=";
86 ret += to_string(hash_);
87 ret += "/";
88 ret += std::to_string(item_->size());
89 return ret;
90}
91
92void
94{
95 XRPL_ASSERT(hash_.isNonZero(), "xrpl::SHAMapLeafNode::invariants : nonzero hash");
96 XRPL_ASSERT(item_, "xrpl::SHAMapLeafNode::invariants : non-null item");
97}
98
99} // namespace xrpl
boost::intrusive_ptr< SHAMapItem const > item_
SHAMapLeafNode(boost::intrusive_ptr< SHAMapItem const > item, std::uint32_t cowid)
void invariants(bool isRoot=false) const final
bool setItem(boost::intrusive_ptr< SHAMapItem const > i)
Set the item that this node points to and update the node's hash.
std::string getString(SHAMapNodeID const &) const final
boost::intrusive_ptr< SHAMapItem const > const & peekItem() const
Identifies a node inside a SHAMap.
std::uint32_t cowid_
Determines the owning SHAMap, if any.
virtual SHAMapNodeType getType() const =0
Determines the type of node.
SHAMapTreeNode(std::uint32_t cowid) noexcept
Construct a node.
virtual std::string getString(SHAMapNodeID const &) const
virtual void updateHash()=0
Recalculate the hash of this node.
std::uint32_t cowid() const
Returns the SHAMap that owns this node.
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
constexpr std::size_t kMinShaMapItemBytes
T to_string(T... args)