Skip to content

Commit

Permalink
fixed_string endian swap steemit#951
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Vandeberg committed May 2, 2017
1 parent d6297b0 commit 51eab68
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 76 deletions.
5 changes: 2 additions & 3 deletions libraries/chain/include/steemit/chain/account_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ namespace steemit { namespace chain {
ordered_unique< tag< by_id >,
member< account_object, account_id_type, &account_object::id > >,
ordered_unique< tag< by_name >,
member< account_object, account_name_type, &account_object::name >,
protocol::string_less >,
member< account_object, account_name_type, &account_object::name > >,
ordered_unique< tag< by_proxy >,
composite_key< account_object,
member< account_object, account_name_type, &account_object::proxy >,
Expand Down Expand Up @@ -374,7 +373,7 @@ namespace steemit { namespace chain {
member< vesting_delegation_object, account_name_type, &vesting_delegation_object::delegator >,
member< vesting_delegation_object, account_name_type, &vesting_delegation_object::delegatee >
>,
composite_key_compare< protocol::string_less, protocol::string_less >
composite_key_compare< std::less< account_name_type >, std::less< account_name_type > >
>
>,
allocator< vesting_delegation_object >
Expand Down
16 changes: 8 additions & 8 deletions libraries/chain/include/steemit/chain/shared_authority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ namespace steemit { namespace chain {
void clear();
void validate()const;

typedef bip::allocator< shared_authority, bip::managed_mapped_file::segment_manager > allocator_type;
typedef bip::allocator< shared_authority, bip::managed_mapped_file::segment_manager > allocator_type;

typedef bip::allocator< std::pair< account_name_type, weight_type >, bip::managed_mapped_file::segment_manager > account_pair_allocator_type;
typedef bip::allocator< std::pair< public_key_type, weight_type >, bip::managed_mapped_file::segment_manager > key_pair_allocator_type;
typedef bip::allocator< std::pair< account_name_type, weight_type >, bip::managed_mapped_file::segment_manager > account_pair_allocator_type;
typedef bip::allocator< std::pair< public_key_type, weight_type >, bip::managed_mapped_file::segment_manager > key_pair_allocator_type;

typedef bip::flat_map< account_name_type, weight_type, protocol::string_less, account_pair_allocator_type > account_authority_map;
typedef bip::flat_map< public_key_type, weight_type, std::less< public_key_type >, key_pair_allocator_type > key_authority_map;
typedef bip::flat_map< account_name_type, weight_type, std::less< account_name_type >, account_pair_allocator_type > account_authority_map;
typedef bip::flat_map< public_key_type, weight_type, std::less< public_key_type >, key_pair_allocator_type > key_authority_map;

uint32_t weight_threshold = 0;
account_authority_map account_auths;
key_authority_map key_auths;
uint32_t weight_threshold = 0;
account_authority_map account_auths;
key_authority_map key_auths;
};

bool operator == ( const shared_authority& a, const shared_authority& b );
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/steemit/chain/steem_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace steemit { namespace chain {
using steemit::protocol::price;
using steemit::protocol::asset_symbol_type;

typedef fc::fixed_string<> reward_fund_name_type;
typedef protocol::fixed_string reward_fund_name_type;

/**
* This object is used to track pending requests to convert sbd to steem
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/include/steemit/chain/witness_objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ namespace steemit { namespace chain {
member< witness_object, share_type, &witness_object::votes >,
member< witness_object, account_name_type, &witness_object::owner >
>,
composite_key_compare< std::greater< share_type >, steemit::protocol::string_less > //std::less< account_name_type > >
composite_key_compare< std::greater< share_type >, std::less< account_name_type > >
>,
ordered_unique< tag< by_schedule_time >,
composite_key< witness_object,
Expand Down
4 changes: 2 additions & 2 deletions libraries/plugins/auth_util/auth_util_api.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#include <fc/container/flat.hpp>

#include <steemit/app/api_context.hpp>
#include <steemit/app/application.hpp>

Expand All @@ -13,6 +11,8 @@
#include <steemit/plugins/auth_util/auth_util_api.hpp>
#include <steemit/plugins/auth_util/auth_util_plugin.hpp>

#include <fc/container/flat.hpp>

namespace steemit { namespace plugin { namespace auth_util {

using boost::container::flat_set;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

#pragma once

#include <steemit/protocol/types.hpp>

#include <fc/api.hpp>
#include <fc/crypto/sha256.hpp>

#include <steemit/protocol/types.hpp>

#include <string>

namespace steemit { namespace app {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using chainbase::allocator;

#define TAGS_PLUGIN_NAME "tags"

typedef fc::fixed_string< fc::sha256 > tag_name_type;
typedef protocol::fixed_string tag_name_type;

// Plugins need to define object type IDs such that they do not conflict
// globally. If each plugin uses the upper 8 bits as a space identifier,
Expand Down
1 change: 1 addition & 0 deletions libraries/protocol/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_library( steemit_protocol
transaction.cpp
block.cpp
asset.cpp
fixed_string.cpp
version.cpp
get_config.cpp

Expand Down
51 changes: 51 additions & 0 deletions libraries/protocol/fixed_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <steemit/protocol/fixed_string.hpp>

#include <boost/endian/conversion.hpp>

namespace fc {
void to_variant( const steemit::protocol::fixed_string& s, variant& v ) { v = std::string( s ); }
void from_variant( const variant& v, steemit::protocol::fixed_string& s ) { s = v.as_string(); }

uint128 endian_reverse( const uint128& u )
{
return uint128( boost::endian::endian_reverse( u.hi ), boost::endian::endian_reverse( u.lo ) );
}
} // fc

namespace steemit { namespace protocol {

fixed_string::fixed_string( const std::string& str )
{
Storage d;
if( str.size() <= sizeof(d) )
memcpy( (char*)&d, str.c_str(), str.size() );
else
memcpy( (char*)&d, str.c_str(), sizeof(d) );

data = boost::endian::big_to_native( d );
}

fixed_string::operator std::string()const
{
Storage d = boost::endian::native_to_big( data );
size_t s;

if( *(((const char*)&d) + sizeof(d) - 1) )
s = sizeof(d);
else
s = strnlen( (const char*)&d, sizeof(d) );

const char* self = (const char*)&d;

return std::string( self, self + s );
}

uint32_t fixed_string::size()const
{
Storage d = boost::endian::native_to_big( data );
if( *(((const char*)&d) + sizeof(d) - 1) )
return sizeof(d);
return strnlen( (const char*)&d, sizeof(d) );
}

} } // steemit::protocol
2 changes: 1 addition & 1 deletion libraries/protocol/include/steemit/protocol/authority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace steemit { namespace protocol {
void clear();
void validate()const;

typedef flat_map< account_name_type, weight_type, string_less > account_authority_map;
typedef flat_map< account_name_type, weight_type > account_authority_map;
typedef flat_map< public_key_type, weight_type > key_authority_map;

uint32_t weight_threshold = 0;
Expand Down
83 changes: 83 additions & 0 deletions libraries/protocol/include/steemit/protocol/fixed_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include <fc/uint128.hpp>
#include <fc/io/raw_fwd.hpp>

namespace steemit { namespace protocol {

/**
* This class is an in-place memory allocation of a 16 character string.
*
* The string will serialize the same way as std::string for variant and raw formats,
* but will be in memory as a 128-bit integer so that we can exploit efficient
* integer comparisons for sorting.
*/
class fixed_string
{
public:
typedef fc::uint128_t Storage;

fixed_string(){}
fixed_string( const fixed_string& c ) : data( c.data ){}
fixed_string( const char* str ) : fixed_string( std::string( str ) ) {}
fixed_string( const std::string& str );

operator std::string()const;

uint32_t size()const;

uint32_t length()const { return size(); }

fixed_string& operator = ( const fixed_string& str )
{
data = str.data;
return *this;
}

fixed_string& operator = ( const char* str )
{
*this = fixed_string( str );
return *this;
}

fixed_string& operator = ( const std::string& str )
{
*this = fixed_string( str );
return *this;
}

friend std::string operator + ( const fixed_string& a, const std::string& b ) { return std::string( a ) + b; }
friend std::string operator + ( const std::string& a, const fixed_string& b ){ return a + std::string( b ); }
friend bool operator < ( const fixed_string& a, const fixed_string& b ) { return a.data < b.data; }
friend bool operator <= ( const fixed_string& a, const fixed_string& b ) { return a.data <= b.data; }
friend bool operator > ( const fixed_string& a, const fixed_string& b ) { return a.data > b.data; }
friend bool operator >= ( const fixed_string& a, const fixed_string& b ) { return a.data >= b.data; }
friend bool operator == ( const fixed_string& a, const fixed_string& b ) { return a.data == b.data; }
friend bool operator != ( const fixed_string& a, const fixed_string& b ) { return a.data != b.data; }

Storage data;
};

} } // steemit::protocol

namespace fc { namespace raw {

template< typename Stream >
inline void pack( Stream& s, const steemit::protocol::fixed_string& u )
{
pack( s, std::string( u ) );
}

template< typename Stream >
inline void unpack( Stream& s, steemit::protocol::fixed_string& u )
{
std::string str;
unpack( s, str );
u = str;
}

} // raw

void to_variant( const steemit::protocol::fixed_string& s, variant& v );
void from_variant( const variant& v, steemit::protocol::fixed_string& s );
} // fc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace steemit { namespace protocol {
uint16_t weight;

// For use by std::sort such that the route is sorted first by name (ascending)
bool operator < ( const beneficiary_route_type& o )const { return string_less()( account, o.account ); }
bool operator < ( const beneficiary_route_type& o )const { return account < o.account; }
};

struct comment_payout_beneficiaries
Expand Down
51 changes: 11 additions & 40 deletions libraries/protocol/include/steemit/protocol/types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <steemit/protocol/config.hpp>
#include <fc/fixed_string.hpp>
#include <steemit/protocol/fixed_string.hpp>

#include <fc/container/flat_fwd.hpp>
#include <fc/io/varint.hpp>
Expand Down Expand Up @@ -66,45 +66,16 @@ namespace steemit {

namespace protocol {

typedef fc::ecc::private_key private_key_type;
typedef fc::sha256 chain_id_type;
typedef fc::fixed_string<> account_name_type;
// typedef std::string account_name_type;

struct string_less
{
bool operator()( const std::string& a, const std::string& b )const
{
return a < b;
}

bool operator()( const fc::fixed_string<>& a, const fc::fixed_string<>& b )const
{
const char* ap = (const char*)&a;
const char* ab = (const char*)&b;
int count = sizeof(a) - 1;
while( *ap == *ab && count > 0 ) { ++ap; ++ab; --count; }
return *ap < *ab;
}

bool operator()( const fc::fixed_string<>& a, const std::string& b )const
{
return std::string( a ) < b;
}

bool operator()( const std::string& a, const fc::fixed_string<>& b )const
{
return a < std::string( b );
}
};

typedef fc::ripemd160 block_id_type;
typedef fc::ripemd160 checksum_type;
typedef fc::ripemd160 transaction_id_type;
typedef fc::sha256 digest_type;
typedef fc::ecc::compact_signature signature_type;
typedef safe<int64_t> share_type;
typedef uint16_t weight_type;
typedef fc::ecc::private_key private_key_type;
typedef fc::sha256 chain_id_type;
typedef fixed_string account_name_type;
typedef fc::ripemd160 block_id_type;
typedef fc::ripemd160 checksum_type;
typedef fc::ripemd160 transaction_id_type;
typedef fc::sha256 digest_type;
typedef fc::ecc::compact_signature signature_type;
typedef safe<int64_t> share_type;
typedef uint16_t weight_type;


struct public_key_type
Expand Down
2 changes: 0 additions & 2 deletions libraries/protocol/steem_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ namespace steemit { namespace protocol {
FC_ASSERT( beneficiaries.size(), "Must specify at least one beneficiary" );
FC_ASSERT( beneficiaries.size() < 128, "Cannot specify more than 127 beneficiaries." ); // Require size serializtion fits in one byte.

string_less str_cmp;

validate_account_name( beneficiaries[0].account );
FC_ASSERT( beneficiaries[0].weight <= STEEMIT_100_PERCENT, "Cannot allocate more than 100% of rewards to one account" );
sum += beneficiaries[0].weight;
Expand Down
28 changes: 14 additions & 14 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#include <graphene/utilities/git_revision.hpp>
#include <graphene/utilities/key_conversion.hpp>
#include <graphene/utilities/words.hpp>

#include <steemit/app/api.hpp>
#include <steemit/protocol/base.hpp>
#include <steemit/follow/follow_operations.hpp>
#include <steemit/private_message/private_message_operations.hpp>
#include <steemit/wallet/wallet.hpp>
#include <steemit/wallet/api_documentation.hpp>
#include <steemit/wallet/reflect_util.hpp>

#include <steemit/account_by_key/account_by_key_api.hpp>

#include <algorithm>
#include <cctype>
#include <iomanip>
Expand Down Expand Up @@ -39,20 +53,6 @@
#include <fc/thread/scoped_lock.hpp>
#include <fc/smart_ref_impl.hpp>

#include <graphene/utilities/git_revision.hpp>
#include <graphene/utilities/key_conversion.hpp>
#include <graphene/utilities/words.hpp>

#include <steemit/app/api.hpp>
#include <steemit/protocol/base.hpp>
#include <steemit/follow/follow_operations.hpp>
#include <steemit/private_message/private_message_operations.hpp>
#include <steemit/wallet/wallet.hpp>
#include <steemit/wallet/api_documentation.hpp>
#include <steemit/wallet/reflect_util.hpp>

#include <steemit/account_by_key/account_by_key_api.hpp>

#ifndef WIN32
# include <sys/types.h>
# include <sys/stat.h>
Expand Down

0 comments on commit 51eab68

Please sign in to comment.