rippled
CancelTicket.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/tx/impl/CancelTicket.h>
21 #include <ripple/basics/Log.h>
22 #include <ripple/ledger/View.h>
23 #include <ripple/protocol/Feature.h>
24 #include <ripple/protocol/Indexes.h>
25 #include <ripple/protocol/TxFlags.h>
26 
27 namespace ripple {
28 
29 NotTEC
31 {
32  if (!ctx.rules.enabled(featureTickets))
33  return temDISABLED;
34 
35  if (ctx.tx.getFlags() & tfUniversalMask)
36  return temINVALID_FLAG;
37 
38  auto const ret = preflight1(ctx);
39  if (!isTesSuccess(ret))
40  return ret;
41 
42  return preflight2(ctx);
43 }
44 
45 TER
47 {
48  uint256 const ticketId = ctx_.tx.getFieldH256(sfTicketID);
49 
50  // VFALCO This is highly suspicious, we're requiring that the
51  // transaction provide the return value of getTicketIndex?
52  SLE::pointer sleTicket = view().peek(keylet::ticket(ticketId));
53 
54  if (!sleTicket)
55  return tecNO_ENTRY;
56 
57  auto const ticket_owner = sleTicket->getAccountID(sfAccount);
58 
59  bool authorized = account_ == ticket_owner;
60 
61  // The target can also always remove a ticket
62  if (!authorized && sleTicket->isFieldPresent(sfTarget))
63  authorized = (account_ == sleTicket->getAccountID(sfTarget));
64 
65  // And finally, anyone can remove an expired ticket
66  if (!authorized && sleTicket->isFieldPresent(sfExpiration))
67  {
68  using tp = NetClock::time_point;
69  using d = tp::duration;
70  auto const expiration = tp{d{sleTicket->getFieldU32(sfExpiration)}};
71 
72  if (view().parentCloseTime() >= expiration)
73  authorized = true;
74  }
75 
76  if (!authorized)
77  return tecNO_PERMISSION;
78 
79  std::uint64_t const hint(sleTicket->getFieldU64(sfOwnerNode));
80 
81  if (!ctx_.view().dirRemove(
82  keylet::ownerDir(ticket_owner), hint, ticketId, false))
83  {
84  return tefBAD_LEDGER;
85  }
86 
87  auto viewJ = ctx_.app.journal("View");
89  view(), view().peek(keylet::account(ticket_owner)), -1, viewJ);
90  ctx_.view().erase(sleTicket);
91 
92  return tesSUCCESS;
93 }
94 
95 } // namespace ripple
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
Definition: Indexes.cpp:285
ripple::preflight2
NotTEC preflight2(PreflightContext const &ctx)
Checks whether the signature appears valid.
Definition: Transactor.cpp:90
ripple::sfTarget
const SF_Account sfTarget(access, STI_ACCOUNT, 7, "Target")
Definition: SField.h:486
ripple::sfTicketID
const SF_U256 sfTicketID(access, STI_HASH256, 20, "TicketID")
Definition: SField.h:432
std::shared_ptr< STLedgerEntry >
ripple::ApplyView::peek
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
ripple::isTesSuccess
bool isTesSuccess(TER x)
Definition: TER.h:576
ripple::ApplyView::erase
virtual void erase(std::shared_ptr< SLE > const &sle)=0
Remove a peeked SLE.
ripple::sfAccount
const SF_Account sfAccount(access, STI_ACCOUNT, 1, "Account")
Definition: SField.h:480
ripple::keylet::ticket
static const ticket_t ticket
Definition: Indexes.h:163
ripple::sfOwnerNode
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
Definition: SField.h:398
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:249
ripple::preflight1
NotTEC preflight1(PreflightContext const &ctx)
Performs early sanity checks on the account and fee fields.
Definition: Transactor.cpp:56
ripple::ApplyContext::app
Application & app
Definition: ApplyContext.h:47
ripple::ApplyView::dirRemove
bool dirRemove(Keylet const &directory, std::uint64_t page, uint256 const &key, bool keepRoot)
Remove an entry from a directory.
Definition: ApplyView.cpp:189
ripple::base_uint< 256 >
ripple::temINVALID_FLAG
@ temINVALID_FLAG
Definition: TER.h:106
ripple::tefBAD_LEDGER
@ tefBAD_LEDGER
Definition: TER.h:146
ripple::adjustOwnerCount
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
Definition: View.cpp:642
ripple::authorized
static bool authorized(Port const &port, std::map< std::string, std::string > const &h)
Definition: ServerHandlerImp.cpp:85
ripple::keylet::account
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition: Indexes.cpp:121
ripple::TERSubset< CanCvtToTER >
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:454
std::uint64_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:103
ripple::featureTickets
const uint256 featureTickets
Definition: Feature.cpp:163
ripple::sfExpiration
const SF_U32 sfExpiration(access, STI_UINT32, 10, "Expiration")
Definition: SField.h:362
ripple::ApplyContext::view
ApplyView & view()
Definition: ApplyContext.h:54
ripple::CancelTicket::preflight
static NotTEC preflight(PreflightContext const &ctx)
Definition: CancelTicket.cpp:30
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::journal
virtual beast::Journal journal(std::string const &name)=0
ripple::Transactor::view
ApplyView & view()
Definition: Transactor.h:107
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:109
ripple::tecNO_PERMISSION
@ tecNO_PERMISSION
Definition: TER.h:263
ripple::Transactor::ctx_
ApplyContext & ctx_
Definition: Transactor.h:89
ripple::Transactor::account_
AccountID account_
Definition: Transactor.h:92
ripple::tecNO_ENTRY
@ tecNO_ENTRY
Definition: TER.h:264
ripple::tfUniversalMask
const std::uint32_t tfUniversalMask
Definition: TxFlags.h:50
ripple::PreflightContext::tx
STTx const & tx
Definition: Transactor.h:36
ripple::PreflightContext
State information when preflighting a tx.
Definition: Transactor.h:32
ripple::PreflightContext::rules
const Rules rules
Definition: Transactor.h:37
ripple::CancelTicket::doApply
TER doApply() override
Definition: CancelTicket.cpp:46
ripple::NetClock::time_point
std::chrono::time_point< NetClock > time_point
Definition: chrono.h:54
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:213
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:48
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:507
ripple::STObject::getFieldH256
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:556