mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Refactor View, MetaView, and tx processing:
This tidies up the View interface and makes transaction application a free function, with the removal of the TransactionEngine class. A new class ApplyContext provides all the state information needed to apply a Transactor. The Transactor is refactored to perform all the processing activities previously part of TransactionEngine. The calculation of metadata from a MetaView is improved. A new apply function performs all the steps for calculating and inserting metadata into the tx map. Transaction processing code path is passed a Config instead of retrieving the global, and uses the Journal supplied in the call to apply() consistently. To support transaction processing and RPC operations, a new POD type ViewInfo is added which consolidates static information about open and closed ledgers, such as the ledger sequence number or the closing times. Ledger and MetaView are refactored to use this info. The ViewInfo now contains the "open ledger" setting. The tapOPEN_LEDGER ViewFlag is removed. The view property of being an open ledger is obtained from the base or by using the MetaView constructor which presents a closed ledger as an open one. View, MetaView: * Fix missing includes * Add apply free function * Use Journal in TransactionEngine * Use BasicView in TransactionEngine * inline NetworkOPs::batchApply * Add shallow_copy, open_ledger MetaView ctor tags * Add ViewInfo with open flag, seq, close times * Make parent_ a reference * Tidy up ctor arguments and base_ name * Remove tapOPEN_LEDGER * add assert to MetaView::apply * ViewInfo comment * Throw, pass Journal in txInsert * Add BasicView::txCount TransactionEngine: * Add apply * Make TransactionEngine private * Refactor MetaView::apply and apply() * Rename to TxMeta * Refactor treatment of metadata in MetaView, TransactionEngine * Rename to ApplyContext * Use ApplyContext& in Transactor * Pass Config in ApplyContext * Declare Transactor classes in headers * Use view flags in Transactor
This commit is contained in:
@@ -18,137 +18,112 @@
|
||||
//==============================================================================
|
||||
|
||||
#include <BeastConfig.h>
|
||||
#include <ripple/app/tx/impl/Transactor.h>
|
||||
#include <ripple/app/ledger/Ledger.h>
|
||||
#include <ripple/app/tx/impl/CreateTicket.h>
|
||||
#include <ripple/basics/Log.h>
|
||||
#include <ripple/protocol/Indexes.h>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
class CreateTicket
|
||||
: public Transactor
|
||||
{
|
||||
public:
|
||||
CreateTicket (
|
||||
STTx const& txn,
|
||||
ViewFlags params,
|
||||
TransactionEngine* engine)
|
||||
: Transactor (
|
||||
txn,
|
||||
params,
|
||||
engine,
|
||||
deprecatedLogs().journal("CreateTicket"))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TER
|
||||
preCheck () override
|
||||
{
|
||||
if (mTxn.isFieldPresent (sfExpiration))
|
||||
{
|
||||
if (mTxn.getFieldU32 (sfExpiration) == 0)
|
||||
{
|
||||
m_journal.warning <<
|
||||
"Malformed transaction: bad expiration";
|
||||
return temBAD_EXPIRATION;
|
||||
}
|
||||
}
|
||||
|
||||
return Transactor::preCheck ();
|
||||
}
|
||||
|
||||
/** Returns the reserve the account would have if an offer was added. */
|
||||
STAmount
|
||||
getAccountReserve (SLE::pointer account)
|
||||
{
|
||||
return STAmount (mEngine->view().fees().accountReserve(
|
||||
account->getFieldU32 (sfOwnerCount) + 1));
|
||||
}
|
||||
|
||||
TER doApply () override
|
||||
{
|
||||
assert (mTxnAccount);
|
||||
|
||||
// A ticket counts against the reserve of the issuing account, but we
|
||||
// check the starting balance because we want to allow dipping into the
|
||||
// reserve to pay fees.
|
||||
if (mPriorBalance < getAccountReserve (mTxnAccount))
|
||||
return tecINSUFFICIENT_RESERVE;
|
||||
|
||||
std::uint32_t expiration (0);
|
||||
|
||||
if (mTxn.isFieldPresent (sfExpiration))
|
||||
{
|
||||
expiration = mTxn.getFieldU32 (sfExpiration);
|
||||
|
||||
if (mEngine->view().time() >= expiration)
|
||||
return tesSUCCESS;
|
||||
}
|
||||
|
||||
SLE::pointer sleTicket = std::make_shared<SLE>(ltTICKET,
|
||||
getTicketIndex (mTxnAccountID, mTxn.getSequence ()));
|
||||
sleTicket->setAccountID (sfAccount, mTxnAccountID);
|
||||
sleTicket->setFieldU32 (sfSequence, mTxn.getSequence ());
|
||||
if (expiration != 0)
|
||||
sleTicket->setFieldU32 (sfExpiration, expiration);
|
||||
mEngine->view().insert (sleTicket);
|
||||
|
||||
if (mTxn.isFieldPresent (sfTarget))
|
||||
{
|
||||
AccountID const target_account (mTxn.getAccountID (sfTarget));
|
||||
|
||||
SLE::pointer sleTarget = mEngine->view().peek (keylet::account(target_account));
|
||||
|
||||
// Destination account does not exist.
|
||||
if (!sleTarget)
|
||||
return tecNO_TARGET;
|
||||
|
||||
// The issuing account is the default account to which the ticket
|
||||
// applies so don't bother saving it if that's what's specified.
|
||||
if (target_account != mTxnAccountID)
|
||||
sleTicket->setAccountID (sfTarget, target_account);
|
||||
}
|
||||
|
||||
std::uint64_t hint;
|
||||
|
||||
auto describer = [&](SLE::pointer p, bool b)
|
||||
{
|
||||
ownerDirDescriber(p, b, mTxnAccountID);
|
||||
};
|
||||
|
||||
TER result = dirAdd(mEngine->view(),
|
||||
hint,
|
||||
getOwnerDirIndex (mTxnAccountID),
|
||||
sleTicket->getIndex (),
|
||||
describer);
|
||||
|
||||
if (m_journal.trace) m_journal.trace <<
|
||||
"Creating ticket " << to_string (sleTicket->getIndex ()) <<
|
||||
": " << transHuman (result);
|
||||
|
||||
if (result != tesSUCCESS)
|
||||
return result;
|
||||
|
||||
sleTicket->setFieldU64(sfOwnerNode, hint);
|
||||
|
||||
// If we succeeded, the new entry counts agains the creator's reserve.
|
||||
adjustOwnerCount(mEngine->view(), mTxnAccount, 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
TER
|
||||
transact_CreateTicket (
|
||||
STTx const& txn,
|
||||
ViewFlags params,
|
||||
TransactionEngine* engine)
|
||||
CreateTicket::preCheck ()
|
||||
{
|
||||
#if ! RIPPLE_ENABLE_TICKETS
|
||||
if (! (engine->view().flags() & tapENABLE_TESTING))
|
||||
if (! (view().flags() & tapENABLE_TESTING))
|
||||
return temDISABLED;
|
||||
#endif
|
||||
return CreateTicket (txn, params, engine).apply ();
|
||||
|
||||
if (mTxn.isFieldPresent (sfExpiration))
|
||||
{
|
||||
if (mTxn.getFieldU32 (sfExpiration) == 0)
|
||||
{
|
||||
j_.warning <<
|
||||
"Malformed transaction: bad expiration";
|
||||
return temBAD_EXPIRATION;
|
||||
}
|
||||
}
|
||||
|
||||
return Transactor::preCheck ();
|
||||
}
|
||||
|
||||
STAmount
|
||||
CreateTicket::getAccountReserve (SLE::pointer account)
|
||||
{
|
||||
return STAmount (view().fees().accountReserve(
|
||||
account->getFieldU32 (sfOwnerCount) + 1));
|
||||
}
|
||||
|
||||
TER
|
||||
CreateTicket::doApply ()
|
||||
{
|
||||
assert (mTxnAccount);
|
||||
|
||||
// A ticket counts against the reserve of the issuing account, but we
|
||||
// check the starting balance because we want to allow dipping into the
|
||||
// reserve to pay fees.
|
||||
if (mPriorBalance < getAccountReserve (mTxnAccount))
|
||||
return tecINSUFFICIENT_RESERVE;
|
||||
|
||||
std::uint32_t expiration (0);
|
||||
|
||||
if (mTxn.isFieldPresent (sfExpiration))
|
||||
{
|
||||
expiration = mTxn.getFieldU32 (sfExpiration);
|
||||
|
||||
if (view().parentCloseTime() >= expiration)
|
||||
return tesSUCCESS;
|
||||
}
|
||||
|
||||
SLE::pointer sleTicket = std::make_shared<SLE>(ltTICKET,
|
||||
getTicketIndex (mTxnAccountID, mTxn.getSequence ()));
|
||||
sleTicket->setAccountID (sfAccount, mTxnAccountID);
|
||||
sleTicket->setFieldU32 (sfSequence, mTxn.getSequence ());
|
||||
if (expiration != 0)
|
||||
sleTicket->setFieldU32 (sfExpiration, expiration);
|
||||
view().insert (sleTicket);
|
||||
|
||||
if (mTxn.isFieldPresent (sfTarget))
|
||||
{
|
||||
AccountID const target_account (mTxn.getAccountID (sfTarget));
|
||||
|
||||
SLE::pointer sleTarget = view().peek (keylet::account(target_account));
|
||||
|
||||
// Destination account does not exist.
|
||||
if (!sleTarget)
|
||||
return tecNO_TARGET;
|
||||
|
||||
// The issuing account is the default account to which the ticket
|
||||
// applies so don't bother saving it if that's what's specified.
|
||||
if (target_account != mTxnAccountID)
|
||||
sleTicket->setAccountID (sfTarget, target_account);
|
||||
}
|
||||
|
||||
std::uint64_t hint;
|
||||
|
||||
auto describer = [&](SLE::pointer p, bool b)
|
||||
{
|
||||
ownerDirDescriber(p, b, mTxnAccountID);
|
||||
};
|
||||
|
||||
TER result = dirAdd(view(),
|
||||
hint,
|
||||
getOwnerDirIndex (mTxnAccountID),
|
||||
sleTicket->getIndex (),
|
||||
describer);
|
||||
|
||||
if (j_.trace) j_.trace <<
|
||||
"Creating ticket " << to_string (sleTicket->getIndex ()) <<
|
||||
": " << transHuman (result);
|
||||
|
||||
if (result != tesSUCCESS)
|
||||
return result;
|
||||
|
||||
sleTicket->setFieldU64(sfOwnerNode, hint);
|
||||
|
||||
// If we succeeded, the new entry counts agains the creator's reserve.
|
||||
adjustOwnerCount(view(), mTxnAccount, 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user