Skip to content

Commit

Permalink
Ledger Object wrapper for Ticket class
Browse files Browse the repository at this point in the history
  • Loading branch information
ckeshava committed Jun 1, 2023
1 parent c6d98e6 commit 0057fcc
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/ripple/app/tx/impl/CreateTicket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<SLE>(ticketKeylet);

sleTicket->setAccountID(sfAccount, account_);
Expand Down
10 changes: 6 additions & 4 deletions src/ripple/app/tx/impl/Transactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <ripple/protocol/Indexes.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/protocol/STAccount.h>
#include <ripple/protocol/Ticket.h>
#include <ripple/protocol/UintTypes.h>

namespace ripple {
Expand Down Expand Up @@ -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<ripple::Tickets> 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.";
Expand Down Expand Up @@ -426,7 +428,7 @@ Transactor::ticketDelete(
adjustOwnerCount(view, *acctRoot, -1, j);

// Remove Ticket from ledger.
view.erase(sleTicket);
view.erase(*ticket);
return tesSUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/ledger/ApplyView.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class ApplyView : public ReadView
std::optional<std::uint64_t>
dirInsert(
Keylet const& directory,
Keylet const& key,
KeyletBase const& key,
std::function<void(std::shared_ptr<SLE> const&)> const& describe)
{
return dirAdd(false, directory, key.key, describe);
Expand Down
8 changes: 4 additions & 4 deletions src/ripple/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
15 changes: 15 additions & 0 deletions src/ripple/protocol/Keylet.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ static_assert(std::is_move_assignable_v<Keylet>);
static_assert(std::is_nothrow_destructible_v<Keylet>);
#endif

template <bool>
class TicketImpl;

struct TicketKeylet final : public KeyletBase
{
template <bool Writable>
using TWrapped = TicketImpl<Writable>;

using KeyletBase::check;

explicit TicketKeylet(uint256 const& key) : KeyletBase(ltTICKET, key)
{
}
};

template <bool>
class AcctRootImpl;

Expand Down
69 changes: 69 additions & 0 deletions src/ripple/protocol/Ticket.h
Original file line number Diff line number Diff line change
@@ -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 <ripple/protocol/LedgerEntryWrapper.h>

namespace ripple {

template <bool Writable>
class TicketImpl final : public LedgerEntryWrapper<Writable>
{
private:
using Base = LedgerEntryWrapper<Writable>;
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<SleT>&& 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<true> to TicketImpl<false>.
operator TicketImpl<true>() const
{
return TicketImpl<false>(
std::const_pointer_cast<std::shared_ptr<STLedgerEntry const>>(
wrapped_));
}

[[nodiscard]] std::uint64_t
getOwnerPage() const
{
return wrapped_->at(sfOwnerNode);
}
};

using TicketsRd = TicketImpl<false>;
using Tickets = TicketImpl<true>;

} // namespace ripple

#endif // RIPPLE_PROTOCOL_TICKET_H_INCLUDED
8 changes: 4 additions & 4 deletions src/ripple/protocol/impl/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0057fcc

Please sign in to comment.