mirror of
https://github.com/XRPLF/rippled.git
synced 2026-07-22 22:50:21 +00:00
Compare commits
41 Commits
bthomee/no
...
ximinez/di
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3141d91fa | ||
|
|
1b1cd4c260 | ||
|
|
a482d6aced | ||
|
|
299627298f | ||
|
|
640798696b | ||
|
|
47512afad5 | ||
|
|
ff3708a757 | ||
|
|
391a1e442c | ||
|
|
9e77212900 | ||
|
|
93f5a0e217 | ||
|
|
71367f361c | ||
|
|
931d21b2a7 | ||
|
|
c99feb82e7 | ||
|
|
7b53a5e0c5 | ||
|
|
4a02518497 | ||
|
|
0b6c3630cc | ||
|
|
4d04ba5be5 | ||
|
|
ed53557d41 | ||
|
|
e4d10393f3 | ||
|
|
fcc7f57e82 | ||
|
|
a25229f154 | ||
|
|
eb6eaf2532 | ||
|
|
ff987fc7c6 | ||
|
|
d21137c4c1 | ||
|
|
ac7db2e621 | ||
|
|
42005a8080 | ||
|
|
1f7b1b3a78 | ||
|
|
80b90544c5 | ||
|
|
00b9a8cd67 | ||
|
|
3be49f814a | ||
|
|
1674fabe81 | ||
|
|
6dfa47ce7a | ||
|
|
bef095be65 | ||
|
|
8e5d774c36 | ||
|
|
fb8fb30f6c | ||
|
|
a553001125 | ||
|
|
57782e84ee | ||
|
|
9d5076c8a9 | ||
|
|
1af379e09f | ||
|
|
1ced0875ae | ||
|
|
53e6d7580a |
@@ -11,7 +11,6 @@
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
@@ -252,11 +251,4 @@ makeSlice(std::basic_string<char, Traits, Alloc> const& s)
|
||||
return Slice(s.data(), s.size());
|
||||
}
|
||||
|
||||
template <class Traits>
|
||||
Slice
|
||||
makeSlice(std::basic_string_view<char, Traits> s)
|
||||
{
|
||||
return Slice(s.data(), s.size());
|
||||
}
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -246,15 +246,7 @@ message TMGetObjectByHash {
|
||||
|
||||
message TMLedgerNode {
|
||||
required bytes nodedata = 1;
|
||||
|
||||
// Used when protocol version <2.3. Not set for ledger base data.
|
||||
optional bytes nodeid = 2;
|
||||
|
||||
// Used when protocol version >=2.3. Neither value is set for ledger base data.
|
||||
oneof reference {
|
||||
bytes id = 3; // Set for inner nodes.
|
||||
uint32 depth = 4; // Set for leaf nodes.
|
||||
}
|
||||
optional bytes nodeid = 2; // missing for ledger base data
|
||||
}
|
||||
|
||||
enum TMLedgerInfoType {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// Add new amendments to the top of this list.
|
||||
// Keep it sorted in reverse chronological order.
|
||||
|
||||
XRPL_FEATURE(DefragDirectories, Supported::No, VoteBehavior::DefaultNo)
|
||||
XRPL_FEATURE(Sponsor, Supported::Yes, VoteBehavior::DefaultNo)
|
||||
XRPL_FEATURE(BatchV1_1, Supported::Yes, VoteBehavior::DefaultNo)
|
||||
XRPL_FEATURE(LendingProtocolV1_1, Supported::No, VoteBehavior::DefaultNo)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <xrpl/basics/Blob.h>
|
||||
#include <xrpl/basics/IntrusivePointer.h>
|
||||
#include <xrpl/basics/SHAMapHash.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/beast/utility/Journal.h>
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
@@ -94,18 +95,6 @@ enum class SHAMapState {
|
||||
*
|
||||
* See https://en.wikipedia.org/wiki/Merkle_tree
|
||||
*/
|
||||
|
||||
/**
|
||||
* Holds a SHAMap node's identity, serialized data, and leaf status. Used by
|
||||
* getNodeFat to return node data for peer synchronization.
|
||||
*/
|
||||
struct SHAMapNodeData
|
||||
{
|
||||
SHAMapNodeID nodeID;
|
||||
bool isLeaf;
|
||||
Blob data; // Placed last, so `isLeaf` can fit into the alignment padding of `nodeID`.
|
||||
};
|
||||
|
||||
class SHAMap
|
||||
{
|
||||
private:
|
||||
@@ -300,10 +289,10 @@ public:
|
||||
std::vector<std::pair<SHAMapNodeID, uint256>>
|
||||
getMissingNodes(int maxNodes, SHAMapSyncFilter const* filter);
|
||||
|
||||
[[nodiscard]] bool
|
||||
bool
|
||||
getNodeFat(
|
||||
SHAMapNodeID const& wanted,
|
||||
std::vector<SHAMapNodeData>& data,
|
||||
std::vector<std::pair<SHAMapNodeID, Blob>>& data,
|
||||
bool fatLeaves,
|
||||
std::uint32_t depth) const;
|
||||
|
||||
@@ -332,45 +321,10 @@ public:
|
||||
void
|
||||
serializeRoot(Serializer& s) const;
|
||||
|
||||
/**
|
||||
* Add a root node to the SHAMap during synchronization.
|
||||
*
|
||||
* This function is used when receiving the root node of a SHAMap from a peer during ledger
|
||||
* synchronization. The node must already have been deserialized.
|
||||
*
|
||||
* @param hash The expected hash of the root node.
|
||||
* @param rootNode A deserialized root node to add.
|
||||
* @param filter Optional sync filter to track received nodes.
|
||||
* @return Status indicating whether the node was useful, duplicate, or invalid.
|
||||
*
|
||||
* @note This function expects the rootNode to be a valid, deserialized SHAMapTreeNode. The
|
||||
* caller is responsible for deserialization and basic validation before calling this
|
||||
* function.
|
||||
*/
|
||||
SHAMapAddNode
|
||||
addRootNode(SHAMapHash const& hash, SHAMapTreeNodePtr rootNode, SHAMapSyncFilter const* filter);
|
||||
|
||||
/**
|
||||
* Add a known node at a specific position in the SHAMap during synchronization.
|
||||
*
|
||||
* This function is used when receiving nodes from peers during ledger synchronization. The node
|
||||
* is inserted at the position specified by nodeID. The node must already have been
|
||||
* deserialized.
|
||||
*
|
||||
* @param nodeID The position in the tree where this node belongs.
|
||||
* @param treeNode A deserialized tree node to add.
|
||||
* @param filter Optional sync filter to track received nodes.
|
||||
* @return Status indicating whether the node was useful, duplicate, or invalid.
|
||||
*
|
||||
* @note This function expects the treeNode to be a valid, deserialized SHAMapTreeNode. The
|
||||
* caller is responsible for deserialization and basic validation before calling this
|
||||
* function. This also means that the nodeID must be consistent with the node's content.
|
||||
*/
|
||||
addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFilter const* filter);
|
||||
SHAMapAddNode
|
||||
addKnownNode(
|
||||
SHAMapNodeID const& nodeID,
|
||||
SHAMapTreeNodePtr treeNode,
|
||||
SHAMapSyncFilter const* filter);
|
||||
addKnownNode(SHAMapNodeID const& nodeID, Slice const& rawNode, SHAMapSyncFilter const* filter);
|
||||
|
||||
// status functions
|
||||
void
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <xrpl/basics/SHAMapHash.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/basics/safe_cast.h>
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
#include <xrpl/shamap/SHAMapItem.h>
|
||||
#include <xrpl/shamap/SHAMapNodeID.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
@@ -63,16 +60,4 @@ public:
|
||||
getString(SHAMapNodeID const&) const final;
|
||||
};
|
||||
|
||||
/**
|
||||
* Return the key of the item held by a SHAMap leaf node.
|
||||
*
|
||||
* @param node a node known to be a leaf (see SHAMapTreeNode::isLeaf).
|
||||
*/
|
||||
inline uint256 const&
|
||||
leafKey(SHAMapTreeNode const& node)
|
||||
{
|
||||
XRPL_ASSERT(node.isLeaf(), "xrpl::leafKey : node is a leaf");
|
||||
return safeDowncast<SHAMapLeafNode const&>(node).peekItem()->key();
|
||||
}
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -26,6 +26,14 @@ namespace xrpl {
|
||||
|
||||
namespace directory {
|
||||
|
||||
struct Gap
|
||||
{
|
||||
uint64_t const page;
|
||||
SLE::pointer node;
|
||||
uint64_t const nextPage;
|
||||
SLE::pointer next;
|
||||
};
|
||||
|
||||
std::uint64_t
|
||||
createRoot(
|
||||
ApplyView& view,
|
||||
@@ -126,7 +134,9 @@ insertPage(
|
||||
if (page == 0)
|
||||
return std::nullopt;
|
||||
if (!view.rules().enabled(fixDirectoryLimit) && page >= kDirNodeMaxPages) // Old pages limit
|
||||
{
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// We are about to create a new node; we'll link it to
|
||||
// the chain first:
|
||||
@@ -147,12 +157,8 @@ insertPage(
|
||||
// Save some space by not specifying the value 0 since it's the default.
|
||||
if (page != 1)
|
||||
node->setFieldU64(sfIndexPrevious, page - 1);
|
||||
XRPL_ASSERT_PARTS(!nextPage, "xrpl::directory::insertPage", "nextPage has default value");
|
||||
/* Reserved for future use when directory pages may be inserted in
|
||||
* between two other pages instead of only at the end of the chain.
|
||||
if (nextPage)
|
||||
node->setFieldU64(sfIndexNext, nextPage);
|
||||
*/
|
||||
describe(node);
|
||||
view.insert(node);
|
||||
|
||||
@@ -168,7 +174,7 @@ ApplyView::dirAdd(
|
||||
uint256 const& key,
|
||||
std::function<void(SLE::ref)> const& describe)
|
||||
{
|
||||
auto root = peek(directory);
|
||||
auto const root = peek(directory);
|
||||
|
||||
if (!root)
|
||||
{
|
||||
@@ -178,6 +184,43 @@ ApplyView::dirAdd(
|
||||
|
||||
auto [page, node, indexes] = directory::findPreviousPage(*this, directory, root);
|
||||
|
||||
if (rules().enabled(featureDefragDirectories))
|
||||
{
|
||||
// If there are more nodes than just the root, and there's no space in
|
||||
// the last one, walk backwards to find one with space, or to find one
|
||||
// missing.
|
||||
std::optional<directory::Gap> gapPages;
|
||||
while (page && indexes.size() >= kDIR_NODE_MAX_PAGES)
|
||||
{
|
||||
// Find a page with space, or a gap in pages.
|
||||
auto [prevPage, prevNode, prevIndexes] =
|
||||
directory::findPreviousPage(*this, directory, node);
|
||||
if (!gapPages && prevPage != page - 1)
|
||||
gapPages.emplace(prevPage, prevNode, page, node);
|
||||
page = prevPage;
|
||||
node = prevNode;
|
||||
indexes = prevIndexes;
|
||||
}
|
||||
// We looped through all the pages back to the root.
|
||||
if (!page)
|
||||
{
|
||||
// If we found a gap, use it.
|
||||
if (gapPages)
|
||||
{
|
||||
return directory::insertPage(
|
||||
*this,
|
||||
gapPages->page,
|
||||
gapPages->node,
|
||||
gapPages->nextPage,
|
||||
gapPages->next,
|
||||
key,
|
||||
directory,
|
||||
describe);
|
||||
}
|
||||
std::tie(page, node, indexes) = directory::findPreviousPage(*this, directory, root);
|
||||
}
|
||||
}
|
||||
|
||||
// If there's space, we use it:
|
||||
if (indexes.size() < kDirNodeMaxEntries)
|
||||
{
|
||||
|
||||
@@ -129,8 +129,7 @@ selectBranch(SHAMapNodeID const& id, uint256 const& hash)
|
||||
SHAMapNodeID
|
||||
SHAMapNodeID::createID(int depth, uint256 const& key)
|
||||
{
|
||||
XRPL_ASSERT(
|
||||
depth >= 0 && depth <= SHAMap::kLeafDepth, "xrpl::SHAMapNodeID::createID : valid depth");
|
||||
XRPL_ASSERT((depth >= 0) && (depth < 65), "xrpl::SHAMapNodeID::createID : valid branch input");
|
||||
return SHAMapNodeID(depth, key & depthMask(depth));
|
||||
}
|
||||
|
||||
|
||||
@@ -157,7 +157,10 @@ SHAMap::visitDifferences(
|
||||
if ((have == nullptr) || !have->hasInnerNode(childID, childHash))
|
||||
stack.emplace(safeDowncast<SHAMapInnerNode*>(next), childID);
|
||||
}
|
||||
else if ((have == nullptr) || !have->hasLeafNode(leafKey(*next), childHash))
|
||||
else if (
|
||||
(have == nullptr) ||
|
||||
!have->hasLeafNode(
|
||||
safeDowncast<SHAMapLeafNode*>(next)->peekItem()->key(), childHash))
|
||||
{
|
||||
if (!function(*next))
|
||||
return;
|
||||
@@ -411,7 +414,7 @@ SHAMap::getMissingNodes(int max, SHAMapSyncFilter const* filter)
|
||||
bool
|
||||
SHAMap::getNodeFat(
|
||||
SHAMapNodeID const& wanted,
|
||||
std::vector<SHAMapNodeData>& data,
|
||||
std::vector<std::pair<SHAMapNodeID, Blob>>& data,
|
||||
bool fatLeaves,
|
||||
std::uint32_t depth) const
|
||||
{
|
||||
@@ -457,7 +460,7 @@ SHAMap::getNodeFat(
|
||||
// Add this node to the reply
|
||||
s.erase();
|
||||
node->serializeForWire(s);
|
||||
data.emplace_back(nodeID, node->isLeaf(), s.getData());
|
||||
data.emplace_back(nodeID, s.getData());
|
||||
|
||||
if (node->isInner())
|
||||
{
|
||||
@@ -487,7 +490,7 @@ SHAMap::getNodeFat(
|
||||
// Just include this node
|
||||
s.erase();
|
||||
childNode->serializeForWire(s);
|
||||
data.emplace_back(childID, childNode->isLeaf(), s.getData());
|
||||
data.emplace_back(childID, s.getData());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -505,33 +508,25 @@ SHAMap::serializeRoot(Serializer& s) const
|
||||
}
|
||||
|
||||
SHAMapAddNode
|
||||
SHAMap::addRootNode(
|
||||
SHAMapHash const& hash,
|
||||
SHAMapTreeNodePtr rootNode,
|
||||
SHAMapSyncFilter const* filter)
|
||||
SHAMap::addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFilter const* filter)
|
||||
{
|
||||
XRPL_ASSERT(cowid_ >= 1, "xrpl::SHAMap::addRootNode : valid cowid");
|
||||
XRPL_ASSERT(rootNode, "xrpl::SHAMap::addRootNode : non-null root node");
|
||||
|
||||
// we already have a root_ node
|
||||
if (root_->getHash().isNonZero())
|
||||
{
|
||||
JLOG(journal_.trace()) << "Got root node, already have one";
|
||||
XRPL_ASSERT(root_->getHash() == hash, "xrpl::SHAMap::addRootNode : valid hash");
|
||||
JLOG(journal_.trace()) << "got root node, already have one";
|
||||
XRPL_ASSERT(root_->getHash() == hash, "xrpl::SHAMap::addRootNode : valid hash input");
|
||||
return SHAMapAddNode::duplicate();
|
||||
}
|
||||
|
||||
if (rootNode->getHash() != hash)
|
||||
{
|
||||
JLOG(journal_.warn()) << "Corrupt root node received: expected hash " << hash << ", got "
|
||||
<< rootNode->getHash();
|
||||
XRPL_ASSERT(cowid_ >= 1, "xrpl::SHAMap::addRootNode : valid cowid");
|
||||
auto node = SHAMapTreeNode::makeFromWire(rootNode);
|
||||
if (!node || node->getHash() != hash)
|
||||
return SHAMapAddNode::invalid();
|
||||
}
|
||||
|
||||
if (backed_)
|
||||
canonicalize(hash, rootNode);
|
||||
canonicalize(hash, node);
|
||||
|
||||
root_ = std::move(rootNode);
|
||||
root_ = node;
|
||||
|
||||
if (root_->isLeaf())
|
||||
clearSynching();
|
||||
@@ -548,18 +543,9 @@ SHAMap::addRootNode(
|
||||
}
|
||||
|
||||
SHAMapAddNode
|
||||
SHAMap::addKnownNode(
|
||||
SHAMapNodeID const& nodeID,
|
||||
SHAMapTreeNodePtr treeNode,
|
||||
SHAMapSyncFilter const* filter)
|
||||
SHAMap::addKnownNode(SHAMapNodeID const& node, Slice const& rawNode, SHAMapSyncFilter const* filter)
|
||||
{
|
||||
XRPL_ASSERT(!nodeID.isRoot(), "xrpl::SHAMap::addKnownNode : valid node");
|
||||
XRPL_ASSERT(treeNode, "xrpl::SHAMap::addKnownNode : non-null tree node");
|
||||
XRPL_ASSERT(
|
||||
!treeNode->isLeaf() ||
|
||||
SHAMapNodeID::createID(nodeID.getDepth(), leafKey(*treeNode)).getNodeID() ==
|
||||
nodeID.getNodeID(),
|
||||
"xrpl::SHAMap::addKnownNode : leaf position consistent with node ID");
|
||||
XRPL_ASSERT(!node.isRoot(), "xrpl::SHAMap::addKnownNode : valid node input");
|
||||
|
||||
if (!isSynching())
|
||||
{
|
||||
@@ -573,15 +559,14 @@ SHAMap::addKnownNode(
|
||||
|
||||
while (currNode->isInner() &&
|
||||
!safeDowncast<SHAMapInnerNode*>(currNode)->isFullBelow(generation) &&
|
||||
(currNodeID.getDepth() < nodeID.getDepth()))
|
||||
(currNodeID.getDepth() < node.getDepth()))
|
||||
{
|
||||
int const branch = selectBranch(currNodeID, nodeID.getNodeID());
|
||||
int const branch = selectBranch(currNodeID, node.getNodeID());
|
||||
XRPL_ASSERT(branch >= 0, "xrpl::SHAMap::addKnownNode : valid branch");
|
||||
auto inner = safeDowncast<SHAMapInnerNode*>(currNode);
|
||||
if (inner->isEmptyBranch(branch))
|
||||
{
|
||||
JLOG(journal_.warn()) << "Add known node " << nodeID << " for empty branch " << branch
|
||||
<< " at " << currNodeID;
|
||||
JLOG(journal_.warn()) << "Add known node for empty branch" << node;
|
||||
return SHAMapAddNode::invalid();
|
||||
}
|
||||
|
||||
@@ -597,45 +582,67 @@ SHAMap::addKnownNode(
|
||||
if (currNode != nullptr)
|
||||
continue;
|
||||
|
||||
if (childHash != treeNode->getHash())
|
||||
auto newNode = SHAMapTreeNode::makeFromWire(rawNode);
|
||||
|
||||
if (!newNode || childHash != newNode->getHash())
|
||||
{
|
||||
JLOG(journal_.warn()) << "Corrupt node " << nodeID << " received: expected hash "
|
||||
<< childHash << ", got " << treeNode->getHash();
|
||||
JLOG(journal_.warn()) << "Corrupt node received";
|
||||
return SHAMapAddNode::invalid();
|
||||
}
|
||||
|
||||
// In rare cases, a node can still be corrupt even after hash
|
||||
// validation. For leaf nodes, we perform an additional check to
|
||||
// ensure the node's position in the tree is consistent with its
|
||||
// content to prevent inconsistencies that could
|
||||
// propagate further down the line.
|
||||
if (newNode->isLeaf())
|
||||
{
|
||||
auto const& actualKey =
|
||||
safeDowncast<SHAMapLeafNode const*>(newNode.get())->peekItem()->key();
|
||||
|
||||
// Validate that this leaf belongs at the target position
|
||||
auto const expectedNodeID = SHAMapNodeID::createID(node.getDepth(), actualKey);
|
||||
if (expectedNodeID.getNodeID() != node.getNodeID())
|
||||
{
|
||||
JLOG(journal_.debug())
|
||||
<< "Leaf node position mismatch: "
|
||||
<< "expected=" << expectedNodeID.getNodeID() << ", actual=" << node.getNodeID();
|
||||
return SHAMapAddNode::invalid();
|
||||
}
|
||||
}
|
||||
|
||||
// Inner nodes must be at a level strictly less than 64
|
||||
// but leaf nodes (while notionally at level 64) can be
|
||||
// at any depth up to and including 64:
|
||||
if ((currNodeID.getDepth() > kLeafDepth) ||
|
||||
(treeNode->isInner() && currNodeID.getDepth() == kLeafDepth))
|
||||
(newNode->isInner() && currNodeID.getDepth() == kLeafDepth))
|
||||
{
|
||||
// Map is provably invalid
|
||||
state_ = SHAMapState::Invalid;
|
||||
return SHAMapAddNode::useful();
|
||||
}
|
||||
|
||||
if (currNodeID != nodeID)
|
||||
if (currNodeID != node)
|
||||
{
|
||||
// Either this node is broken or we didn't request it (yet)
|
||||
JLOG(journal_.warn()) << "unable to hook node " << nodeID;
|
||||
JLOG(journal_.warn()) << "unable to hook node " << node;
|
||||
JLOG(journal_.info()) << " stuck at " << currNodeID;
|
||||
JLOG(journal_.info()) << "got depth=" << nodeID.getDepth()
|
||||
JLOG(journal_.info()) << "got depth=" << node.getDepth()
|
||||
<< ", walked to= " << currNodeID.getDepth();
|
||||
return SHAMapAddNode::useful();
|
||||
}
|
||||
|
||||
if (backed_)
|
||||
canonicalize(childHash, treeNode);
|
||||
canonicalize(childHash, newNode);
|
||||
|
||||
treeNode = prevNode->canonicalizeChild(branch, std::move(treeNode));
|
||||
newNode = prevNode->canonicalizeChild(branch, std::move(newNode));
|
||||
|
||||
if (filter != nullptr)
|
||||
{
|
||||
Serializer s;
|
||||
treeNode->serializeWithPrefix(s);
|
||||
newNode->serializeWithPrefix(s);
|
||||
filter->gotNode(
|
||||
false, childHash, ledgerSeq_, std::move(s.modData()), treeNode->getType());
|
||||
false, childHash, ledgerSeq_, std::move(s.modData()), newNode->getType());
|
||||
}
|
||||
|
||||
return SHAMapAddNode::useful();
|
||||
|
||||
@@ -1,260 +0,0 @@
|
||||
#include <xrpld/app/ledger/LedgerNodeHelpers.h>
|
||||
|
||||
#include <xrpl/basics/IntrusivePointer.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/beast/unit_test/suite.h>
|
||||
#include <xrpl/protocol/Serializer.h>
|
||||
#include <xrpl/shamap/SHAMap.h>
|
||||
#include <xrpl/shamap/SHAMapAccountStateLeafNode.h>
|
||||
#include <xrpl/shamap/SHAMapInnerNode.h>
|
||||
#include <xrpl/shamap/SHAMapItem.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <boost/smart_ptr/intrusive_ptr.hpp>
|
||||
|
||||
#include <xrpl.pb.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
namespace xrpl::tests {
|
||||
|
||||
class LedgerNodeHelpers_test : public beast::unit_test::Suite
|
||||
{
|
||||
static boost::intrusive_ptr<SHAMapItem>
|
||||
makeTestItem(std::uint32_t seed)
|
||||
{
|
||||
Serializer s;
|
||||
s.add32(seed);
|
||||
s.add32(seed + 1);
|
||||
s.add32(seed + 2);
|
||||
return makeShamapitem(s.getSHA512Half(), s.slice());
|
||||
}
|
||||
|
||||
static std::string
|
||||
serializeNode(SHAMapTreeNodePtr const& node)
|
||||
{
|
||||
Serializer s;
|
||||
node->serializeForWire(s);
|
||||
auto const slice = s.slice();
|
||||
return std::string(slice.begin(), slice.end());
|
||||
}
|
||||
|
||||
void
|
||||
testGetTreeNode()
|
||||
{
|
||||
testcase("getTreeNode");
|
||||
|
||||
// Valid: inner node. It must have at least one child for `serializeNode` to work.
|
||||
{
|
||||
auto const innerNode = intr_ptr::makeShared<SHAMapInnerNode>(1);
|
||||
auto const childNode = intr_ptr::makeShared<SHAMapInnerNode>(1);
|
||||
innerNode->setChild(0, childNode);
|
||||
auto const innerData = serializeNode(innerNode);
|
||||
auto const result = getTreeNode(innerData);
|
||||
BEAST_EXPECT(result && result->isInner());
|
||||
}
|
||||
|
||||
// Valid: leaf node.
|
||||
{
|
||||
auto const leafItem = makeTestItem(12345);
|
||||
auto const leafNode = intr_ptr::makeShared<SHAMapAccountStateLeafNode>(leafItem, 1);
|
||||
auto const leafData = serializeNode(leafNode);
|
||||
auto const result = getTreeNode(leafData);
|
||||
BEAST_EXPECT(result && result->isLeaf());
|
||||
}
|
||||
|
||||
// Invalid: empty data.
|
||||
{
|
||||
auto const result = getTreeNode("");
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
|
||||
// Invalid: garbage data.
|
||||
{
|
||||
auto const result = getTreeNode("invalid");
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
|
||||
// Invalid: truncated data.
|
||||
{
|
||||
auto const leafItem = makeTestItem(54321);
|
||||
auto const leafNode = intr_ptr::makeShared<SHAMapAccountStateLeafNode>(leafItem, 1);
|
||||
// Truncate the data to trigger an exception in SHAMapTreeNode::makeAccountState when
|
||||
// the data is used to deserialize the node.
|
||||
uint256 const tag;
|
||||
auto const leafData = serializeNode(leafNode).substr(0, tag.kBytes - 1);
|
||||
auto const result = getTreeNode(leafData);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testGetSHAMapNodeID()
|
||||
{
|
||||
testcase("getSHAMapNodeID");
|
||||
|
||||
{
|
||||
// Tests using inner nodes at various depths.
|
||||
auto const innerNode = intr_ptr::makeShared<SHAMapInnerNode>(1);
|
||||
auto const childNode = intr_ptr::makeShared<SHAMapInnerNode>(1);
|
||||
innerNode->setChild(0, childNode);
|
||||
auto const innerData = serializeNode(innerNode);
|
||||
|
||||
// Valid: legacy `nodeid` field at arbitrary depth.
|
||||
{
|
||||
auto const innerDepth = 3;
|
||||
auto const innerID = SHAMapNodeID::createID(innerDepth, uint256{});
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(innerData);
|
||||
ledgerNode.set_nodeid(innerID.getRawString());
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *innerNode);
|
||||
BEAST_EXPECT(result == innerID);
|
||||
}
|
||||
|
||||
// Valid: new `id` field at minimum depth.
|
||||
{
|
||||
auto const innerDepth = 0;
|
||||
auto const innerID = SHAMapNodeID::createID(innerDepth, uint256{});
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(innerData);
|
||||
ledgerNode.set_id(innerID.getRawString());
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *innerNode);
|
||||
BEAST_EXPECT(result == innerID);
|
||||
}
|
||||
|
||||
// Invalid: new `depth` field should not be used for inner nodes.
|
||||
{
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(innerData);
|
||||
ledgerNode.set_depth(10);
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *innerNode);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
|
||||
// Invalid: both legacy `nodeid` and new `id` fields set for an inner node.
|
||||
{
|
||||
auto const innerDepth = 9;
|
||||
auto const innerID = SHAMapNodeID::createID(innerDepth, uint256{});
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(innerData);
|
||||
ledgerNode.set_nodeid(innerID.getRawString());
|
||||
ledgerNode.set_id(innerID.getRawString());
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *innerNode);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Tests using leaf nodes at various depths.
|
||||
auto const leafItem = makeTestItem(12345);
|
||||
auto const leafNode = intr_ptr::makeShared<SHAMapAccountStateLeafNode>(leafItem, 1);
|
||||
auto const leafData = serializeNode(leafNode);
|
||||
auto const leafKey = leafItem->key();
|
||||
|
||||
// Valid: legacy `nodeid` field at arbitrary depth.
|
||||
{
|
||||
auto const kLeafDepth = 5;
|
||||
auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey);
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(leafData);
|
||||
ledgerNode.set_nodeid(leafID.getRawString());
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *leafNode);
|
||||
BEAST_EXPECT(result == leafID);
|
||||
}
|
||||
|
||||
// Invalid: new `id` field should not be used for leaf nodes.
|
||||
{
|
||||
auto const kLeafDepth = 5;
|
||||
auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey);
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(leafData);
|
||||
ledgerNode.set_id(leafID.getRawString());
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *leafNode);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
|
||||
// Valid: new `depth` field at minimum depth.
|
||||
{
|
||||
auto const kLeafDepth = 0;
|
||||
auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey);
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(leafData);
|
||||
ledgerNode.set_depth(kLeafDepth);
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *leafNode);
|
||||
BEAST_EXPECT(result == leafID);
|
||||
}
|
||||
|
||||
// Valid: new `depth` field at arbitrary depth between minimum and maximum.
|
||||
{
|
||||
auto const kLeafDepth = 10;
|
||||
auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey);
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(leafData);
|
||||
ledgerNode.set_depth(kLeafDepth);
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *leafNode);
|
||||
BEAST_EXPECT(result == leafID);
|
||||
}
|
||||
|
||||
// Valid: new `depth` field at maximum depth.
|
||||
// Note that we do not test a depth greater than the maximum depth, because the proto
|
||||
// message is assumed to have been validated by the time the getSHAMapNodeID function is
|
||||
// called.
|
||||
{
|
||||
auto const kLeafDepth = SHAMap::kLeafDepth;
|
||||
auto const leafID = SHAMapNodeID::createID(kLeafDepth, leafKey);
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(leafData);
|
||||
ledgerNode.set_depth(kLeafDepth);
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *leafNode);
|
||||
BEAST_EXPECT(result == leafID);
|
||||
}
|
||||
|
||||
// Invalid: legacy `nodeid` field where the node ID is inconsistent with the key.
|
||||
{
|
||||
auto const otherItem = makeTestItem(54321);
|
||||
auto const otherNode =
|
||||
intr_ptr::makeShared<SHAMapAccountStateLeafNode>(otherItem, 1);
|
||||
auto const otherData = serializeNode(otherNode);
|
||||
auto const otherKey = otherItem->key();
|
||||
auto const otherDepth = 1;
|
||||
auto const otherID = SHAMapNodeID::createID(otherDepth, otherKey);
|
||||
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata(otherData);
|
||||
ledgerNode.set_nodeid(otherID.getRawString());
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *leafNode);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
}
|
||||
|
||||
// Invalid: no field set.
|
||||
{
|
||||
auto const innerNode = intr_ptr::makeShared<SHAMapInnerNode>(1);
|
||||
protocol::TMLedgerNode ledgerNode;
|
||||
ledgerNode.set_nodedata("test_data");
|
||||
auto const result = getSHAMapNodeID(ledgerNode, *innerNode);
|
||||
BEAST_EXPECT(!result);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testGetTreeNode();
|
||||
testGetSHAMapNodeID();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(LedgerNodeHelpers, app, xrpl);
|
||||
|
||||
} // namespace xrpl::tests
|
||||
@@ -63,8 +63,8 @@ public:
|
||||
negotiateProtocolVersion("RTXP/1.2, XRPL/2.0, XRPL/2.1") == makeProtocol(2, 1));
|
||||
BEAST_EXPECT(negotiateProtocolVersion("XRPL/2.2") == makeProtocol(2, 2));
|
||||
BEAST_EXPECT(
|
||||
negotiateProtocolVersion("RTXP/1.2, XRPL/2.3, XRPL/2.4, XRPL/999.999") ==
|
||||
makeProtocol(2, 3));
|
||||
negotiateProtocolVersion("RTXP/1.2, XRPL/2.2, XRPL/2.3, XRPL/999.999") ==
|
||||
makeProtocol(2, 2));
|
||||
BEAST_EXPECT(negotiateProtocolVersion("XRPL/999.999, WebSocket/1.0") == std::nullopt);
|
||||
BEAST_EXPECT(negotiateProtocolVersion("") == std::nullopt);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <xrpl/basics/Blob.h>
|
||||
#include <xrpl/basics/SHAMapHash.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
@@ -111,16 +112,14 @@ TEST_F(SHAMapSyncTest, sync)
|
||||
destination.setSynching();
|
||||
|
||||
{
|
||||
std::vector<SHAMapNodeData> a;
|
||||
std::vector<std::pair<SHAMapNodeID, Blob>> a;
|
||||
|
||||
ASSERT_TRUE(source.getNodeFat(SHAMapNodeID(), a, randBool(eng_), randInt(eng_, 2)));
|
||||
|
||||
ASSERT_FALSE(a.empty()) << "NodeSize";
|
||||
|
||||
auto node = SHAMapTreeNode::makeFromWire(makeSlice(a[0].data));
|
||||
if (!node)
|
||||
FAIL() << "Could not create node";
|
||||
ASSERT_TRUE(destination.addRootNode(source.getHash(), std::move(node), nullptr).isGood());
|
||||
ASSERT_TRUE(
|
||||
destination.addRootNode(source.getHash(), makeSlice(a[0].second), nullptr).isGood());
|
||||
}
|
||||
|
||||
do
|
||||
@@ -134,7 +133,7 @@ TEST_F(SHAMapSyncTest, sync)
|
||||
break;
|
||||
|
||||
// get as many nodes as possible based on this information
|
||||
std::vector<SHAMapNodeData> b;
|
||||
std::vector<std::pair<SHAMapNodeID, Blob>> b;
|
||||
|
||||
for (auto& it : nodesMissing)
|
||||
{
|
||||
@@ -156,12 +155,7 @@ TEST_F(SHAMapSyncTest, sync)
|
||||
// Keep failures fatal here because this loop is data-dependent.
|
||||
// non-deterministic number of times and the number of tests run
|
||||
// should be deterministic
|
||||
auto node = SHAMapTreeNode::makeFromWire(makeSlice(i.data));
|
||||
if (!node)
|
||||
FAIL() << "Could not create node";
|
||||
if (i.isLeaf != node->isLeaf())
|
||||
FAIL() << "Node is not a leaf";
|
||||
if (!destination.addKnownNode(i.nodeID, std::move(node), nullptr).isUseful())
|
||||
if (!destination.addKnownNode(i.first, makeSlice(i.second), nullptr).isUseful())
|
||||
FAIL() << "Known node was not useful";
|
||||
}
|
||||
} while (true);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <xrpld/overlay/PeerSet.h>
|
||||
|
||||
#include <xrpl/basics/CountedObject.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/beast/clock/abstract_clock.h>
|
||||
#include <xrpl/json/json_value.h>
|
||||
@@ -23,7 +24,7 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <set>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@@ -153,19 +154,16 @@ private:
|
||||
processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData const& data);
|
||||
|
||||
bool
|
||||
takeHeader(std::string_view data);
|
||||
takeHeader(std::string const& data);
|
||||
|
||||
void
|
||||
receiveNode(
|
||||
std::shared_ptr<Peer> const& peer,
|
||||
protocol::TMLedgerData const& packet,
|
||||
SHAMapAddNode& san);
|
||||
receiveNode(protocol::TMLedgerData const& packet, SHAMapAddNode&);
|
||||
|
||||
bool
|
||||
takeTxRootNode(std::string_view data, SHAMapAddNode& san);
|
||||
takeTxRootNode(Slice const& data, SHAMapAddNode&);
|
||||
|
||||
bool
|
||||
takeAsRootNode(std::string_view data, SHAMapAddNode& san);
|
||||
takeAsRootNode(Slice const& data, SHAMapAddNode&);
|
||||
|
||||
std::vector<uint256>
|
||||
neededTxHashes(int max, SHAMapSyncFilter const* filter) const;
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <xrpl/shamap/SHAMapNodeID.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
|
||||
namespace protocol {
|
||||
class TMLedgerNode;
|
||||
} // namespace protocol
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
/**
|
||||
* @brief Deserializes a SHAMapTreeNode from wire format data.
|
||||
*
|
||||
* This function attempts to create a SHAMapTreeNode from the provided data string. If the data is
|
||||
* malformed or deserialization fails, the function returns a nullptr instead of throwing an
|
||||
* exception.
|
||||
*
|
||||
* @param data The serialized node data in wire format.
|
||||
* @return The deserialized tree node if successful, or a nullptr if deserialization fails.
|
||||
*/
|
||||
[[nodiscard]] SHAMapTreeNodePtr
|
||||
getTreeNode(std::string_view data);
|
||||
|
||||
/**
|
||||
* @brief Extracts or reconstructs the SHAMapNodeID from a ledger node proto message.
|
||||
*
|
||||
* This function retrieves the SHAMapNodeID for a tree node, with behavior that depends on which
|
||||
* field is set and the node type (inner vs. leaf).
|
||||
*
|
||||
* When the legacy `nodeid` field is set in the message:
|
||||
* - For all nodes: Deserializes the node ID from the field.
|
||||
* - For leaf nodes: Validates that the node ID is consistent with the leaf's key.
|
||||
*
|
||||
* When the new `id` or `depth` field is set in the message:
|
||||
* - For inner nodes: Deserializes the node ID from the `id` field.
|
||||
* - For leaf nodes: Reconstructs the node ID using both the depth from the `depth` field and the
|
||||
* key from the leaf node's item.
|
||||
* Note that root nodes may be inner nodes or leaf nodes.
|
||||
*
|
||||
* @param ledgerNode The validated protocol message containing the ledger node data.
|
||||
* @param treeNode The deserialized tree node (inner or leaf node).
|
||||
* @return An optional containing the node ID if extraction/reconstruction succeeds, or std::nullopt
|
||||
* if the required fields are missing or validation fails.
|
||||
*/
|
||||
[[nodiscard]] std::optional<SHAMapNodeID>
|
||||
getSHAMapNodeID(protocol::TMLedgerNode const& ledgerNode, SHAMapTreeNode const& treeNode);
|
||||
|
||||
} // namespace xrpl
|
||||
@@ -3,7 +3,6 @@
|
||||
#include <xrpld/app/ledger/AccountStateSF.h>
|
||||
#include <xrpld/app/ledger/InboundLedgers.h>
|
||||
#include <xrpld/app/ledger/LedgerMaster.h>
|
||||
#include <xrpld/app/ledger/LedgerNodeHelpers.h>
|
||||
#include <xrpld/app/ledger/TransactionStateSF.h>
|
||||
#include <xrpld/app/ledger/detail/TimeoutCounter.h>
|
||||
#include <xrpld/app/main/Application.h>
|
||||
@@ -45,8 +44,8 @@
|
||||
#include <mutex>
|
||||
#include <random>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
@@ -780,7 +779,7 @@ InboundLedger::filterNodes(
|
||||
*/
|
||||
// data must not have hash prefix
|
||||
bool
|
||||
InboundLedger::takeHeader(std::string_view data)
|
||||
InboundLedger::takeHeader(std::string const& data)
|
||||
{
|
||||
// Return value: true=normal, false=bad data
|
||||
JLOG(journal_.trace()) << "got header acquiring ledger " << hash_;
|
||||
@@ -826,10 +825,7 @@ InboundLedger::takeHeader(std::string_view data)
|
||||
* Call with a lock
|
||||
*/
|
||||
void
|
||||
InboundLedger::receiveNode(
|
||||
std::shared_ptr<Peer> const& peer,
|
||||
protocol::TMLedgerData const& packet,
|
||||
SHAMapAddNode& san)
|
||||
InboundLedger::receiveNode(protocol::TMLedgerData const& packet, SHAMapAddNode& san)
|
||||
{
|
||||
if (!haveHeader_)
|
||||
{
|
||||
@@ -872,51 +868,32 @@ InboundLedger::receiveNode(
|
||||
{
|
||||
auto const f = filter.get();
|
||||
|
||||
for (auto const& ledgerNode : packet.nodes())
|
||||
for (auto const& node : packet.nodes())
|
||||
{
|
||||
auto treeNode = getTreeNode(ledgerNode.nodedata());
|
||||
if (!treeNode)
|
||||
{
|
||||
JLOG(journal_.warn())
|
||||
<< "Got invalid node data for ledger " << hash_ << " from peer " << peer->id();
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_node.node_data invalid");
|
||||
san.incInvalid();
|
||||
return;
|
||||
}
|
||||
auto const nodeID = deserializeSHAMapNodeID(node.nodeid());
|
||||
|
||||
auto const nodeID = getSHAMapNodeID(ledgerNode, *treeNode);
|
||||
if (!nodeID)
|
||||
{
|
||||
JLOG(journal_.warn())
|
||||
<< "Got invalid node id for ledger " << hash_ << " from peer " << peer->id();
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_node.node_id invalid");
|
||||
san.incInvalid();
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("data does not properly deserialize");
|
||||
|
||||
if (nodeID->isRoot())
|
||||
{
|
||||
san += map.addRootNode(rootHash, std::move(treeNode), f);
|
||||
san += map.addRootNode(rootHash, makeSlice(node.nodedata()), f);
|
||||
}
|
||||
else
|
||||
{
|
||||
san += map.addKnownNode(*nodeID, std::move(treeNode), f);
|
||||
san += map.addKnownNode(*nodeID, makeSlice(node.nodedata()), f);
|
||||
}
|
||||
|
||||
if (!san.isGood())
|
||||
{
|
||||
JLOG(journal_.warn()) << "Got invalid node " << *nodeID << " for ledger " << hash_
|
||||
<< " from peer " << peer->id();
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_node invalid");
|
||||
JLOG(journal_.warn()) << "Received bad node data";
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
// If we get here it is not necessarily because the node was bad, so don't charge the peer.
|
||||
JLOG(journal_.error()) << "Could not process node for ledger " << hash_ << " from peer "
|
||||
<< peer->id() << ": " << e.what();
|
||||
JLOG(journal_.error()) << "Received bad node data: " << e.what();
|
||||
san.incInvalid();
|
||||
return;
|
||||
}
|
||||
@@ -945,7 +922,7 @@ InboundLedger::receiveNode(
|
||||
* Call with a lock
|
||||
*/
|
||||
bool
|
||||
InboundLedger::takeAsRootNode(std::string_view data, SHAMapAddNode& san)
|
||||
InboundLedger::takeAsRootNode(Slice const& data, SHAMapAddNode& san)
|
||||
{
|
||||
if (failed_ || haveState_)
|
||||
{
|
||||
@@ -961,17 +938,9 @@ InboundLedger::takeAsRootNode(std::string_view data, SHAMapAddNode& san)
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
auto treeNode = getTreeNode(data);
|
||||
if (!treeNode)
|
||||
{
|
||||
JLOG(journal_.warn()) << "Got invalid AS root node data for ledger " << hash_;
|
||||
san.incInvalid();
|
||||
return false;
|
||||
}
|
||||
|
||||
AccountStateSF filter(ledger_->stateMap().family().db(), app_.getLedgerMaster());
|
||||
san += ledger_->stateMap().addRootNode(
|
||||
SHAMapHash{ledger_->header().accountHash}, std::move(treeNode), &filter);
|
||||
san +=
|
||||
ledger_->stateMap().addRootNode(SHAMapHash{ledger_->header().accountHash}, data, &filter);
|
||||
return san.isGood();
|
||||
}
|
||||
|
||||
@@ -980,7 +949,7 @@ InboundLedger::takeAsRootNode(std::string_view data, SHAMapAddNode& san)
|
||||
* Call with a lock
|
||||
*/
|
||||
bool
|
||||
InboundLedger::takeTxRootNode(std::string_view data, SHAMapAddNode& san)
|
||||
InboundLedger::takeTxRootNode(Slice const& data, SHAMapAddNode& san)
|
||||
{
|
||||
if (failed_ || haveTransactions_)
|
||||
{
|
||||
@@ -996,17 +965,8 @@ InboundLedger::takeTxRootNode(std::string_view data, SHAMapAddNode& san)
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
auto treeNode = getTreeNode(data);
|
||||
if (!treeNode)
|
||||
{
|
||||
JLOG(journal_.warn()) << "Got invalid TX root node data for ledger " << hash_;
|
||||
san.incInvalid();
|
||||
return false;
|
||||
}
|
||||
|
||||
TransactionStateSF filter(ledger_->txMap().family().db(), app_.getLedgerMaster());
|
||||
san += ledger_->txMap().addRootNode(
|
||||
SHAMapHash{ledger_->header().txHash}, std::move(treeNode), &filter);
|
||||
san += ledger_->txMap().addRootNode(SHAMapHash{ledger_->header().txHash}, data, &filter);
|
||||
return san.isGood();
|
||||
}
|
||||
|
||||
@@ -1105,33 +1065,20 @@ InboundLedger::processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData co
|
||||
}
|
||||
|
||||
if (!haveState_ && (packet.nodes().size() > 1) &&
|
||||
!takeAsRootNode(packet.nodes(1).nodedata(), san))
|
||||
!takeAsRootNode(makeSlice(packet.nodes(1).nodedata()), san))
|
||||
{
|
||||
JLOG(journal_.warn()) << "Included AS root invalid for ledger " << hash_
|
||||
<< " from peer " << peer->id();
|
||||
if (san.isInvalid())
|
||||
{
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_data invalid AS root");
|
||||
return -1;
|
||||
}
|
||||
JLOG(journal_.warn()) << "Included AS root invalid";
|
||||
}
|
||||
|
||||
if (!haveTransactions_ && (packet.nodes().size() > 2) &&
|
||||
!takeTxRootNode(packet.nodes(2).nodedata(), san))
|
||||
!takeTxRootNode(makeSlice(packet.nodes(2).nodedata()), san))
|
||||
{
|
||||
JLOG(journal_.warn()) << "Included TX root invalid for ledger " << hash_
|
||||
<< " from peer " << peer->id();
|
||||
if (san.isInvalid())
|
||||
{
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_data invalid TX root");
|
||||
return -1;
|
||||
}
|
||||
JLOG(journal_.warn()) << "Included TX root invalid";
|
||||
}
|
||||
}
|
||||
catch (std::exception const& ex)
|
||||
{
|
||||
JLOG(journal_.warn()) << "Included AS/TX root invalid for ledger " << hash_
|
||||
<< " from peer " << peer->id() << ": " << ex.what();
|
||||
JLOG(journal_.warn()) << "Included AS/TX root invalid: " << ex.what();
|
||||
using namespace std::string_literals;
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_data "s + ex.what());
|
||||
return -1;
|
||||
@@ -1155,18 +1102,24 @@ InboundLedger::processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData co
|
||||
|
||||
ScopedLockType const sl(mtx_);
|
||||
|
||||
// Verify node IDs and data are complete
|
||||
for (auto const& node : packet.nodes())
|
||||
{
|
||||
if (!node.has_nodeid() || !node.has_nodedata())
|
||||
{
|
||||
JLOG(journal_.warn()) << "Got bad node";
|
||||
peer->charge(Resource::kFeeMalformedRequest, "ledger_data bad node");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
SHAMapAddNode san;
|
||||
receiveNode(peer, packet, san);
|
||||
receiveNode(packet, san);
|
||||
|
||||
JLOG(journal_.debug()) << "Ledger "
|
||||
<< ((packet.type() == protocol::liTX_NODE) ? "TX" : "AS")
|
||||
<< " node stats: " << san.get();
|
||||
|
||||
// Note: Peer charges for invalid/malformed data are issued from within receiveNode at the
|
||||
// exact failure site, so the peer is only charged for problems they are responsible for.
|
||||
if (san.isInvalid())
|
||||
return -1;
|
||||
|
||||
if (san.isUseful())
|
||||
progress_ = true;
|
||||
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
#include <xrpld/app/ledger/InboundLedger.h>
|
||||
#include <xrpld/app/ledger/LedgerMaster.h>
|
||||
#include <xrpld/app/ledger/LedgerNodeHelpers.h>
|
||||
#include <xrpld/app/main/Application.h>
|
||||
#include <xrpld/overlay/PeerSet.h>
|
||||
|
||||
#include <xrpl/basics/Blob.h>
|
||||
#include <xrpl/basics/DecayingSample.h>
|
||||
#include <xrpl/basics/Log.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/UnorderedContainers.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/basics/scope.h>
|
||||
@@ -252,17 +252,23 @@ public:
|
||||
Serializer s;
|
||||
try
|
||||
{
|
||||
for (auto const& ledgerNode : packetPtr->nodes())
|
||||
for (int i = 0; i < packetPtr->nodes().size(); ++i)
|
||||
{
|
||||
auto const treeNode = getTreeNode(ledgerNode.nodedata());
|
||||
if (!treeNode)
|
||||
auto const& node = packetPtr->nodes(i);
|
||||
|
||||
if (!node.has_nodeid() || !node.has_nodedata())
|
||||
return;
|
||||
|
||||
auto newNode = SHAMapTreeNode::makeFromWire(makeSlice(node.nodedata()));
|
||||
|
||||
if (!newNode)
|
||||
return;
|
||||
|
||||
s.erase();
|
||||
treeNode->serializeWithPrefix(s);
|
||||
newNode->serializeWithPrefix(s);
|
||||
|
||||
app_.getLedgerMaster().addFetchPack(
|
||||
treeNode->getHash().asUInt256(), std::make_shared<Blob>(s.begin(), s.end()));
|
||||
newNode->getHash().asUInt256(), std::make_shared<Blob>(s.begin(), s.end()));
|
||||
}
|
||||
}
|
||||
catch (std::exception const&) // NOLINT(bugprone-empty-catch)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
#include <xrpld/app/ledger/InboundTransactions.h>
|
||||
|
||||
#include <xrpld/app/ledger/LedgerNodeHelpers.h>
|
||||
#include <xrpld/app/ledger/detail/TransactionAcquire.h>
|
||||
#include <xrpld/app/main/Application.h>
|
||||
#include <xrpld/overlay/PeerSet.h>
|
||||
|
||||
#include <xrpl/basics/Log.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/UnorderedContainers.h>
|
||||
#include <xrpl/beast/insight/Collector.h>
|
||||
#include <xrpl/protocol/RippleLedgerHash.h>
|
||||
@@ -14,7 +14,6 @@
|
||||
#include <xrpl/shamap/SHAMap.h>
|
||||
#include <xrpl/shamap/SHAMapMissingNode.h>
|
||||
#include <xrpl/shamap/SHAMapNodeID.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <xrpl.pb.h>
|
||||
|
||||
@@ -138,45 +137,34 @@ public:
|
||||
|
||||
if (ta == nullptr)
|
||||
{
|
||||
peer->charge(Resource::kFeeUselessData, "ledger_data useless");
|
||||
peer->charge(Resource::kFeeUselessData, "ledger_data");
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<std::pair<SHAMapNodeID, SHAMapTreeNodePtr>> data;
|
||||
std::vector<std::pair<SHAMapNodeID, Slice>> data;
|
||||
data.reserve(packet.nodes().size());
|
||||
|
||||
for (auto const& ledgerNode : packet.nodes())
|
||||
for (auto const& node : packet.nodes())
|
||||
{
|
||||
auto treeNode = getTreeNode(ledgerNode.nodedata());
|
||||
if (!treeNode)
|
||||
if (!node.has_nodeid() || !node.has_nodedata())
|
||||
{
|
||||
JLOG(j_.warn()) << "Got invalid node data for TX set " << hash << " from peer "
|
||||
<< peer->id();
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_node.node_data invalid");
|
||||
peer->charge(Resource::kFeeMalformedRequest, "ledger_data");
|
||||
return;
|
||||
}
|
||||
|
||||
auto const nodeID = getSHAMapNodeID(ledgerNode, *treeNode);
|
||||
if (!nodeID)
|
||||
auto const id = deserializeSHAMapNodeID(node.nodeid());
|
||||
|
||||
if (!id)
|
||||
{
|
||||
JLOG(j_.warn()) << "Got invalid node id for TX set " << hash << " from peer "
|
||||
<< peer->id();
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_node.node_id invalid");
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_data");
|
||||
return;
|
||||
}
|
||||
|
||||
data.emplace_back(*nodeID, std::move(treeNode));
|
||||
data.emplace_back(*id, makeSlice(node.nodedata()));
|
||||
}
|
||||
|
||||
auto const san = ta->takeNodes(std::move(data), peer);
|
||||
if (san.isInvalid())
|
||||
{
|
||||
peer->charge(Resource::kFeeInvalidData, "ledger_data invalid");
|
||||
}
|
||||
else if (!san.isUseful())
|
||||
{
|
||||
peer->charge(Resource::kFeeUselessData, "ledger_data useless");
|
||||
}
|
||||
if (!ta->takeNodes(data, peer).isUseful())
|
||||
peer->charge(Resource::kFeeUselessData, "ledger_data not useful");
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
#include <xrpld/app/ledger/LedgerNodeHelpers.h>
|
||||
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
#include <xrpl/shamap/SHAMap.h>
|
||||
#include <xrpl/shamap/SHAMapLeafNode.h>
|
||||
#include <xrpl/shamap/SHAMapNodeID.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <xrpl.pb.h>
|
||||
|
||||
#include <exception>
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
SHAMapTreeNodePtr
|
||||
getTreeNode(std::string_view data)
|
||||
{
|
||||
auto const slice = makeSlice(data);
|
||||
try
|
||||
{
|
||||
return SHAMapTreeNode::makeFromWire(slice);
|
||||
}
|
||||
catch (std::exception const&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<SHAMapNodeID>
|
||||
getSHAMapNodeID(protocol::TMLedgerNode const& ledgerNode, SHAMapTreeNode const& treeNode)
|
||||
{
|
||||
if (ledgerNode.has_id() || ledgerNode.has_depth())
|
||||
{
|
||||
// Reject ambiguous messages that mix the legacy and new reference fields.
|
||||
if (ledgerNode.has_nodeid())
|
||||
return std::nullopt;
|
||||
|
||||
if (treeNode.isInner())
|
||||
{
|
||||
if (!ledgerNode.has_id())
|
||||
return std::nullopt;
|
||||
|
||||
return deserializeSHAMapNodeID(ledgerNode.id());
|
||||
}
|
||||
|
||||
if (treeNode.isLeaf())
|
||||
{
|
||||
if (!ledgerNode.has_depth() || ledgerNode.depth() > SHAMap::kLeafDepth)
|
||||
return std::nullopt;
|
||||
|
||||
auto const key = leafKey(treeNode);
|
||||
return SHAMapNodeID::createID(ledgerNode.depth(), key);
|
||||
}
|
||||
// LCOV_EXCL_START
|
||||
UNREACHABLE("xrpl::getSHAMapNodeID : tree node is neither inner nor leaf");
|
||||
return std::nullopt;
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
if (!ledgerNode.has_nodeid())
|
||||
return std::nullopt;
|
||||
|
||||
auto nodeID = deserializeSHAMapNodeID(ledgerNode.nodeid());
|
||||
if (!nodeID.has_value())
|
||||
return std::nullopt;
|
||||
|
||||
if (treeNode.isLeaf())
|
||||
{
|
||||
auto const key = leafKey(treeNode);
|
||||
auto const expectedID = SHAMapNodeID::createID(static_cast<int>(nodeID->getDepth()), key);
|
||||
if (nodeID->getNodeID() != expectedID.getNodeID())
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return nodeID;
|
||||
}
|
||||
|
||||
} // namespace xrpl
|
||||
@@ -7,13 +7,13 @@
|
||||
#include <xrpld/overlay/PeerSet.h>
|
||||
|
||||
#include <xrpl/basics/Log.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/core/Job.h>
|
||||
#include <xrpl/server/NetworkOPs.h>
|
||||
#include <xrpl/shamap/SHAMap.h>
|
||||
#include <xrpl/shamap/SHAMapAddNode.h>
|
||||
#include <xrpl/shamap/SHAMapMissingNode.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <xrpl.pb.h>
|
||||
|
||||
@@ -171,7 +171,7 @@ TransactionAcquire::trigger(std::shared_ptr<Peer> const& peer)
|
||||
|
||||
SHAMapAddNode
|
||||
TransactionAcquire::takeNodes(
|
||||
std::vector<std::pair<SHAMapNodeID, SHAMapTreeNodePtr>> data,
|
||||
std::vector<std::pair<SHAMapNodeID, Slice>> const& data,
|
||||
std::shared_ptr<Peer> const& peer)
|
||||
{
|
||||
ScopedLockType const sl(mtx_);
|
||||
@@ -195,7 +195,7 @@ TransactionAcquire::takeNodes(
|
||||
|
||||
ConsensusTransSetSF sf(app_, app_.getTempNodeCache());
|
||||
|
||||
for (auto& d : data)
|
||||
for (auto const& d : data)
|
||||
{
|
||||
if (d.first.isRoot())
|
||||
{
|
||||
@@ -203,22 +203,18 @@ TransactionAcquire::takeNodes(
|
||||
{
|
||||
JLOG(journal_.debug()) << "Got root TXS node, already have it";
|
||||
}
|
||||
else if (!map_->addRootNode(SHAMapHash{hash_}, std::move(d.second), nullptr)
|
||||
.isGood())
|
||||
else if (!map_->addRootNode(SHAMapHash{hash_}, d.second, nullptr).isGood())
|
||||
{
|
||||
JLOG(journal_.warn()) << "TX acquire got bad root node for TX set " << hash_
|
||||
<< " from peer " << peer->id();
|
||||
return SHAMapAddNode::invalid();
|
||||
JLOG(journal_.warn()) << "TX acquire got bad root node";
|
||||
}
|
||||
else
|
||||
{
|
||||
haveRoot_ = true;
|
||||
}
|
||||
}
|
||||
else if (!map_->addKnownNode(d.first, std::move(d.second), &sf).isGood())
|
||||
else if (!map_->addKnownNode(d.first, d.second, &sf).isGood())
|
||||
{
|
||||
JLOG(journal_.warn()) << "TX acquire got bad non-root node " << d.first
|
||||
<< " for TX set " << hash_ << " from peer " << peer->id();
|
||||
JLOG(journal_.warn()) << "TX acquire got bad non-root node";
|
||||
return SHAMapAddNode::invalid();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
#include <xrpld/overlay/PeerSet.h>
|
||||
|
||||
#include <xrpl/basics/CountedObject.h>
|
||||
#include <xrpl/basics/Slice.h>
|
||||
#include <xrpl/basics/base_uint.h>
|
||||
#include <xrpl/shamap/SHAMap.h>
|
||||
#include <xrpl/shamap/SHAMapAddNode.h>
|
||||
#include <xrpl/shamap/SHAMapTreeNode.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
@@ -32,8 +32,8 @@ public:
|
||||
|
||||
SHAMapAddNode
|
||||
takeNodes(
|
||||
std::vector<std::pair<SHAMapNodeID, SHAMapTreeNodePtr>> data,
|
||||
std::shared_ptr<Peer> const& peer);
|
||||
std::vector<std::pair<SHAMapNodeID, Slice>> const& data,
|
||||
std::shared_ptr<Peer> const&);
|
||||
|
||||
void
|
||||
init(int startPeers);
|
||||
|
||||
@@ -23,7 +23,6 @@ enum class ProtocolFeature {
|
||||
ValidatorListPropagation,
|
||||
ValidatorList2Propagation,
|
||||
LedgerReplay,
|
||||
LedgerNodeDepth,
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
#include <xrpld/app/ledger/InboundLedgers.h>
|
||||
#include <xrpld/app/ledger/InboundTransactions.h>
|
||||
#include <xrpld/app/ledger/LedgerMaster.h>
|
||||
#include <xrpld/app/ledger/LedgerNodeHelpers.h>
|
||||
#include <xrpld/app/ledger/TransactionMaster.h>
|
||||
#include <xrpld/app/misc/Transaction.h>
|
||||
#include <xrpld/app/misc/ValidatorList.h>
|
||||
@@ -62,7 +61,6 @@
|
||||
#include <xrpl/resource/Gossip.h>
|
||||
#include <xrpl/server/LoadFeeTrack.h>
|
||||
#include <xrpl/server/NetworkOPs.h>
|
||||
#include <xrpl/shamap/SHAMap.h>
|
||||
#include <xrpl/shamap/SHAMapNodeID.h>
|
||||
#include <xrpl/tx/apply.h>
|
||||
|
||||
@@ -545,8 +543,6 @@ PeerImp::supportsFeature(ProtocolFeature f) const
|
||||
return protocol_ >= makeProtocol(2, 1);
|
||||
case ProtocolFeature::ValidatorList2Propagation:
|
||||
return protocol_ >= makeProtocol(2, 2);
|
||||
case ProtocolFeature::LedgerNodeDepth:
|
||||
return protocol_ >= makeProtocol(2, 3);
|
||||
case ProtocolFeature::LedgerReplay:
|
||||
return ledgerReplayEnabled_;
|
||||
}
|
||||
@@ -1481,12 +1477,23 @@ PeerImp::onMessage(std::shared_ptr<protocol::TMGetLedger> const& m)
|
||||
}
|
||||
}
|
||||
|
||||
// Verify ledger node counts. Full parsing of the node IDs is deferred to the job, so the I/O
|
||||
// thread is not burdened with SHAMapNodeID deserialization for every TMGetLedger message.
|
||||
if (itype != protocol::liBASE && m->nodeids_size() <= 0)
|
||||
// Verify ledger node IDs
|
||||
if (itype != protocol::liBASE)
|
||||
{
|
||||
badData("Invalid ledger node IDs");
|
||||
return;
|
||||
if (m->nodeids_size() <= 0)
|
||||
{
|
||||
badData("Invalid ledger node IDs");
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto const& nodeId : m->nodeids())
|
||||
{
|
||||
if (deserializeSHAMapNodeID(nodeId) == std::nullopt)
|
||||
{
|
||||
badData("Invalid SHAMap node ID");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Verify query type
|
||||
@@ -1506,40 +1513,11 @@ PeerImp::onMessage(std::shared_ptr<protocol::TMGetLedger> const& m)
|
||||
}
|
||||
}
|
||||
|
||||
// Queue a job to process the request.
|
||||
// Queue a job to process the request
|
||||
std::weak_ptr<PeerImp> const weak = shared_from_this();
|
||||
app_.getJobQueue().addJob(JtLedgerReq, "RcvGetLedger", [weak, m, itype]() {
|
||||
auto peer = weak.lock();
|
||||
if (!peer)
|
||||
return;
|
||||
|
||||
std::vector<SHAMapNodeID> nodeIDs;
|
||||
if (itype != protocol::liBASE)
|
||||
{
|
||||
nodeIDs.reserve(std::min(m->nodeids_size(), Tuning::kSoftMaxReplyNodes));
|
||||
for (auto const& nodeId : m->nodeids())
|
||||
{
|
||||
if (nodeIDs.size() >= Tuning::kSoftMaxReplyNodes)
|
||||
{
|
||||
// Charge the peer for requesting too many node IDs, but continue processing the
|
||||
// received node IDs up to the limit. If the request is legitimate then at least
|
||||
// they will get a response and won't have to resend these nodes in their next
|
||||
// request.
|
||||
peer->charge(
|
||||
Resource::kFeeModerateBurdenPeer, "TMGetLedger: too many node IDs");
|
||||
break;
|
||||
}
|
||||
auto parsed = deserializeSHAMapNodeID(nodeId);
|
||||
if (!parsed)
|
||||
{
|
||||
peer->charge(Resource::kFeeInvalidData, "TMGetLedger: Invalid node ID");
|
||||
return;
|
||||
}
|
||||
nodeIDs.push_back(std::move(*parsed));
|
||||
}
|
||||
}
|
||||
|
||||
peer->processLedgerRequest(m, std::move(nodeIDs));
|
||||
app_.getJobQueue().addJob(JtLedgerReq, "RcvGetLedger", [weak, m]() {
|
||||
if (auto peer = weak.lock())
|
||||
peer->processLedgerRequest(m);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1704,86 +1682,12 @@ PeerImp::onMessage(std::shared_ptr<protocol::TMLedgerData> const& m)
|
||||
return;
|
||||
}
|
||||
|
||||
// If there is a request cookie, attempt to relay the message.
|
||||
// If there is a request cookie, attempt to relay the message
|
||||
if (m->has_requestcookie())
|
||||
{
|
||||
if (auto peer = overlay_.findPeerByShortID(m->requestcookie()))
|
||||
{
|
||||
m->clear_requestcookie();
|
||||
|
||||
// If the original requester doesn't support the new depth-based format, rewrite any
|
||||
// nodes that use it back to the legacy nodeid format before relaying. Once all nodes
|
||||
// have upgraded, the old protocol version and this code can be removed. To add some
|
||||
// protection against malicious peers sending arbitrary data, we check that the format
|
||||
// of the nodes is consistent - either all use the legacy format or the new format.
|
||||
auto const peerSupportsNodeDepth =
|
||||
peer->supportsFeature(ProtocolFeature::LedgerNodeDepth);
|
||||
enum class MessageType { Unknown, Legacy, NodeDepth };
|
||||
MessageType messageType = MessageType::Unknown;
|
||||
for (int i = 0; i < m->nodes_size(); ++i)
|
||||
{
|
||||
auto* ledgerNode = m->mutable_nodes(i);
|
||||
|
||||
MessageType const msgType =
|
||||
ledgerNode->has_nodeid() ? MessageType::Legacy : MessageType::NodeDepth;
|
||||
if (messageType != MessageType::Unknown && messageType != msgType)
|
||||
{
|
||||
badData(
|
||||
"Received mixed mode message while relaying ledger data for " +
|
||||
to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " +
|
||||
std::to_string(peer->id()));
|
||||
return;
|
||||
}
|
||||
messageType = msgType;
|
||||
|
||||
if (peerSupportsNodeDepth || msgType == MessageType::Legacy)
|
||||
continue;
|
||||
|
||||
switch (ledgerNode->reference_case())
|
||||
{
|
||||
case protocol::TMLedgerNode::kId: {
|
||||
// We can directly copy the `id` field, because it uses the same wire format
|
||||
// as the legacy `nodeid` field.
|
||||
ledgerNode->set_nodeid(ledgerNode->id());
|
||||
ledgerNode->clear_id();
|
||||
break;
|
||||
}
|
||||
case protocol::TMLedgerNode::kDepth: {
|
||||
// We need to regenerate the node ID from the node data and depth.
|
||||
auto treeNode = getTreeNode(ledgerNode->nodedata());
|
||||
if (!treeNode)
|
||||
{
|
||||
badData(
|
||||
"Unable to get tree node while relaying ledger data for " +
|
||||
to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " +
|
||||
std::to_string(peer->id()));
|
||||
return;
|
||||
}
|
||||
|
||||
auto const nodeID = getSHAMapNodeID(*ledgerNode, *treeNode);
|
||||
if (!nodeID)
|
||||
{
|
||||
badData(
|
||||
"Unable to get node ID while relaying ledger data for " +
|
||||
to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " +
|
||||
std::to_string(peer->id()));
|
||||
return;
|
||||
}
|
||||
|
||||
ledgerNode->set_nodeid(nodeID->getRawString());
|
||||
ledgerNode->clear_depth();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
badData(
|
||||
"Empty node reference while relaying ledger data for " +
|
||||
to_string(uint256::fromRaw(m->ledgerhash())) + " to peer " +
|
||||
std::to_string(peer->id()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
peer->send(std::make_shared<Message>(*m, protocol::mtLEDGER_DATA));
|
||||
}
|
||||
else
|
||||
@@ -3383,9 +3287,7 @@ PeerImp::getTxSet(std::shared_ptr<protocol::TMGetLedger> const& m) const
|
||||
}
|
||||
|
||||
void
|
||||
PeerImp::processLedgerRequest(
|
||||
std::shared_ptr<protocol::TMGetLedger> const& m,
|
||||
std::vector<SHAMapNodeID> nodeIDs)
|
||||
PeerImp::processLedgerRequest(std::shared_ptr<protocol::TMGetLedger> const& m)
|
||||
{
|
||||
// Do not resource charge a peer responding to a relay
|
||||
if (!m->has_requestcookie())
|
||||
@@ -3470,25 +3372,26 @@ PeerImp::processLedgerRequest(
|
||||
}
|
||||
|
||||
// Add requested node data to reply
|
||||
if (!nodeIDs.empty())
|
||||
if (m->nodeids_size() > 0)
|
||||
{
|
||||
std::uint32_t const defaultDepth = isHighLatency() ? 2 : 1;
|
||||
auto const queryDepth{m->has_querydepth() ? m->querydepth() : defaultDepth};
|
||||
|
||||
std::vector<SHAMapNodeData> data;
|
||||
data.reserve(Tuning::kSoftMaxReplyNodes);
|
||||
auto const useLedgerNodeDepth = supportsFeature(ProtocolFeature::LedgerNodeDepth);
|
||||
std::vector<std::pair<SHAMapNodeID, Blob>> data;
|
||||
|
||||
for (auto const& nodeID : nodeIDs)
|
||||
for (int i = 0;
|
||||
i < m->nodeids_size() && ledgerData.nodes_size() < Tuning::kSoftMaxReplyNodes;
|
||||
++i)
|
||||
{
|
||||
if (ledgerData.nodes_size() >= Tuning::kSoftMaxReplyNodes)
|
||||
break;
|
||||
auto const shaMapNodeId{deserializeSHAMapNodeID(m->nodeids(i))};
|
||||
|
||||
data.clear();
|
||||
data.reserve(Tuning::kSoftMaxReplyNodes);
|
||||
|
||||
try
|
||||
{
|
||||
if (map->getNodeFat(nodeID, data, fatLeaves, queryDepth))
|
||||
// NOLINTNEXTLINE(bugprone-unchecked-optional-access) nodeids checked in onGetLedger
|
||||
if (map->getNodeFat(*shaMapNodeId, data, fatLeaves, queryDepth))
|
||||
{
|
||||
JLOG(pJournal_.trace())
|
||||
<< "processLedgerRequest: getNodeFat got " << data.size() << " nodes";
|
||||
@@ -3497,25 +3400,9 @@ PeerImp::processLedgerRequest(
|
||||
{
|
||||
if (ledgerData.nodes_size() >= Tuning::kHardMaxReplyNodes)
|
||||
break;
|
||||
|
||||
protocol::TMLedgerNode* node{ledgerData.add_nodes()};
|
||||
node->set_nodedata(d.data.data(), d.data.size());
|
||||
|
||||
// When the LedgerNodeDepth protocol feature is not supported by the peer,
|
||||
// we always set the `nodeid` field. However, when it is supported then we
|
||||
// set the `id` field for inner nodes and the `depth` field for leaf nodes.
|
||||
if (!useLedgerNodeDepth)
|
||||
{
|
||||
node->set_nodeid(d.nodeID.getRawString());
|
||||
}
|
||||
else if (d.isLeaf)
|
||||
{
|
||||
node->set_depth(d.nodeID.getDepth());
|
||||
}
|
||||
else
|
||||
{
|
||||
node->set_id(d.nodeID.getRawString());
|
||||
}
|
||||
node->set_nodeid(d.first.getRawString());
|
||||
node->set_nodedata(d.second.data(), d.second.size());
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -3554,7 +3441,7 @@ PeerImp::processLedgerRequest(
|
||||
info += ", no hash specified";
|
||||
|
||||
JLOG(pJournal_.warn())
|
||||
<< "processLedgerRequest: getNodeFat with nodeId " << nodeID
|
||||
<< "processLedgerRequest: getNodeFat with nodeId " << *shaMapNodeId
|
||||
<< " and ledger info type " << info << " throws exception: " << e.what();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@
|
||||
#include <xrpl/resource/Consumer.h>
|
||||
#include <xrpl/resource/Fees.h>
|
||||
#include <xrpl/server/Handoff.h>
|
||||
#include <xrpl/shamap/SHAMapNodeID.h>
|
||||
|
||||
#include <boost/circular_buffer.hpp>
|
||||
#include <boost/endian/conversion.hpp>
|
||||
@@ -680,9 +679,7 @@ private:
|
||||
getTxSet(std::shared_ptr<protocol::TMGetLedger> const& m) const;
|
||||
|
||||
void
|
||||
processLedgerRequest(
|
||||
std::shared_ptr<protocol::TMGetLedger> const& m,
|
||||
std::vector<SHAMapNodeID> nodeIDs);
|
||||
processLedgerRequest(std::shared_ptr<protocol::TMGetLedger> const& m);
|
||||
|
||||
protected:
|
||||
// Kept `protected` so test subclasses (see
|
||||
|
||||
@@ -29,7 +29,6 @@ namespace xrpl {
|
||||
constexpr ProtocolVersion const kSupportedProtocolList[]{
|
||||
{2, 1},
|
||||
{2, 2},
|
||||
{2, 3},
|
||||
};
|
||||
|
||||
// This ugly construct ensures that supportedProtocolList is sorted in strictly
|
||||
|
||||
Reference in New Issue
Block a user