Files
rippled/src/test/app/RCLValidations_test.cpp
Bart 3ee3f3740c fix: Refuse to make an invalid SHAMap or Ledger immutable
SHAMap::setImmutable() now returns [[nodiscard]] bool and refuses a map
already proven impossible. Every state change goes through trySetState(),
the only writer of state_ past construction, so the order between the
states is stated once: its compare-exchange can't leave Invalid however it
interleaves with another thread's, and setInvalid() stores through the same
funnel rather than behind its back. Invalid is stored unconditionally
there, since only the map itself reaches that verdict and a walk that
reaches it has to win against a thread settling the map; refusing to
overwrite Immutable would leave a map proven impossible reporting itself
sound, which is what nothing downstream could recover from.

Ledger::setImmutable()/setAccepted() do the same one level up, checking
mapsValid() before touching anything and settling both maps independently
so neither is left mid-sync because the other refused. The map hashes are
read before the maps are settled, since getHash() can unshare a dirty
tree, but written to the header only once both maps have made it. A walk
that invalidates a map in between therefore leaves the header describing
what the ledger was built from rather than a map that has since been
abandoned.

Every call site now branches on the result. The two genesis paths and
buildLedgerImpl() call logicError(), since consensus can't tolerate an
invalid ledger; the load paths return early instead; InboundLedger and
TransactionAcquire withdraw complete_ alongside the failure, since for
them a refusal is an outcome a peer can produce. A test helper that cannot
reach BEAST_EXPECT throws instead, so a refusal cannot hand a broken
ledger to the assertions below.
2026-08-28 20:16:37 -04:00

328 lines
12 KiB
C++

#include <test/jtx/Env.h>
#include <xrpld/app/consensus/RCLValidations.h>
#include <xrpld/core/Config.h>
#include <xrpl/basics/base_uint.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/beast/unit_test/suite.h>
#include <xrpl/consensus/LedgerTrie.h>
#include <xrpl/ledger/Ledger.h>
#include <xrpl/protocol/Indexes.h>
#include <xrpl/protocol/KeyType.h>
#include <xrpl/protocol/PublicKey.h>
#include <xrpl/protocol/SField.h>
#include <xrpl/protocol/STValidation.h>
#include <xrpl/protocol/SecretKey.h>
#include <memory>
#include <vector>
namespace xrpl::test {
class RCLValidations_test : public beast::unit_test::Suite
{
void
testChangeTrusted()
{
testcase("Change validation trusted status");
auto keys = randomKeyPair(KeyType::Secp256k1);
auto v = std::make_shared<STValidation>(
xrpl::NetClock::time_point{},
keys.first,
keys.second,
calcNodeID(keys.first),
[&](STValidation& v) { v.setFieldU32(sfLedgerSequence, 123456); });
BEAST_EXPECT(v->isTrusted());
v->setUntrusted();
BEAST_EXPECT(!v->isTrusted());
RCLValidation rcv{v};
BEAST_EXPECT(!rcv.trusted());
rcv.setTrusted();
BEAST_EXPECT(rcv.trusted());
rcv.setUntrusted();
BEAST_EXPECT(!rcv.trusted());
}
void
testRCLValidatedLedger()
{
testcase("RCLValidatedLedger ancestry");
using Seq = RCLValidatedLedger::Seq;
using ID = RCLValidatedLedger::ID;
// This tests RCLValidatedLedger properly implements the type
// requirements of a LedgerTrie ledger, with its added behavior that
// only the 256 prior ledger hashes are available to determine ancestry.
Seq const maxAncestors = 256;
//----------------------------------------------------------------------
// Generate two ledger histories that agree on the first maxAncestors
// ledgers, then diverge.
std::vector<std::shared_ptr<Ledger const>> history;
jtx::Env env(*this);
Config const config;
auto prev = std::make_shared<Ledger const>(
kCreateGenesis,
Rules{config.features},
config.fees.toFees(),
std::vector<uint256>{},
env.app().getNodeFamily());
history.push_back(prev);
for (auto i = 0; i < ((2 * maxAncestors) + 1); ++i)
{
auto next = std::make_shared<Ledger>(*prev, env.app().getTimeKeeper().closeTime());
next->updateSkipList();
history.push_back(next);
prev = next;
}
// altHistory agrees with first half of regular history
Seq const diverge = history.size() / 2;
std::vector<std::shared_ptr<Ledger const>> altHistory(
history.begin(), history.begin() + diverge);
// advance clock to get new ledgers
using namespace std::chrono_literals;
env.timeKeeper().set(env.timeKeeper().now() + 1200s);
prev = altHistory.back();
bool forceHash = true;
while (altHistory.size() < history.size())
{
auto next = std::make_shared<Ledger>(*prev, env.app().getTimeKeeper().closeTime());
// Force a different hash on the first iteration
next->updateSkipList();
BEAST_EXPECT(next->read(keylet::feeSettings()));
if (forceHash)
{
BEAST_EXPECT(next->setImmutable());
forceHash = false;
}
altHistory.push_back(next);
prev = next;
}
//----------------------------------------------------------------------
// Empty ledger
{
RCLValidatedLedger const a{RCLValidatedLedger::MakeGenesis{}};
BEAST_EXPECT(a.seq() == Seq{0});
BEAST_EXPECT(a[Seq{0}] == ID{0});
BEAST_EXPECT(a.minSeq() == Seq{0});
}
// Full history ledgers
{
std::shared_ptr<Ledger const> const ledger = history.back();
RCLValidatedLedger const a{ledger, env.journal};
BEAST_EXPECT(a.seq() == ledger->header().seq);
BEAST_EXPECT(a.minSeq() == a.seq() - maxAncestors);
// Ensure the ancestral 256 ledgers have proper ID
for (Seq s = a.seq(); s > 0; s--)
{
if (s >= a.minSeq())
{
BEAST_EXPECT(a[s] == history[s - 1]->header().hash);
}
else
{
BEAST_EXPECT(a[s] == ID{0});
}
}
}
// Mismatch tests
// Empty with non-empty
{
RCLValidatedLedger const a{RCLValidatedLedger::MakeGenesis{}};
for (auto const& ledger : {history.back(), history[maxAncestors - 1]})
{
RCLValidatedLedger const b{ledger, env.journal};
BEAST_EXPECT(mismatch(a, b) == 1);
BEAST_EXPECT(mismatch(b, a) == 1);
}
}
// Same chains, different seqs
{
RCLValidatedLedger const a{history.back(), env.journal};
for (Seq s = a.seq(); s > 0; s--)
{
RCLValidatedLedger const b{history[s - 1], env.journal};
if (s >= a.minSeq())
{
BEAST_EXPECT(mismatch(a, b) == b.seq() + 1);
BEAST_EXPECT(mismatch(b, a) == b.seq() + 1);
}
else
{
BEAST_EXPECT(mismatch(a, b) == Seq{1});
BEAST_EXPECT(mismatch(b, a) == Seq{1});
}
}
}
// Different chains, same seqs
{
// Alt history diverged at history.size()/2
for (Seq s = 1; s < history.size(); ++s)
{
RCLValidatedLedger const a{history[s - 1], env.journal};
RCLValidatedLedger const b{altHistory[s - 1], env.journal};
BEAST_EXPECT(a.seq() == b.seq());
if (s <= diverge)
{
BEAST_EXPECT(a[a.seq()] == b[b.seq()]);
BEAST_EXPECT(mismatch(a, b) == a.seq() + 1);
BEAST_EXPECT(mismatch(b, a) == a.seq() + 1);
}
else
{
BEAST_EXPECT(a[a.seq()] != b[b.seq()]);
BEAST_EXPECT(mismatch(a, b) == diverge + 1);
BEAST_EXPECT(mismatch(b, a) == diverge + 1);
}
}
}
// Different chains, different seqs
{
// Compare around the divergence point
RCLValidatedLedger const a{history[diverge], env.journal};
for (Seq offset = diverge / 2; offset < 3 * diverge / 2; ++offset)
{
RCLValidatedLedger const b{altHistory[offset - 1], env.journal};
if (offset <= diverge)
{
BEAST_EXPECT(mismatch(a, b) == b.seq() + 1);
}
else
{
BEAST_EXPECT(mismatch(a, b) == diverge + 1);
}
}
}
}
void
testLedgerTrieRCLValidatedLedger()
{
testcase("RCLValidatedLedger LedgerTrie");
// This test exposes an issue with the limited 256
// ancestor hash design of RCLValidatedLedger.
// There is only a single chain of validated ledgers
// but the 256 gap causes a "split" in the LedgerTrie
// due to the lack of ancestry information for a later ledger.
// This exposes a bug in which we are unable to remove
// support for a ledger hash which is already in the trie.
using Seq = RCLValidatedLedger::Seq;
// Max known ancestors for each ledger
Seq const maxAncestors = 256;
std::vector<std::shared_ptr<Ledger const>> history;
// Generate a chain of 256 + 10 ledgers
jtx::Env env(*this);
auto& j = env.journal;
Config const config;
auto prev = std::make_shared<Ledger const>(
kCreateGenesis,
Rules{config.features},
config.fees.toFees(),
std::vector<uint256>{},
env.app().getNodeFamily());
history.push_back(prev);
for (auto i = 0; i < (maxAncestors + 10); ++i)
{
auto next = std::make_shared<Ledger>(*prev, env.app().getTimeKeeper().closeTime());
next->updateSkipList();
history.push_back(next);
prev = next;
}
LedgerTrie<RCLValidatedLedger> trie;
// First, create the single branch trie, with ledgers
// separated by exactly 256 ledgers
auto ledg002 = RCLValidatedLedger{history[1], j};
auto ledg258 = RCLValidatedLedger{history[257], j};
auto ledg259 = RCLValidatedLedger{history[258], j};
trie.insert(ledg002);
trie.insert(ledg258, 4);
// trie.dump(std::cout);
// 000000[0,1)(T:0,B:5)
// |-AB868A..36C8[1,3)(T:1,B:5)
// |-AB868A..37C8[3,259)(T:4,B:4)
BEAST_EXPECT(trie.tipSupport(ledg002) == 1);
BEAST_EXPECT(trie.branchSupport(ledg002) == 5);
BEAST_EXPECT(trie.tipSupport(ledg258) == 4);
BEAST_EXPECT(trie.branchSupport(ledg258) == 4);
// Move three of the s258 ledgers to s259, which splits the trie
// due to the 256 ancestry limit
BEAST_EXPECT(trie.remove(ledg258, 3));
trie.insert(ledg259, 3);
[[maybe_unused]] auto unused1 = trie.getPreferred(1);
// trie.dump(std::cout);
// 000000[0,1)(T:0,B:5)
// |-AB868A..37C9[1,260)(T:3,B:3)
// |-AB868A..36C8[1,3)(T:1,B:2)
// |-AB868A..37C8[3,259)(T:1,B:1)
BEAST_EXPECT(trie.tipSupport(ledg002) == 1);
BEAST_EXPECT(trie.branchSupport(ledg002) == 2);
BEAST_EXPECT(trie.tipSupport(ledg258) == 1);
BEAST_EXPECT(trie.branchSupport(ledg258) == 1);
BEAST_EXPECT(trie.tipSupport(ledg259) == 3);
BEAST_EXPECT(trie.branchSupport(ledg259) == 3);
// The last call to trie.getPreferred cycled the children of the root
// node to make the new branch the first child (since it has support 3)
// then verify the remove call works
// past bug: remove had assumed the first child of a node in the trie
// which matches is the *only* child in the trie which matches.
// This is **NOT** true with the limited 256 ledger ancestry
// quirk of RCLValidation and prevents deleting the old support
// for ledger 257
BEAST_EXPECT(trie.remove(RCLValidatedLedger{history[257], env.journal}, 1));
trie.insert(RCLValidatedLedger{history[258], env.journal}, 1);
[[maybe_unused]] auto unused2 = trie.getPreferred(1);
// trie.dump(std::cout);
// 000000[0,1)(T:0,B:5)
// |-AB868A..37C9[1,260)(T:4,B:4)
// |-AB868A..36C8[1,3)(T:1,B:1)
BEAST_EXPECT(trie.tipSupport(ledg002) == 1);
BEAST_EXPECT(trie.branchSupport(ledg002) == 1);
BEAST_EXPECT(trie.tipSupport(ledg258) == 0);
// 258 no longer lives on a tip in the tree, BUT it is an ancestor
// of 259 which is a tip and therefore gets it's branchSupport value
// implicitly
BEAST_EXPECT(trie.branchSupport(ledg258) == 4);
BEAST_EXPECT(trie.tipSupport(ledg259) == 4);
BEAST_EXPECT(trie.branchSupport(ledg259) == 4);
}
public:
void
run() override
{
testChangeTrusted();
testRCLValidatedLedger();
testLedgerTrieRCLValidatedLedger();
}
};
BEAST_DEFINE_TESTSUITE(RCLValidations, app, xrpl);
} // namespace xrpl::test