Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strongly Typed Wrapper for Escrow objects #27

Open
wants to merge 2 commits into
base: wrapper
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions src/ripple/app/tx/impl/Escrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <ripple/basics/Log.h>
#include <ripple/basics/XRPAmount.h>
#include <ripple/basics/chrono.h>
#include <ripple/basics/safe_cast.h>
#include <ripple/conditions/Condition.h>
#include <ripple/conditions/Fulfillment.h>
#include <ripple/ledger/ApplyView.h>
Expand All @@ -32,7 +31,8 @@
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/TxFlags.h>
#include <ripple/protocol/digest.h>
#include <ripple/protocol/st.h>

#include <ripple/protocol/Escrow.h>

// During an EscrowFinish, the transaction must specify both
// a condition and a fulfillment. We track whether that
Expand Down Expand Up @@ -228,9 +228,9 @@ EscrowCreate::doApply()
return tecNO_TARGET;
}

// Create escrow in ledger. Note that we we use the value from the
// Create escrow in ledger. Note that we use the value from the
// sequence or ticket. For more explanation see comments in SeqProxy.h.
Keylet const escrowKeylet =
EscrowKeylet const escrowKeylet =
keylet::escrow(account, ctx_.tx.getSeqProxy().value());
auto const slep = std::make_shared<SLE>(escrowKeylet);
(*slep)[sfAmount] = ctx_.tx[sfAmount];
Expand Down Expand Up @@ -355,8 +355,8 @@ TER
EscrowFinish::doApply()
{
auto const k = keylet::escrow(ctx_.tx[sfOwner], ctx_.tx[sfOfferSequence]);
auto const slep = ctx_.view().peekSLE(k);
if (!slep)
std::optional<Escrow> escrowObject = ctx_.view().peek(k);
if (!escrowObject)
return tecNO_TARGET;

// If a cancel time is present, a finish operation should only succeed prior
Expand All @@ -367,25 +367,27 @@ EscrowFinish::doApply()
auto const now = ctx_.view().info().parentCloseTime;

// Too soon: can't execute before the finish time
if ((*slep)[~sfFinishAfter] && !after(now, (*slep)[sfFinishAfter]))
if (escrowObject->finishTime() &&
!after(now, *escrowObject->finishTime()))
return tecNO_PERMISSION;

// Too late: can't execute after the cancel time
if ((*slep)[~sfCancelAfter] && after(now, (*slep)[sfCancelAfter]))
if (escrowObject->cancelTime() &&
after(now, *escrowObject->cancelTime()))
return tecNO_PERMISSION;
}
else
{
// Too soon?
if ((*slep)[~sfFinishAfter] &&
if (escrowObject->finishTime() &&
ctx_.view().info().parentCloseTime.time_since_epoch().count() <=
(*slep)[sfFinishAfter])
escrowObject->finishTime())
return tecNO_PERMISSION;

// Too late?
if ((*slep)[~sfCancelAfter] &&
if (escrowObject->cancelTime() &&
ctx_.view().info().parentCloseTime.time_since_epoch().count() <=
(*slep)[sfCancelAfter])
escrowObject->cancelTime())
return tecNO_PERMISSION;
}

Expand Down Expand Up @@ -420,7 +422,7 @@ EscrowFinish::doApply()
return tecCRYPTOCONDITION_ERROR;

// Check against condition in the ledger entry:
auto const cond = (*slep)[~sfCondition];
auto const cond = escrowObject->checkCondition();

// If a condition wasn't specified during creation,
// one shouldn't be included now.
Expand All @@ -437,7 +439,7 @@ EscrowFinish::doApply()
}

// NOTE: Escrow payments cannot be used to fund accounts.
AccountID const destID = (*slep)[sfDestination];
AccountID const destID = escrowObject->getEscrowRecipient();
auto destAcctRoot = ctx_.view().peek(keylet::account(destID));
if (!destAcctRoot)
return tecNO_DST;
Expand All @@ -459,11 +461,11 @@ EscrowFinish::doApply()
}
}

AccountID const account = (*slep)[sfAccount];
AccountID const account = escrowObject->accountID();

// Remove escrow from owner directory
{
auto const page = (*slep)[sfOwnerNode];
auto const page = escrowObject->getOwnerNode();
if (!ctx_.view().dirRemove(
keylet::ownerDir(account), page, k.key, true))
{
Expand All @@ -473,7 +475,7 @@ EscrowFinish::doApply()
}

// Remove escrow from recipient's owner directory, if present.
if (auto const optPage = (*slep)[~sfDestinationNode])
if (auto const optPage = escrowObject->getRecipientNode())
{
if (!ctx_.view().dirRemove(
keylet::ownerDir(destID), *optPage, k.key, true))
Expand All @@ -484,7 +486,7 @@ EscrowFinish::doApply()
}

// Transfer amount to destination
destAcctRoot->setBalance(destAcctRoot->balance() + (*slep)[sfAmount]);
destAcctRoot->setBalance(destAcctRoot->balance() + escrowObject->amount());
ctx_.view().update(*destAcctRoot);

// Adjust source owner count
Expand All @@ -493,7 +495,7 @@ EscrowFinish::doApply()
ctx_.view().update(*acctRoot);

// Remove escrow from ledger
ctx_.view().erase(slep);
ctx_.view().erase(*escrowObject);

return tesSUCCESS;
}
Expand All @@ -516,36 +518,36 @@ TER
EscrowCancel::doApply()
{
auto const k = keylet::escrow(ctx_.tx[sfOwner], ctx_.tx[sfOfferSequence]);
auto const slep = ctx_.view().peekSLE(k);
if (!slep)
std::optional<Escrow> escrowObject = ctx_.view().peek(k);
if (!escrowObject)
return tecNO_TARGET;

if (ctx_.view().rules().enabled(fix1571))
{
auto const now = ctx_.view().info().parentCloseTime;

// No cancel time specified: can't execute at all.
if (!(*slep)[~sfCancelAfter])
if (!escrowObject->cancelTime())
return tecNO_PERMISSION;

// Too soon: can't execute before the cancel time.
if (!after(now, (*slep)[sfCancelAfter]))
if (!after(now, *escrowObject->cancelTime()))
return tecNO_PERMISSION;
}
else
{
// Too soon?
if (!(*slep)[~sfCancelAfter] ||
if (!escrowObject->cancelTime() ||
ctx_.view().info().parentCloseTime.time_since_epoch().count() <=
(*slep)[sfCancelAfter])
escrowObject->cancelTime())
return tecNO_PERMISSION;
}

AccountID const account = (*slep)[sfAccount];
AccountID const account = escrowObject->accountID();

// Remove escrow from owner directory
{
auto const page = (*slep)[sfOwnerNode];
auto const page = escrowObject->getOwnerNode();
if (!ctx_.view().dirRemove(
keylet::ownerDir(account), page, k.key, true))
{
Expand All @@ -555,10 +557,10 @@ EscrowCancel::doApply()
}

// Remove escrow from recipient's owner directory, if present.
if (auto const optPage = (*slep)[~sfDestinationNode]; optPage)
if (auto const optPage = escrowObject->getRecipientNode(); optPage)
{
if (!ctx_.view().dirRemove(
keylet::ownerDir((*slep)[sfDestination]),
keylet::ownerDir(escrowObject->getEscrowRecipient()),
*optPage,
k.key,
true))
Expand All @@ -570,12 +572,12 @@ EscrowCancel::doApply()

// Transfer amount back to owner, decrement owner count
auto acctRoot = ctx_.view().peek(keylet::account(account));
acctRoot->setBalance(acctRoot->balance() + (*slep)[sfAmount]);
acctRoot->setBalance(acctRoot->balance() + escrowObject->amount());
adjustOwnerCount(ctx_.view(), *acctRoot, -1, ctx_.journal);
ctx_.view().update(*acctRoot);

// Remove escrow from ledger
ctx_.view().erase(slep);
ctx_.view().erase(*escrowObject);

return tesSUCCESS;
}
Expand Down
115 changes: 115 additions & 0 deletions src/ripple/protocol/Escrow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2023 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.
*/
//==============================================================================

#ifndef RIPPLE_PROTOCOL_ESCROW_H_INCLUDED
#define RIPPLE_PROTOCOL_ESCROW_H_INCLUDED

#include <ripple/protocol/LedgerEntryWrapper.h>

namespace ripple {

template <bool Writable>
class EscrowImpl final : public LedgerEntryWrapper<Writable>
{
private:
using Base = LedgerEntryWrapper<Writable>;
using SleT = typename Base::SleT;
using Base::wrapped_;

// This constructor is private so only the factory functions can
// construct an EscrowImpl.
EscrowImpl(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 EscrowImpl<true> to EscrowImpl<false>.
operator EscrowImpl<true>() const
{
return EscrowImpl<false>(
std::const_pointer_cast<std::shared_ptr<STLedgerEntry const>>(
wrapped_));
}

[[nodiscard]] std::optional<uint32_t>
finishTime() const
{
return wrapped_->at(~sfFinishAfter);
}

[[nodiscard]] std::optional<uint32_t>
cancelTime() const
{
return wrapped_->at(~sfCancelAfter);
}

[[nodiscard]] const std::optional<ripple::Slice>
checkCondition() const
{
return wrapped_->at(~sfCondition);
}

[[nodiscard]] AccountID
getEscrowRecipient() const
{
return wrapped_->at(sfDestination);
}

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

// This function returns the appropriate page from inside a ledger object.
[[nodiscard]] std::uint64_t
getOwnerNode() const
{
return wrapped_->at(sfOwnerNode);
}

// Keshava: Should I explicitly specify the return type or use auto keyword?
// returns ripple::OptionalProxy<ripple::STInteger<unsigned long long>>
// but OptionalProxy is not accesible from this file
[[nodiscard]] auto
getRecipientNode() const
{
return wrapped_->at(~sfDestinationNode);
}

[[nodiscard]] STAmount
amount() const
{
return wrapped_->at(sfAmount);
}
};

using Escrow = EscrowImpl<true>;
using EscrowRd = EscrowImpl<false>;

} // namespace ripple

#endif // RIPPLE_PROTOCOL_ESCROW_H_INCLUDED
2 changes: 1 addition & 1 deletion src/ripple/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ page(Keylet const& root, std::uint64_t index = 0) noexcept
/** @} */

/** An escrow entry */
Keylet
EscrowKeylet
escrow(AccountID const& src, std::uint32_t seq) noexcept;

/** A PaymentChannel */
Expand Down
15 changes: 15 additions & 0 deletions src/ripple/protocol/Keylet.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ static_assert(std::is_move_assignable_v<AccountRootKeylet>);
static_assert(std::is_nothrow_destructible_v<AccountRootKeylet>);
#endif

template <bool>
class EscrowImpl;

struct EscrowKeylet final : public KeyletBase
{
template <bool Writable>
using TWrapped = EscrowImpl<Writable>;

using KeyletBase::check;

EscrowKeylet(uint256 const& key) : KeyletBase(ltESCROW, key)
{
}
};

} // namespace ripple

#endif
4 changes: 2 additions & 2 deletions src/ripple/protocol/impl/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ page(uint256 const& key, std::uint64_t index) noexcept
return {ltDIR_NODE, indexHash(LedgerNameSpace::DIR_NODE, key, index)};
}

Keylet
EscrowKeylet
escrow(AccountID const& src, std::uint32_t seq) noexcept
{
return {ltESCROW, indexHash(LedgerNameSpace::ESCROW, src, seq)};
return {indexHash(LedgerNameSpace::ESCROW, src, seq)};
}

Keylet
Expand Down