refactor: Use named constant for leaf item size (#39) (#7130)

Co-authored-by: Ed Hennis <ed@ripple.com>
This commit is contained in:
Valentin Balaschenko
2026-05-13 14:53:01 +01:00
committed by GitHub
parent 411286c519
commit 4ad94ae2ff
3 changed files with 47 additions and 6 deletions

View File

@@ -19,7 +19,7 @@ SHAMapLeafNode::SHAMapLeafNode(boost::intrusive_ptr<SHAMapItem const> item, std:
: SHAMapTreeNode(cowid), item_(std::move(item))
{
XRPL_ASSERT(
item_->size() >= 12,
item_->size() >= kMIN_SHA_MAP_ITEM_BYTES,
"xrpl::SHAMapLeafNode::SHAMapLeafNode(boost::intrusive_ptr<"
"SHAMapItem const>, std::uint32_t) : minimum input size");
}
@@ -31,7 +31,7 @@ SHAMapLeafNode::SHAMapLeafNode(
: SHAMapTreeNode(cowid, hash), item_(std::move(item))
{
XRPL_ASSERT(
item_->size() >= 12,
item_->size() >= kMIN_SHA_MAP_ITEM_BYTES,
"xrpl::SHAMapLeafNode::SHAMapLeafNode(boost::intrusive_ptr<"
"SHAMapItem const>, std::uint32_t, SHAMapHash const&) : minimum input "
"size");

View File

@@ -28,6 +28,13 @@ namespace xrpl {
intr_ptr::SharedPtr<SHAMapTreeNode>
SHAMapTreeNode::makeTransaction(Slice data, SHAMapHash const& hash, bool hashValid)
{
if (data.size() < kMIN_SHA_MAP_ITEM_BYTES)
{
Throw<std::runtime_error>(
"Short TXN node: " + std::to_string(data.size()) + " bytes (minimum " +
std::to_string(kMIN_SHA_MAP_ITEM_BYTES) + " required)");
}
auto item = makeShamapitem(sha512Half(HashPrefix::TransactionId, data), data);
if (hashValid)
@@ -44,14 +51,30 @@ SHAMapTreeNode::makeTransactionWithMeta(Slice data, SHAMapHash const& hash, bool
uint256 tag;
if (s.size() < tag.kBYTES)
Throw<std::runtime_error>("Short TXN+MD node");
{
Throw<std::runtime_error>(
"Short TXN+MD node: " + std::to_string(s.size()) + " bytes (minimum " +
std::to_string(tag.kBYTES) + " required for tag)");
}
// FIXME: improve this interface so that the above check isn't needed
if (!s.getBitString(tag, s.size() - tag.kBYTES))
Throw<std::out_of_range>("Short TXN+MD node (" + std::to_string(s.size()) + ")");
{
Throw<std::out_of_range>(
"Short TXN+MD node: failed to read tag at offset " +
std::to_string(s.size() - tag.kBYTES));
}
s.chop(tag.kBYTES);
if (s.size() < kMIN_SHA_MAP_ITEM_BYTES)
{
Throw<std::runtime_error>(
"Short TXN+MD node: " + std::to_string(s.size()) +
" bytes after tag removal (minimum " + std::to_string(kMIN_SHA_MAP_ITEM_BYTES) +
" required)");
}
auto item = makeShamapitem(tag, s.slice());
if (hashValid)
@@ -68,17 +91,31 @@ SHAMapTreeNode::makeAccountState(Slice data, SHAMapHash const& hash, bool hashVa
uint256 tag;
if (s.size() < tag.kBYTES)
Throw<std::runtime_error>("short AS node");
{
Throw<std::runtime_error>(
"Short AS node: " + std::to_string(s.size()) + " bytes (minimum " +
std::to_string(tag.kBYTES) + " required for tag)");
}
// FIXME: improve this interface so that the above check isn't needed
if (!s.getBitString(tag, s.size() - tag.kBYTES))
Throw<std::out_of_range>("Short AS node (" + std::to_string(s.size()) + ")");
{
Throw<std::out_of_range>(
"Short AS node: failed to read tag at offset " + std::to_string(s.size() - tag.kBYTES));
}
s.chop(tag.kBYTES);
if (tag.isZero())
Throw<std::runtime_error>("Invalid AS node");
if (s.size() < kMIN_SHA_MAP_ITEM_BYTES)
{
Throw<std::runtime_error>(
"Short AS node: " + std::to_string(s.size()) + " bytes after tag removal (minimum " +
std::to_string(kMIN_SHA_MAP_ITEM_BYTES) + " required)");
}
auto item = makeShamapitem(tag, s.slice());
if (hashValid)