Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper class for Amendments ledger object #23

Open
wants to merge 3 commits into
base: wrapper
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/ripple/app/tx/impl/Change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ Change::applyAmendment()

auto const k = keylet::amendments();

// amendmentObject is better off being a SLE rather than a
// Amendments object. It is accessing multiple get/set methods from
// STObject. It is expensive to accomplish the same with
// Amendments object.
SLE::pointer amendmentObject = view().peekSLE(k);

if (!amendmentObject)
Expand Down
16 changes: 10 additions & 6 deletions src/ripple/ledger/ApplyView.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,11 @@ class ApplyView : public ReadView
erase(std::shared_ptr<SLE> const& sle) = 0;

template <class T>
requires(std::is_convertible_v<
decltype(std::declval<T>().slePtr()),
std::shared_ptr<SLE> const&>) void erase(T& wrapper)
requires(std::is_convertible_v<
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this and the below change was introduced by clang-format tool

decltype(std::declval<T>().slePtr()),
std::shared_ptr<SLE> const&>)
void
erase(T& wrapper)
{
erase(wrapper.slePtr());
}
Expand Down Expand Up @@ -249,9 +251,11 @@ class ApplyView : public ReadView
update(std::shared_ptr<SLE> const& sle) = 0;

template <class T>
requires(std::is_convertible_v<
decltype(std::declval<T>().slePtr()),
std::shared_ptr<SLE> const&>) void update(T& wrapper)
requires(std::is_convertible_v<
decltype(std::declval<T>().slePtr()),
std::shared_ptr<SLE> const&>)
void
update(T& wrapper)
{
update(wrapper.slePtr());
}
Expand Down
12 changes: 8 additions & 4 deletions src/ripple/ledger/impl/ReadView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <ripple/ledger/ReadView.h>
#include <ripple/protocol/Amendments.h>

namespace ripple {

Expand Down Expand Up @@ -76,13 +77,16 @@ makeRulesGivenLedger(
DigestAwareReadView const& ledger,
std::unordered_set<uint256, beast::uhash<>> const& presets)
{
Keylet const k = keylet::amendments();
AmendmentsKeylet const k = keylet::amendments();
std::optional digest = ledger.digest(k.key);
if (digest)
{
auto const sle = ledger.readSLE(k);
if (sle)
return Rules(presets, digest, sle->getFieldV256(sfAmendments));
auto const amendsObj = ledger.read(k);
if (amendsObj)
return Rules(
presets,
digest,
amendsObj->slePtr()->getFieldV256(sfAmendments));
}
return Rules(presets);
}
Expand Down
14 changes: 8 additions & 6 deletions src/ripple/ledger/impl/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <ripple/basics/contract.h>
#include <ripple/ledger/ReadView.h>
#include <ripple/ledger/View.h>
#include <ripple/protocol/Amendments.h>
#include <ripple/protocol/Feature.h>
#include <ripple/protocol/Protocol.h>
#include <ripple/protocol/Quality.h>
Expand Down Expand Up @@ -607,11 +608,11 @@ getEnabledAmendments(ReadView const& view)
{
std::set<uint256> amendments;

if (auto const sle = view.readSLE(keylet::amendments()))
if (auto const amendsObj = view.read(keylet::amendments()))
{
if (sle->isFieldPresent(sfAmendments))
if (amendsObj->slePtr()->isFieldPresent(sfAmendments))
{
auto const& v = sle->getFieldV256(sfAmendments);
auto const& v = amendsObj->slePtr()->getFieldV256(sfAmendments);
amendments.insert(v.begin(), v.end());
}
}
Expand All @@ -624,14 +625,15 @@ getMajorityAmendments(ReadView const& view)
{
majorityAmendments_t ret;

if (auto const sle = view.readSLE(keylet::amendments()))
if (auto const amendsObj = view.read(keylet::amendments()))
{
if (sle->isFieldPresent(sfMajorities))
if (amendsObj->slePtr()->isFieldPresent(sfMajorities))
{
using tp = NetClock::time_point;
using d = tp::duration;

auto const majorities = sle->getFieldArray(sfMajorities);
auto const majorities =
amendsObj->slePtr()->getFieldArray(sfMajorities);

for (auto const& m : majorities)
ret[m.getFieldH256(sfAmendment)] =
Expand Down
69 changes: 46 additions & 23 deletions src/ripple/protocol/AcctRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setSequence(std::uint32_t seq) requires Writable
setSequence(std::uint32_t seq)
requires Writable
{
wrapped_->at(sfSequence) = seq;
}
Expand All @@ -81,7 +82,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setBalance(STAmount const& amount) requires Writable
setBalance(STAmount const& amount)
requires Writable
{
wrapped_->at(sfBalance) = amount;
}
Expand All @@ -93,7 +95,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setOwnerCount(std::uint32_t newCount) requires Writable
setOwnerCount(std::uint32_t newCount)
requires Writable
{
wrapped_->at(sfOwnerCount) = newCount;
}
Expand All @@ -105,7 +108,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setPreviousTxnID(uint256 prevTxID) requires Writable
setPreviousTxnID(uint256 prevTxID)
requires Writable
{
wrapped_->at(sfPreviousTxnID) = prevTxID;
}
Expand All @@ -117,7 +121,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setPreviousTxnLgrSeq(std::uint32_t prevTxLgrSeq) requires Writable
setPreviousTxnLgrSeq(std::uint32_t prevTxLgrSeq)
requires Writable
{
wrapped_->at(sfPreviousTxnLgrSeq) = prevTxLgrSeq;
}
Expand All @@ -129,13 +134,15 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setAccountTxnID(uint256 const& newAcctTxnID) requires Writable
setAccountTxnID(uint256 const& newAcctTxnID)
requires Writable
{
this->setOptional(sfAccountTxnID, newAcctTxnID);
}

void
clearAccountTxnID() requires Writable
clearAccountTxnID()
requires Writable
{
this->clearOptional(sfAccountTxnID);
}
Expand All @@ -147,13 +154,15 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setRegularKey(AccountID const& newRegKey) requires Writable
setRegularKey(AccountID const& newRegKey)
requires Writable
{
this->setOptional(sfRegularKey, newRegKey);
}

void
clearRegularKey() requires Writable
clearRegularKey()
requires Writable
{
this->clearOptional(sfRegularKey);
}
Expand All @@ -165,7 +174,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setEmailHash(uint128 const& newEmailHash) requires Writable
setEmailHash(uint128 const& newEmailHash)
requires Writable
{
this->setOrClearBaseUintIfZero(sfEmailHash, newEmailHash);
}
Expand All @@ -177,7 +187,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setWalletLocator(uint256 const& newWalletLocator) requires Writable
setWalletLocator(uint256 const& newWalletLocator)
requires Writable
{
this->setOrClearBaseUintIfZero(sfWalletLocator, newWalletLocator);
}
Expand All @@ -195,7 +206,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setMessageKey(Blob const& newMessageKey) requires Writable
setMessageKey(Blob const& newMessageKey)
requires Writable
{
this->setOrClearVLIfEmpty(sfMessageKey, newMessageKey);
}
Expand All @@ -207,13 +219,15 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setTransferRate(std::uint32_t newTransferRate) requires Writable
setTransferRate(std::uint32_t newTransferRate)
requires Writable
{
this->setOptional(sfTransferRate, newTransferRate);
}

void
clearTransferRate() requires Writable
clearTransferRate()
requires Writable
{
this->clearOptional(sfTransferRate);
}
Expand All @@ -225,7 +239,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setDomain(Blob const& newDomain) requires Writable
setDomain(Blob const& newDomain)
requires Writable
{
this->setOrClearVLIfEmpty(sfDomain, newDomain);
}
Expand All @@ -237,13 +252,15 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setTickSize(std::uint8_t newTickSize) requires Writable
setTickSize(std::uint8_t newTickSize)
requires Writable
{
this->setOptional(sfTickSize, newTickSize);
}

void
clearTickSize() requires Writable
clearTickSize()
requires Writable
{
this->clearOptional(sfTickSize);
}
Expand All @@ -255,13 +272,15 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setTicketCount(std::uint32_t newTicketCount) requires Writable
setTicketCount(std::uint32_t newTicketCount)
requires Writable
{
this->setOptional(sfTicketCount, newTicketCount);
}

void
clearTicketCount() requires Writable
clearTicketCount()
requires Writable
{
this->clearOptional(sfTicketCount);
}
Expand All @@ -273,13 +292,15 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setNFTokenMinter(AccountID const& newMinter) requires Writable
setNFTokenMinter(AccountID const& newMinter)
requires Writable
{
this->setOptional(sfNFTokenMinter, newMinter);
}

void
clearNFTokenMinter() requires Writable
clearNFTokenMinter()
requires Writable
{
this->clearOptional(sfNFTokenMinter);
}
Expand All @@ -291,7 +312,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setMintedNFTokens(std::uint32_t newMintedCount) requires Writable
setMintedNFTokens(std::uint32_t newMintedCount)
requires Writable
{
this->setOptional(sfMintedNFTokens, newMintedCount);
}
Expand All @@ -303,7 +325,8 @@ class AcctRootImpl final : public LedgerEntryWrapper<Writable>
}

void
setBurnedNFTokens(std::uint32_t newBurnedCount) requires Writable
setBurnedNFTokens(std::uint32_t newBurnedCount)
requires Writable
{
this->setOptional(sfBurnedNFTokens, newBurnedCount);
}
Expand Down
63 changes: 63 additions & 0 deletions src/ripple/protocol/Amendments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//------------------------------------------------------------------------------
/*
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_AMENDMENTS_H_INCLUDED
#define RIPPLE_PROTOCOL_AMENDMENTS_H_INCLUDED

#include <ripple/protocol/LedgerEntryWrapper.h>

namespace ripple {

template <bool Writable>
class AmendmentsImpl 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 AmendmentsImpl.
AmendmentsImpl(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 AmendmentsImpl<true> to AmendmentsImpl<false>.
operator AmendmentsImpl<true>() const
{
return AmendmentsImpl<false>(
std::const_pointer_cast<std::shared_ptr<STLedgerEntry const>>(
wrapped_));
}
};

using Amendments = AmendmentsImpl<true>;
using AmendmentsRd = AmendmentsImpl<false>;

} // namespace ripple

#endif // RIPPLE_PROTOCOL_AMENDMENTS_H_INCLUDED
Loading