Skip to content

Commit

Permalink
[FOLD] Keylet: getQualityIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
nbougalis committed Apr 11, 2020
1 parent bb81256 commit 99b4ea2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
14 changes: 3 additions & 11 deletions src/ripple/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,9 @@ Keylet offer(
/** @} */

/** The initial directory page for a specific quality */
struct quality_t
{
explicit quality_t() = default;

Keylet operator()(Keylet const& k,
std::uint64_t q) const;
};
static quality_t const quality {};
Keylet quality(
Keylet const& k,
std::uint64_t q) noexcept;

/** The directory for the next lower quality */
struct next_t
Expand Down Expand Up @@ -227,9 +222,6 @@ payChan(AccountID const& src, AccountID const& dst, std::uint32_t seq) noexcept;
uint256
getBookBase (Book const& book);

uint256
getQualityIndex (uint256 const& uBase, const std::uint64_t uNodeDir = 0);

uint256
getQualityNext (uint256 const& uBase);

Expand Down
45 changes: 22 additions & 23 deletions src/ripple/protocol/impl/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,16 @@ uint256
getBookBase (Book const& book)
{
assert (isConsistent (book));
// Return with quality 0.
return getQualityIndex(
indexHash(LedgerNameSpace::BOOK_DIR,
book.in.currency, book.out.currency,
book.in.account, book.out.account));
}

uint256
getQualityIndex (uint256 const& uBase, const std::uint64_t uNodeDir)
{
// Indexes are stored in big endian format: they print as hex as stored.
// Most significant bytes are first. Least significant bytes represent
// adjacent entries. We place uNodeDir in the 8 right most bytes to be
// adjacent. Want uNodeDir in big endian format so ++ goes to the next
// entry for indexes.
uint256 uNode (uBase);
auto const index = indexHash(LedgerNameSpace::BOOK_DIR,
book.in.currency, book.out.currency,
book.in.account, book.out.account);

// TODO(tom): there must be a better way.
// VFALCO [base_uint] This assumes a certain storage format
((std::uint64_t*) uNode.end ())[-1] = boost::endian::native_to_big (uNodeDir);
// Return with quality 0.
auto k = keylet::quality(
{ ltDIR_NODE, index }, 0);

return uNode;
return k.key;
}

uint256
Expand Down Expand Up @@ -206,12 +194,23 @@ Keylet offer(
indexHash(LedgerNameSpace::OFFER, id, seq) };
}

Keylet quality_t::operator()(Keylet const& k,
std::uint64_t q) const
Keylet quality(
Keylet const& k,
std::uint64_t q) noexcept
{
assert(k.type == ltDIR_NODE);
return { ltDIR_NODE,
getQualityIndex(k.key, q) };

// Indexes are stored in big endian format: they print as hex as stored.
// Most significant bytes are first and the least significant bytes
// represent adjacent entries. We place the quality, in big endian format,
// in the 8 right most bytes; this way, incrementing goes to the next entry
// for indexes.
uint256 x = k.key;

// FIXME This is ugly and we can and should do better...
((std::uint64_t*)x.end())[-1] = boost::endian::native_to_big (q);

return { ltDIR_NODE, x };
}

Keylet next_t::operator()(Keylet const& k) const
Expand Down

0 comments on commit 99b4ea2

Please sign in to comment.