|
| 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 | +namespace ripple { |
| 21 | + |
| 22 | +class CreateTicket |
| 23 | + : public Transactor |
| 24 | +{ |
| 25 | +public: |
| 26 | + CreateTicket ( |
| 27 | + SerializedTransaction const& txn, |
| 28 | + TransactionEngineParams params, |
| 29 | + TransactionEngine* engine) |
| 30 | + : Transactor ( |
| 31 | + txn, |
| 32 | + params, |
| 33 | + engine, |
| 34 | + deprecatedLogs().journal("CreateTicket")) |
| 35 | + { |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + TER doApply () override |
| 40 | + { |
| 41 | + assert (mTxnAccount); |
| 42 | + |
| 43 | + // A ticket counts against the reserve of the issuing account, but we check |
| 44 | + // the starting balance because we want to allow dipping into the reserve to |
| 45 | + // pay fees. |
| 46 | + auto const accountReserve (mEngine->getLedger ()->getReserve ( |
| 47 | + mTxnAccount->getFieldU32 (sfOwnerCount) + 1)); |
| 48 | + |
| 49 | + if (mPriorBalance.getNValue () < accountReserve) |
| 50 | + return tecINSUFFICIENT_RESERVE; |
| 51 | + |
| 52 | + std::uint32_t expiration (0); |
| 53 | + |
| 54 | + if (mTxn.isFieldPresent (sfExpiration)) |
| 55 | + { |
| 56 | + expiration = mTxn.getFieldU32 (sfExpiration); |
| 57 | + |
| 58 | + if (!expiration) |
| 59 | + { |
| 60 | + m_journal.warning << |
| 61 | + "Malformed ticket requestion: bad expiration"; |
| 62 | + |
| 63 | + return temBAD_EXPIRATION; |
| 64 | + } |
| 65 | + |
| 66 | + if (mEngine->getLedger ()->getParentCloseTimeNC () >= expiration) |
| 67 | + return tesSUCCESS; |
| 68 | + } |
| 69 | + |
| 70 | + SLE::pointer sleTicket = mEngine->entryCreate (ltTICKET, |
| 71 | + Ledger::getTicketIndex (mTxnAccountID, mTxn.getSequence ())); |
| 72 | + |
| 73 | + sleTicket->setFieldAccount (sfAccount, mTxnAccountID); |
| 74 | + sleTicket->setFieldU32 (sfSequence, mTxn.getSequence ()); |
| 75 | + |
| 76 | + if (expiration != 0) |
| 77 | + sleTicket->setFieldU32 (sfExpiration, expiration); |
| 78 | + |
| 79 | + if (mTxn.isFieldPresent (sfTarget)) |
| 80 | + { |
| 81 | + Account const target_account (mTxn.getFieldAccount160 (sfTarget)); |
| 82 | + |
| 83 | + SLE::pointer sleTarget = mEngine->entryCache (ltACCOUNT_ROOT, |
| 84 | + Ledger::getAccountRootIndex (target_account)); |
| 85 | + |
| 86 | + // Destination account does not exist. |
| 87 | + if (!sleTarget) |
| 88 | + return tecNO_TARGET; |
| 89 | + |
| 90 | + // The issuing account is the default account to which the ticket |
| 91 | + // applies so don't bother saving it if that's what's specified. |
| 92 | + if (target_account != mTxnAccountID) |
| 93 | + sleTicket->setFieldAccount (sfTarget, target_account); |
| 94 | + } |
| 95 | + |
| 96 | + std::uint64_t hint; |
| 97 | + |
| 98 | + TER result = mEngine->view ().dirAdd (hint, |
| 99 | + Ledger::getOwnerDirIndex (mTxnAccountID), sleTicket->getIndex (), |
| 100 | + std::bind (&Ledger::ownerDirDescriber, |
| 101 | + std::placeholders::_1, std::placeholders::_2, |
| 102 | + mTxnAccountID)); |
| 103 | + |
| 104 | + m_journal.trace << |
| 105 | + "Creating ticket " << to_string (sleTicket->getIndex ()) << |
| 106 | + ": " << transHuman (result); |
| 107 | + |
| 108 | + if (result != tesSUCCESS) |
| 109 | + return result; |
| 110 | + |
| 111 | + sleTicket->setFieldU64(sfOwnerNode, hint); |
| 112 | + |
| 113 | + // If we succeeded, the new entry counts agains the creator's reserve. |
| 114 | + mEngine->view ().ownerCountAdjust (mTxnAccountID, 1, mTxnAccount); |
| 115 | + |
| 116 | + return result; |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +TER |
| 121 | +transact_CreateTicket ( |
| 122 | + SerializedTransaction const& txn, |
| 123 | + TransactionEngineParams params, |
| 124 | + TransactionEngine* engine) |
| 125 | +{ |
| 126 | + return CreateTicket (txn, params, engine).apply (); |
| 127 | +} |
| 128 | + |
| 129 | +} |
0 commit comments