diff --git a/.travis.yml b/.travis.yml index bc23682..c1439fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: c++ +cache: ccache + git: depth: 1 @@ -10,19 +12,25 @@ sudo: true install: - echo "deb http://de.archive.ubuntu.com/ubuntu xenial main restricted universe multiverse" | sudo tee -a /etc/apt/sources.list - sudo apt-get update - - sudo apt-get install --allow-unauthenticated g++ libboost-all-dev cmake libreadline-dev libssl-dev autoconf parallel + - sudo apt-get install --allow-unauthenticated g++ libboost-all-dev cmake libreadline-dev libssl-dev autoconf parallel ccache addons: sonarcloud: - organization: "djxeo0smpujky4mpro50l67fsv74ijvt" + organization: "soundac" token: - secure: "aJ1aXr7JCsd7EwN0GQDJibKK24SW27AYrc5RDS7+MIJ5CCSX8e/cZ8v0Se6JmaXqt4t/VKAuVtv9JW2HZMMv2c2ACDpGWysKftS0Nb3JJCDHth+NZKGWhl9rFGQ90BigL3qQu2c9haJ77uCXko2BudcSqd9ejfSpQLA0tLrT7QGr/iS+pdmEQOVAsNWF4157hYSSxwz1WIHX2uxci2y9zCiE0ckmkpxwmI6qFiIExC6Y7LAATBeE1fT4KocPYZSAbeYx6D0C1llaWnw0ICf1jVF9ZEG79GDlnM9Uqj9rAAWIhgJFie15gYFvsfsv/W1R7u4xh0204/86HEfHCaV5JLUKGuxWRvtHhMmOC4LRf4siscT52R28AUq+QIak4dgL1H4QL6AvhpCK4SeeZj644XA8VJUp7skjpk2cNJ6RZA4TdOY/qSAGSSf/wHizftRdjb7oPn0nHgadr3biDI2LrhGI4N4lAZdxrBjUODkzENsgR4vQ4RtFw62gpxvUpqMwFkNIEYxWo9bVNXmqYzBKJqqL/lHsm7QZHJgpOcn/jFow+R9n73LjKGGDbJcCZ7TUbWWFzhaGiw7aDyYFuZgGVkEuT81KQdxaLjG6X5hMrLGaB8No9T8Vj+4121rcCxX3r8MK0uvVzcaKt9Md51zCJ6GoF4FcQbI8TkOj2O7i4ZM=" + secure: "Cr3q74yHUsS+kcAsAtB+yB2AyDptL8Rpmw+Q8HR+1MIEnIqyp5BLGNzOZnlucNQwUSUeVF81MpVTRgJT7zzVRa3XbVIngT25a3mm7zgULtG+RJG2rIFoeFWmgmj8kf+1HAis2lsJNUDylhrdumRQS3fMXBA/5Hz4Sbrve/TUg40RutAOsaXipwxqsanxnt/vwFndbb8Y/wprmgHrj45IMG+5bdJ6+DjaahJ9c1QDtWhtn9kPVYCJlO+XRzbHoVCOJOw8+r3FW8I5bDKMxbwGnAcAt4h0ZaxWtUSvt/iECpZqmOgirmVKQ9FJFzcCWBw+PY5Z+bnjwLjDCm2HqoNDk08cWLTn4Na/ZFO5AbBjS9L2VYC5PLpboTJnAygKcEcFDxsNDpnJK5mbcyV/Yzcmwwu5bIEfaDJS4aE1hVCqKhWP7Ecg2QxLtDSG4NHmyrG0CCUorhSemgUK5XvZ9QsSMqnBYl5ee9y/mc6swjHTK3DshJOHzWHO0QpoHg+fv5At7CXWvF/0F7/l24RvCweSni6/eT40MsM6tQCAEJRe6RFBeZAXs1RW+uh+A/CVeTKjNOtJNsV8AmJyu3bR3C7Kug3t+jbZ7cqms+wxyeIt01nYkgJM84kAnnJI51x+c3YtdJ4zK7dic7XWfy0jWdGVb9/JydPmhxI2PEKpPqnFmyk=" + +env: + global: + - CCACHE_COMPRESS=exists_means_true + - CCACHE_MAXSIZE=1Gi + - CCACHE_SLOPPINESS=include_file_ctime,include_file_mtime,time_macros script: - cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS=--coverage -DCMAKE_CXX_FLAGS=--coverage -DBoost_USE_STATIC_LIBS=OFF -DCMAKE_CXX_OUTPUT_EXTENSION_REPLACE=ON . - 'which build-wrapper-linux-x86-64 && build-wrapper-linux-x86-64 --out-dir bw-output make -j 2 || make -j 2' - set -o pipefail - - tests/run-parallel-tests.sh tests/all_tests + - tests/all_tests 2>&1 | cat - tests/api 2>&1 | cat - tests/ecc_test README.md 2>&1 | cat - tests/log_test 2>&1 | cat diff --git a/CMakeLists.txt b/CMakeLists.txt index b6345e4..ca311ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -281,7 +281,6 @@ set( fc_sources src/network/rate_limiting.cpp src/network/resolve.cpp src/network/url.cpp - src/compress/smaz.cpp src/compress/zlib.cpp ) @@ -367,15 +366,6 @@ else() set( ZLIB_LIBRARIES "" ) endif( ZLIB_FOUND ) -find_package( BZip2 ) -if( BZIP2_FOUND ) - MESSAGE( STATUS "bzip2 found" ) - add_definitions( -DHAS_BZIP2 ) -else() - MESSAGE( STATUS "bzip2 not found" ) - set( BZIP2_LIBRARIES "" ) -endif( BZIP2_FOUND ) - # This will become unnecessary once we update to websocketpp which fixes upstream issue #395 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWEBSOCKETPP_STRICT_MASKING") @@ -406,11 +396,10 @@ target_include_directories(fc ${CMAKE_CURRENT_SOURCE_DIR}/vendor/secp256k1-zkp ) -#target_link_libraries( fc PUBLIC ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} ${ECC_LIB} ) IF(NOT WIN32) set(LINK_USR_LOCAL_LIB -L/usr/local/lib) ENDIF() -target_link_libraries( fc PUBLIC ${LINK_USR_LOCAL_LIB} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${BZIP2_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} ${editline_libraries} ${ECC_LIB} ) +target_link_libraries( fc PUBLIC ${LINK_USR_LOCAL_LIB} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} ${editline_libraries} ${ECC_LIB} ) if(MSVC) set_source_files_properties( src/network/http/websocket.cpp PROPERTIES COMPILE_FLAGS "/bigobj" ) @@ -522,10 +511,12 @@ ENDIF() IF("${OPENSSL_ROOT_DIR}" STREQUAL "") get_filename_component(OPENSSL_ROOT_DIR "${OPENSSL_INCLUDE_DIR}/.." REALPATH) ENDIF() - SET(OPENSSL_CONF_SOURCE "${OPENSSL_ROOT_DIR}/ssl/openssl.cnf") - IF(MINGW) - SET(OPENSSL_CONF_SOURCE "${OPENSSL_ROOT_DIR}/openssl.cnf") - ENDIF(MINGW) + IF("${OPENSSL_CONF_SOURCE}" STREQUAL "") + SET(OPENSSL_CONF_SOURCE "${OPENSSL_ROOT_DIR}/ssl/openssl.cnf") + IF(MINGW) + SET(OPENSSL_CONF_SOURCE "${OPENSSL_ROOT_DIR}/openssl.cnf") + ENDIF(MINGW) + ENDIF() SET(POST_BUILD_STEP_COMMANDS ${POST_BUILD_STEP_COMMANDS} COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENSSL_CONF_SOURCE}" "${OPENSSL_CONF_TARGET}/openssl.cnf") ENDIF(WIN32) diff --git a/README.md b/README.md index 2416021..bcd6f6b 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ fc == -[![](https://travis-ci.org/themuseblockchain/Muse-FC.svg?branch=master)](https://travis-ci.org/themuseblockchain/Muse-FC) +[![](https://travis-ci.org/soundac/SounDAC-FC.svg?branch=master)](https://travis-ci.org/soundac/SounDAC-FC) FC stands for fast-compiling c++ library and provides a set of utility libraries useful for the development of asynchronous libraries. Some of the highlights include: diff --git a/include/fc/bloom_filter.hpp b/include/fc/bloom_filter.hpp index 67aa5cb..3728d9c 100644 --- a/include/fc/bloom_filter.hpp +++ b/include/fc/bloom_filter.hpp @@ -57,6 +57,20 @@ class bloom_parameters random_seed(0xA5A5A5A55A5A5A5AULL) {} + bloom_parameters(unsigned long long int projected_element_count, + double false_positive_probability, + unsigned long long int maximum_size) : + minimum_size(1), + maximum_size(maximum_size), + minimum_number_of_hashes(1), + maximum_number_of_hashes(std::numeric_limits::max()), + projected_element_count(projected_element_count), + false_positive_probability(false_positive_probability), + random_seed(0xA5A5A5A55A5A5A5AULL) + { + compute_optimal_parameters(); + } + virtual ~bloom_parameters() {} diff --git a/include/fc/compress/smaz.hpp b/include/fc/compress/smaz.hpp deleted file mode 100644 index ad60292..0000000 --- a/include/fc/compress/smaz.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include - -namespace fc { - - std::string smaz_compress( const std::string& in ); - std::string smaz_decompress( const std::string& compressed ); - -} // namespace fc diff --git a/include/fc/container/flat.hpp b/include/fc/container/flat.hpp index 2eca459..5e6a363 100644 --- a/include/fc/container/flat.hpp +++ b/include/fc/container/flat.hpp @@ -11,7 +11,7 @@ namespace fc { inline void pack( Stream& s, const flat_set& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -38,7 +38,7 @@ namespace fc { inline void pack( Stream& s, const flat_map& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -67,7 +67,7 @@ namespace fc { void pack( Stream& s, const bip::vector& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + pack( s, unsigned_int(value.size()), _max_depth ); if( !std::is_fundamental::value ) { auto itr = value.begin(); auto end = value.end(); diff --git a/include/fc/interprocess/container.hpp b/include/fc/interprocess/container.hpp index 85aa56b..6fefedb 100644 --- a/include/fc/interprocess/container.hpp +++ b/include/fc/interprocess/container.hpp @@ -116,7 +116,7 @@ namespace fc { inline void pack( Stream& s, const bip::vector& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { diff --git a/include/fc/io/fstream.hpp b/include/fc/io/fstream.hpp index 8344ce9..9a0c99d 100644 --- a/include/fc/io/fstream.hpp +++ b/include/fc/io/fstream.hpp @@ -2,17 +2,17 @@ #include #include #include +#include namespace fc { class path; class ofstream : virtual public ostream { public: - enum mode { out, binary }; ofstream(); - ofstream( const fc::path& file, int m = binary ); + ofstream( const fc::path& file, std::ios_base::openmode m = std::ios_base::out | std::ios_base::binary ); ~ofstream(); - void open( const fc::path& file, int m = binary ); + void open( const fc::path& file, std::ios_base::openmode m = std::ios_base::out | std::ios_base::binary ); size_t writesome( const char* buf, size_t len ); size_t writesome(const std::shared_ptr& buffer, size_t len, size_t offset); void put( char c ); diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 9dbf9b8..6b439cb 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -158,16 +158,6 @@ namespace fc { fc::raw::unpack( s, *v, _max_depth - 1 ); } FC_RETHROW_EXCEPTIONS( warn, "std::shared_ptr", ("type",fc::get_typename::name()) ) } - template inline void pack( Stream& s, const signed_int& v, uint32_t _max_depth ) { - uint32_t val = (v.value<<1) ^ (v.value>>31); - do { - uint8_t b = uint8_t(val) & 0x7f; - val >>= 7; - b |= ((val > 0) << 7); - s.write((char*)&b,1);//.put(b); - } while( val ); - } - template inline void pack( Stream& s, const unsigned_int& v, uint32_t _max_depth ) { uint64_t val = v.value; do { @@ -178,25 +168,16 @@ namespace fc { }while( val ); } - template inline void unpack( Stream& s, signed_int& vi, uint32_t _max_depth ) { - uint32_t v = 0; char b = 0; int by = 0; - do { - s.get(b); - v |= uint32_t(uint8_t(b) & 0x7f) << by; - by += 7; - } while( uint8_t(b) & 0x80 ); - vi.value = ((v>>1) ^ (v>>31)) + (v&0x01); - vi.value = v&0x01 ? vi.value : -vi.value; - vi.value = -vi.value; - } template inline void unpack( Stream& s, unsigned_int& vi, uint32_t _max_depth ) { uint64_t v = 0; char b = 0; uint8_t by = 0; do { s.get(b); - v |= uint32_t(uint8_t(b) & 0x7f) << by; + if( by >= 64 || (by == 63 && uint8_t(b) > 1) ) + FC_THROW_EXCEPTION( overflow_exception, "Invalid packed unsigned_int!" ); + v |= uint64_t(uint8_t(b) & 0x7f) << by; by += 7; } while( uint8_t(b) & 0x80 ); - vi.value = static_cast(v); + vi.value = static_cast(v); } template inline void unpack( Stream& s, const T& vi, uint32_t _max_depth ) @@ -273,9 +254,9 @@ namespace fc { // std::vector template inline void pack( Stream& s, const std::vector& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth - 1 ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth - 1 ); if( value.size() ) - s.write( &value.front(), (uint32_t)value.size() ); + s.write( &value.front(), value.size() ); } template inline void unpack( Stream& s, std::vector& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); @@ -289,7 +270,7 @@ namespace fc { // fc::string template inline void pack( Stream& s, const fc::string& v, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, unsigned_int((uint32_t)v.size()), _max_depth - 1 ); + fc::raw::pack( s, unsigned_int(v.size()), _max_depth - 1 ); if( v.size() ) s.write( v.c_str(), v.size() ); } @@ -433,7 +414,7 @@ namespace fc { inline void pack( Stream& s, const std::unordered_set& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -478,7 +459,7 @@ namespace fc { inline void pack( Stream& s, const std::unordered_map& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -506,7 +487,7 @@ namespace fc { inline void pack( Stream& s, const std::map& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -534,7 +515,7 @@ namespace fc { inline void pack( Stream& s, const std::deque& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -562,7 +543,7 @@ namespace fc { inline void pack( Stream& s, const std::vector& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { @@ -590,7 +571,7 @@ namespace fc { inline void pack( Stream& s, const std::set& value, uint32_t _max_depth ) { FC_ASSERT( _max_depth > 0 ); --_max_depth; - fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth ); + fc::raw::pack( s, unsigned_int(value.size()), _max_depth ); auto itr = value.begin(); auto end = value.end(); while( itr != end ) { diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index fecb05b..d3b8d46 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -116,9 +116,6 @@ namespace fc { template inline void pack( Stream& s, const std::vector& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, std::vector& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template inline void pack( Stream& s, const signed_int& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template inline void unpack( Stream& s, signed_int& vi, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template inline void pack( Stream& s, const unsigned_int& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, unsigned_int& vi, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); diff --git a/include/fc/io/raw_variant.hpp b/include/fc/io/raw_variant.hpp index b07fbaa..59b93d9 100644 --- a/include/fc/io/raw_variant.hpp +++ b/include/fc/io/raw_variant.hpp @@ -128,7 +128,7 @@ namespace fc { namespace raw { { FC_ASSERT( _max_depth > 0 ); --_max_depth; - unsigned_int vs = (uint32_t)v.size(); + unsigned_int vs = v.size(); pack( s, vs, _max_depth ); for( auto itr = v.begin(); itr != v.end(); ++itr ) { diff --git a/include/fc/io/varint.hpp b/include/fc/io/varint.hpp index 00a8023..30e42bb 100644 --- a/include/fc/io/varint.hpp +++ b/include/fc/io/varint.hpp @@ -4,74 +4,37 @@ namespace fc { struct unsigned_int { - unsigned_int( uint32_t v = 0 ):value(v){} + unsigned_int( uint64_t v = 0 ):value(v){} template unsigned_int( T v ):value(v){} - //operator uint32_t()const { return value; } - //operator uint64_t()const { return value; } - template operator T()const { return static_cast(value); } - unsigned_int& operator=( int32_t v ) { value = v; return *this; } + unsigned_int& operator=( uint64_t v ) { value = v; return *this; } - uint32_t value; + uint64_t value; - friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; } - friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; } + friend bool operator==( const unsigned_int& i, const uint64_t& v ) { return i.value == v; } + friend bool operator==( const uint64_t& i, const unsigned_int& v ) { return i == v.value; } friend bool operator==( const unsigned_int& i, const unsigned_int& v ) { return i.value == v.value; } - friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; } - friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; } + friend bool operator!=( const unsigned_int& i, const uint64_t& v ) { return i.value != v; } + friend bool operator!=( const uint64_t& i, const unsigned_int& v ) { return i != v.value; } friend bool operator!=( const unsigned_int& i, const unsigned_int& v ) { return i.value != v.value; } - friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; } - friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; } + friend bool operator<( const unsigned_int& i, const uint64_t& v ) { return i.value < v; } + friend bool operator<( const uint64_t& i, const unsigned_int& v ) { return i < v.value; } friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; } - friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; } - friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; } + friend bool operator>=( const unsigned_int& i, const uint64_t& v ) { return i.value >= v; } + friend bool operator>=( const uint64_t& i, const unsigned_int& v ) { return i >= v.value; } friend bool operator>=( const unsigned_int& i, const unsigned_int& v ) { return i.value >= v.value; } }; -/** - * @brief serializes a 32 bit signed interger in as few bytes as possible - * - * Uses the google protobuf algorithm for seralizing signed numbers - */ -struct signed_int { - signed_int( int32_t v = 0 ):value(v){} - operator int32_t()const { return value; } - template - signed_int& operator=( const T& v ) { value = v; return *this; } - signed_int operator++(int) { return value++; } - signed_int& operator++(){ ++value; return *this; } - - int32_t value; - - friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; } - friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; } - friend bool operator==( const signed_int& i, const signed_int& v ) { return i.value == v.value; } - - friend bool operator!=( const signed_int& i, const int32_t& v ) { return i.value != v; } - friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; } - friend bool operator!=( const signed_int& i, const signed_int& v ) { return i.value != v.value; } - - friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; } - friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; } - friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; } - - friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; } - friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; } - friend bool operator>=( const signed_int& i, const signed_int& v ) { return i.value >= v.value; } -}; - class variant; -void to_variant( const signed_int& var, variant& vo, uint32_t max_depth = 1 ); -void from_variant( const variant& var, signed_int& vo, uint32_t max_depth = 1 ); void to_variant( const unsigned_int& var, variant& vo, uint32_t max_depth = 1 ); void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth = 1 ); @@ -80,22 +43,13 @@ void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth = 1 #include namespace std { - template<> - struct hash - { - public: - size_t operator()(const fc::signed_int &a) const - { - return std::hash()(a.value); - } - }; template<> struct hash { public: - size_t operator()(const fc::signed_int &a) const + size_t operator()(const fc::unsigned_int &a) const { - return std::hash()(a.value); + return std::hash()(a.value); } }; } diff --git a/include/fc/reflect/typename.hpp b/include/fc/reflect/typename.hpp index 5cd55c9..7223d83 100644 --- a/include/fc/reflect/typename.hpp +++ b/include/fc/reflect/typename.hpp @@ -2,10 +2,12 @@ #include #include +#include #include #include #include +#include #include #include @@ -69,10 +71,34 @@ namespace fc { return n.c_str(); } }; + template struct get_typename< std::set > + { + static const char* name() + { + static std::string n = std::string("std::set<") + std::string(get_typename::name()) + std::string(">"); + return n.c_str(); + } + }; + template struct get_typename< std::pair > + { + static const char* name() + { + static std::string n = std::string("std::pair<") + get_typename::name() + "," + get_typename::name() + ">"; + return n.c_str(); + } + }; + template struct get_typename< fc::smart_ref > + { + static const char* name() + { + static std::string n = std::string("fc::smart_ref<") + get_typename::name() + std::string(">"); + return n.c_str(); + } + }; - struct signed_int; struct unsigned_int; - template<> struct get_typename { static const char* name() { return "signed_int"; } }; + struct variant_object; template<> struct get_typename { static const char* name() { return "unsigned_int"; } }; + template<> struct get_typename { static const char* name() { return "fc::variant_object"; } }; } diff --git a/include/fc/static_variant.hpp b/include/fc/static_variant.hpp index e369216..e6d84a7 100644 --- a/include/fc/static_variant.hpp +++ b/include/fc/static_variant.hpp @@ -343,7 +343,7 @@ struct visitor { typedef Result result_type; }; - struct from_static_variant + struct from_static_variant { variant& var; const uint32_t _max_depth; @@ -387,5 +387,46 @@ struct visitor { s.visit( to_static_variant(ar[1], max_depth - 1) ); } - template struct get_typename { static const char* name() { return typeid(static_variant).name(); } }; + template< typename... T > struct get_comma_separated_typenames; + + template<> + struct get_comma_separated_typenames<> + { + static const char* names() { return ""; } + }; + + template< typename T > + struct get_comma_separated_typenames + { + static const char* names() + { + static const std::string n = get_typename::name(); + return n.c_str(); + } + }; + + template< typename T, typename... Ts > + struct get_comma_separated_typenames + { + static const char* names() + { + static const std::string n = + std::string( get_typename::name() )+","+ + std::string( get_comma_separated_typenames< Ts... >::names() ); + return n.c_str(); + } + }; + + template< typename... T > + struct get_typename< static_variant< T... > > + { + static const char* name() + { + static const std::string n = std::string( "fc::static_variant<" ) + + get_comma_separated_typenames::names() + + ">"; + return n.c_str(); + } + }; + } // namespace fc diff --git a/include/fc/variant.hpp b/include/fc/variant.hpp index 574b0a5..a7675e3 100644 --- a/include/fc/variant.hpp +++ b/include/fc/variant.hpp @@ -178,7 +178,7 @@ namespace fc * and variant_object's. * * variant's allocate everything but strings, arrays, and objects on the - * stack and are 'move aware' for values allcoated on the heap. + * stack and are 'move aware' for values allocated on the heap. * * Memory usage on 64 bit systems is 16 bytes and 12 bytes on 32 bit systems. */ @@ -357,7 +357,7 @@ namespace fc void clear(); private: void init(); - double _data; ///< Alligned according to double requirements + double _data; ///< Aligned according to double requirements char _type[sizeof(void*)]; ///< pad to void* size }; typedef optional ovariant; diff --git a/sonar-project.properties b/sonar-project.properties index 22ffcdc..694c191 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -1,10 +1,10 @@ -sonar.projectKey=MUSE-FC -sonar.projectName=MUSE-FC +sonar.projectKey=SounDAC-FC +sonar.projectName=SounDAC-FC sonar.links.homepage=https://peertracks.com/ -sonar.links.ci=https://travis-ci.org/themuseblockchain/Muse-FC -sonar.links.issue=https://github.com/themuseblockchain/Muse-FC/issues -sonar.links.scm=https://github.com/themuseblockchain/Muse-FC +sonar.links.ci=https://travis-ci.org/soundac/SounDAC-FC +sonar.links.issue=https://github.com/soundac/SounDAC-FC/issues +sonar.links.scm=https://github.com/soundac/SounDAC-FC sonar.tests=tests sonar.exclusions=vendor/**/*,tests/**/* diff --git a/src/compress/smaz.cpp b/src/compress/smaz.cpp deleted file mode 100644 index da1521a..0000000 --- a/src/compress/smaz.cpp +++ /dev/null @@ -1,223 +0,0 @@ -#include -#include -#include -#include -namespace fc { - -typedef const char* const_char_ptr; -/* Our compression codebook, used for compression */ -static const_char_ptr Smaz_cb[241] = { -"\002s,\266", "\003had\232\002leW", "\003on \216", "", "\001yS", -"\002ma\255\002li\227", "\003or \260", "", "\002ll\230\003s t\277", -"\004fromg\002mel", "", "\003its\332", "\001z\333", "\003ingF", "\001>\336", -"\001 \000\003 (\002nc\344", "\002nd=\003 on\312", -"\002ne\213\003hat\276\003re q", "", "\002ngT\003herz\004have\306\003s o\225", -"", "\003ionk\003s a\254\002ly\352", "\003hisL\003 inN\003 be\252", "", -"\003 fo\325\003 of \003 ha\311", "", "\002of\005", -"\003 co\241\002no\267\003 ma\370", "", "", "\003 cl\356\003enta\003 an7", -"\002ns\300\001\"e", "\003n t\217\002ntP\003s, \205", -"\002pe\320\003 we\351\002om\223", "\002on\037", "", "\002y G", "\003 wa\271", -"\003 re\321\002or*", "", "\002=\"\251\002ot\337", "\003forD\002ou[", -"\003 toR", "\003 th\r", "\003 it\366", -"\003but\261\002ra\202\003 wi\363\002<\346", "\002to\024", "\003arew", "\001d\030", -"\002tr\303", "", "\001\n1\003 a \222", "\003f tv\002veo", "\002un\340", "", -"\003e o\242", "\002a \243\002wa\326\001e\002", "\002ur\226\003e a\274", -"\002us\244\003\n\r\n\247", "\002ut\304\003e c\373", "\002we\221", "", "", -"\002wh\302", "\001f,", "", "", "", "\003d t\206", "", "", "\003th \343", -"\001g;", "", "", "\001\r9\003e s\265", "\003e t\234", "", "\003to Y", -"\003e\r\n\236", "\002d \036\001h\022", "", "\001,Q", "\002 a\031", "\002 b^", -"\002\r\n\025\002 cI", "\002 d\245", "\002 e\253", "\002 fh\001i\b\002e \v", -"", "\002 hU\001-\314", "\002 i8", "", "", "\002 l\315", "\002 m{", -"\002f :\002 n\354", "\002 o\035", "\002 p}\001.n\003\r\n\r\250", "", -"\002 r\275", "\002 s>", "\002 t\016", "", "\002g \235\005which+\003whi\367", -"\002 w5", "\001/\305", "\003as \214", "\003at \207", "", "\003who\331", "", -"\001l\026\002h \212", "", "\002, $", "", "\004withV", "", "", "", "\001m-", "", -"", "\002ac\357", "\002ad\350", "\003TheH", "", "", "\004this\233\001n\t", -"", "\002. y", "", "\002alX\003e, \365", "\003tio\215\002be\\", -"\002an\032\003ver\347", "", "\004that0\003tha\313\001o\006", "\003was2", -"\002arO", "\002as.", "\002at'\003the\001\004they\200\005there\322\005theird", -"\002ce\210", "\004were]", "", "\002ch\231\002l \264\001p<", "", "", -"\003one\256", "", "\003he \023\002dej", "\003ter\270", "\002cou", "", -"\002by\177\002di\201\002eax", "", "\002ec\327", "\002edB", "\002ee\353", "", -"", "\001r\f\002n )", "", "", "", "\002el\262", "", "\003in i\002en3", "", -"\002o `\001s\n", "", "\002er\033", "\003is t\002es6", "", "\002ge\371", -"\004.com\375", "\002fo\334\003our\330", "\003ch \301\001t\003", "\002hab", "", -"\003men\374", "", "\002he\020", "", "", "\001u&", "\002hif", "", -"\003not\204\002ic\203", "\003ed @\002id\355", "", "", "\002ho\273", -"\002r K\001vm", "", "", "", "\003t t\257\002il\360", "\002im\342", -"\003en \317\002in\017", "\002io\220", "\002s \027\001wA", "", "\003er |", -"\003es ~\002is%", "\002it/", "", "\002iv\272", "", -"\002t #\ahttp://C\001x\372", "\002la\211", "\001<\341", "\003, a\224" -}; - - -/* Reverse compression codebook, used for decompression */ -static const_char_ptr Smaz_rcb[254] = { -" ", "the", "e", "t", "a", "of", "o", "and", "i", "n", "s", "e ", "r", " th", -" t", "in", "he", "th", "h", "he ", "to", "\r\n", "l", "s ", "d", " a", "an", -"er", "c", " o", "d ", "on", " of", "re", "of ", "t ", ", ", "is", "u", "at", -" ", "n ", "or", "which", "f", "m", "as", "it", "that", "\n", "was", "en", -" ", " w", "es", " an", " i", "\r", "f ", "g", "p", "nd", " s", "nd ", "ed ", -"w", "ed", "http://", "for", "te", "ing", "y ", "The", " c", "ti", "r ", "his", -"st", " in", "ar", "nt", ",", " to", "y", "ng", " h", "with", "le", "al", "to ", -"b", "ou", "be", "were", " b", "se", "o ", "ent", "ha", "ng ", "their", "\"", -"hi", "from", " f", "in ", "de", "ion", "me", "v", ".", "ve", "all", "re ", -"ri", "ro", "is ", "co", "f t", "are", "ea", ". ", "her", " m", "er ", " p", -"es ", "by", "they", "di", "ra", "ic", "not", "s, ", "d t", "at ", "ce", "la", -"h ", "ne", "as ", "tio", "on ", "n t", "io", "we", " a ", "om", ", a", "s o", -"ur", "li", "ll", "ch", "had", "this", "e t", "g ", "e\r\n", " wh", "ere", -" co", "e o", "a ", "us", " d", "ss", "\n\r\n", "\r\n\r", "=\"", " be", " e", -"s a", "ma", "one", "t t", "or ", "but", "el", "so", "l ", "e s", "s,", "no", -"ter", " wa", "iv", "ho", "e a", " r", "hat", "s t", "ns", "ch ", "wh", "tr", -"ut", "/", "have", "ly ", "ta", " ha", " on", "tha", "-", " l", "ati", "en ", -"pe", " re", "there", "ass", "si", " fo", "wa", "ec", "our", "who", "its", "z", -"fo", "rs", ">", "ot", "un", "<", "im", "th ", "nc", "ate", "><", "ver", "ad", -" we", "ly", "ee", " n", "id", " cl", "ac", "il", " 1) h2 += in[1]; - if (inlen > 2) h3 = h2^in[2]; - if (j > inlen) j = inlen; - - /* Try to lookup substrings into the hash table, starting from the - * longer to the shorter substrings */ - for (; j > 0; j--) { - switch(j) { - case 1: slot = Smaz_cb[h1%241]; break; - case 2: slot = Smaz_cb[h2%241]; break; - default: slot = Smaz_cb[h3%241]; break; - } - while(slot[0]) { - if (slot[0] == j && memcmp(slot+1,in,j) == 0) { - /* Match found in the hash table, - * prepare a verbatim bytes flush if needed */ - if (verblen) { - needed = (verblen == 1) ? 2 : 2+verblen; - flush = out; - out += needed; - outlen -= needed; - } - /* Emit the byte */ - if (outlen <= 0) return _outlen+1; - out[0] = slot[slot[0]+1]; - out++; - outlen--; - inlen -= j; - in += j; - goto out; - } else { - slot += slot[0]+2; - } - } - } - /* Match not found - add the byte to the verbatim buffer */ - verb[verblen] = in[0]; - verblen++; - inlen--; - in++; -out: - /* Prepare a flush if we reached the flush length limit, and there - * is not already a pending flush operation. */ - if (!flush && (verblen == 256 || (verblen > 0 && inlen == 0))) { - needed = (verblen == 1) ? 2 : 2+verblen; - flush = out; - out += needed; - outlen -= needed; - if (outlen < 0) return _outlen+1; - } - /* Perform a verbatim flush if needed */ - if (flush) { - if (verblen == 1) { - flush[0] = (signed char)254; - flush[1] = verb[0]; - } else { - flush[0] = (signed char)255; - flush[1] = (signed char)(verblen-1); - memcpy(flush+2,verb,verblen); - } - flush = NULL; - verblen = 0; - } - } - return out-_out; -} - -void smaz_decompress(const char *in, uint32_t inlen, std::stringstream& ss ) { - const unsigned char *c = (const unsigned char*) in; -// char *_out = out; -// int _outlen = outlen; - - while(inlen) { - if (*c == 254) { - /* Verbatim byte */ - //if (outlen < 1) return _outlen+1; - //*out = *(c+1); - ss.put( *(c+1) ); - //out++; - //outlen--; - c += 2; - inlen -= 2; - } else if (*c == 255) { - /* Verbatim string */ - int len = (*(c+1))+1; - //if (outlen < len) return _outlen+1; - ss.write( (const char*)(c+2),len ); - - //memcpy(out,c+2,len); - //out += len; - //outlen -= len; - c += 2+len; - inlen -= 2+len; - } else { - /* Codebook entry */ - const char *s = Smaz_rcb[*c]; - int len = strlen(s); - - //if (outlen < len) return _outlen+1; - //memcpy(out,s,len); - ss.write( s, len ); - //out += len; - //outlen -= len; - c++; - inlen--; - } - } -} - - - - std::string smaz_compress( const std::string& in ) - { - std::string out; - out.resize(in.size()); - auto out_len = smaz_compress( in.c_str(), in.size(), &out[0], out.size() ); - FC_ASSERT( out_len <= out.size() ); - out.resize(out_len); - return out; - } - std::string smaz_decompress( const std::string& compressed ) - { - std::stringstream ss; - smaz_decompress( compressed.c_str(), compressed.length(), ss ); - return ss.str(); - } - -} // namespace fc diff --git a/src/crypto/elliptic_common.cpp b/src/crypto/elliptic_common.cpp index a395c5e..f3f9c44 100644 --- a/src/crypto/elliptic_common.cpp +++ b/src/crypto/elliptic_common.cpp @@ -140,7 +140,7 @@ namespace fc { namespace ecc { std::string public_key::to_base58( const public_key_data &key ) { uint32_t check = (uint32_t)sha256::hash(key.data, sizeof(key))._hash[0]; - assert(key.size() + sizeof(check) == 37); + static_assert(sizeof(key) + sizeof(check) == 37, "Elliptic public key size (or its hash) is incorrect"); array data; memcpy(data.data, key.begin(), key.size()); memcpy(data.begin() + key.size(), (const char*)&check, sizeof(check)); @@ -192,7 +192,7 @@ namespace fc { namespace ecc { BN_mod(secexp, secexp, order, ctx); fc::sha256 secret; - assert(BN_num_bytes(secexp) <= int64_t(sizeof(secret))); + FC_ASSERT(BN_num_bytes(secexp) <= int64_t(sizeof(secret))); auto shift = sizeof(secret) - BN_num_bytes(secexp); BN_bn2bin(secexp, ((unsigned char*)&secret)+shift); return regenerate( secret ); diff --git a/src/crypto/pke.cpp b/src/crypto/pke.cpp index 3ec0199..28cd60d 100644 --- a/src/crypto/pke.cpp +++ b/src/crypto/pke.cpp @@ -75,13 +75,13 @@ namespace fc { bool public_key::verify( const sha1& digest, const signature& sig )const { - assert( sig.size() == 2048/8 ); + static_assert( sig.size() == 2048/8, "Invalid signature size" ); return 0 != RSA_verify( NID_sha1, (const uint8_t*)&digest, 20, (uint8_t*)sig.data(), 2048/8, my->rsa ); } bool public_key::verify( const sha256& digest, const signature& sig )const { - assert( sig.size() == 2048/8 ); + static_assert( sig.size() == 2048/8, "Invalid signature size" ); return 0 != RSA_verify( NID_sha256, (const uint8_t*)&digest, 32, (uint8_t*)sig.data(), 2048/8, my->rsa ); } diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 6b5d654..d7c8d85 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -245,10 +245,16 @@ namespace fc { void remove_all( const path& p ) { boost::filesystem::remove_all(p); } void copy( const path& f, const path& t ) { try { - boost::filesystem::copy( boost::filesystem::path(f), boost::filesystem::path(t) ); + boost::system::error_code ec; + boost::filesystem::copy( boost::filesystem::path(f), boost::filesystem::path(t), ec ); + if( ec ) + { + FC_THROW( "Copy from ${srcfile} to ${dstfile} failed because ${code} : ${message}", + ("srcfile",f)("dstfile",t)("code",ec.value())("message",ec.message()) ); + } } catch ( boost::system::system_error& e ) { FC_THROW( "Copy from ${srcfile} to ${dstfile} failed because ${reason}", - ("srcfile",f)("dstfile",t)("reason",e.what() ) ); + ("srcfile",f)("dstfile",t)("reason",std::string(e.what()) ) ); } catch ( ... ) { FC_THROW( "Copy from ${srcfile} to ${dstfile} failed", ("srcfile",f)("dstfile",t)("inner", fc::except_str() ) ); @@ -262,7 +268,7 @@ namespace fc { catch ( boost::system::system_error& e ) { FC_THROW( "Resize file '${f}' to size ${s} failed: ${reason}", - ("f",f)("s",t)( "reason", e.what() ) ); + ("f",f)("s",t)( "reason", std::string(e.what()) ) ); } catch ( ... ) { @@ -301,13 +307,14 @@ namespace fc { void rename( const path& f, const path& t ) { try { boost::filesystem::rename( boost::filesystem::path(f), boost::filesystem::path(t) ); - } catch ( boost::system::system_error& ) { - try{ - boost::filesystem::copy( boost::filesystem::path(f), boost::filesystem::path(t) ); - boost::filesystem::remove( boost::filesystem::path(f)); - } catch ( boost::system::system_error& e ) { - FC_THROW( "Rename from ${srcfile} to ${dstfile} failed because ${reason}", - ("srcfile",f)("dstfile",t)("reason",e.what() ) ); + } catch ( boost::system::system_error& er ) { + try { + copy( f, t ); + remove( f ); + } catch ( fc::exception& e ) { + FC_RETHROW_EXCEPTION( e, error, + "Rename from ${srcfile} to ${dstfile} failed due to ${reason}, trying to copy then remove", + ("srcfile",f)("dstfile",t)("reason",std::string(er.what())) ); } } catch ( ... ) { FC_THROW( "Rename from ${srcfile} to ${dstfile} failed", @@ -368,7 +375,7 @@ namespace fc { } if (create) { - fc::ofstream ofs(*_path, fc::ofstream::out | fc::ofstream::binary); + fc::ofstream ofs(*_path, std::ios_base::out | std::ios_base::binary); ofs.close(); } } diff --git a/src/io/fstream.cpp b/src/io/fstream.cpp index a40bbeb..042791d 100644 --- a/src/io/fstream.cpp +++ b/src/io/fstream.cpp @@ -10,6 +10,8 @@ #include #include +using namespace std; + namespace fc { class ofstream::impl : public fc::retainable { public: @@ -23,13 +25,13 @@ namespace fc { ofstream::ofstream() :my( new impl() ){} - ofstream::ofstream( const fc::path& file, int m ) + ofstream::ofstream( const fc::path& file, std::ios_base::openmode m ) :my( new impl() ) { this->open( file, m ); } ofstream::~ofstream(){} - void ofstream::open( const fc::path& file, int m ) { + void ofstream::open( const fc::path& file, std::ios_base::openmode m ) { const boost::filesystem::path& bfp = file; - my->ofs.open( bfp, std::ios::binary ); + my->ofs.open( bfp, std::ios_base::out | std::ios_base::binary | m ); } size_t ofstream::writesome( const char* buf, size_t len ) { my->ofs.write(buf,len); diff --git a/src/io/varint.cpp b/src/io/varint.cpp index 6d5df6c..a5c6bbb 100644 --- a/src/io/varint.cpp +++ b/src/io/varint.cpp @@ -3,8 +3,6 @@ namespace fc { -void to_variant( const signed_int& var, variant& vo, uint32_t max_depth ) { vo = var.value; } -void from_variant( const variant& var, signed_int& vo, uint32_t max_depth ) { vo.value = static_cast(var.as_int64()); } void to_variant( const unsigned_int& var, variant& vo, uint32_t max_depth ) { vo = var.value; } -void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth ) { vo.value = static_cast(var.as_uint64()); } +void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth ) { vo.value = var.as_uint64(); } } diff --git a/src/log/file_appender.cpp b/src/log/file_appender.cpp index 1edc24a..65a71a3 100644 --- a/src/log/file_appender.cpp +++ b/src/log/file_appender.cpp @@ -42,7 +42,7 @@ namespace fc { - _rotation_task = async( [this]() { rotate_files( true ); }, "rotate_files(1)" ); + _rotation_task = fc::async( [this]() { rotate_files( true ); }, "rotate_files(1)" ); } } diff --git a/src/network/http/websocket.cpp b/src/network/http/websocket.cpp index 7f26b4d..0246c0b 100644 --- a/src/network/http/websocket.cpp +++ b/src/network/http/websocket.cpp @@ -226,6 +226,7 @@ namespace fc { namespace http { fc::async([current_con, request_body, con] { std::string response = current_con->on_http(request_body); + idump((response)); con->set_body( response ); con->set_status( websocketpp::http::status_code::ok ); con->send_http_response(); @@ -348,7 +349,7 @@ namespace fc { namespace http { auto con = _server.get_con_from_hdl(hdl); wdump(("server")(con->get_request_body())); auto response = current_con->on_http( con->get_request_body() ); - + idump((response)); con->set_body( response ); con->set_status( websocketpp::http::status_code::ok ); } catch ( const fc::exception& e ) diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index e521c41..0012ba6 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -177,7 +177,7 @@ namespace fc { keepalive_settings.keepaliveinterval = (ULONG)(interval.count() / fc::milliseconds(1).count()); DWORD dwBytesRet = 0; - if (WSAIoctl(my->_sock.native(), SIO_KEEPALIVE_VALS, &keepalive_settings, sizeof(keepalive_settings), + if (WSAIoctl(my->_sock.native_handle(), SIO_KEEPALIVE_VALS, &keepalive_settings, sizeof(keepalive_settings), NULL, 0, &dwBytesRet, NULL, NULL) == SOCKET_ERROR) wlog("Error setting TCP keepalive values"); #elif !defined(__clang__) || (__clang_major__ >= 6) diff --git a/src/rpc/cli.cpp b/src/rpc/cli.cpp index 0060c05..313772f 100644 --- a/src/rpc/cli.cpp +++ b/src/rpc/cli.cpp @@ -112,6 +112,11 @@ void cli::run() catch ( const fc::exception& e ) { std::cout << e.to_detail_string() << "\n"; + + if (e.code() == fc::canceled_exception_code) + { + break; + } } } } diff --git a/src/stacktrace.cpp b/src/stacktrace.cpp index 2504bb1..0e00044 100644 --- a/src/stacktrace.cpp +++ b/src/stacktrace.cpp @@ -2,6 +2,7 @@ // A stacktrace handler for bitshares // #include +#include // only include stacktrace stuff if boost >= 1.65 #if BOOST_VERSION / 100000 >= 1 && ((BOOST_VERSION / 100) % 1000) >= 65 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d2b79bd..d6f0cc8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable( all_tests all_tests.cpp io/json_tests.cpp io/stream_tests.cpp io/tcp_test.cpp + io/varint_tests.cpp network/http/websocket_test.cpp thread/task_cancel.cpp thread/thread_tests.cpp @@ -42,5 +43,6 @@ add_executable( all_tests all_tests.cpp time_test.cpp utf8_test.cpp variant_test.cpp + logging_tests.cpp ) target_link_libraries( all_tests fc ) diff --git a/tests/compress/compress.cpp b/tests/compress/compress.cpp index a30255e..bc5d3aa 100644 --- a/tests/compress/compress.cpp +++ b/tests/compress/compress.cpp @@ -2,43 +2,11 @@ #include #include -#include #include #include BOOST_AUTO_TEST_SUITE(compress) -BOOST_AUTO_TEST_CASE(smaz_test) -{ - std::ifstream testfile; - testfile.open("README.md"); - - std::stringstream buffer; - std::string line; - std::getline( testfile, line ); - while( testfile.good() ) - { - buffer << line << "\n"; - try { - std::string compressed = fc::smaz_compress( line ); - std::string decomp = fc::smaz_decompress( compressed ); - BOOST_CHECK_EQUAL( decomp, line ); - } - catch ( fc::exception& e ) - { - std::cout< + +#include +#include +#include + +BOOST_AUTO_TEST_SUITE(varint_tests) + +#define UINT_LENGTH (1 + 5*1 + 3*2 + 3*3 + 3*4 + 2*5 + 3*6 + 3*7 + 3*8 + 3*9 + 2*10) +static const std::string EXPECTED_UINTS( "\036" // 30 = length of array + "\0\1\2\020\177" + "\200\1\200\2\377\177" + "\200\200\1\200\200\2\377\377\177" + "\200\200\200\1\200\200\200\2\377\377\377\177" + "\200\200\200\200\1" + "\252\325\252\325\012" + "\200\200\200\200\200\1" + "\200\200\200\200\200\2" + "\377\377\377\377\377\177" + "\200\200\200\200\200\200\1" + "\200\200\200\200\200\200\2" + "\377\377\377\377\377\377\177" + "\200\200\200\200\200\200\200\1" + "\200\200\200\200\200\200\200\2" + "\377\377\377\377\377\377\377\177" + "\200\200\200\200\200\200\200\200\1" + "\200\200\200\200\200\200\200\200\2" + "\377\377\377\377\377\377\377\377\177" + "\200\200\200\200\200\200\200\200\200\1" + "\377\377\377\377\377\377\377\377\377\1", + UINT_LENGTH ); +static const std::vector TEST_U = { + 0, // \0 + 1, // \1 + 2, // \2 + 0x10, // \020 + 0x7f, // \177 + 0x80, // \200\1 + 0x100, // \200\2 + 0x3fff, // \377\177 + 0x4000, // \200\200\1 + 0x8000, // \200\200\2 + 0x1fffff, // \377\377\177 + 0x200000, // \200\200\200\1 + 0x400000, // \200\200\200\2 + 0xfffffff, // \377\377\377\177 + 0x10000000, // \200\200\200\200\1 + 0xaaaaaaaa, // \252\325\252\325\012 + 0x800000000ULL, // \200\200\200\200\200\1 + 0x1000000000ULL, // \200\200\200\200\200\2 + 0x3ffffffffffULL, // \377\377\377\377\377\177 + 0x40000000000ULL, // \200\200\200\200\200\200\1 + 0x80000000000ULL, // \200\200\200\200\200\200\2 + 0x1ffffffffffffULL, // \377\377\377\377\377\377\177 + 0x2000000000000ULL, // \200\200\200\200\200\200\200\1 + 0x4000000000000ULL, // \200\200\200\200\200\200\200\2 + 0xffffffffffffffULL, // \377\377\377\377\377\377\377\177 + 0x100000000000000ULL, // \200\200\200\200\200\200\200\200\1 + 0x200000000000000ULL, // \200\200\200\200\200\200\200\200\2 + 0x7fffffffffffffffULL, // \377\377\377\377\377\377\377\377\177 + 0x8000000000000000ULL, // \200\200\200\200\200\200\200\200\200\1 + 0xffffffffffffffffULL // \377\377\377\377\377\377\377\377\377\1 + }; +BOOST_AUTO_TEST_CASE( test_unsigned ) +{ try { + const std::vector packed_u = fc::raw::pack>( TEST_U, 3 ); + BOOST_CHECK_EQUAL( UINT_LENGTH, packed_u.size() ); + BOOST_CHECK_EQUAL( EXPECTED_UINTS, std::string( packed_u.data(), packed_u.size() ) ); + std::vector unpacked_u; + fc::raw::unpack>( packed_u, unpacked_u, 3 ); + BOOST_CHECK_EQUAL( TEST_U.size(), unpacked_u.size() ); + for( size_t i = 0; i < TEST_U.size(); i++ ) + BOOST_CHECK_EQUAL( TEST_U[i].value, unpacked_u[i].value ); + + const std::string json_u = fc::json::to_string(fc::variant( TEST_U, 3 )); + BOOST_CHECK_EQUAL( "[0,1,2,16,127,128,256,16383,16384,32768,2097151,2097152,4194304,268435455,268435456,2863311530," + "\"34359738368\",\"68719476736\",\"4398046511103\",\"4398046511104\"," + "\"8796093022208\",\"562949953421311\",\"562949953421312\"," + "\"1125899906842624\",\"72057594037927935\",\"72057594037927936\"," + "\"144115188075855872\",\"9223372036854775807\",\"9223372036854775808\"," + "\"18446744073709551615\"]", json_u ); + std::vector unjson_u = fc::json::from_string( json_u ).as>( 3 ); + BOOST_CHECK_EQUAL( TEST_U.size(), unjson_u.size() ); + for( size_t i = 0; i < TEST_U.size(); i++ ) + BOOST_CHECK_EQUAL( TEST_U[i].value, unjson_u[i].value ); +} FC_LOG_AND_RETHROW() } + +BOOST_AUTO_TEST_CASE( test_limits ) +{ try { + static const std::string overflow = "\200\200\200\200\200\200\200\200\200\2"; // = 2^64 + static const std::string overlong = "\200\200\200\200\200\200\200\200\300\200\1"; + + fc::unsigned_int dest; + BOOST_CHECK_THROW( fc::raw::unpack( std::vector( overflow.begin(), overflow.end() ), dest, 3 ), fc::overflow_exception ); + BOOST_CHECK_THROW( fc::raw::unpack( std::vector( overlong.begin(), overlong.end() ), dest, 3 ), fc::overflow_exception ); +} FC_LOG_AND_RETHROW() } + +BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/logging_tests.cpp b/tests/logging_tests.cpp new file mode 100644 index 0000000..659c80d --- /dev/null +++ b/tests/logging_tests.cpp @@ -0,0 +1,72 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +BOOST_AUTO_TEST_SUITE(logging_tests) + +BOOST_AUTO_TEST_CASE(log_reboot) +{ + BOOST_TEST_MESSAGE("Setting up logger"); + fc::file_appender::config conf; + conf.filename = "/tmp/my.log"; + conf.format = "${timestamp} ${thread_name} ${context} ${file}:${line} ${method} ${level}] ${message}"; + conf.flush = true; + conf.rotate = true; + conf.rotation_interval = fc::seconds(5); // rotate every 5 seconds + conf.rotation_limit = fc::seconds(20); // Don't keep files older than 20 seconds + conf.max_object_depth = 200; + + fc::appender::ptr fa = fc::appender::create("file", "file", fc::variant(conf, 200)); + + fc::path prev_log_filename = ""; + + BOOST_TEST_MESSAGE("Starting Loop"); + for(int i = 0; i < conf.rotation_limit.to_seconds(); i++) + { + fc::log_context ctx(fc::log_level::all, "my_file.cpp", i, "my_method()"); + fc::log_message my_log_message( ctx, "${message}", {"message","This is a test"} ); + fa->log(my_log_message); + + fc::time_point now = fc::time_point::now(); + int64_t interval_seconds = conf.rotation_interval.to_seconds(); + int64_t file_number = now.sec_since_epoch() / interval_seconds; + fc::time_point_sec start_time = fc::time_point_sec( (uint32_t)(file_number * interval_seconds) ); + fc::string timestamp_string = start_time.to_non_delimited_iso_string(); + fc::path link_filename = conf.filename; + fc::path log_filename = link_filename.parent_path() / (link_filename.filename().string() + "." + timestamp_string); + + if (prev_log_filename != log_filename) { + if (i > conf.rotation_interval.to_seconds()) { + std::string rez; + fc::read_file_contents(prev_log_filename, rez); + std::size_t found = rez.find("my_file.cpp:" + std::to_string(i - 1)); + BOOST_CHECK(found != std::string::npos); + + fc::read_file_contents(log_filename, rez); + found = rez.find("my_file.cpp:" + std::to_string(i)); + BOOST_CHECK(found != std::string::npos); + } + prev_log_filename = log_filename; + } + + fc::usleep(fc::seconds(1)); + } + BOOST_TEST_MESSAGE("Loop complete"); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/tests/time_test.cpp b/tests/time_test.cpp index dee272d..4c45720 100644 --- a/tests/time_test.cpp +++ b/tests/time_test.cpp @@ -1,5 +1,5 @@ #include - +#include #include using namespace fc; @@ -37,9 +37,10 @@ BOOST_AUTO_TEST_CASE(time_point_sec_test) BOOST_CHECK_EQUAL( "20380119T031408", tp2g.to_non_delimited_iso_string() ); time_point_sec tp3g(0xc0000000U); - // commented next 2 tests as they will only work with boost >= 1.64 - //BOOST_CHECK_EQUAL( "2072-01-28T16:51:12", tp3g.to_iso_string() ); - //BOOST_CHECK_EQUAL( "20720128T165112", tp3g.to_non_delimited_iso_string() ); + if (BOOST_VERSION >= BOOST_VERSION_NUMBER(1,64,0)) { + BOOST_CHECK_EQUAL( "2072-01-28T16:51:12", tp3g.to_iso_string() ); + BOOST_CHECK_EQUAL( "20720128T165112", tp3g.to_non_delimited_iso_string() ); + } BOOST_CHECK( tp0 == time_point_sec() ); BOOST_CHECK( tp0 < tp1 ); diff --git a/vendor/editline b/vendor/editline index 5be965d..405f091 160000 --- a/vendor/editline +++ b/vendor/editline @@ -1 +1 @@ -Subproject commit 5be965deec0527b48b393b7aca9cb2a76aec2a99 +Subproject commit 405f09188868eb69483c2efc55b9837c9ce04494