From fad2dea7c44a76142b3468d8278031c6e41d55c9 Mon Sep 17 00:00:00 2001 From: Chenna Keshava Date: Thu, 1 Jun 2023 12:47:19 -0700 Subject: [PATCH] Strongly Typed wrapper for Negative UNL ledger object --- src/ripple/app/ledger/Ledger.cpp | 12 ++-- src/ripple/app/tx/impl/Change.cpp | 8 +-- src/ripple/protocol/Indexes.h | 2 +- src/ripple/protocol/Keylet.h | 17 +++++ src/ripple/protocol/NegativeUNL.h | 92 ++++++++++++++++++++++++++++ src/ripple/protocol/impl/Indexes.cpp | 5 +- 6 files changed, 122 insertions(+), 14 deletions(-) create mode 100644 src/ripple/protocol/NegativeUNL.h diff --git a/src/ripple/app/ledger/Ledger.cpp b/src/ripple/app/ledger/Ledger.cpp index 441f18983d1..745e9bc075f 100644 --- a/src/ripple/app/ledger/Ledger.cpp +++ b/src/ripple/app/ledger/Ledger.cpp @@ -712,10 +712,10 @@ hash_set Ledger::negativeUNL() const { hash_set negUnl; - if (auto sle = readSLE(keylet::negativeUNL()); - sle && sle->isFieldPresent(sfDisabledValidators)) + if (auto nUNLLedgerObj = read(keylet::negativeUNL()); + nUNLLedgerObj && nUNLLedgerObj->isFieldPresent(sfDisabledValidators)) // Keshava: Can we avoid these expensive conversions into ->slePtr() { - auto const& nUnlData = sle->getFieldArray(sfDisabledValidators); + auto const& nUnlData = nUNLLedgerObj->getFieldArray(sfDisabledValidators); for (auto const& n : nUnlData) { if (n.isFieldPresent(sfPublicKey)) @@ -737,10 +737,10 @@ Ledger::negativeUNL() const std::optional Ledger::validatorToDisable() const { - if (auto sle = readSLE(keylet::negativeUNL()); - sle && sle->isFieldPresent(sfValidatorToDisable)) + if (auto nUNLLedgerObj = read(keylet::negativeUNL()); + nUNLLedgerObj && nUNLLedgerObj->isFieldPresent(sfValidatorToDisable)) { - auto d = sle->getFieldVL(sfValidatorToDisable); + auto d = nUNLLedgerObj->getFieldVL(sfValidatorToDisable); auto s = makeSlice(d); if (publicKeyType(s)) return PublicKey(s); diff --git a/src/ripple/app/tx/impl/Change.cpp b/src/ripple/app/tx/impl/Change.cpp index 7387a231e60..040842ff93b 100644 --- a/src/ripple/app/tx/impl/Change.cpp +++ b/src/ripple/app/tx/impl/Change.cpp @@ -425,11 +425,11 @@ Change::applyUNLModify() << " validator data:" << strHex(validator); auto const k = keylet::negativeUNL(); - SLE::pointer negUnlObject = view().peekSLE(k); + std::optional negUnlObject = view().peek(k); if (!negUnlObject) { - negUnlObject = std::make_shared(k); - view().insert(negUnlObject); + negUnlObject = NegUNL(std::make_shared(k)); + view().insert(negUnlObject->slePtr()); } bool const found = [&] { @@ -508,7 +508,7 @@ Change::applyUNLModify() negUnlObject->setFieldVL(sfValidatorToReEnable, validator); } - view().update(negUnlObject); + view().update(*negUnlObject); return tesSUCCESS; } diff --git a/src/ripple/protocol/Indexes.h b/src/ripple/protocol/Indexes.h index e7170eff7cc..784a0357c85 100644 --- a/src/ripple/protocol/Indexes.h +++ b/src/ripple/protocol/Indexes.h @@ -87,7 +87,7 @@ Keylet const& fees() noexcept; /** The (fixed) index of the object containing the ledger negativeUNL. */ -Keylet const& +NegUNLKeylet const& negativeUNL() noexcept; /** The beginning of an order book */ diff --git a/src/ripple/protocol/Keylet.h b/src/ripple/protocol/Keylet.h index c34b484a150..9990296c849 100644 --- a/src/ripple/protocol/Keylet.h +++ b/src/ripple/protocol/Keylet.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace ripple { @@ -95,6 +96,22 @@ static_assert(std::is_move_assignable_v); static_assert(std::is_nothrow_destructible_v); #endif +template +class NegUNLImpl; + +struct NegUNLKeylet final : public KeyletBase +{ + template + using TWrapped = NegUNLImpl; + + using KeyletBase::check; + + explicit NegUNLKeylet(uint256 const& key) + : KeyletBase(ltNEGATIVE_UNL, key) + { + } +}; + template class AcctRootImpl; diff --git a/src/ripple/protocol/NegativeUNL.h b/src/ripple/protocol/NegativeUNL.h new file mode 100644 index 00000000000..b8d670f0944 --- /dev/null +++ b/src/ripple/protocol/NegativeUNL.h @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +/* + This file is part of rippled: https://github.com/ripple/rippled + Copyright (c) 2021 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_NEGATIVE_UNL_H_INCLUDED +#define RIPPLE_PROTOCOL_NEGATIVE_UNL_H_INCLUDED + +#include +//#include +//#include + +namespace ripple { + +template +class NegUNLImpl final : public LedgerEntryWrapper +{ +private: + using Base = LedgerEntryWrapper; + using SleT = typename Base::SleT; + using Base::wrapped_; + + + // 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: + // This constructor is used in Change::applyUNLModify() function. + // Keshava: Is it mandatory to use make_XYZ factory functions for this usecase? + // What are the perils of having a public constructor? + NegUNLImpl(std::shared_ptr&& w) : Base(std::move(w)) + { + } + + // Conversion operator from NegUNLImpl to NegUNLImpl. + operator NegUNLImpl() const + { + return NegUNLImpl( + std::const_pointer_cast>( + wrapped_)); + } + + [[nodiscard]] bool + isFieldPresent(SField const& field) const { + return wrapped_->isFieldPresent(field); + } + + [[nodiscard]] Blob + getFieldVL(SField const& field) const { + return wrapped_->getFieldVL(field); + } + + void + setFieldVL(SField const& field, Slice const& sl) const { + return wrapped_->setFieldVL(field, sl); + } + + void + setFieldVL(SField const& field, Blob const& blob) const { + return wrapped_->setFieldVL(field, blob); + } + + [[nodiscard]] const auto& + getFieldArray(SField const& field) const { + return wrapped_->getFieldArray(field); + } +}; + +using NegUNL = NegUNLImpl; +using NegUNLRd = NegUNLImpl; + +} // namespace ripple + +#endif // RIPPLE_PROTOCOL_NEGATIVE_UNL_H_INCLUDED diff --git a/src/ripple/protocol/impl/Indexes.cpp b/src/ripple/protocol/impl/Indexes.cpp index 2cc80ff41e6..00b53807451 100644 --- a/src/ripple/protocol/impl/Indexes.cpp +++ b/src/ripple/protocol/impl/Indexes.cpp @@ -175,11 +175,10 @@ fees() noexcept return ret; } -Keylet const& +NegUNLKeylet const& negativeUNL() noexcept { - static Keylet const ret{ - ltNEGATIVE_UNL, indexHash(LedgerNameSpace::NEGATIVE_UNL)}; + static NegUNLKeylet const ret(indexHash(LedgerNameSpace::NEGATIVE_UNL)); return ret; }