Skip to content

Commit

Permalink
Checks (RIPD-1487):
Browse files Browse the repository at this point in the history
Introduce a new ledger type: ltCHECK
Introduce three new transactions that operate on checks:

- "CheckCreate" which adds the check entry to the ledger.  The
  check is a promise from the source of the check that the
  destination of the check may cash the check and receive up to
  the SendMax specified on the check.  The check may have an
  expiration, after which the check may no longer be cashed.

- "CheckCash" is a request by the destination of the check to
  transfer a requested amount of funds, up to the check's SendMax,
  from the source to the destination.  The destination may receive
  less than the SendMax due to transfer fees.

  When cashing a check, the destination specifies the smallest
  amount of funds that will be acceptable.  If the transfer
  completes and delivers the requested amount, then the check is
  considered cashed and removed from the ledger.  If enough funds
  cannot be delivered, then the transaction fails and the check
  remains in the ledger.

  Attempting to cash the check after its expiration will fail.

- "CheckCancel" removes the check from the ledger without
  transferring funds.  Either the check's source or destination
  can cancel the check at any time.  After a check has expired,
  any account can cancel the check.

Facilities related to checks are on the "Checks" amendment.
  • Loading branch information
scottschurr committed Jan 17, 2018
1 parent 76ad06e commit 2d5ddbf
Show file tree
Hide file tree
Showing 31 changed files with 2,985 additions and 111 deletions.
22 changes: 22 additions & 0 deletions Builds/VisualStudio2015/RippleD.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,12 @@
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\BookTip.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CancelCheck.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CancelCheck.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CancelOffer.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
Expand All @@ -1321,12 +1327,24 @@
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CancelTicket.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CashCheck.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CashCheck.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\Change.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\Change.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CreateCheck.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CreateCheck.h">
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CreateOffer.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
Expand Down Expand Up @@ -4634,6 +4652,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\test\app\Check_test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\test\app\CrossingLimits_test.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
Expand Down
21 changes: 21 additions & 0 deletions Builds/VisualStudio2015/RippleD.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,12 @@
<ClInclude Include="..\..\src\ripple\app\tx\impl\BookTip.h">
<Filter>ripple\app\tx\impl</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CancelCheck.cpp">
<Filter>ripple\app\tx\impl</Filter>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CancelCheck.h">
<Filter>ripple\app\tx\impl</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CancelOffer.cpp">
<Filter>ripple\app\tx\impl</Filter>
</ClCompile>
Expand All @@ -1890,12 +1896,24 @@
<ClInclude Include="..\..\src\ripple\app\tx\impl\CancelTicket.h">
<Filter>ripple\app\tx\impl</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CashCheck.cpp">
<Filter>ripple\app\tx\impl</Filter>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CashCheck.h">
<Filter>ripple\app\tx\impl</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\Change.cpp">
<Filter>ripple\app\tx\impl</Filter>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\Change.h">
<Filter>ripple\app\tx\impl</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CreateCheck.cpp">
<Filter>ripple\app\tx\impl</Filter>
</ClCompile>
<ClInclude Include="..\..\src\ripple\app\tx\impl\CreateCheck.h">
<Filter>ripple\app\tx\impl</Filter>
</ClInclude>
<ClCompile Include="..\..\src\ripple\app\tx\impl\CreateOffer.cpp">
<Filter>ripple\app\tx\impl</Filter>
</ClCompile>
Expand Down Expand Up @@ -5592,6 +5610,9 @@
<ClCompile Include="..\..\src\test\app\AmendmentTable_test.cpp">
<Filter>test\app</Filter>
</ClCompile>
<ClCompile Include="..\..\src\test\app\Check_test.cpp">
<Filter>test\app</Filter>
</ClCompile>
<ClCompile Include="..\..\src\test\app\CrossingLimits_test.cpp">
<Filter>test\app</Filter>
</ClCompile>
Expand Down
136 changes: 136 additions & 0 deletions src/ripple/app/tx/impl/CancelCheck.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2017 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 <BeastConfig.h>
#include <ripple/app/tx/impl/CancelCheck.h>
#include <ripple/app/ledger/Ledger.h>
#include <ripple/basics/Log.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/TER.h>
#include <ripple/protocol/TxFlags.h>

namespace ripple {

TER
CancelCheck::preflight (PreflightContext const& ctx)
{
if (! ctx.rules.enabled (featureChecks))
return temDISABLED;

TER const ret {preflight1 (ctx)};
if (! isTesSuccess (ret))
return ret;

if (ctx.tx.getFlags() & tfUniversalMask)
{
// There are no flags (other than universal) for CreateCheck yet.
JLOG(ctx.j.warn()) << "Malformed transaction: Invalid flags set.";
return temINVALID_FLAG;
}

return preflight2 (ctx);
}

TER
CancelCheck::preclaim (PreclaimContext const& ctx)
{
auto const sleCheck = ctx.view.read (keylet::check (ctx.tx[sfCheckID]));
if (! sleCheck)
{
JLOG(ctx.j.warn()) << "Check does not exist.";
return tecNO_ENTRY;
}

using duration = NetClock::duration;
using timepoint = NetClock::time_point;
auto const optExpiry = (*sleCheck)[~sfExpiration];

// Expiration is defined in terms of the close time of the parent
// ledger, because we definitively know the time that it closed but
// we do not know the closing time of the ledger that is under
// construction.
if (! optExpiry ||
(ctx.view.parentCloseTime() < timepoint {duration {*optExpiry}}))
{
// If the check is not yet expired, then only the creator or the
// destination may cancel the check.
AccountID const acctId {ctx.tx[sfAccount]};
if (acctId != (*sleCheck)[sfAccount] &&
acctId != (*sleCheck)[sfDestination])
{
JLOG(ctx.j.warn()) << "Check is not expired and canceler is "
"neither check source nor destination.";
return tecNO_PERMISSION;
}
}
return tesSUCCESS;
}

TER
CancelCheck::doApply ()
{
uint256 const checkId {ctx_.tx[sfCheckID]};
auto const sleCheck = view().peek (keylet::check (checkId));
if (! sleCheck)
{
// Error should have been caught in preclaim.
JLOG(j_.warn()) << "Check does not exist.";
return tecNO_ENTRY;
}

AccountID const srcId {sleCheck->getAccountID (sfAccount)};
AccountID const dstId {sleCheck->getAccountID (sfDestination)};
auto viewJ = ctx_.app.journal ("View");

// If the check is not written to self (and it shouldn't be), remove the
// check from the destination account root.
if (srcId != dstId)
{
std::uint64_t const page {(*sleCheck)[sfDestinationNode]};
TER const ter {dirDelete (view(), true, page,
keylet::ownerDir (dstId), checkId, false, false, viewJ)};
if (! isTesSuccess (ter))
{
JLOG(j_.warn()) << "Unable to delete check from destination.";
return ter;
}
}
{
std::uint64_t const page {(*sleCheck)[sfOwnerNode]};
TER const ter {dirDelete (view(), true, page,
keylet::ownerDir (srcId), checkId, false, false, viewJ)};
if (! isTesSuccess (ter))
{
JLOG(j_.warn()) << "Unable to delete check from owner.";
return ter;
}
}

// If we succeeded, update the check owner's reserve.
auto const sleSrc = view().peek (keylet::account (srcId));
adjustOwnerCount (view(), sleSrc, -1, viewJ);

// Remove check from ledger.
view().erase (sleCheck);
return tesSUCCESS;
}

} // namespace ripple
49 changes: 49 additions & 0 deletions src/ripple/app/tx/impl/CancelCheck.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2017 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_TX_CANCELCHECK_H_INCLUDED
#define RIPPLE_TX_CANCELCHECK_H_INCLUDED

#include <ripple/app/tx/impl/Transactor.h>

namespace ripple {

class CancelCheck
: public Transactor
{
public:
CancelCheck (ApplyContext& ctx)
: Transactor (ctx)
{
}

static
TER
preflight (PreflightContext const& ctx);

static
TER
preclaim (PreclaimContext const& ctx);

TER doApply () override;
};

}

#endif
Loading

0 comments on commit 2d5ddbf

Please sign in to comment.