Skip to content

Commit

Permalink
Remove default pack/unpack functions for class
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Mar 14, 2018
1 parent 85ea20e commit d9ac269
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 101 deletions.
1 change: 1 addition & 0 deletions include/fc/crypto/aes.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <fc/io/raw_fwd.hpp>
#include <fc/crypto/sha512.hpp>
#include <fc/crypto/sha256.hpp>
#include <fc/uint128.hpp>
Expand Down
14 changes: 14 additions & 0 deletions include/fc/crypto/ripemd160.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ class ripemd160
uint32_t _hash[5];
};

namespace raw {

template<typename T>
inline void pack( T& ds, const ripemd160& ep, uint32_t _max_depth ) {
ds << ep;
}

template<typename T>
inline void unpack( T& ds, ripemd160& ep, uint32_t _max_depth ) {
ds >> ep;
}

}

class variant;
void to_variant( const ripemd160& bi, variant& v );
void from_variant( const variant& v, ripemd160& bi );
Expand Down
14 changes: 14 additions & 0 deletions include/fc/crypto/sha224.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ class sha224
uint32_t _hash[7];
};

namespace raw {

template<typename T>
inline void pack( T& ds, const sha224& ep, uint32_t _max_depth ) {
ds << ep;
}

template<typename T>
inline void unpack( T& ds, sha224& ep, uint32_t _max_depth ) {
ds >> ep;
}

}

class variant;
void to_variant( const sha224& bi, variant& v );
void from_variant( const variant& v, sha224& bi );
Expand Down
14 changes: 14 additions & 0 deletions include/fc/crypto/sha256.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ class sha256
uint64_t _hash[4];
};

namespace raw {

template<typename T>
inline void pack( T& ds, const sha256& ep, uint32_t _max_depth ) {
ds << ep;
}

template<typename T>
inline void unpack( T& ds, sha256& ep, uint32_t _max_depth ) {
ds >> ep;
}

}

typedef sha256 uint256;

class variant;
Expand Down
14 changes: 14 additions & 0 deletions include/fc/crypto/sha512.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ class sha512
uint64_t _hash[8];
};

namespace raw {

template<typename T>
inline void pack( T& ds, const sha512& ep, uint32_t _max_depth ) {
ds << ep;
}

template<typename T>
inline void unpack( T& ds, sha512& ep, uint32_t _max_depth ) {
ds >> ep;
}

}

typedef fc::sha512 uint512;

class variant;
Expand Down
15 changes: 3 additions & 12 deletions include/fc/io/raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,19 +351,10 @@ namespace fc {
const uint32_t max_depth;
};

// Default pack/unpack functions for classes (if_class<fc::true_type>) are removed due to recursion issue.
// Classes should implement pack/unpack functions explicitly.
template<typename IsClass=fc::true_type>
struct if_class{
template<typename Stream, typename T>
static inline void pack( Stream& s, const T& v, uint32_t _max_depth )
{
FC_ASSERT( false, "Please implement pack(...)" );
}
template<typename Stream, typename T>
static inline void unpack( Stream& s, T& v, uint32_t _max_depth )
{
FC_ASSERT( false, "Please implement unpack(...)" );
}
};
struct if_class;

template<>
struct if_class<fc::false_type> {
Expand Down
14 changes: 14 additions & 0 deletions include/fc/io/raw_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ namespace fc {
class path;
template<typename... Types> class static_variant;

class sha224;
class sha256;
class sha512;
class ripemd160;

template<typename IntType, typename EnumType> class enum_type;
namespace ip { class endpoint; }

Expand Down Expand Up @@ -96,6 +101,15 @@ namespace fc {
template<typename Stream> void unpack( Stream& s, fc::ecc::private_key&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> void pack( Stream& s, const fc::ecc::private_key&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );

template<typename Stream> inline void unpack( Stream& s, fc::sha224&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void pack( Stream& s, const fc::sha224&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void unpack( Stream& s, fc::sha256&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void pack( Stream& s, const fc::sha256&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void unpack( Stream& s, fc::sha512&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void pack( Stream& s, const fc::sha512&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void unpack( Stream& s, fc::ripemd160&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void pack( Stream& s, const fc::ripemd160&, uint32_t _max_depth=FC_PACK_MAX_DEPTH );

template<typename Stream, typename T> inline void pack( Stream& s, const T& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream, typename T> inline void unpack( Stream& s, T& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );

Expand Down
89 changes: 0 additions & 89 deletions tests/serialization_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,60 +35,9 @@ namespace fc { namespace test {
inline bool operator < ( const item& a, const item& b )
{ return ( std::tie( a.level, a.w ) < std::tie( b.level, b.w ) ); }

class class_item;

class class_item_wrapper
{
public:
class_item_wrapper() {}
class_item_wrapper(class_item&& it) { v.reserve(1); v.emplace_back( it ); }
std::vector<class class_item> v;
};
inline bool operator == ( const class_item_wrapper& a, const class_item_wrapper& b )
{ return ( std::tie( a.v ) == std::tie( b.v ) ); }

class class_item
{
public:
class_item(int32_t lvl = 0) : level(lvl) {}
class_item(class_item_wrapper&& wp, int32_t lvl = 0) : level(lvl), w(wp) {}
int32_t level;
class_item_wrapper w;
};
inline bool operator == ( const class_item& a, const class_item& b )
{ return ( std::tie( a.level, a.w ) == std::tie( b.level, b.w ) ); }

template<typename Stream>
void operator >> ( Stream& s, class_item_wrapper& w )
{
fc::raw::unpack( s, w.v );
}
template<typename Stream>
void operator << ( Stream& s, const class_item_wrapper& w )
{
fc::raw::pack( s, w.v );
}

template<typename Stream>
void operator >> ( Stream& s, class_item& it )
{
fc::raw::unpack( s, it.level );
fc::raw::unpack( s, it.w );
}
template<typename Stream>
void operator << ( Stream& s, const class_item& it )
{
fc::raw::pack( s, it.level );
fc::raw::pack( s, it.w );
}

} } // namespace fc::test

namespace fc {
template<> struct get_typename<fc::test::class_item> { static const char* name() { return "class_item"; } };
template<> struct get_typename<fc::test::class_item_wrapper> { static const char* name() { return "class_item_wrapper"; } };
}

FC_REFLECT( fc::test::item_wrapper, (v) );
FC_REFLECT( fc::test::item, (level)(w) );

Expand All @@ -111,20 +60,6 @@ BOOST_AUTO_TEST_CASE( nested_objects_test )
return nested;
};

auto create_nested_class_object = []( uint32_t level )
{
ilog( "Creating nested class object with ${lv} level(s)", ("lv",level) );
fc::test::class_item nested;
for( uint32_t i = 1; i <= level; i++ )
{
if( i % 100 == 0 )
ilog( "Creating level ${lv}", ("lv",i) );
fc::test::class_item_wrapper wp( std::move(nested) );
nested = fc::test::class_item( std::move(wp), i );
}
return nested;
};

// 100 levels, should be allowed
{
auto nested = create_nested_object( 100 );
Expand Down Expand Up @@ -165,30 +100,6 @@ BOOST_AUTO_TEST_CASE( nested_objects_test )
BOOST_CHECK_THROW( fc::raw::unpack( ss, unpacked ), fc::assert_exception );
}

// 150 levels, by default packing will fail
{
auto nested = create_nested_class_object( 150 );

std::stringstream ss;

BOOST_TEST_MESSAGE( "About to pack." );
BOOST_CHECK_THROW( fc::raw::pack( ss, nested ), fc::assert_exception );
}

// 150 levels and allow packing, unpacking will fail
{
auto nested = create_nested_object( 150 );

std::stringstream ss;

BOOST_TEST_MESSAGE( "About to pack." );
fc::raw::pack( ss, nested, 1500 );

BOOST_TEST_MESSAGE( "About to unpack as class object." );
fc::test::class_item unpacked;
BOOST_CHECK_THROW( fc::raw::unpack( ss, unpacked ), fc::assert_exception );
}

} FC_CAPTURE_LOG_AND_RETHROW ( (0) ) }

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit d9ac269

Please sign in to comment.