diff --git a/include/xrpl/protocol/ValidatorBitset.h b/include/xrpl/protocol/ValidatorBitset.h index aebc7a39f..b702a0697 100644 --- a/include/xrpl/protocol/ValidatorBitset.h +++ b/include/xrpl/protocol/ValidatorBitset.h @@ -32,7 +32,7 @@ namespace ripple { class ValidatedValidatorBitset { - Slice bitset_; + Blob bitset_; std::size_t memberCount_ = 0; std::size_t selected_ = 0; @@ -40,15 +40,14 @@ class ValidatedValidatorBitset Slice bitset, std::size_t memberCount, std::size_t selected) - : bitset_(bitset), memberCount_(memberCount), selected_(selected) + : bitset_(bitset.begin(), bitset.end()) + , memberCount_(memberCount) + , selected_(selected) { } public: - /** Construct a validated, non-owning view over a validator bitset. - - The caller must keep the bitset storage alive while using the view. - */ + /** Construct an owning, validated validator bitset. */ static std::optional make(Slice bitset, std::size_t memberCount); diff --git a/src/test/protocol/ValidatorBitset_test.cpp b/src/test/protocol/ValidatorBitset_test.cpp index ca0edd7dc..00238f9d1 100644 --- a/src/test/protocol/ValidatorBitset_test.cpp +++ b/src/test/protocol/ValidatorBitset_test.cpp @@ -87,12 +87,43 @@ public: BEAST_EXPECT(full->selected() == 8); } + void + testValidatedOwnership() + { + testcase("validated ownership"); + + auto const temporary = validateValidatorBitset( + makeSlice(makeValidatorBitset( + 10, [](std::size_t i) { return i == 1 || i == 9; })), + 10); + BEAST_EXPECT(temporary); + if (temporary) + { + BEAST_EXPECT(temporary->selected() == 2); + BEAST_EXPECT(temporary->contains(1)); + BEAST_EXPECT(temporary->contains(9)); + } + + Blob mutableBacking{0x01, 0x00}; + auto const copied = + validateValidatorBitset(makeSlice(mutableBacking), 10); + BEAST_EXPECT(copied); + mutableBacking = Blob{0x00, 0x02}; + if (copied) + { + BEAST_EXPECT(copied->selected() == 1); + BEAST_EXPECT(copied->contains(0)); + BEAST_EXPECT(!copied->contains(9)); + } + } + void run() override { testByteCount(); testConstructionAndPopulation(); testCanonicalShape(); + testValidatedOwnership(); } };