fix(protocol): reset moved validator bitsets

This commit is contained in:
Nicholas Dudfield
2026-07-13 19:05:22 +07:00
parent 5657f75d3c
commit 37d9eaaa2b
2 changed files with 66 additions and 0 deletions

View File

@@ -27,6 +27,7 @@
#include <cstddef>
#include <cstdint>
#include <optional>
#include <utility>
namespace ripple {
@@ -47,6 +48,36 @@ class ValidatedValidatorBitset
}
public:
ValidatedValidatorBitset(ValidatedValidatorBitset const&) = default;
ValidatedValidatorBitset&
operator=(ValidatedValidatorBitset const&) = default;
ValidatedValidatorBitset(ValidatedValidatorBitset&& other) noexcept
: bitset_(std::move(other.bitset_))
, memberCount_(other.memberCount_)
, selected_(other.selected_)
{
other.bitset_.clear();
other.memberCount_ = 0;
other.selected_ = 0;
}
ValidatedValidatorBitset&
operator=(ValidatedValidatorBitset&& other) noexcept
{
if (this != &other)
{
bitset_ = std::move(other.bitset_);
memberCount_ = other.memberCount_;
selected_ = other.selected_;
other.bitset_.clear();
other.memberCount_ = 0;
other.selected_ = 0;
}
return *this;
}
/** Construct an owning, validated validator bitset. */
static std::optional<ValidatedValidatorBitset>
make(Slice bitset, std::size_t memberCount);

View File

@@ -19,6 +19,8 @@
#include <xrpl/beast/unit_test.h>
#include <xrpl/protocol/ValidatorBitset.h>
#include <utility>
namespace ripple {
namespace test {
@@ -117,6 +119,38 @@ public:
}
}
void
testValueSemantics()
{
testcase("value semantics");
Blob const bits{0x01, 0x02};
auto source = validateValidatorBitset(makeSlice(bits), 10);
BEAST_EXPECT(source);
if (!source)
return;
auto copy = *source;
BEAST_EXPECT(copy.selected() == 2);
BEAST_EXPECT(copy.contains(0));
BEAST_EXPECT(copy.contains(9));
auto moved = std::move(*source);
BEAST_EXPECT(moved.selected() == 2);
BEAST_EXPECT(moved.contains(0));
BEAST_EXPECT(moved.contains(9));
BEAST_EXPECT(source->selected() == 0);
BEAST_EXPECT(!source->contains(0));
auto assigned = copy;
assigned = std::move(moved);
BEAST_EXPECT(assigned.selected() == 2);
BEAST_EXPECT(assigned.contains(0));
BEAST_EXPECT(assigned.contains(9));
BEAST_EXPECT(moved.selected() == 0);
BEAST_EXPECT(!moved.contains(0));
}
void
run() override
{
@@ -124,6 +158,7 @@ public:
testConstructionAndPopulation();
testCanonicalShape();
testValidatedOwnership();
testValueSemantics();
}
};