Skip to content

Commit

Permalink
Introduce Strongly typed DepositPreAuth wrapper. A new DepositPreAuth…
Browse files Browse the repository at this point in the history
…Keylet is used to distinguish between various other types of SLEs

update readSLE and peekSLE function calls associated with keylet::depositPreAuth into the new read, peek functions
  • Loading branch information
scottschurr authored and ckeshava committed Jun 9, 2023
1 parent c6d98e6 commit f5e48bf
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 18 deletions.
14 changes: 7 additions & 7 deletions src/ripple/app/tx/impl/DepositPreauth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ DepositPreauth::doApply()
// Preclaim already verified that the Preauth entry does not yet exist.
// Create and populate the Preauth entry.
AccountID const auth{ctx_.tx[sfAuthorize]};
Keylet const preauthKeylet = keylet::depositPreauth(account_, auth);
auto const preauthKeylet = keylet::depositPreauth(account_, auth);
auto slePreauth = std::make_shared<SLE>(preauthKeylet);

slePreauth->setAccountID(sfAccount, account_);
Expand Down Expand Up @@ -171,16 +171,16 @@ DepositPreauth::removeFromLedger(
{
// Verify that the Preauth entry they asked to remove is
// in the ledger.
std::shared_ptr<SLE> const slePreauth{
view.peekSLE(keylet::depositPreauth(preauthIndex))};
if (!slePreauth)
std::optional<DepPreAuth> preAuth{
view.peek(keylet::depositPreauth(preauthIndex))};
if (!preAuth)
{
JLOG(j.warn()) << "Selected DepositPreauth does not exist.";
return tecNO_ENTRY;
}

AccountID const account{(*slePreauth)[sfAccount]};
std::uint64_t const page{(*slePreauth)[sfOwnerNode]};
AccountID const account{preAuth->accountID()};
std::uint64_t const page{preAuth->getOwner()};
if (!view.dirRemove(keylet::ownerDir(account), page, preauthIndex, false))
{
JLOG(j.fatal()) << "Unable to delete DepositPreauth from owner.";
Expand All @@ -195,7 +195,7 @@ DepositPreauth::removeFromLedger(
adjustOwnerCount(view, *ownerAcctRoot, -1, app.journal("View"));

// Remove DepositPreauth from ledger.
view.erase(slePreauth);
view.erase(*preAuth);

return tesSUCCESS;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ripple/ledger/ApplyView.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class ApplyView : public ReadView
std::optional<std::uint64_t>
dirInsert(
Keylet const& directory,
Keylet const& key,
KeyletBase const& key,
std::function<void(std::shared_ptr<SLE> const&)> const& describe)
{
return dirAdd(false, directory, key.key, describe);
Expand Down
73 changes: 73 additions & 0 deletions src/ripple/protocol/DepPreAuth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2021 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include <ripple/protocol/LedgerEntryWrapper.h>
#include <ripple/protocol/STAccount.h>

namespace ripple {

// Wrapper class for DepositPreAuth Ledger Object.
// This class encapsulates the std::shared_ptr<SLE> to the actual ledger object.
template <bool Writable>
class DepPreAuthImpl final : public LedgerEntryWrapper<Writable>
{
private:
using Base = LedgerEntryWrapper<Writable>;
using SleT = typename Base::SleT;
using Base::wrapped_;

DepPreAuthImpl(std::shared_ptr<SleT>&& w) : Base(std::move(w))
{
}

// Friend declarations of factory functions.
//
// For classes that contain factories we must declare the entire class
// as a friend unless the class declaration is visible at this point.
friend class ReadView;
friend class ApplyView;

public:
// Conversion operator from DepPreAuthImpl<true> to DepPreAuthImpl<false>.
operator DepPreAuthImpl<true>() const
{
return DepPreAuthImpl<false>(
std::const_pointer_cast<std::shared_ptr<STLedgerEntry const>>(
wrapped_));
}

[[nodiscard]] AccountID
accountID() const
{
return wrapped_->at(sfAccount);
}

[[nodiscard]] std::uint64_t
getOwner() const
{
return wrapped_->at(sfOwnerNode);
}
};

// The below type alias indicates a read-only non-writable DepPreAuth wrapper
// around the appropriate SLE
using DepPreAuthRd = DepPreAuthImpl<false>;
using DepPreAuth = DepPreAuthImpl<true>;

} // namespace ripple
6 changes: 3 additions & 3 deletions src/ripple/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ check(uint256 const& key) noexcept

/** A DepositPreauth */
/** @{ */
Keylet
DepPreAuthKeylet
depositPreauth(AccountID const& owner, AccountID const& preauthorized) noexcept;

inline Keylet
inline DepPreAuthKeylet
depositPreauth(uint256 const& key) noexcept
{
return {ltDEPOSIT_PREAUTH, key};
return DepPreAuthKeylet(key);
}
/** @} */

Expand Down
17 changes: 17 additions & 0 deletions src/ripple/protocol/Keylet.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <ripple/basics/base_uint.h>
#include <ripple/protocol/AcctRoot.h>
#include <ripple/protocol/DepPreAuth.h>
#include <ripple/protocol/LedgerFormats.h>

namespace ripple {
Expand Down Expand Up @@ -120,6 +121,22 @@ static_assert(std::is_move_assignable_v<AccountRootKeylet>);
static_assert(std::is_nothrow_destructible_v<AccountRootKeylet>);
#endif

template <bool>
class DepPreAuthImpl;

struct DepPreAuthKeylet final : public KeyletBase
{
template <bool Writable>
using TWrapped = DepPreAuthImpl<Writable>;

using KeyletBase::check;

explicit DepPreAuthKeylet(uint256 const& key)
: KeyletBase(ltDEPOSIT_PREAUTH, key)
{
}
};

} // namespace ripple

#endif
7 changes: 3 additions & 4 deletions src/ripple/protocol/impl/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,11 @@ check(AccountID const& id, std::uint32_t seq) noexcept
return {ltCHECK, indexHash(LedgerNameSpace::CHECK, id, seq)};
}

Keylet
DepPreAuthKeylet
depositPreauth(AccountID const& owner, AccountID const& preauthorized) noexcept
{
return {
ltDEPOSIT_PREAUTH,
indexHash(LedgerNameSpace::DEPOSIT_PREAUTH, owner, preauthorized)};
return DepPreAuthKeylet(
(indexHash(LedgerNameSpace::DEPOSIT_PREAUTH, owner, preauthorized)));
}

//------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions src/ripple/rpc/handlers/DepositAuthorized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ doDepositAuthorized(RPC::JsonContext& context)
if (destAcctRoot->isFlag(lsfDepositAuth))
{
// See if a preauthorization entry is in the ledger.
auto const sleDepositAuth =
ledger->readSLE(keylet::depositPreauth(dstAcct, srcAcct));
depositAuthorized = static_cast<bool>(sleDepositAuth);
depositAuthorized =
ledger->read(keylet::depositPreauth(dstAcct, srcAcct))
.has_value();
}
}
result[jss::source_account] = params[jss::source_account].asString();
Expand Down

0 comments on commit f5e48bf

Please sign in to comment.