Skip to content

Commit

Permalink
Strongly Typed wrapper for Negative UNL ledger object
Browse files Browse the repository at this point in the history
  • Loading branch information
ckeshava committed Jun 1, 2023
1 parent c6d98e6 commit fad2dea
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/ripple/app/ledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,10 @@ hash_set<PublicKey>
Ledger::negativeUNL() const
{
hash_set<PublicKey> 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))
Expand All @@ -737,10 +737,10 @@ Ledger::negativeUNL() const
std::optional<PublicKey>
Ledger::validatorToDisable() const
{
if (auto sle = readSLE(keylet::negativeUNL());
sle && sle->isFieldPresent(sfValidatorToDisable))
if (auto nUNLLedgerObj = read<NegUNLKeylet, NegUNLRd>(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);
Expand Down
8 changes: 4 additions & 4 deletions src/ripple/app/tx/impl/Change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,11 @@ Change::applyUNLModify()
<< " validator data:" << strHex(validator);

auto const k = keylet::negativeUNL();
SLE::pointer negUnlObject = view().peekSLE(k);
std::optional<NegUNL> negUnlObject = view().peek(k);
if (!negUnlObject)
{
negUnlObject = std::make_shared<SLE>(k);
view().insert(negUnlObject);
negUnlObject = NegUNL(std::make_shared<SLE>(k));
view().insert(negUnlObject->slePtr());
}

bool const found = [&] {
Expand Down Expand Up @@ -508,7 +508,7 @@ Change::applyUNLModify()
negUnlObject->setFieldVL(sfValidatorToReEnable, validator);
}

view().update(negUnlObject);
view().update(*negUnlObject);
return tesSUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ripple/protocol/Indexes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
17 changes: 17 additions & 0 deletions src/ripple/protocol/Keylet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ripple/basics/base_uint.h>
#include <ripple/protocol/AcctRoot.h>
#include <ripple/protocol/LedgerFormats.h>
#include <ripple/protocol/NegativeUNL.h>

namespace ripple {

Expand Down Expand Up @@ -95,6 +96,22 @@ static_assert(std::is_move_assignable_v<Keylet>);
static_assert(std::is_nothrow_destructible_v<Keylet>);
#endif

template <bool>
class NegUNLImpl;

struct NegUNLKeylet final : public KeyletBase
{
template <bool Writable>
using TWrapped = NegUNLImpl<Writable>;

using KeyletBase::check;

explicit NegUNLKeylet(uint256 const& key)
: KeyletBase(ltNEGATIVE_UNL, key)
{
}
};

template <bool>
class AcctRootImpl;

Expand Down
92 changes: 92 additions & 0 deletions src/ripple/protocol/NegativeUNL.h
Original file line number Diff line number Diff line change
@@ -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 <ripple/protocol/LedgerEntryWrapper.h>
//#include <ripple/protocol/STAccount.h>
//#include <ripple/protocol/STAmount.h>

namespace ripple {

template <bool Writable>
class NegUNLImpl final : public LedgerEntryWrapper<Writable>
{
private:
using Base = LedgerEntryWrapper<Writable>;
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<SleT>&& w) : Base(std::move(w))
{
}

// Conversion operator from NegUNLImpl<true> to NegUNLImpl<false>.
operator NegUNLImpl<true>() const
{
return NegUNLImpl<false>(
std::const_pointer_cast<std::shared_ptr<STLedgerEntry const>>(
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<true>;
using NegUNLRd = NegUNLImpl<false>;

} // namespace ripple

#endif // RIPPLE_PROTOCOL_NEGATIVE_UNL_H_INCLUDED
5 changes: 2 additions & 3 deletions src/ripple/protocol/impl/Indexes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit fad2dea

Please sign in to comment.