-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2011 from AntelopeIO/block_extension_qc
IF: add latest commit QC known to proposer to block extension (part 1)
- Loading branch information
Showing
12 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <fc/variant.hpp> | ||
#include <boost/dynamic_bitset.hpp> | ||
|
||
namespace fc | ||
{ | ||
template<typename T> void to_variant( const boost::dynamic_bitset<T>& bs, fc::variant& v ) { | ||
auto num_blocks = bs.num_blocks(); | ||
if ( num_blocks > MAX_NUM_ARRAY_ELEMENTS ) throw std::range_error( "number of blocks of dynamic_bitset cannot be greather than MAX_NUM_ARRAY_ELEMENTS" ); | ||
|
||
std::vector<T> blocks(num_blocks); | ||
boost::to_block_range(bs, blocks.begin()); | ||
|
||
v = fc::variant(blocks); | ||
} | ||
|
||
template<typename T> void from_variant( const fc::variant& v, boost::dynamic_bitset<T>& bs ) { | ||
const std::vector<fc::variant>& vars = v.get_array(); | ||
auto num_vars = vars.size(); | ||
if( num_vars > MAX_NUM_ARRAY_ELEMENTS ) throw std::range_error( "number of variants for dynamic_bitset cannot be greather than MAX_NUM_ARRAY_ELEMENTS" ); | ||
|
||
std::vector<T> blocks; | ||
blocks.reserve(num_vars); | ||
for( const auto& var: vars ) { | ||
blocks.push_back( var.as<T>() ); | ||
} | ||
|
||
bs = { blocks.cbegin(), blocks.cend() }; | ||
} | ||
} // namespace fc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <fc/exception/exception.hpp> | ||
#include <fc/io/raw.hpp> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
#include <boost/dynamic_bitset.hpp> | ||
|
||
using namespace fc; | ||
|
||
BOOST_AUTO_TEST_SUITE(raw_test_suite) | ||
|
||
|
||
BOOST_AUTO_TEST_CASE(dynamic_bitset_test) | ||
{ | ||
constexpr uint8_t bits = 0b00011110; | ||
boost::dynamic_bitset<uint8_t> bs1(8, bits); // bit set size 8 | ||
|
||
char buff[4]; | ||
datastream<char*> ds(buff, sizeof(buff)); | ||
|
||
fc::raw::pack( ds, bs1 ); | ||
|
||
boost::dynamic_bitset<uint8_t> bs2(8); | ||
ds.seekp(0); | ||
fc::raw::unpack( ds, bs2 ); | ||
|
||
// 0b00011110 | ||
BOOST_CHECK(!bs2.test(0)); | ||
BOOST_CHECK(bs2.test(1)); | ||
BOOST_CHECK(bs2.test(2)); | ||
BOOST_CHECK(bs2.test(2)); | ||
BOOST_CHECK(bs2.test(3)); | ||
BOOST_CHECK(bs2.test(4)); | ||
BOOST_CHECK(!bs2.test(5)); | ||
BOOST_CHECK(!bs2.test(6)); | ||
BOOST_CHECK(!bs2.test(7)); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
libraries/libfc/test/variant/test_variant_dynamic_bitset.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <fc/variant_dynamic_bitset.hpp> | ||
#include <fc/variant_object.hpp> | ||
#include <fc/exception/exception.hpp> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <string> | ||
|
||
using namespace fc; | ||
using std::string; | ||
|
||
BOOST_AUTO_TEST_SUITE(dynamic_bitset_test_suite) | ||
|
||
BOOST_AUTO_TEST_CASE(dynamic_bitset_test) | ||
{ | ||
constexpr uint8_t bits = 0b0000000001010100; | ||
boost::dynamic_bitset<uint8_t> bs(16, bits); // 2 blocks of uint8_t | ||
|
||
fc::mutable_variant_object mu; | ||
mu("bs", bs); | ||
|
||
// a vector of 2 blocks | ||
const variants& vars = mu["bs"].get_array(); | ||
BOOST_CHECK_EQUAL(vars.size(), 2u); | ||
|
||
// blocks can be in any order | ||
if (vars[0].as<uint32_t>() == bits ) { | ||
BOOST_CHECK_EQUAL(vars[1].as<uint32_t>(), 0u); | ||
} else if (vars[1].as<uint32_t>() == bits ) { | ||
BOOST_CHECK_EQUAL(vars[0].as<uint32_t>(), 0u); | ||
} else { | ||
BOOST_CHECK(false); | ||
} | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |