Skip to content

Commit

Permalink
refactor: use refactored BitVector
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitriy Khaustov aka xDimon <[email protected]>
  • Loading branch information
xDimon committed Feb 22, 2025
1 parent 26561a9 commit 912d19c
Show file tree
Hide file tree
Showing 34 changed files with 300 additions and 304 deletions.
4 changes: 2 additions & 2 deletions cmake/Hunter/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ hunter_config(

hunter_config(
scale
URL https://github.com/qdrvm/scale-codec-cpp/archive/e5792b13643f938713a95b2bb5116b7994755b27.tar.gz
SHA1 5306ec9dc8e26f1198967f9c49ce86eeff3c6934
URL https://github.com/qdrvm/scale-codec-cpp/archive/915a7057a19fe53e10c93c08018fc9d22d155cca.tar.gz
SHA1 c7994178afed86a473cc5aae27e696dedae8dbfe
KEEP_PACKAGE_SOURCES
)

Expand Down
2 changes: 1 addition & 1 deletion core/benchmark/block_execution_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ namespace kagome::benchmark {
OUTCOME_TRY(batch, storage.getEphemeralBatchAt(state));

OUTCOME_TRY(enc_block_weight, batch->get(BLOCK_WEIGHT_KEY));
scale::DecoderFromBytes decoder{enc_block_weight.view()};
scale::DecoderFromSpan decoder{enc_block_weight.view()};
ConsumedWeight block_weight;
try {
decode(block_weight, decoder);
Expand Down
4 changes: 4 additions & 0 deletions core/crypto/type_hasher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ namespace kagome::crypto {
~EncoderToHash() override = default;
EncoderToHash &operator=(EncoderToHash &&) noexcept = delete;
EncoderToHash &operator=(const EncoderToHash &) = delete;
/// @node Does not matter here. Implemented to follow interface
[[nodiscard]] constexpr bool isContinuousReceiver() const override {
return true;
}
void put(uint8_t byte) override {
[[unlikely]] if (finalized_) {
scale::raise(EncodeForHashError::ALREADY_FINALIZED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,16 @@ namespace kagome::dispute {
}
auto &dispute_state = it->second;

const auto size = dispute_state.validators_against.bits.size();
const auto size = dispute_state.validators_against.size();
auto supermajority =
static_cast<ssize_t>(size - (std::min<size_t>(1, size) - 1) / 3);

// Check if there are enough onchain votes for or against to conclude
// the dispute
bool concluded_onchain = false;
for (const auto &bits : {dispute_state.validators_for.bits,
dispute_state.validators_against.bits}) {
if (std::ranges::count(bits, true) >= supermajority) {
for (const auto &bits :
{dispute_state.validators_for, dispute_state.validators_against}) {
if (std::count(bits.begin(), bits.end(), true) >= supermajority) {
concluded_onchain = true;
break;
}
Expand Down Expand Up @@ -330,12 +330,12 @@ namespace kagome::dispute {
}

auto in_validators_for =
onchain_state.validators_for.bits.size() < validator_index
? onchain_state.validators_for.bits[validator_index]
onchain_state.validators_for.size() < validator_index
? onchain_state.validators_for[validator_index]
: false;
auto in_validators_against =
onchain_state.validators_against.bits.size() < validator_index
? onchain_state.validators_against.bits[validator_index]
onchain_state.validators_against.size() < validator_index
? onchain_state.validators_against[validator_index]
: false;

if (in_validators_for and in_validators_against) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

#pragma once

#include <scale/bit_vector.hpp>

#include "dispute_coordinator/types.hpp"
#include "log/logger.hpp"
#include "scale/bitvec.hpp"

namespace kagome::runtime {
class ParachainHost;
Expand All @@ -23,9 +24,9 @@ namespace kagome::dispute {
/// The entire state of a dispute.
struct DisputeState {
/// A bitfield indicating all validators for the candidate.
scale::BitVec validators_for;
scale::BitVector validators_for;
/// A bitfield indicating all validators against the candidate.
scale::BitVec validators_against;
scale::BitVector validators_against;
/// The block number at which the dispute started on-chain.
primitives::BlockNumber start;
/// The block number at which the dispute concluded on-chain.
Expand Down
22 changes: 11 additions & 11 deletions core/network/types/collator_messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <boost/variant.hpp>
#include <libp2p/peer/peer_info.hpp>
#include <scale/bitvec.hpp>
#include <scale/bit_vector.hpp>
#include <tuple>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -173,13 +173,13 @@ namespace kagome::network {
struct BackedCandidate {
CommittedCandidateReceipt candidate;
std::vector<ValidityAttestation> validity_votes;
scale::BitVec validator_indices;
scale::BitVector validator_indices;

/// Creates `BackedCandidate` from args.
static BackedCandidate from(
CommittedCandidateReceipt candidate_,
std::vector<ValidityAttestation> validity_votes_,
scale::BitVec validator_indices_,
scale::BitVector validator_indices_,
std::optional<CoreIndex> core_index_) {
BackedCandidate backed{
.candidate = std::move(candidate_),
Expand All @@ -195,16 +195,16 @@ namespace kagome::network {
}

void inject_core_index(CoreIndex core_index) {
scale::BitVec core_index_to_inject;
core_index_to_inject.bits.assign(8, false);
scale::BitVector core_index_to_inject;
core_index_to_inject.assign(8, false);

auto val = uint8_t(core_index);
for (size_t i = 0; i < 8; ++i) {
core_index_to_inject.bits[i] = (val >> i) & 1;
core_index_to_inject[i] = (val >> i) & 1;
}
validator_indices.bits.insert(validator_indices.bits.end(),
core_index_to_inject.bits.begin(),
core_index_to_inject.bits.end());
validator_indices.insert(validator_indices.end(),
core_index_to_inject.begin(),
core_index_to_inject.end());
}
};

Expand Down Expand Up @@ -232,7 +232,7 @@ namespace kagome::network {
};

/// Signed availability bitfield.
using SignedBitfield = parachain::IndexedAndSigned<scale::BitVec>;
using SignedBitfield = parachain::IndexedAndSigned<scale::BitVector>;

struct BitfieldDistribution {
primitives::BlockHash relay_parent; /// Hash of the relay chain block
Expand Down Expand Up @@ -423,7 +423,7 @@ struct fmt::formatter<kagome::network::SignedBitfield> {
auto format(const kagome::network::SignedBitfield &val,
FormatContext &ctx) const -> decltype(ctx.out()) {
char buf[8] = {0};
const auto &bits = val.payload.payload.bits;
const auto &bits = val.payload.payload;

static_assert(sizeof(buf) > 1, "Because of last zero-terminate symbol");
size_t ix = 0;
Expand Down
57 changes: 27 additions & 30 deletions core/network/types/collator_messages_vstaging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <vector>

#include <boost/variant.hpp>
#include <scale/bitvec.hpp>
#include <scale/bit_vector.hpp>

#include "common/blob.hpp"
#include "consensus/grandpa/common.hpp"
Expand Down Expand Up @@ -39,7 +39,7 @@ namespace kagome::network::vstaging {
struct Assignment {
kagome::parachain::approval::IndirectAssignmentCertV2
indirect_assignment_cert;
scale::BitVec candidate_bitfield;
scale::BitVector candidate_bitfield;
};

struct Assignments {
Expand Down Expand Up @@ -187,14 +187,14 @@ namespace kagome::network::vstaging {

struct StatementFilter {
/// Seconded statements. '1' is known or undesired.
scale::BitVec seconded_in_group;
scale::BitVector seconded_in_group;
/// Valid statements. '1' is known or undesired.
scale::BitVec validated_in_group;
scale::BitVector validated_in_group;

StatementFilter() = default;
StatementFilter(size_t len, bool val = false) {
seconded_in_group.bits.assign(len, val);
validated_in_group.bits.assign(len, val);
seconded_in_group.assign(len, val);
validated_in_group.assign(len, val);
}

bool operator==(const StatementFilter &other) const = default;
Expand All @@ -204,27 +204,27 @@ namespace kagome::network::vstaging {
validated_in_group);

public:
void mask_seconded(const scale::BitVec &mask) {
for (size_t i = 0; i < seconded_in_group.bits.size(); ++i) {
const bool m = (i < mask.bits.size()) ? mask.bits[i] : false;
seconded_in_group.bits[i] = seconded_in_group.bits[i] && !m;
void mask_seconded(const scale::BitVector &mask) {
for (size_t i = 0; i < seconded_in_group.size(); ++i) {
const bool m = (i < mask.size()) ? mask[i] : false;
seconded_in_group[i] = seconded_in_group[i] && !m;
}
}

void mask_valid(const scale::BitVec &mask) {
for (size_t i = 0; i < validated_in_group.bits.size(); ++i) {
const bool m = (i < mask.bits.size()) ? mask.bits[i] : false;
validated_in_group.bits[i] = validated_in_group.bits[i] && !m;
void mask_valid(const scale::BitVector &mask) {
for (size_t i = 0; i < validated_in_group.size(); ++i) {
const bool m = (i < mask.size()) ? mask[i] : false;
validated_in_group[i] = validated_in_group[i] && !m;
}
}

bool has_len(size_t len) const {
return seconded_in_group.bits.size() == len
&& validated_in_group.bits.size() == len;
return seconded_in_group.size() == len
&& validated_in_group.size() == len;
}

bool has_seconded() const {
for (const auto x : seconded_in_group.bits) {
for (const auto x : seconded_in_group) {
if (x) {
return true;
}
Expand All @@ -233,13 +233,12 @@ namespace kagome::network::vstaging {
}

size_t backing_validators() const {
BOOST_ASSERT(seconded_in_group.bits.size()
== validated_in_group.bits.size());
BOOST_ASSERT(seconded_in_group.size() == validated_in_group.size());

size_t count = 0;
for (size_t ix = 0; ix < seconded_in_group.bits.size(); ++ix) {
const auto s = seconded_in_group.bits[ix];
const auto v = validated_in_group.bits[ix];
for (size_t ix = 0; ix < seconded_in_group.size(); ++ix) {
const auto s = seconded_in_group[ix];
const auto v = validated_in_group[ix];
count += size_t(s || v);
}
return count;
Expand All @@ -248,25 +247,23 @@ namespace kagome::network::vstaging {
bool contains(size_t index, StatementKind statement_kind) const {
switch (statement_kind) {
case StatementKind::Seconded:
return index < seconded_in_group.bits.size()
&& seconded_in_group.bits[index];
return index < seconded_in_group.size() && seconded_in_group[index];
case StatementKind::Valid:
return index < validated_in_group.bits.size()
&& validated_in_group.bits[index];
return index < validated_in_group.size() && validated_in_group[index];
}
return false;
}

void set(size_t index, StatementKind statement_kind) {
switch (statement_kind) {
case StatementKind::Seconded:
if (index < seconded_in_group.bits.size()) {
seconded_in_group.bits[index] = true;
if (index < seconded_in_group.size()) {
seconded_in_group[index] = true;
}
break;
case StatementKind::Valid:
if (index < validated_in_group.bits.size()) {
validated_in_group.bits[index] = true;
if (index < validated_in_group.size()) {
validated_in_group[index] = true;
}
break;
}
Expand Down
12 changes: 6 additions & 6 deletions core/parachain/approval/approval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace kagome::parachain::approval {
/// relay-chain block where the candidates were included.
struct RelayVRFModuloCompact {
/// A bitfield representing the core indices claimed by this assignment.
scale::BitVec core_bitfield;
scale::BitVector core_bitfield;
bool operator==(const RelayVRFModuloCompact &other) const = default;
};

Expand Down Expand Up @@ -143,15 +143,15 @@ namespace kagome::parachain::approval {

/// The index of the candidate in the list of candidates fully included
/// as-of the block.
scale::BitVec candidate_indices;
scale::BitVector candidate_indices;

static IndirectApprovalVoteV2 from(const IndirectApprovalVote &value) {
scale::BitVec v;
v.bits.resize(value.candidate_index + 1);
v.bits[value.candidate_index] = true;
scale::BitVector bits;
bits.resize(value.candidate_index + 1);
bits[value.candidate_index] = true;
return IndirectApprovalVoteV2{
.block_hash = value.block_hash,
.candidate_indices = std::move(v),
.candidate_indices = std::move(bits),
};
}
};
Expand Down
Loading

0 comments on commit 912d19c

Please sign in to comment.