From 0057fccc0ee778eff0e206b45ef0ffa10c1d0d22 Mon Sep 17 00:00:00 2001 From: Chenna Keshava Date: Thu, 1 Jun 2023 12:00:14 -0700 Subject: [PATCH] Ledger Object wrapper for Ticket class --- src/ripple/app/tx/impl/CreateTicket.cpp | 3 +- src/ripple/app/tx/impl/Transactor.cpp | 10 ++-- src/ripple/ledger/ApplyView.h | 2 +- src/ripple/protocol/Indexes.h | 8 +-- src/ripple/protocol/Keylet.h | 15 ++++++ src/ripple/protocol/Ticket.h | 69 +++++++++++++++++++++++++ src/ripple/protocol/impl/Indexes.cpp | 8 +-- 7 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 src/ripple/protocol/Ticket.h diff --git a/src/ripple/app/tx/impl/CreateTicket.cpp b/src/ripple/app/tx/impl/CreateTicket.cpp index d89cd3e8e3e..f01acea0823 100644 --- a/src/ripple/app/tx/impl/CreateTicket.cpp +++ b/src/ripple/app/tx/impl/CreateTicket.cpp @@ -116,7 +116,8 @@ CreateTicket::doApply() for (std::uint32_t i = 0; i < ticketCount; ++i) { std::uint32_t const curTicketSeq = firstTicketSeq + i; - Keylet const ticketKeylet = keylet::ticket(account_, curTicketSeq); + TicketKeylet const ticketKeylet = + keylet::ticket(account_, curTicketSeq); SLE::pointer sleTicket = std::make_shared(ticketKeylet); sleTicket->setAccountID(sfAccount, account_); diff --git a/src/ripple/app/tx/impl/Transactor.cpp b/src/ripple/app/tx/impl/Transactor.cpp index 65568e5f818..e9250e53aa7 100644 --- a/src/ripple/app/tx/impl/Transactor.cpp +++ b/src/ripple/app/tx/impl/Transactor.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include namespace ripple { @@ -386,14 +387,15 @@ Transactor::ticketDelete( { // Delete the Ticket, adjust the account root ticket count, and // reduce the owner count. - SLE::pointer const sleTicket = view.peekSLE(keylet::ticket(ticketIndex)); - if (!sleTicket) + std::optional ticket = + view.peek(keylet::ticket(ticketIndex)); + if (!ticket) { JLOG(j.fatal()) << "Ticket disappeared from ledger."; return tefBAD_LEDGER; } - std::uint64_t const page{(*sleTicket)[sfOwnerNode]}; + std::uint64_t const page{ticket->getOwnerPage()}; if (!view.dirRemove(keylet::ownerDir(account), page, ticketIndex, true)) { JLOG(j.fatal()) << "Unable to delete Ticket from owner."; @@ -426,7 +428,7 @@ Transactor::ticketDelete( adjustOwnerCount(view, *acctRoot, -1, j); // Remove Ticket from ledger. - view.erase(sleTicket); + view.erase(*ticket); return tesSUCCESS; } diff --git a/src/ripple/ledger/ApplyView.h b/src/ripple/ledger/ApplyView.h index f7578411a23..e75405e8e2d 100644 --- a/src/ripple/ledger/ApplyView.h +++ b/src/ripple/ledger/ApplyView.h @@ -343,7 +343,7 @@ class ApplyView : public ReadView std::optional dirInsert( Keylet const& directory, - Keylet const& key, + KeyletBase const& key, std::function const&)> const& describe) { return dirAdd(false, directory, key.key, describe); diff --git a/src/ripple/protocol/Indexes.h b/src/ripple/protocol/Indexes.h index e7170eff7cc..58709cae5b8 100644 --- a/src/ripple/protocol/Indexes.h +++ b/src/ripple/protocol/Indexes.h @@ -152,16 +152,16 @@ struct ticket_t { explicit ticket_t() = default; - Keylet + TicketKeylet operator()(AccountID const& id, std::uint32_t ticketSeq) const; - Keylet + TicketKeylet operator()(AccountID const& id, SeqProxy ticketSeq) const; - Keylet + TicketKeylet operator()(uint256 const& key) const { - return {ltTICKET, key}; + return TicketKeylet(key); } }; static ticket_t const ticket{}; diff --git a/src/ripple/protocol/Keylet.h b/src/ripple/protocol/Keylet.h index c34b484a150..63a4c380677 100644 --- a/src/ripple/protocol/Keylet.h +++ b/src/ripple/protocol/Keylet.h @@ -95,6 +95,21 @@ static_assert(std::is_move_assignable_v); static_assert(std::is_nothrow_destructible_v); #endif +template +class TicketImpl; + +struct TicketKeylet final : public KeyletBase +{ + template + using TWrapped = TicketImpl; + + using KeyletBase::check; + + explicit TicketKeylet(uint256 const& key) : KeyletBase(ltTICKET, key) + { + } +}; + template class AcctRootImpl; diff --git a/src/ripple/protocol/Ticket.h b/src/ripple/protocol/Ticket.h new file mode 100644 index 00000000000..0a2fec4adcc --- /dev/null +++ b/src/ripple/protocol/Ticket.h @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2023 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_PROTOCOL_TICKET_H_INCLUDED +#define RIPPLE_PROTOCOL_TICKET_H_INCLUDED + +#include + +namespace ripple { + +template +class TicketImpl final : public LedgerEntryWrapper +{ +private: + using Base = LedgerEntryWrapper; + using SleT = typename Base::SleT; + using Base::wrapped_; + + // This constructor is private so only the factory functions can + // construct an TicketImpl. + TicketImpl(std::shared_ptr&& w) : Base(std::move(w)) + { + } + + // Friend declarations of factory functions. + // + // For classes that contain factories we must declare the entire class + // as a friend unless the class declaration is visible at this point. + friend class ReadView; + friend class ApplyView; + +public: + // Conversion operator from TicketImpl to TicketImpl. + operator TicketImpl() const + { + return TicketImpl( + std::const_pointer_cast>( + wrapped_)); + } + + [[nodiscard]] std::uint64_t + getOwnerPage() const + { + return wrapped_->at(sfOwnerNode); + } +}; + +using TicketsRd = TicketImpl; +using Tickets = TicketImpl; + +} // namespace ripple + +#endif // RIPPLE_PROTOCOL_TICKET_H_INCLUDED diff --git a/src/ripple/protocol/impl/Indexes.cpp b/src/ripple/protocol/impl/Indexes.cpp index 2cc80ff41e6..e931fcbde5f 100644 --- a/src/ripple/protocol/impl/Indexes.cpp +++ b/src/ripple/protocol/impl/Indexes.cpp @@ -249,16 +249,16 @@ next_t::operator()(Keylet const& k) const return {ltDIR_NODE, getQualityNext(k.key)}; } -Keylet +TicketKeylet ticket_t::operator()(AccountID const& id, std::uint32_t ticketSeq) const { - return {ltTICKET, getTicketIndex(id, ticketSeq)}; + return TicketKeylet(getTicketIndex(id, ticketSeq)); } -Keylet +TicketKeylet ticket_t::operator()(AccountID const& id, SeqProxy ticketSeq) const { - return {ltTICKET, getTicketIndex(id, ticketSeq)}; + return TicketKeylet(getTicketIndex(id, ticketSeq)); } // This function is presently static, since it's never accessed from anywhere