Compare commits

..

3 Commits

Author SHA1 Message Date
Pratik Mankawde
6bc3070856 activate tsan and asan for gcc and clang
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-19 15:25:38 +00:00
Pratik Mankawde
21a52226bf Merge branch 'develop' into pratik/use_boost_coroutine2
Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
2026-02-19 15:23:11 +00:00
Pratik Mankawde
7def28c8ef switch to ucontext and coroutine2
Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

updated conan profiles

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

removed no-pie

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

minor change

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

fixes for boost flags

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

minor fix

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

added boost flags to cxx_flags

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

added boost context to conanfile

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

added dependency to coroutine2

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

now building coroutine2

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

using without_coroutine2=false as well

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>

fix build

Signed-off-by: Pratik Mankawde <pmankawde@ripple.com>
2025-11-21 12:11:13 +00:00
27 changed files with 189 additions and 800 deletions

View File

@@ -5,7 +5,7 @@ Loop: test.jtx test.unit_test
test.unit_test == test.jtx
Loop: xrpld.app xrpld.overlay
xrpld.overlay == xrpld.app
xrpld.overlay ~= xrpld.app
Loop: xrpld.app xrpld.peerfinder
xrpld.peerfinder == xrpld.app

View File

@@ -236,10 +236,12 @@ def generate_strategy_matrix(all: bool, config: Config) -> list:
# names get truncated.
# Add Address and Thread (both coupled with UB) sanitizers for specific bookworm distros.
# GCC-Asan rippled-embedded tests are failing because of https://github.com/google/sanitizers/issues/856
if (
os["distro_version"] == "bookworm"
and f"{os['compiler_name']}-{os['compiler_version']}" == "clang-20"
):
if os[
"distro_version"
] == "bookworm" and f"{os['compiler_name']}-{os['compiler_version']}" in [
"clang-20",
"gcc-15",
]:
# Add ASAN + UBSAN configuration.
configurations.append(
{
@@ -254,7 +256,7 @@ def generate_strategy_matrix(all: bool, config: Config) -> list:
}
)
# TSAN is deactivated due to seg faults with latest compilers.
activate_tsan = False
activate_tsan = True
if activate_tsan:
configurations.append(
{

View File

@@ -17,7 +17,7 @@ find_dependency(Boost
chrono
container
context
coroutine
coroutine2
date_time
filesystem
program_options

View File

@@ -4,7 +4,7 @@ include(XrplSanitizers)
find_package(Boost REQUIRED
COMPONENTS chrono
container
coroutine
context
date_time
filesystem
json
@@ -21,7 +21,7 @@ target_link_libraries(
INTERFACE Boost::headers
Boost::chrono
Boost::container
Boost::coroutine
Boost::context
Boost::date_time
Boost::filesystem
Boost::json

View File

@@ -13,15 +13,18 @@ include(default)
{% if "address" in sanitizers %}
{% set _ = sanitizer_list.append("address") %}
{% set model_code = "-mcmodel=large" %}
tools.build:defines+=["BOOST_USE_ASAN", "BOOST_USE_UCONTEXT"]
{% elif "thread" in sanitizers %}
{% set _ = sanitizer_list.append("thread") %}
{% set model_code = "-mcmodel=medium" %}
{% set _ = extra_cxxflags.append("-Wno-tsan") %}
tools.build:defines+=["BOOST_USE_TSAN", "BOOST_USE_UCONTEXT"]
{% endif %}
{% if "undefinedbehavior" in sanitizers %}
{% set _ = sanitizer_list.append("undefined") %}
{% set _ = sanitizer_list.append("float-divide-by-zero") %}
tools.build:defines+=["BOOST_USE_UBSAN", "BOOST_USE_UCONTEXT"]
{% endif %}
{% set sanitizer_flags = "-fsanitize=" ~ ",".join(sanitizer_list) ~ " " ~ model_code %}
@@ -37,14 +40,17 @@ include(default)
{% if "address" in sanitizers %}
{% set _ = sanitizer_list.append("address") %}
tools.build:defines+=["BOOST_USE_ASAN", "BOOST_USE_UCONTEXT"]
{% elif "thread" in sanitizers %}
{% set _ = sanitizer_list.append("thread") %}
tools.build:defines+=["BOOST_USE_TSAN", "BOOST_USE_UCONTEXT"]
{% endif %}
{% if "undefinedbehavior" in sanitizers %}
{% set _ = sanitizer_list.append("undefined") %}
{% set _ = sanitizer_list.append("float-divide-by-zero") %}
{% set _ = sanitizer_list.append("unsigned-integer-overflow") %}
tools.build:defines+=[ "BOOST_USE_UBSAN", "BOOST_USE_UCONTEXT"]
{% endif %}
{% set sanitizer_flags = "-fsanitize=" ~ ",".join(sanitizer_list) %}
@@ -56,4 +62,20 @@ include(default)
{% endif %}
{% endif %}
tools.info.package_id:confs+=["tools.build:cxxflags", "tools.build:exelinkflags", "tools.build:sharedlinkflags"]
tools.info.package_id:confs+=["tools.build:cxxflags", "tools.build:exelinkflags", "tools.build:sharedlinkflags", "tools.build:defines"]
[options]
{% if compiler == "gcc" or compiler == "apple-clang" or compiler == "clang" %}
{% if sanitizers == "Address" or sanitizers == "Thread" %}
boost/*:without_context=False
boost/*:without_stacktrace=True
boost/*:without_coroutine2=False
{% if sanitizers == "Address" %}
boost/*:extra_b2_flags="context-impl=ucontext address-sanitizer=norecover undefined-sanitizer=norecover --with-coroutine2"
{% elif sanitizers == "Thread" %}
boost/*:extra_b2_flags="context-impl=ucontext thread-sanitizer=norecover undefined-sanitizer=norecover --with-coroutine2"
{% endif %}
{% endif %}
{% endif %}

View File

@@ -125,6 +125,7 @@ class Xrpl(ConanFile):
self.options["boost"].visibility = "global"
if self.settings.compiler in ["clang", "gcc"]:
self.options["boost"].without_cobalt = True
self.options["boost"].without_coroutine2 = False
def requirements(self):
# Conan 2 requires transitive headers to be specified
@@ -196,7 +197,8 @@ class Xrpl(ConanFile):
"boost::headers",
"boost::chrono",
"boost::container",
"boost::coroutine",
"boost::context",
"boost::coroutine2",
"boost::date_time",
"boost::filesystem",
"boost::json",

View File

@@ -173,6 +173,7 @@ words:
- nftpage
- nikb
- nonxrp
- norecover
- noripple
- nudb
- nullptr

View File

@@ -10,16 +10,14 @@ JobQueue::Coro::Coro(Coro_create_t, JobQueue& jq, JobType type, std::string cons
, type_(type)
, name_(name)
, running_(false)
, coro_(
[this, fn = std::forward<F>(f)](boost::coroutines::asymmetric_coroutine<void>::push_type& do_yield) {
yield_ = &do_yield;
yield();
fn(shared_from_this());
, coro_([this, fn = std::forward<F>(f)](boost::coroutines2::asymmetric_coroutine<void>::push_type& do_yield) {
yield_ = &do_yield;
yield();
fn(shared_from_this());
#ifndef NDEBUG
finished_ = true;
finished_ = true;
#endif
},
boost::coroutines::attributes(megabytes(1)))
})
{
}

View File

@@ -7,7 +7,7 @@
#include <xrpl/core/detail/Workers.h>
#include <xrpl/json/json_value.h>
#include <boost/coroutine/all.hpp>
#include <boost/coroutine2/all.hpp>
#include <set>
@@ -48,8 +48,8 @@ public:
std::mutex mutex_;
std::mutex mutex_run_;
std::condition_variable cv_;
boost::coroutines::asymmetric_coroutine<void>::pull_type coro_;
boost::coroutines::asymmetric_coroutine<void>::push_type* yield_;
boost::coroutines2::asymmetric_coroutine<void>::pull_type coro_;
boost::coroutines2::asymmetric_coroutine<void>::push_type* yield_;
#ifndef NDEBUG
bool finished_ = false;
#endif
@@ -326,7 +326,7 @@ private:
other requests while the RPC command completes its work asynchronously.
postCoro() creates a Coro object. When the Coro ctor is called, and its
coro_ member is initialized (a boost::coroutines::pull_type), execution
coro_ member is initialized (a boost::coroutines2::pull_type), execution
automatically passes to the coroutine, which we don't want at this point,
since we are still in the handler thread context. It's important to note
here that construction of a boost pull_type automatically passes execution to

View File

@@ -244,15 +244,7 @@ message TMGetObjectByHash {
message TMLedgerNode {
required bytes nodedata = 1;
// Used when fixLedgerNodeID is disabled.
optional bytes nodeid = 2; // missing for ledger base data
// Used when fixLedgerNodeID is enabled. Neither value is set for ledger base data.
oneof reference {
bytes id = 3; // Set for inner nodes.
uint32 depth = 4; // Set for leaf nodes.
}
}
enum TMLedgerInfoType {

View File

@@ -15,7 +15,6 @@
// Add new amendments to the top of this list.
// Keep it sorted in reverse chronological order.
XRPL_FIX (LedgerNodeID, Supported::yes, VoteBehavior::DefaultYes)
XRPL_FIX (PermissionedDomainInvariant, Supported::yes, VoteBehavior::DefaultNo)
XRPL_FIX (ExpiredNFTokenOfferRemoval, Supported::yes, VoteBehavior::DefaultNo)
XRPL_FIX (BatchInnerSigs, Supported::yes, VoteBehavior::DefaultNo)

View File

@@ -252,7 +252,7 @@ public:
bool
getNodeFat(
SHAMapNodeID const& wanted,
std::vector<std::tuple<SHAMapNodeID, Blob, bool>>& data,
std::vector<std::pair<SHAMapNodeID, Blob>>& data,
bool fatLeaves,
std::uint32_t depth) const;
@@ -280,12 +280,9 @@ public:
serializeRoot(Serializer& s) const;
SHAMapAddNode
addRootNode(SHAMapHash const& hash, intr_ptr::SharedPtr<SHAMapTreeNode> rootNode, SHAMapSyncFilter const* filter);
addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFilter* filter);
SHAMapAddNode
addKnownNode(
SHAMapNodeID const& nodeID,
intr_ptr::SharedPtr<SHAMapTreeNode> treeNode,
SHAMapSyncFilter const* filter);
addKnownNode(SHAMapNodeID const& nodeID, Slice const& rawNode, SHAMapSyncFilter* filter);
// status functions
void
@@ -336,7 +333,7 @@ private:
cacheLookup(SHAMapHash const& hash) const;
void
canonicalize(SHAMapHash const& hash, intr_ptr::SharedPtr<SHAMapTreeNode>& node) const;
canonicalize(SHAMapHash const& hash, intr_ptr::SharedPtr<SHAMapTreeNode>&) const;
// database operations
intr_ptr::SharedPtr<SHAMapTreeNode>
@@ -344,11 +341,11 @@ private:
intr_ptr::SharedPtr<SHAMapTreeNode>
fetchNodeNT(SHAMapHash const& hash) const;
intr_ptr::SharedPtr<SHAMapTreeNode>
fetchNodeNT(SHAMapHash const& hash, SHAMapSyncFilter const* filter) const;
fetchNodeNT(SHAMapHash const& hash, SHAMapSyncFilter* filter) const;
intr_ptr::SharedPtr<SHAMapTreeNode>
fetchNode(SHAMapHash const& hash) const;
intr_ptr::SharedPtr<SHAMapTreeNode>
checkFilter(SHAMapHash const& hash, SHAMapSyncFilter const* filter) const;
checkFilter(SHAMapHash const& hash, SHAMapSyncFilter* filter) const;
/** Update hashes up to the root */
void
@@ -408,11 +405,10 @@ private:
// If pending, callback is called as if it called fetchNodeNT
using descendCallback = std::function<void(intr_ptr::SharedPtr<SHAMapTreeNode>, SHAMapHash const&)>;
SHAMapTreeNode*
descendAsync(SHAMapInnerNode* parent, int branch, SHAMapSyncFilter const* filter, bool& pending, descendCallback&&)
const;
descendAsync(SHAMapInnerNode* parent, int branch, SHAMapSyncFilter* filter, bool& pending, descendCallback&&) const;
std::pair<SHAMapTreeNode*, SHAMapNodeID>
descend(SHAMapInnerNode* parent, SHAMapNodeID const& parentID, int branch, SHAMapSyncFilter const* filter) const;
descend(SHAMapInnerNode* parent, SHAMapNodeID const& parentID, int branch, SHAMapSyncFilter* filter) const;
// Non-storing
// Does not hook the returned node to its parent

View File

@@ -172,7 +172,7 @@ SHAMap::finishFetch(SHAMapHash const& hash, std::shared_ptr<NodeObject> const& o
// See if a sync filter has a node
intr_ptr::SharedPtr<SHAMapTreeNode>
SHAMap::checkFilter(SHAMapHash const& hash, SHAMapSyncFilter const* filter) const
SHAMap::checkFilter(SHAMapHash const& hash, SHAMapSyncFilter* filter) const
{
if (auto nodeData = filter->getNode(hash))
{
@@ -198,7 +198,7 @@ SHAMap::checkFilter(SHAMapHash const& hash, SHAMapSyncFilter const* filter) cons
// Get a node without throwing
// Used on maps where missing nodes are expected
intr_ptr::SharedPtr<SHAMapTreeNode>
SHAMap::fetchNodeNT(SHAMapHash const& hash, SHAMapSyncFilter const* filter) const
SHAMap::fetchNodeNT(SHAMapHash const& hash, SHAMapSyncFilter* filter) const
{
auto node = cacheLookup(hash);
if (node)
@@ -307,7 +307,7 @@ SHAMap::descendNoStore(SHAMapInnerNode& parent, int branch) const
}
std::pair<SHAMapTreeNode*, SHAMapNodeID>
SHAMap::descend(SHAMapInnerNode* parent, SHAMapNodeID const& parentID, int branch, SHAMapSyncFilter const* filter) const
SHAMap::descend(SHAMapInnerNode* parent, SHAMapNodeID const& parentID, int branch, SHAMapSyncFilter* filter) const
{
XRPL_ASSERT(parent->isInner(), "xrpl::SHAMap::descend : valid parent input");
XRPL_ASSERT((branch >= 0) && (branch < branchFactor), "xrpl::SHAMap::descend : valid branch input");
@@ -334,7 +334,7 @@ SHAMapTreeNode*
SHAMap::descendAsync(
SHAMapInnerNode* parent,
int branch,
SHAMapSyncFilter const* filter,
SHAMapSyncFilter* filter,
bool& pending,
descendCallback&& callback) const
{

View File

@@ -113,7 +113,7 @@ selectBranch(SHAMapNodeID const& id, uint256 const& hash)
SHAMapNodeID
SHAMapNodeID::createID(int depth, uint256 const& key)
{
XRPL_ASSERT(depth >= 0 && depth <= SHAMap::leafDepth, "xrpl::SHAMapNodeID::createID : valid branch input");
XRPL_ASSERT((depth >= 0) && (depth < 65), "xrpl::SHAMapNodeID::createID : valid branch input");
return SHAMapNodeID(depth, key & depthMask(depth));
}

View File

@@ -374,7 +374,7 @@ SHAMap::getMissingNodes(int max, SHAMapSyncFilter* filter)
bool
SHAMap::getNodeFat(
SHAMapNodeID const& wanted,
std::vector<std::tuple<SHAMapNodeID, Blob, bool>>& data,
std::vector<std::pair<SHAMapNodeID, Blob>>& data,
bool fatLeaves,
std::uint32_t depth) const
{
@@ -419,7 +419,7 @@ SHAMap::getNodeFat(
// Add this node to the reply
s.erase();
node->serializeForWire(s);
data.emplace_back(std::make_tuple(nodeID, s.getData(), node->isLeaf()));
data.emplace_back(std::make_pair(nodeID, s.getData()));
if (node->isInner())
{
@@ -449,7 +449,7 @@ SHAMap::getNodeFat(
// Just include this node
s.erase();
childNode->serializeForWire(s);
data.emplace_back(std::make_tuple(childID, s.getData(), childNode->isLeaf()));
data.emplace_back(std::make_pair(childID, s.getData()));
}
}
}
@@ -467,10 +467,7 @@ SHAMap::serializeRoot(Serializer& s) const
}
SHAMapAddNode
SHAMap::addRootNode(
SHAMapHash const& hash,
intr_ptr::SharedPtr<SHAMapTreeNode> rootNode,
SHAMapSyncFilter const* filter)
SHAMap::addRootNode(SHAMapHash const& hash, Slice const& rootNode, SHAMapSyncFilter* filter)
{
// we already have a root_ node
if (root_->getHash().isNonZero())
@@ -481,13 +478,14 @@ SHAMap::addRootNode(
}
XRPL_ASSERT(cowid_ >= 1, "xrpl::SHAMap::addRootNode : valid cowid");
if (rootNode->getHash() != hash)
auto node = SHAMapTreeNode::makeFromWire(rootNode);
if (!node || node->getHash() != hash)
return SHAMapAddNode::invalid();
if (backed_)
canonicalize(hash, rootNode);
canonicalize(hash, node);
root_ = rootNode;
root_ = node;
if (root_->isLeaf())
clearSynching();
@@ -503,12 +501,9 @@ SHAMap::addRootNode(
}
SHAMapAddNode
SHAMap::addKnownNode(
SHAMapNodeID const& nodeID,
intr_ptr::SharedPtr<SHAMapTreeNode> treeNode,
SHAMapSyncFilter const* filter)
SHAMap::addKnownNode(SHAMapNodeID const& node, Slice const& rawNode, SHAMapSyncFilter* filter)
{
XRPL_ASSERT(!nodeID.isRoot(), "xrpl::SHAMap::addKnownNode : valid node input");
XRPL_ASSERT(!node.isRoot(), "xrpl::SHAMap::addKnownNode : valid node input");
if (!isSynching())
{
@@ -521,14 +516,14 @@ SHAMap::addKnownNode(
auto currNode = root_.get();
while (currNode->isInner() && !static_cast<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 = static_cast<SHAMapInnerNode*>(currNode);
if (inner->isEmptyBranch(branch))
{
JLOG(journal_.warn()) << "Add known node for empty branch" << nodeID;
JLOG(journal_.warn()) << "Add known node for empty branch" << node;
return SHAMapAddNode::invalid();
}
@@ -544,41 +539,62 @@ 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 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 = static_cast<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() > leafDepth) || (treeNode->isInner() && currNodeID.getDepth() == leafDepth))
if ((currNodeID.getDepth() > leafDepth) || (newNode->isInner() && currNodeID.getDepth() == leafDepth))
{
// 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() << ", walked to= " << currNodeID.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)
{
Serializer s;
treeNode->serializeWithPrefix(s);
filter->gotNode(false, childHash, ledgerSeq_, std::move(s.modData()), treeNode->getType());
newNode->serializeWithPrefix(s);
filter->gotNode(false, childHash, ledgerSeq_, std::move(s.modData()), newNode->getType());
}
return SHAMapAddNode::useful();

View File

@@ -1,352 +0,0 @@
#include <test/jtx.h>
#include <test/jtx/Env.h>
#include <test/shamap/common.h>
#include <test/unit_test/SuiteJournal.h>
#include <xrpld/app/ledger/detail/LedgerNodeHelpers.h>
#include <xrpl/basics/random.h>
#include <xrpl/beast/unit_test.h>
#include <xrpl/proto/xrpl.pb.h>
#include <xrpl/shamap/SHAMap.h>
#include <xrpl/shamap/SHAMapItem.h>
#include <xrpl/shamap/SHAMapLeafNode.h>
namespace xrpl {
namespace tests {
class LedgerNodeHelpers_test : public beast::unit_test::suite
{
// Helper to create a simple SHAMapItem for testing
boost::intrusive_ptr<SHAMapItem>
makeTestItem(std::uint32_t seed)
{
Serializer s;
s.add32(seed);
s.add32(seed + 1);
s.add32(seed + 2);
return make_shamapitem(s.getSHA512Half(), s.slice());
}
// Helper to serialize a tree node to wire format
std::string
serializeNode(intr_ptr::SharedPtr<SHAMapTreeNode> const& node)
{
Serializer s;
node->serializeWithPrefix(s);
auto const slice = s.slice();
return std::string(reinterpret_cast<char const*>(slice.data()), slice.size());
}
void
testValidateLedgerNode()
{
testcase("validateLedgerNode");
using namespace test::jtx;
/*// Test with amendment disabled.
{
Env env{*this, testable_amendments() - fixLedgerNodeID};
auto& app = env.app();
// Valid node with nodeid.
protocol::TMLedgerNode node1;
node1.set_nodedata("test_data");
node1.set_nodeid("test_nodeid");
BEAST_EXPECT(validateLedgerNode(app, node1));
// Invalid: missing nodedata.
protocol::TMLedgerNode node2;
node2.set_nodeid("test_nodeid");
BEAST_EXPECT(!validateLedgerNode(app, node2));
// Invalid: has new field (id).
protocol::TMLedgerNode node3;
node3.set_nodedata("test_data");
node3.set_nodeid("test_nodeid");
node3.set_id("test_id");
BEAST_EXPECT(!validateLedgerNode(app, node3));
// Invalid: has new field (depth).
protocol::TMLedgerNode node4;
node4.set_nodedata("test_data");
node4.set_nodeid("test_nodeid");
node4.set_depth(5);
BEAST_EXPECT(!validateLedgerNode(app, node4));
}
// Test with amendment enabled.
{
Env env{*this, testable_amendments() | fixLedgerNodeID};
auto& app = env.app();
// Valid inner node with id.
protocol::TMLedgerNode node1;
node1.set_nodedata("test_data");
node1.set_id("test_id");
BEAST_EXPECT(validateLedgerNode(app, node1));
// Valid leaf node with depth.
protocol::TMLedgerNode node2;
node2.set_nodedata("test_data");
node2.set_depth(5);
BEAST_EXPECT(validateLedgerNode(app, node2));
// Valid leaf node at max depth.
protocol::TMLedgerNode node3;
node3.set_nodedata("test_data");
node3.set_depth(SHAMap::leafDepth);
BEAST_EXPECT(validateLedgerNode(app, node3));
// Invalid: depth exceeds max depth.
protocol::TMLedgerNode node4;
node4.set_nodedata("test_data");
node4.set_depth(SHAMap::leafDepth + 1);
BEAST_EXPECT(!validateLedgerNode(app, node4));
// Invalid: has legacy field (nodeid).
protocol::TMLedgerNode node5;
node5.set_nodedata("test_data");
node5.set_nodeid("test_nodeid");
BEAST_EXPECT(!validateLedgerNode(app, node5));
// Invalid: missing both id and depth.
protocol::TMLedgerNode node6;
node6.set_nodedata("test_data");
BEAST_EXPECT(!validateLedgerNode(app, node6));
// Invalid: missing nodedata.
protocol::TMLedgerNode node7;
node7.set_id("test_id");
BEAST_EXPECT(!validateLedgerNode(app, node7));
}*/
}
/*void
testGetTreeNode()
{
testcase("getTreeNode");
using namespace test::jtx;
test::SuiteJournal journal("LedgerNodeHelpers_test", *this);
TestNodeFamily f(journal);
// Test with valid inner node
{
auto innerNode = std::make_shared<SHAMapInnerNode>(1);
auto serialized = serializeNode(innerNode);
auto result = getTreeNode(serialized);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT((*result)->isInner());
}
// Test with valid leaf node
{
auto item = makeTestItem(12345);
auto leafNode =
std::make_shared<SHAMapLeafNode>(item, SHAMapNodeType::tnACCOUNT_STATE);
auto serialized = serializeNode(leafNode);
auto result = getTreeNode(serialized);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT((*result)->isLeaf());
}
// Test with invalid data - empty string
{
auto result = getTreeNode("");
BEAST_EXPECT(!result.has_value());
}
// Test with invalid data - garbage data
{
std::string garbage = "This is not a valid serialized node!";
auto result = getTreeNode(garbage);
BEAST_EXPECT(!result.has_value());
}
// Test with malformed data - truncated
{
auto item = makeTestItem(54321);
auto leafNode =
std::make_shared<SHAMapLeafNode>(item, SHAMapNodeType::tnACCOUNT_STATE);
auto serialized = serializeNode(leafNode);
// Truncate the data
serialized = serialized.substr(0, 5);
auto result = getTreeNode(serialized);
BEAST_EXPECT(!result.has_value());
}
}
void
testGetSHAMapNodeID()
{
testcase("getSHAMapNodeID");
using namespace test::jtx;
test::SuiteJournal journal("LedgerNodeHelpers_test", *this);
TestNodeFamily f(journal);
auto item = makeTestItem(99999);
auto leafNode =
std::make_shared<SHAMapLeafNode>(item, SHAMapNodeType::tnACCOUNT_STATE);
auto innerNode = std::make_shared<SHAMapInnerNode>(1);
// Test with amendment disabled
{
Env env{*this};
auto& app = env.app();
// Test with leaf node - valid nodeid
{
auto nodeID = SHAMapNodeID::createID(5, item->key());
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_nodeid(nodeID.getRawString());
auto result = getSHAMapNodeID(app, ledgerNode, leafNode);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(*result == nodeID);
}
// Test with leaf node - invalid nodeid (wrong depth)
{
auto nodeID = SHAMapNodeID::createID(7, item->key());
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_nodeid(nodeID.getRawString());
auto wrongNodeID = SHAMapNodeID::createID(5, item->key());
ledgerNode.set_nodeid(wrongNodeID.getRawString());
auto result = getSHAMapNodeID(app, ledgerNode, leafNode);
// Should fail because nodeid doesn't match the leaf's key at the
// given depth
BEAST_EXPECT(!result.has_value());
}
// Test with inner node
{
auto nodeID = SHAMapNodeID::createID(3, uint256{});
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_nodeid(nodeID.getRawString());
auto result = getSHAMapNodeID(app, ledgerNode, innerNode);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(*result == nodeID);
}
// Test missing nodeid
{
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
auto result = getSHAMapNodeID(app, ledgerNode, leafNode);
BEAST_EXPECT(!result.has_value());
}
}
// Test with amendment enabled
{
Env env{*this, testable_amendments() | fixLedgerNodeID};
auto& app = env.app();
// Test with leaf node - valid depth
{
std::uint32_t depth = 5;
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_depth(depth);
auto result = getSHAMapNodeID(app, ledgerNode, leafNode);
BEAST_EXPECT(result.has_value());
auto expectedID = SHAMapNodeID::createID(depth, item->key());
BEAST_EXPECT(*result == expectedID);
}
// Test with leaf node - missing depth
{
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
auto result = getSHAMapNodeID(app, ledgerNode, leafNode);
BEAST_EXPECT(!result.has_value());
}
// Test with inner node - valid id
{
auto nodeID = SHAMapNodeID::createID(3, uint256{});
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_id(nodeID.getRawString());
auto result = getSHAMapNodeID(app, ledgerNode, innerNode);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(*result == nodeID);
}
// Test with inner node - missing id
{
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
auto result = getSHAMapNodeID(app, ledgerNode, innerNode);
BEAST_EXPECT(!result.has_value());
}
// Test that nodeid is rejected when amendment is enabled
{
auto nodeID = SHAMapNodeID::createID(5, item->key());
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_nodeid(nodeID.getRawString());
// Should fail validation before getSHAMapNodeID is called,
// but let's verify getSHAMapNodeID behavior
// Note: validateLedgerNode should be called first in practice
BEAST_EXPECT(!validateLedgerNode(app, ledgerNode));
}
// Test with root node (inner node at depth 0)
{
auto rootNode = std::make_shared<SHAMapInnerNode>(1);
auto nodeID = SHAMapNodeID{}; // root node ID
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_id(nodeID.getRawString());
auto result = getSHAMapNodeID(app, ledgerNode, rootNode);
BEAST_EXPECT(result.has_value());
BEAST_EXPECT(*result == nodeID);
}
// Test with leaf at maximum depth
{
std::uint32_t depth = SHAMap::leafDepth;
protocol::TMLedgerNode ledgerNode;
ledgerNode.set_nodedata("test_data");
ledgerNode.set_depth(depth);
auto result = getSHAMapNodeID(app, ledgerNode, leafNode);
BEAST_EXPECT(result.has_value());
auto expectedID = SHAMapNodeID::createID(depth, item->key());
BEAST_EXPECT(*result == expectedID);
}
}
}*/
public:
void
run() override
{
testValidateLedgerNode();
// testGetTreeNode();
// testGetSHAMapNodeID();
}
};
BEAST_DEFINE_TESTSUITE(LedgerNodeHelpers, app, xrpl);
} // namespace tests
} // namespace xrpl

View File

@@ -1010,7 +1010,7 @@ public:
// Charlie - queue a transaction, with a higher fee
// than default
env(noop(charlie), fee(15), queued);
checkMetrics(*this, env, 6, initQueueMax, 4, 3, 256);
checkMetrics(*this, env, 6, initQueueMax, 4, 3, 257);
BEAST_EXPECT(env.seq(alice) == aliceSeq);
BEAST_EXPECT(env.seq(bob) == bobSeq);

View File

@@ -103,14 +103,13 @@ public:
destination.setSynching();
{
std::vector<std::tuple<SHAMapNodeID, Blob, bool>> a;
std::vector<std::pair<SHAMapNodeID, Blob>> a;
BEAST_EXPECT(source.getNodeFat(SHAMapNodeID(), a, rand_bool(eng_), rand_int(eng_, 2)));
unexpected(a.size() < 1, "NodeSize");
auto node = SHAMapTreeNode::makeFromWire(makeSlice(std::get<1>(a[0])));
BEAST_EXPECT(destination.addRootNode(source.getHash(), std::move(node), nullptr).isGood());
BEAST_EXPECT(destination.addRootNode(source.getHash(), makeSlice(a[0].second), nullptr).isGood());
}
do
@@ -124,7 +123,7 @@ public:
break;
// get as many nodes as possible based on this information
std::vector<std::tuple<SHAMapNodeID, Blob, bool>> b;
std::vector<std::pair<SHAMapNodeID, Blob>> b;
for (auto& it : nodesMissing)
{
@@ -146,8 +145,7 @@ public:
// Don't use BEAST_EXPECT here b/c it will be called a
// non-deterministic number of times and the number of tests run
// should be deterministic
auto node = SHAMapTreeNode::makeFromWire(makeSlice(std::get<1>(b[i])));
if (!destination.addKnownNode(std::get<0>(b[i]), std::move(node), nullptr).isUseful())
if (!destination.addKnownNode(b[i].first, makeSlice(b[i].second), nullptr).isUseful())
fail("", __FILE__, __LINE__);
}
} while (true);

View File

@@ -137,10 +137,10 @@ private:
receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode&);
bool
takeTxRootNode(std::string const& data, SHAMapAddNode&);
takeTxRootNode(Slice const& data, SHAMapAddNode&);
bool
takeAsRootNode(std::string const& data, SHAMapAddNode&);
takeAsRootNode(Slice const& data, SHAMapAddNode&);
std::vector<uint256>
neededTxHashes(int max, SHAMapSyncFilter* filter) const;

View File

@@ -3,7 +3,6 @@
#include <xrpld/app/ledger/InboundLedgers.h>
#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/app/ledger/TransactionStateSF.h>
#include <xrpld/app/ledger/detail/LedgerNodeHelpers.h>
#include <xrpld/app/main/Application.h>
#include <xrpld/overlay/Overlay.h>
@@ -816,44 +815,39 @@ InboundLedger::receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode& san)
std::make_unique<AccountStateSF>(mLedger->stateMap().family().db(), app_.getLedgerMaster())};
}();
auto const f = filter.get();
for (auto const& ledger_node : packet.nodes())
try
{
if (!validateLedgerNode(app_, ledger_node))
{
JLOG(journal_.warn()) << "Got malformed ledger node";
san.incInvalid();
return;
}
auto const f = filter.get();
auto treeNode = getTreeNode(ledger_node.nodedata());
if (!treeNode)
for (auto const& node : packet.nodes())
{
JLOG(journal_.warn()) << "Got invalid node data";
san.incInvalid();
return;
}
auto const nodeID = deserializeSHAMapNodeID(node.nodeid());
auto const nodeID = getSHAMapNodeID(app_, ledger_node, *treeNode);
if (!nodeID)
{
JLOG(journal_.warn()) << "Got invalid node id";
san.incInvalid();
return;
}
if (!nodeID)
throw std::runtime_error("data does not properly deserialize");
if (nodeID->isRoot())
san += map.addRootNode(rootHash, std::move(*treeNode), f);
else
san += map.addKnownNode(*nodeID, std::move(*treeNode), f);
if (nodeID->isRoot())
{
san += map.addRootNode(rootHash, makeSlice(node.nodedata()), f);
}
else
{
san += map.addKnownNode(*nodeID, makeSlice(node.nodedata()), f);
}
if (!san.isGood())
{
JLOG(journal_.warn()) << "Received bad node data";
return;
if (!san.isGood())
{
JLOG(journal_.warn()) << "Received bad node data";
return;
}
}
}
catch (std::exception const& e)
{
JLOG(journal_.error()) << "Received bad node data: " << e.what();
san.incInvalid();
return;
}
if (!map.isSynching())
{
@@ -874,7 +868,7 @@ InboundLedger::receiveNode(protocol::TMLedgerData& packet, SHAMapAddNode& san)
Call with a lock
*/
bool
InboundLedger::takeAsRootNode(std::string const& data, SHAMapAddNode& san)
InboundLedger::takeAsRootNode(Slice const& data, SHAMapAddNode& san)
{
if (failed_ || mHaveState)
{
@@ -890,16 +884,8 @@ InboundLedger::takeAsRootNode(std::string const& data, SHAMapAddNode& san)
// LCOV_EXCL_STOP
}
auto treeNode = getTreeNode(data);
if (!treeNode)
{
JLOG(journal_.warn()) << "Got invalid node data";
san.incInvalid();
return false;
}
AccountStateSF filter(mLedger->stateMap().family().db(), app_.getLedgerMaster());
san += mLedger->stateMap().addRootNode(SHAMapHash{mLedger->header().accountHash}, *treeNode, &filter);
san += mLedger->stateMap().addRootNode(SHAMapHash{mLedger->header().accountHash}, data, &filter);
return san.isGood();
}
@@ -907,7 +893,7 @@ InboundLedger::takeAsRootNode(std::string const& data, SHAMapAddNode& san)
Call with a lock
*/
bool
InboundLedger::takeTxRootNode(std::string const& data, SHAMapAddNode& san)
InboundLedger::takeTxRootNode(Slice const& data, SHAMapAddNode& san)
{
if (failed_ || mHaveTransactions)
{
@@ -923,16 +909,8 @@ InboundLedger::takeTxRootNode(std::string const& data, SHAMapAddNode& san)
// LCOV_EXCL_STOP
}
auto treeNode = getTreeNode(data);
if (!treeNode)
{
JLOG(journal_.warn()) << "Got invalid node data";
san.incInvalid();
return false;
}
TransactionStateSF filter(mLedger->txMap().family().db(), app_.getLedgerMaster());
san += mLedger->txMap().addRootNode(SHAMapHash{mLedger->header().txHash}, *treeNode, &filter);
san += mLedger->txMap().addRootNode(SHAMapHash{mLedger->header().txHash}, data, &filter);
return san.isGood();
}
@@ -1026,12 +1004,14 @@ InboundLedger::processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& p
san.incUseful();
}
if (!mHaveState && (packet.nodes().size() > 1) && !takeAsRootNode(packet.nodes(1).nodedata(), san))
if (!mHaveState && (packet.nodes().size() > 1) &&
!takeAsRootNode(makeSlice(packet.nodes(1).nodedata()), san))
{
JLOG(journal_.warn()) << "Included AS root invalid";
}
if (!mHaveTransactions && (packet.nodes().size() > 2) && !takeTxRootNode(packet.nodes(2).nodedata(), san))
if (!mHaveTransactions && (packet.nodes().size() > 2) &&
!takeTxRootNode(makeSlice(packet.nodes(2).nodedata()), san))
{
JLOG(journal_.warn()) << "Included TX root invalid";
}
@@ -1064,13 +1044,13 @@ InboundLedger::processData(std::shared_ptr<Peer> peer, protocol::TMLedgerData& p
ScopedLockType sl(mtx_);
// Verify nodes are complete
for (auto const& ledger_node : packet.nodes())
// Verify node IDs and data are complete
for (auto const& node : packet.nodes())
{
if (!validateLedgerNode(app_, ledger_node))
if (!node.has_nodeid() || !node.has_nodedata())
{
JLOG(journal_.warn()) << "Got malformed ledger node";
peer->charge(Resource::feeMalformedRequest, "ledger_node");
JLOG(journal_.warn()) << "Got bad node";
peer->charge(Resource::feeMalformedRequest, "ledger_data bad node");
return -1;
}
}

View File

@@ -1,6 +1,5 @@
#include <xrpld/app/ledger/InboundLedgers.h>
#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/app/ledger/detail/LedgerNodeHelpers.h>
#include <xrpld/app/main/Application.h>
#include <xrpl/basics/DecayingSample.h>
@@ -214,20 +213,29 @@ public:
gotStaleData(std::shared_ptr<protocol::TMLedgerData> packet_ptr) override
{
Serializer s;
for (auto const& ledger_node : packet_ptr->nodes())
try
{
if (!validateLedgerNode(app_, ledger_node))
return;
for (int i = 0; i < packet_ptr->nodes().size(); ++i)
{
auto const& node = packet_ptr->nodes(i);
auto const treeNode = getTreeNode(ledger_node.nodedata());
if (!treeNode)
return;
auto const tn = *treeNode;
if (!node.has_nodeid() || !node.has_nodedata())
return;
s.erase();
tn->serializeWithPrefix(s);
auto newNode = SHAMapTreeNode::makeFromWire(makeSlice(node.nodedata()));
app_.getLedgerMaster().addFetchPack(tn->getHash().as_uint256(), std::make_shared<Blob>(s.begin(), s.end()));
if (!newNode)
return;
s.erase();
newNode->serializeWithPrefix(s);
app_.getLedgerMaster().addFetchPack(
newNode->getHash().as_uint256(), std::make_shared<Blob>(s.begin(), s.end()));
}
}
catch (std::exception const&)
{
}
}

View File

@@ -1,6 +1,5 @@
#include <xrpld/app/ledger/InboundLedgers.h>
#include <xrpld/app/ledger/InboundTransactions.h>
#include <xrpld/app/ledger/detail/LedgerNodeHelpers.h>
#include <xrpld/app/ledger/detail/TransactionAcquire.h>
#include <xrpld/app/main/Application.h>
@@ -128,35 +127,26 @@ public:
return;
}
std::vector<std::pair<SHAMapNodeID, intr_ptr::SharedPtr<SHAMapTreeNode>>> data;
std::vector<std::pair<SHAMapNodeID, Slice>> data;
data.reserve(packet.nodes().size());
for (auto const& ledger_node : packet.nodes())
for (auto const& node : packet.nodes())
{
if (!validateLedgerNode(app_, ledger_node))
if (!node.has_nodeid() || !node.has_nodedata())
{
JLOG(j_.warn()) << "Got malformed ledger node";
peer->charge(Resource::feeMalformedRequest, "ledger_node");
peer->charge(Resource::feeMalformedRequest, "ledger_data");
return;
}
auto const treeNode = getTreeNode(ledger_node.nodedata());
if (!treeNode)
auto const id = deserializeSHAMapNodeID(node.nodeid());
if (!id)
{
JLOG(j_.warn()) << "Got invalid node data";
peer->charge(Resource::feeInvalidData, "node_data");
peer->charge(Resource::feeInvalidData, "ledger_data");
return;
}
auto const nodeID = getSHAMapNodeID(app_, ledger_node, *treeNode);
if (!nodeID)
{
JLOG(j_.warn()) << "Got invalid node id";
peer->charge(Resource::feeInvalidData, "node_id");
return;
}
data.emplace_back(std::make_pair(*nodeID, *treeNode));
data.emplace_back(std::make_pair(*id, makeSlice(node.nodedata())));
}
if (!ta->takeNodes(data, peer).isUseful())

View File

@@ -1,163 +0,0 @@
#include <xrpld/app/ledger/detail/LedgerNodeHelpers.h>
#include <xrpld/app/main/Application.h>
#include <xrpl/basics/IntrusivePointer.h>
#include <xrpl/beast/utility/instrumentation.h>
#include <xrpl/ledger/AmendmentTable.h>
#include <xrpl/shamap/SHAMapLeafNode.h>
#include <xrpl/shamap/SHAMapNodeID.h>
#include <xrpl/shamap/SHAMapTreeNode.h>
namespace xrpl {
/**
* @brief Validates a ledger node based on the fixLedgerNodeID amendment status.
*
* This function checks whether a ledger node has the expected fields based on
* whether the fixLedgerNodeID amendment is enabled. The validation rules differ
* depending on the amendment state:
*
* When the amendment is enabled:
* - The node must have `nodedata`.
* - The legacy `nodeid` field must NOT be present.
* - For inner nodes: the `id` field must be present.
* - For leaf nodes: the `depth` field must be present and <= SHAMap::leafDepth.
*
* When the amendment is disabled:
* - The node must have `nodedata`.
* - The `nodeid` field must be present.
* - The new `id` and `depth` fields must NOT be present.
*
* @param app The application instance used to check amendment status.
* @param ledger_node The ledger node to validate.
* @return true if the ledger node has the expected fields, false otherwise.
*/
bool
validateLedgerNode(Application& app, protocol::TMLedgerNode const& ledger_node)
{
if (!ledger_node.has_nodedata())
return false;
if (app.getAmendmentTable().isEnabled(fixLedgerNodeID))
{
// Note that we cannot confirm here whether the node is actually an
// inner or leaf node, and will need to perform additional checks
// separately.
if (ledger_node.has_nodeid())
return false;
if (ledger_node.has_id())
return true;
return ledger_node.has_depth() && ledger_node.depth() <= SHAMap::leafDepth;
}
if (ledger_node.has_id() || ledger_node.has_depth())
return false;
return ledger_node.has_nodeid();
}
/**
* @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 std::nullopt instead of throwing an exception.
*
* @param data The serialized node data in wire format.
* @return An optional containing the deserialized tree node if successful, or
* std::nullopt if deserialization fails.
*/
std::optional<intr_ptr::SharedPtr<SHAMapTreeNode>>
getTreeNode(std::string const& data)
{
auto const slice = makeSlice(data);
try
{
return SHAMapTreeNode::makeFromWire(slice);
}
catch (std::exception const&)
{
return std::nullopt;
}
}
/**
* @brief Extracts or reconstructs the SHAMapNodeID from a ledger node.
*
* This function retrieves the SHAMapNodeID for a tree node, with behavior that
* depends on the fixLedgerNodeID amendment status and the node type (inner vs.
* leaf).
*
* When the fixLedgerNodeID amendment is enabled:
* - For inner nodes: Deserializes the node ID from the `ledger_node.id` field.
* Note that root nodes are also inner nodes.
* - For leaf nodes: Reconstructs the node ID using both the depth from the
* `ledger_node.depth` field and the key from the leaf node's item.
*
* When the amendment is disabled:
* - For all nodes: Deserializes the node ID from the `ledger_node.nodeid`
* field.
* - For leaf nodes: Validates that the node ID is consistent with the leaf's
* key.
*
* @param app The application instance used to check amendment status.
* @param ledger_node The 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.
*/
std::optional<SHAMapNodeID>
getSHAMapNodeID(
Application& app,
protocol::TMLedgerNode const& ledger_node,
intr_ptr::SharedPtr<SHAMapTreeNode> const& treeNode)
{
// When the amendment is enabled and a node depth is present, we can calculate the node ID.
if (app.getAmendmentTable().isEnabled(fixLedgerNodeID))
{
if (treeNode->isInner())
{
XRPL_ASSERT(ledger_node.has_id(), "xrpl::getSHAMapNodeID : node ID is present");
if (!ledger_node.has_id())
return std::nullopt;
return deserializeSHAMapNodeID(ledger_node.id());
}
if (treeNode->isLeaf())
{
XRPL_ASSERT(ledger_node.has_depth(), "xrpl::getSHAMapNodeID : node depth is present");
if (!ledger_node.has_depth())
return std::nullopt;
auto const key = static_cast<SHAMapLeafNode const*>(treeNode.get())->peekItem()->key();
return SHAMapNodeID::createID(ledger_node.depth(), key);
}
UNREACHABLE("xrpl::getSHAMapNodeID : tree node is neither inner nor leaf");
return std::nullopt;
}
// When the amendment is disabled, we expect the node ID to always be present. For leaf nodes
// we perform an extra check to ensure the node's position in the tree is consistent with its
// content.
XRPL_ASSERT(ledger_node.has_nodeid(), "xrpl::getSHAMapNodeID : node ID is present");
if (!ledger_node.has_nodeid())
return std::nullopt;
auto const nodeID = deserializeSHAMapNodeID(ledger_node.nodeid());
if (!nodeID)
return std::nullopt;
if (treeNode->isLeaf())
{
auto const key = static_cast<SHAMapLeafNode const*>(treeNode.get())->peekItem()->key();
auto const expected_id = SHAMapNodeID::createID(static_cast<int>(nodeID->getDepth()), key);
if (nodeID->getNodeID() != expected_id.getNodeID())
return std::nullopt;
}
return nodeID;
}
} // namespace xrpl

View File

@@ -1,83 +0,0 @@
#pragma once
#include <xrpld/app/main/Application.h>
#include <xrpl/basics/IntrusivePointer.h>
#include <xrpl/shamap/SHAMapLeafNode.h>
#include <xrpl/shamap/SHAMapNodeID.h>
#include <xrpl/shamap/SHAMapTreeNode.h>
namespace xrpl {
/**
* @brief Validates a ledger node based on the fixLedgerNodeID amendment status.
*
* This function checks whether a ledger node has the expected fields based on
* whether the fixLedgerNodeID amendment is enabled. The validation rules differ
* depending on the amendment state:
*
* When the amendment is enabled:
* - The node must have `nodedata`.
* - The legacy `nodeid` field must NOT be present.
* - For inner nodes: the `id` field must be present.
* - For leaf nodes: the `depth` field must be present and <= SHAMap::leafDepth.
*
* When the amendment is disabled:
* - The node must have `nodedata`.
* - The `nodeid` field must be present.
* - The new `id` and `depth` fields must NOT be present.
*
* @param app The application instance used to check amendment status.
* @param ledger_node The ledger node to validate.
* @return true if the ledger node has the expected fields, false otherwise.
*/
bool
validateLedgerNode(Application& app, protocol::TMLedgerNode const& ledger_node);
/**
* @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 std::nullopt instead of throwing an exception.
*
* @param data The serialized node data in wire format.
* @return An optional containing the deserialized tree node if successful, or
* std::nullopt if deserialization fails.
*/
std::optional<intr_ptr::SharedPtr<SHAMapTreeNode>>
getTreeNode(std::string const& data);
/**
* @brief Extracts or reconstructs the SHAMapNodeID from a ledger node.
*
* This function retrieves the SHAMapNodeID for a tree node, with behavior that
* depends on the fixLedgerNodeID amendment status and the node type (inner vs.
* leaf).
*
* When the fixLedgerNodeID amendment is enabled:
* - For inner nodes: Deserializes the node ID from the `ledger_node.id` field.
* Note that root nodes are also inner nodes.
* - For leaf nodes: Reconstructs the node ID using both the depth from the
* `ledger_node.depth` field and the key from the leaf node's item.
*
* When the amendment is disabled:
* - For all nodes: Deserializes the node ID from the `ledger_node.nodeid`
* field.
* - For leaf nodes: Validates that the node ID is consistent with the leaf's
* key.
*
* @param app The application instance used to check amendment status.
* @param ledger_node The 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.
*/
std::optional<SHAMapNodeID>
getSHAMapNodeID(
Application& app,
protocol::TMLedgerNode const& ledger_node,
intr_ptr::SharedPtr<SHAMapTreeNode> const& treeNode);
} // namespace xrpl

View File

@@ -144,7 +144,7 @@ TransactionAcquire::trigger(std::shared_ptr<Peer> const& peer)
SHAMapAddNode
TransactionAcquire::takeNodes(
std::vector<std::pair<SHAMapNodeID, intr_ptr::SharedPtr<SHAMapTreeNode>>>& data,
std::vector<std::pair<SHAMapNodeID, Slice>> const& data,
std::shared_ptr<Peer> const& peer)
{
ScopedLockType sl(mtx_);
@@ -168,22 +168,20 @@ TransactionAcquire::takeNodes(
ConsensusTransSetSF sf(app_, app_.getTempNodeCache());
for (auto& d : data)
for (auto const& d : data)
{
if (d.first.isRoot() && mHaveRoot)
{
JLOG(journal_.debug()) << "Got root TXS node, already have it";
continue;
}
if (d.first.isRoot())
{
if (!mMap->addRootNode(SHAMapHash{hash_}, std::move(d.second), nullptr).isGood())
if (mHaveRoot)
JLOG(journal_.debug()) << "Got root TXS node, already have it";
else if (!mMap->addRootNode(SHAMapHash{hash_}, d.second, nullptr).isGood())
{
JLOG(journal_.warn()) << "TX acquire got bad root node";
}
else
mHaveRoot = true;
}
else if (!mMap->addKnownNode(d.first, std::move(d.second), &sf).isGood())
else if (!mMap->addKnownNode(d.first, d.second, &sf).isGood())
{
JLOG(journal_.warn()) << "TX acquire got bad non-root node";
return SHAMapAddNode::invalid();

View File

@@ -19,9 +19,7 @@ public:
~TransactionAcquire() = default;
SHAMapAddNode
takeNodes(
std::vector<std::pair<SHAMapNodeID, intr_ptr::SharedPtr<SHAMapTreeNode>>>& data,
std::shared_ptr<Peer> const&);
takeNodes(std::vector<std::pair<SHAMapNodeID, Slice>> const& data, std::shared_ptr<Peer> const&);
void
init(int startPeers);

View File

@@ -15,7 +15,6 @@
#include <xrpl/basics/safe_cast.h>
#include <xrpl/core/HashRouter.h>
#include <xrpl/core/PerfLog.h>
#include <xrpl/ledger/AmendmentTable.h>
#include <xrpl/protocol/TxFlags.h>
#include <xrpl/protocol/digest.h>
#include <xrpl/server/LoadFeeTrack.h>
@@ -3132,7 +3131,7 @@ PeerImp::processLedgerRequest(std::shared_ptr<protocol::TMGetLedger> const& m)
{
auto const queryDepth{m->has_querydepth() ? m->querydepth() : (isHighLatency() ? 2 : 1)};
std::vector<std::tuple<SHAMapNodeID, Blob, bool>> data;
std::vector<std::pair<SHAMapNodeID, Blob>> data;
for (int i = 0; i < m->nodeids_size() && ledgerData.nodes_size() < Tuning::softMaxReplyNodes; ++i)
{
@@ -3151,21 +3150,9 @@ PeerImp::processLedgerRequest(std::shared_ptr<protocol::TMGetLedger> const& m)
{
if (ledgerData.nodes_size() >= Tuning::hardMaxReplyNodes)
break;
protocol::TMLedgerNode* node{ledgerData.add_nodes()};
auto const& node_data = std::get<1>(d);
node->set_nodedata(node_data.data(), node_data.size());
// When the amendment is disabled, we always set the node ID. However, when the amendment is
// enabled, we only set it for inner nodes, while for leaf nodes we set the node depth instead.
auto const& nodeID = std::get<0>(d);
if (!app_.getAmendmentTable().isEnabled(fixLedgerNodeID))
node->set_nodeid(nodeID.getRawString());
else if (std::get<2>(d))
node->set_depth(nodeID.getDepth());
else
node->set_id(nodeID.getRawString());
node->set_nodeid(d.first.getRawString());
node->set_nodedata(d.second.data(), d.second.size());
}
}
else