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/protocol/Feature.h>
23 #include <ripple/protocol/Indexes.h>
24 #include <ripple/protocol/TxFlags.h>
25 #include <ripple/ledger/View.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 =
58  sleTicket->getAccountID (sfAccount);
59 
60  bool authorized =
61  account_ == ticket_owner;
62 
63  // The target can also always remove a ticket
64  if (!authorized && sleTicket->isFieldPresent (sfTarget))
65  authorized = (account_ == sleTicket->getAccountID (sfTarget));
66 
67  // And finally, anyone can remove an expired ticket
68  if (!authorized && sleTicket->isFieldPresent (sfExpiration))
69  {
70  using tp = NetClock::time_point;
71  using d = tp::duration;
72  auto const expiration = tp{d{sleTicket->getFieldU32(sfExpiration)}};
73 
74  if (view().parentCloseTime() >= expiration)
75  authorized = true;
76  }
77 
78  if (!authorized)
79  return tecNO_PERMISSION;
80 
81  std::uint64_t const hint (sleTicket->getFieldU64 (sfOwnerNode));
82 
83  if (! ctx_.view().dirRemove(
84  keylet::ownerDir(ticket_owner), hint, ticketId, false))
85  {
86  return tefBAD_LEDGER;
87  }
88 
89  auto viewJ = ctx_.app.journal ("View");
90  adjustOwnerCount(view(), view().peek(
91  keylet::account(ticket_owner)), -1, viewJ);
92  ctx_.view ().erase (sleTicket);
93 
94  return tesSUCCESS;
95 }
96 
97 }
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:466
ripple::sfTicketID
const SF_U256 sfTicketID(access, STI_HASH256, 20, "TicketID")
Definition: SField.h:416
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:501
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:460
ripple::keylet::ticket
static const ticket_t ticket
Definition: Indexes.h:225
ripple::sfOwnerNode
const SF_U64 sfOwnerNode(access, STI_UINT64, 4, "OwnerNode")
Definition: SField.h:382
ripple::ReadView::parentCloseTime
NetClock::time_point parentCloseTime() const
Returns the close time of the previous ledger.
Definition: ReadView.h:251
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:44
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:109
ripple::tefBAD_LEDGER
@ tefBAD_LEDGER
Definition: TER.h:150
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:629
ripple::authorized
static bool authorized(Port const &port, std::map< std::string, std::string > const &h)
Definition: ServerHandlerImp.cpp:90
ripple::keylet::account
static const account_t account
Definition: Indexes.h:116
ripple::TERSubset< CanCvtToTER >
ripple::keylet::ownerDir
Keylet ownerDir(AccountID const &id)
The root page of an account's directory.
Definition: Indexes.cpp:318
ripple::STObject::getFlags
std::uint32_t getFlags() const
Definition: STObject.cpp:439
std::uint64_t
ripple::Rules::enabled
bool enabled(uint256 const &id) const
Returns true if a feature is enabled.
Definition: ReadView.cpp:107
ripple::featureTickets
const uint256 featureTickets
Definition: Feature.cpp:157
ripple::sfExpiration
const SF_U32 sfExpiration(access, STI_UINT32, 10, "Expiration")
Definition: SField.h:346
ripple::ApplyContext::view
ApplyView & view()
Definition: ApplyContext.h:51
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:98
ripple::temDISABLED
@ temDISABLED
Definition: TER.h:112
ripple::tecNO_PERMISSION
@ tecNO_PERMISSION
Definition: TER.h:270
ripple::Transactor::ctx_
ApplyContext & ctx_
Definition: Transactor.h:81
ripple::Transactor::account_
AccountID account_
Definition: Transactor.h:84
ripple::tecNO_ENTRY
@ tecNO_ENTRY
Definition: TER.h:271
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:53
ripple::tesSUCCESS
@ tesSUCCESS
Definition: TER.h:219
ripple::ApplyContext::tx
STTx const & tx
Definition: ApplyContext.h:45
ripple::NotTEC
TERSubset< CanCvtToNotTEC > NotTEC
Definition: TER.h:461
ripple::STObject::getFieldH256
uint256 getFieldH256(SField const &field) const
Definition: STObject.cpp:532