Skip to content

Commit

Permalink
Changed svnn_ibc contract bitset to use uint32_t block size to align …
Browse files Browse the repository at this point in the history
…with hs_bitset internal storage
  • Loading branch information
systemzax committed Apr 16, 2024
1 parent 5900424 commit 7186456
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 52 deletions.
2 changes: 1 addition & 1 deletion unittests/svnn_ibc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ BOOST_AUTO_TEST_SUITE(svnn_ibc)
("from_block_num", 1)
("policy", mvo()
("generation", 1)
("fthreshold", 2)
("threshold", 2)
("last_block_num", 0)
("finalizers", fc::variants({
mvo()
Expand Down
28 changes: 14 additions & 14 deletions unittests/test-contracts/svnn_ibc/bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,44 @@ using namespace eosio;

class bitset {
public:
bitset(size_t size)
: num_bits(size), data((size + 63) / 64) {}
bitset(uint32_t size)
: num_bits(size), data((size + 31) / 32) {}

bitset(size_t size, const std::vector<uint64_t>& raw_bitset)
bitset(uint32_t size, const std::vector<uint32_t>& raw_bitset)
: num_bits(size), data(raw_bitset) {
check(raw_bitset.size() == (size + 63) / 64, "invalid raw bitset size");
check(raw_bitset.size() == (size + 31) / 32, "invalid raw bitset size");

}

// Set a bit to 1
void set(size_t index) {
void set(uint32_t index) {
check_bounds(index);
data[index / 64] |= (1ULL << (index % 64));
data[index / 32] |= (1ULL << (index % 32));
}

// Clear a bit (set to 0)
void clear(size_t index) {
void clear(uint32_t index) {
check_bounds(index);
data[index / 64] &= ~(1ULL << (index % 64));
data[index / 32] &= ~(1ULL << (index % 32));
}

// Check if a bit is set
bool test(size_t index) const {
bool test(uint32_t index) const {
check_bounds(index);
return (data[index / 64] & (1ULL << (index % 64))) != 0;
return (data[index / 32] & (1ULL << (index % 32))) != 0;
}

// Size of the bitset
size_t size() const {
uint32_t size() const {
return num_bits;
}

private:
size_t num_bits;
std::vector<uint64_t> data;
uint32_t num_bits;
std::vector<uint32_t> data;

// Check if the index is within bounds
void check_bounds(size_t index) const {
void check_bounds(uint32_t index) const {
check(index < num_bits, "bitset index out of bounds");
}
};
33 changes: 2 additions & 31 deletions unittests/test-contracts/svnn_ibc/svnn_ibc.abi
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@
}
]
},
{
"name": "clear",
"base": "",
"fields": []
},
{
"name": "dynamic_data_v0",
"base": "",
Expand Down Expand Up @@ -152,7 +147,7 @@
"type": "uint32"
},
{
"name": "fthreshold",
"name": "threshold",
"type": "uint64"
},
{
Expand Down Expand Up @@ -253,7 +248,7 @@
"fields": [
{
"name": "finalizers",
"type": "uint64[]"
"type": "uint32[]"
},
{
"name": "signature",
Expand Down Expand Up @@ -320,20 +315,6 @@
"type": "time_point"
}
]
},
{
"name": "test",
"base": "",
"fields": [
{
"name": "pk",
"type": "bytes"
},
{
"name": "sig",
"type": "bytes"
}
]
}
],
"actions": [
Expand All @@ -342,20 +323,10 @@
"type": "checkproof",
"ricardian_contract": ""
},
{
"name": "clear",
"type": "clear",
"ricardian_contract": ""
},
{
"name": "setfpolicy",
"type": "setfpolicy",
"ricardian_contract": ""
},
{
"name": "test",
"type": "test",
"ricardian_contract": ""
}
],
"tables": [
Expand Down
6 changes: 3 additions & 3 deletions unittests/test-contracts/svnn_ibc/svnn_ibc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void svnn_ibc::_maybe_set_finalizer_policy(const fpolicy& policy, const uint32_t
}
svnn_ibc::storedpolicy spolicy;
spolicy.generation = policy.generation;
spolicy.fthreshold = policy.fthreshold;
spolicy.threshold = policy.threshold;
spolicy.finalizers = policy.finalizers;

//policy is in force until a newer policy is proven
Expand Down Expand Up @@ -99,7 +99,7 @@ void svnn_ibc::_check_qc(const quorum_certificate& qc, const checksum256& finali
auto fa_itr = target_policy.finalizers.begin();
auto fa_end_itr = target_policy.finalizers.end();
size_t finalizer_count = std::distance(fa_itr, fa_end_itr);
std::vector<uint64_t> bitset_data(qc.finalizers);
std::vector<uint32_t> bitset_data(qc.finalizers);
bitset b(finalizer_count, bitset_data);

bool first = true;
Expand All @@ -124,7 +124,7 @@ void svnn_ibc::_check_qc(const quorum_certificate& qc, const checksum256& finali
}

//verify that we have enough vote weight to meet the quorum threshold of the target policy
check(weight>=target_policy.fthreshold, "insufficient signatures to reach quorum");
check(weight>=target_policy.threshold, "insufficient signatures to reach quorum");
std::array<uint8_t, 32> data = finality_digest.extract_as_byte_array();
std::vector<const char> v_data(data.begin(), data.end());
//verify signature validity
Expand Down
6 changes: 3 additions & 3 deletions unittests/test-contracts/svnn_ibc/svnn_ibc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ CONTRACT svnn_ibc : public contract {
}

struct quorum_certificate {
std::vector<uint64_t> finalizers;
std::vector<uint32_t> finalizers;
bls_signature signature;
};

Expand All @@ -85,7 +85,7 @@ CONTRACT svnn_ibc : public contract {
struct fpolicy {

uint32_t generation = 0; ///< sequentially incrementing version number
uint64_t fthreshold = 0; ///< vote weight threshold to finalize blocks
uint64_t threshold = 0; ///< vote weight threshold to finalize blocks
std::vector<finalizer_authority> finalizers; ///< Instant Finality voter set

checksum256 digest() const {
Expand All @@ -105,7 +105,7 @@ CONTRACT svnn_ibc : public contract {
uint64_t primary_key() const {return generation;}
uint64_t by_cache_expiry()const { return cache_expiry.sec_since_epoch(); }

EOSLIB_SERIALIZE( storedpolicy, (generation)(fthreshold)(finalizers)(last_block_num)(cache_expiry))
EOSLIB_SERIALIZE( storedpolicy, (generation)(threshold)(finalizers)(last_block_num)(cache_expiry))

};

Expand Down
Binary file modified unittests/test-contracts/svnn_ibc/svnn_ibc.wasm
Binary file not shown.

0 comments on commit 7186456

Please sign in to comment.