Skip to content

Commit

Permalink
Always create the FeeSettings object in genesis ledger:
Browse files Browse the repository at this point in the history
* Initialize with default values from the config. Removes the need to
  pass a Config down into the Ledger initialization functions,
  including setup().
* Because old mainnet ledgers (prior to 562177 - yes, I looked it up),
  don't have FeeSettings, some of the other ctors will default them to
  the config values before setup() tries to load the object.
* Update default Config fee values to match mainnet.
* Fix unit tests:
  * Updated fees: Some tests are converted to use computed
    values of fee object, but the default Env config was
    also updated to fix the rest.
  * Unit tests that check the structure of the ledger have updated
    hashes and counts.
  • Loading branch information
ximinez committed Dec 16, 2022
1 parent fa94a73 commit cde0ce5
Show file tree
Hide file tree
Showing 25 changed files with 223 additions and 131 deletions.
67 changes: 52 additions & 15 deletions src/ripple/app/ledger/Ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,33 @@ Ledger::Ledger(
rawInsert(sle);
}

{
auto sle = std::make_shared<SLE>(keylet::fees());
// Whether featureXRPFees is supported will depend on startup options.
if (std::find(amendments.begin(), amendments.end(), featureXRPFees) !=
amendments.end())
{
sle->at(sfBaseFeeDrops) = config.FEE_DEFAULT;
sle->at(sfReserveBaseDrops) = config.FEE_ACCOUNT_RESERVE;
sle->at(sfReserveIncrementDrops) = config.FEE_OWNER_RESERVE;
}
else
{
if (auto const f = config.FEE_DEFAULT.dropsAs<std::uint64_t>())
sle->at(sfBaseFee) = *f;
if (auto const f =
config.FEE_ACCOUNT_RESERVE.dropsAs<std::uint32_t>())
sle->at(sfReserveBase) = *f;
if (auto const f =
config.FEE_OWNER_RESERVE.dropsAs<std::uint32_t>())
sle->at(sfReserveIncrement) = *f;
sle->at(sfReferenceFeeUnits) = Config::FEE_UNITS_DEPRECATED;
}
rawInsert(sle);
}

stateMap_->flushDirty(hotACCOUNT_NODE);
setImmutable(config);
setImmutable();
}

Ledger::Ledger(
Expand Down Expand Up @@ -259,7 +284,8 @@ Ledger::Ledger(
txMap_->setImmutable();
stateMap_->setImmutable();

if (!setup(config))
defaultFees(config);
if (!setup())
loaded = false;

if (!loaded)
Expand Down Expand Up @@ -329,11 +355,12 @@ Ledger::Ledger(
info_.seq = ledgerSeq;
info_.closeTime = closeTime;
info_.closeTimeResolution = ledgerDefaultTimeResolution;
setup(config);
defaultFees(config);
setup();
}

void
Ledger::setImmutable(Config const& config, bool rehash)
Ledger::setImmutable(bool rehash)
{
// Force update, since this is the only
// place the hash transitions to valid
Expand All @@ -349,23 +376,22 @@ Ledger::setImmutable(Config const& config, bool rehash)
mImmutable = true;
txMap_->setImmutable();
stateMap_->setImmutable();
setup(config);
setup();
}

void
Ledger::setAccepted(
NetClock::time_point closeTime,
NetClock::duration closeResolution,
bool correctCloseTime,
Config const& config)
bool correctCloseTime)
{
// Used when we witnessed the consensus.
assert(!open());

info_.closeTime = closeTime;
info_.closeTimeResolution = closeResolution;
info_.closeFlags = correctCloseTime ? 0 : sLCF_NoConsensusTime;
setImmutable(config);
setImmutable();
}

bool
Expand Down Expand Up @@ -587,13 +613,13 @@ Ledger::rawTxInsertWithHash(
}

bool
Ledger::setup(Config const& config)
Ledger::setup()
{
bool ret = true;

try
{
rules_ = makeRulesGivenLedger(*this, config.features);
rules_ = makeRulesGivenLedger(*this, rules_);
}
catch (SHAMapMissingNode const&)
{
Expand All @@ -604,10 +630,6 @@ Ledger::setup(Config const& config)
Rethrow();
}

fees_.base = config.FEE_DEFAULT;
fees_.reserve = config.FEE_ACCOUNT_RESERVE;
fees_.increment = config.FEE_OWNER_RESERVE;

try
{
if (auto const sle = read(keylet::fees()))
Expand Down Expand Up @@ -667,6 +689,18 @@ Ledger::setup(Config const& config)
return ret;
}

void
Ledger::defaultFees(Config const& config)
{
assert(fees_.base == 0 && fees_.reserve == 0 && fees_.increment == 0);
if (fees_.base == 0)
fees_.base = config.FEE_DEFAULT;
if (fees_.reserve == 0)
fees_.reserve = config.FEE_ACCOUNT_RESERVE;
if (fees_.increment == 0)
fees_.increment = config.FEE_OWNER_RESERVE;
}

std::shared_ptr<SLE>
Ledger::peek(Keylet const& k) const
{
Expand Down Expand Up @@ -1071,7 +1105,10 @@ finishLoadByIndexOrHash(
if (!ledger)
return;

ledger->setImmutable(config);
assert(
ledger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
ledger->read(keylet::fees()));
ledger->setImmutable();

JLOG(j.trace()) << "Loaded ledger: " << to_string(ledger->info().hash);

Expand Down
10 changes: 6 additions & 4 deletions src/ripple/app/ledger/Ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,10 @@ class Ledger final : public std::enable_shared_from_this<Ledger>,
setAccepted(
NetClock::time_point closeTime,
NetClock::duration closeResolution,
bool correctCloseTime,
Config const& config);
bool correctCloseTime);

void
setImmutable(Config const& config, bool rehash = true);
setImmutable(bool rehash = true);

bool
isImmutable() const
Expand Down Expand Up @@ -395,7 +394,10 @@ class Ledger final : public std::enable_shared_from_this<Ledger>,
class txs_iter_impl;

bool
setup(Config const& config);
setup();

void
defaultFees(Config const& config);

bool mImmutable;

Expand Down
6 changes: 4 additions & 2 deletions src/ripple/app/ledger/impl/BuildLedger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ buildLedgerImpl(
built->unshare();

// Accept ledger
built->setAccepted(
closeTime, closeResolution, closeTimeCorrect, app.config());
assert(
built->info().seq < XRP_LEDGER_EARLIEST_FEES ||
built->read(keylet::fees()));
built->setAccepted(closeTime, closeResolution, closeTimeCorrect);

return built;
}
Expand Down
15 changes: 12 additions & 3 deletions src/ripple/app/ledger/impl/InboundLedger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ InboundLedger::init(ScopedLockType& collectionLock)

JLOG(journal_.debug()) << "Acquiring ledger we already have in "
<< " local store. " << hash_;
mLedger->setImmutable(app_.config());
assert(
mLedger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
mLedger->read(keylet::fees()));
mLedger->setImmutable();

if (mReason == Reason::HISTORY || mReason == Reason::SHARD)
return;
Expand Down Expand Up @@ -416,7 +419,10 @@ InboundLedger::tryDB(NodeStore::Database& srcDB)
{
JLOG(journal_.debug()) << "Had everything locally";
complete_ = true;
mLedger->setImmutable(app_.config());
assert(
mLedger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
mLedger->read(keylet::fees()));
mLedger->setImmutable();
}
}

Expand Down Expand Up @@ -513,7 +519,10 @@ InboundLedger::done()

if (complete_ && !failed_ && mLedger)
{
mLedger->setImmutable(app_.config());
assert(
mLedger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
mLedger->read(keylet::fees()));
mLedger->setImmutable();
switch (mReason)
{
case Reason::SHARD:
Expand Down
17 changes: 13 additions & 4 deletions src/ripple/app/main/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ ApplicationImp::fdRequired() const
void
ApplicationImp::startGenesisLedger()
{
std::vector<uint256> initialAmendments =
std::vector<uint256> const initialAmendments =
(config_->START_UP == Config::FRESH) ? m_amendmentTable->getDesired()
: std::vector<uint256>{};

Expand All @@ -1710,7 +1710,10 @@ ApplicationImp::startGenesisLedger()
auto const next =
std::make_shared<Ledger>(*genesis, timeKeeper().closeTime());
next->updateSkipList();
next->setImmutable(*config_);
assert(
next->info().seq < XRP_LEDGER_EARLIEST_FEES ||
next->read(keylet::fees()));
next->setImmutable();
openLedger_.emplace(next, cachedSLEs_, logs_->journal("OpenLedger"));
m_ledgerMaster->storeLedger(next);
m_ledgerMaster->switchLCL(next);
Expand All @@ -1728,7 +1731,10 @@ ApplicationImp::getLastFullLedger()
if (!ledger)
return ledger;

ledger->setImmutable(*config_);
assert(
ledger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
ledger->read(keylet::fees()));
ledger->setImmutable();

if (getLedgerMaster().haveLedger(seq))
ledger->setValidated();
Expand Down Expand Up @@ -1879,8 +1885,11 @@ ApplicationImp::loadLedgerFromFile(std::string const& name)

loadLedger->stateMap().flushDirty(hotACCOUNT_NODE);

assert(
loadLedger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
loadLedger->read(keylet::fees()));
loadLedger->setAccepted(
closeTime, closeTimeResolution, !closeTimeEstimated, *config_);
closeTime, closeTimeResolution, !closeTimeEstimated);

return loadLedger;
}
Expand Down
5 changes: 4 additions & 1 deletion src/ripple/app/reporting/ReportingETL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ ReportingETL::flushLedger(std::shared_ptr<Ledger>& ledger)
auto& txHash = ledger->info().txHash;
auto& ledgerHash = ledger->info().hash;

ledger->setImmutable(app_.config(), false);
assert(
ledger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
ledger->read(keylet::fees()));
ledger->setImmutable(false);
auto start = std::chrono::system_clock::now();

auto numFlushed = ledger->stateMap().flushDirty(hotACCOUNT_NODE);
Expand Down
4 changes: 2 additions & 2 deletions src/ripple/core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ class Config : public BasicConfig
VALIDATION_QUORUM; // validations to consider ledger authoritative

XRPAmount FEE_DEFAULT{10};
XRPAmount FEE_ACCOUNT_RESERVE{200 * DROPS_PER_XRP};
XRPAmount FEE_OWNER_RESERVE{50 * DROPS_PER_XRP};
XRPAmount FEE_ACCOUNT_RESERVE{10 * DROPS_PER_XRP};
XRPAmount FEE_OWNER_RESERVE{2 * DROPS_PER_XRP};

// Node storage configuration
std::uint32_t LEDGER_HISTORY = 256;
Expand Down
3 changes: 3 additions & 0 deletions src/ripple/ledger/ReadView.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ getCloseAgree(LedgerInfo const& info)
void
addRaw(LedgerInfo const&, Serializer&, bool includeHash = false);

Rules
makeRulesGivenLedger(DigestAwareReadView const& ledger, Rules const& current);

Rules
makeRulesGivenLedger(
DigestAwareReadView const& ledger,
Expand Down
6 changes: 6 additions & 0 deletions src/ripple/ledger/impl/ReadView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ ReadView::txs_type::end() const -> iterator
return iterator(view_, view_->txsEnd());
}

Rules
makeRulesGivenLedger(DigestAwareReadView const& ledger, Rules const& current)
{
return makeRulesGivenLedger(ledger, current.presets());
}

Rules
makeRulesGivenLedger(
DigestAwareReadView const& ledger,
Expand Down
5 changes: 4 additions & 1 deletion src/ripple/nodestore/impl/Shard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,10 @@ Shard::finalize(bool writeSQLite, std::optional<uint256> const& referenceHash)

ledger->stateMap().setLedgerSeq(ledgerSeq);
ledger->txMap().setLedgerSeq(ledgerSeq);
ledger->setImmutable(config);
assert(
ledger->info().seq < XRP_LEDGER_EARLIEST_FEES ||
ledger->read(keylet::fees()));
ledger->setImmutable();
if (!ledger->stateMap().fetchRoot(
SHAMapHash{ledger->info().accountHash}, nullptr))
{
Expand Down
8 changes: 8 additions & 0 deletions src/ripple/protocol/Rules.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class Rules

private:
// Allow a friend function to construct Rules.
friend Rules
makeRulesGivenLedger(
DigestAwareReadView const& ledger,
Rules const& current);

friend Rules
makeRulesGivenLedger(
DigestAwareReadView const& ledger,
Expand All @@ -66,6 +71,9 @@ class Rules
std::optional<uint256> const& digest,
STVector256 const& amendments);

std::unordered_set<uint256, beast::uhash<>> const&
presets() const;

public:
/** Returns `true` if a feature is enabled. */
bool
Expand Down
4 changes: 4 additions & 0 deletions src/ripple/protocol/SystemParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ systemCurrencyCode()
/** The XRP ledger network's earliest allowed sequence */
static constexpr std::uint32_t XRP_LEDGER_EARLIEST_SEQ{32570u};

/** The XRP Ledger mainnet's earliest ledger with a FeeSettings object. Only
* used in asserts and tests. */
static constexpr std::uint32_t XRP_LEDGER_EARLIEST_FEES{562177u};

/** The number of ledgers in a shard */
static constexpr std::uint32_t DEFAULT_LEDGERS_PER_SHARD{16384u};

Expand Down
13 changes: 13 additions & 0 deletions src/ripple/protocol/impl/Rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class Rules::Impl
set_.insert(amendments.begin(), amendments.end());
}

std::unordered_set<uint256, beast::uhash<>> const&
presets() const
{
return presets_;
}

bool
enabled(uint256 const& feature) const
{
Expand All @@ -60,6 +66,7 @@ class Rules::Impl
return true;
if (!digest_ || !other.digest_)
return false;
assert(presets_ == other.presets_);
return *digest_ == *other.digest_;
}
};
Expand All @@ -77,6 +84,12 @@ Rules::Rules(
{
}

std::unordered_set<uint256, beast::uhash<>> const&
Rules::presets() const
{
return impl_->presets();
}

bool
Rules::enabled(uint256 const& feature) const
{
Expand Down
Loading

0 comments on commit cde0ce5

Please sign in to comment.