diff --git a/.gitmodules b/.gitmodules index 448bb563e622a15820cc40e1dbe9f69e8fa73151..57f8625795546b7e6eab08edd800a36ecf18ba3c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ [submodule "libraries/fc"] path = libraries/fc - url = https://github.com/bitshares/bitshares-fc.git + url = https://github.com/eosio/fc.git ignore = dirty [submodule "libraries/chainbase"] path = libraries/chainbase diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index ae8708f430cfc681b91534520549500aad94ff78..d29fba1e2e6697940d9fd81e3ec9aae9c7e3dce3 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -1,7 +1,7 @@ add_subdirectory( fc ) add_subdirectory( chainbase ) add_subdirectory( wren ) -#add_subdirectory( types ) +add_subdirectory( types ) add_subdirectory( deterministic_openssl_rand ) add_subdirectory( chain ) add_subdirectory( egenesis ) diff --git a/libraries/chain/CMakeLists.txt b/libraries/chain/CMakeLists.txt index 76d7a5534c1c066c71a766c1977459d583fc6d3c..6b063f0a9a1d90a778fc1040d443c0f766bac63c 100644 --- a/libraries/chain/CMakeLists.txt +++ b/libraries/chain/CMakeLists.txt @@ -5,7 +5,6 @@ add_library( eos_chain database.cpp fork_database.cpp - types.cpp transaction.cpp block.cpp @@ -18,7 +17,7 @@ add_library( eos_chain ${PROTOCOL_HEADERS} ) -target_link_libraries( eos_chain fc chainbase ) +target_link_libraries( eos_chain fc chainbase eos_types ) target_include_directories( eos_chain PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "${CMAKE_CURRENT_BINARY_DIR}/include" ) diff --git a/libraries/chain/database.cpp b/libraries/chain/database.cpp index 107fa621a332adfabb60de8a63851ed22dfb3747..bff5801cf8f76cdad9da0f643577cb724eab43f3 100644 --- a/libraries/chain/database.cpp +++ b/libraries/chain/database.cpp @@ -513,9 +513,11 @@ void database::validate_referenced_accounts(const signed_transaction& trx)const const account_name* previous_notify_account = nullptr; for(const auto& current_notify_account : msg.notify) { get_account(current_notify_account); - if(previous_notify_account) + if(previous_notify_account) { EOS_ASSERT(current_notify_account < *previous_notify_account, message_validate_exception, "Message notify accounts out of order. Possibly a bug in the wallet?"); + } + EOS_ASSERT(current_notify_account != msg.sender, message_validate_exception, "Message sender is listed in accounts to notify. Possibly a bug in the wallet?"); EOS_ASSERT(current_notify_account != msg.recipient, message_validate_exception, @@ -745,7 +747,7 @@ void database::init_genesis(const genesis_state_type& genesis_state) create([&acct](account_object& a) { a.name = acct.name.c_str(); a.balance = acct.balance; - idump((acct.name)(a.balance)); +// idump((acct.name)(a.balance)); // a.active_key = acct.active_key; // a.owner_key = acct.owner_key; }); diff --git a/libraries/chain/include/eos/chain/account_object.hpp b/libraries/chain/include/eos/chain/account_object.hpp index b663e2d059ba4859131a7780c3dd60743f8b3b79..412b44999aa0d0d13ae8bc3d2504e3315258ddca 100644 --- a/libraries/chain/include/eos/chain/account_object.hpp +++ b/libraries/chain/include/eos/chain/account_object.hpp @@ -51,9 +51,9 @@ namespace eos { namespace chain { return *this; } - uint32_t threshold = 0; - shared_vector accounts; - shared_vector keys; + UInt32 threshold = 0; + shared_vector accounts; + shared_vector keys; }; class account_object : public chainbase::object @@ -61,11 +61,11 @@ namespace eos { namespace chain { OBJECT_CTOR(account_object) id_type id; - account_name name; - uint64_t balance = 0; - uint64_t votes = 0; - uint64_t converting_votes = 0; - time_point_sec last_vote_conversion; + AccountName name; + Asset balance; + UInt64 votes = 0; + UInt64 converting_votes = 0; + Time last_vote_conversion; }; struct by_name; @@ -73,7 +73,7 @@ namespace eos { namespace chain { account_object, indexed_by< ordered_unique, member>, - ordered_unique, member> + ordered_unique, member> > >; diff --git a/libraries/chain/include/eos/chain/authority.hpp b/libraries/chain/include/eos/chain/authority.hpp index f5defd0239ec2b646f223671690015c5e8692495..0a6ac6799f0e52e20038ec0d660645fd7bb28480 100644 --- a/libraries/chain/include/eos/chain/authority.hpp +++ b/libraries/chain/include/eos/chain/authority.hpp @@ -1,45 +1,27 @@ #pragma once #include - - -namespace eos { namespace chain { - - struct PermissionLevel { - account_name account; - permission_name level; - uint16_t weight; - }; - - struct KeyPermission { - public_key_type key; - uint16_t weight; - }; - - struct Authority { - uint32_t threshold = 0; - vector accounts; - vector keys; - - bool validate() const { - if (threshold == 0) - return false; - uint32_t score = 0; - for (const auto& p : accounts) - score += p.weight; - for (const auto& p : keys) - score += p.weight; - return score >= threshold; +#include + +namespace eos { + inline bool operator < ( const AccountPermission& a, const AccountPermission& b ) { + return std::tie( a.account, a.permission ) < std::tie( b.account, b.permission ); + } + + /** + * Makes sure all keys are unique and sorted and all account permissions are unique and sorted + */ + inline bool validate( eos::Authority& auth ) { + const KeyPermissionWeight* prev = nullptr; + for( const auto& k : auth.keys ) { + if( !prev ) prev = &k; + else if( prev->key < k.key ) return false; } - set referenced_accounts() const { - set results; - std::transform(accounts.begin(), accounts.end(), std::inserter(results, results.begin()), - [](const PermissionLevel& p) { return p.account; }); - return results; + const AccountPermissionWeight* pa = nullptr; + for( const auto& a : auth.accounts ) { + if( !pa ) pa = &a; + else if( pa->permission < a.permission ) return false; } - }; - -} } // eos::chain + return true; + } +} -FC_REFLECT(eos::chain::PermissionLevel, (account)(level)(weight)) -FC_REFLECT(eos::chain::KeyPermission, (key)(weight)) -FC_REFLECT(eos::chain::Authority, (threshold)(accounts)(keys)) diff --git a/libraries/chain/include/eos/chain/genesis_state.hpp b/libraries/chain/include/eos/chain/genesis_state.hpp index 324e57eb85b5b7c2c4a55e0358581e6cb8f75cf6..7bb37295c6225fe8917b8a49eacde80b159a24b4 100644 --- a/libraries/chain/include/eos/chain/genesis_state.hpp +++ b/libraries/chain/include/eos/chain/genesis_state.hpp @@ -47,7 +47,7 @@ struct genesis_state_type { active_key(active_key == public_key_type()? owner_key : active_key) {} string name; - uint64_t balance = 0; + Asset balance; public_key_type owner_key; public_key_type active_key; }; diff --git a/libraries/chain/include/eos/chain/transaction.hpp b/libraries/chain/include/eos/chain/transaction.hpp index aee1f5fea21515a7e83bb76eb27f185244d39a40..f5ee106a801912197f6524980a5324c53b529e88 100644 --- a/libraries/chain/include/eos/chain/transaction.hpp +++ b/libraries/chain/include/eos/chain/transaction.hpp @@ -94,7 +94,6 @@ namespace eos { namespace chain { /// Calculate the digest used for signature validation digest_type sig_digest(const chain_id_type& chain_id)const; - void set_expiration(fc::time_point_sec expiration_time); void set_reference_block(const block_id_type& reference_block); bool verify_reference_block(const block_id_type& reference_block)const; }; diff --git a/libraries/chain/include/eos/chain/types.hpp b/libraries/chain/include/eos/chain/types.hpp index 5b183e075908d6328c5fe1cf927755132424d676..c618a3240e50969fa17c722871ec33c29b6efef5 100644 --- a/libraries/chain/include/eos/chain/types.hpp +++ b/libraries/chain/include/eos/chain/types.hpp @@ -26,9 +26,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -39,6 +36,10 @@ #include #include #include +#include +#include +#include +#include #include #include @@ -105,12 +106,14 @@ namespace eos { namespace chain { using private_key_type = fc::ecc::private_key; using chain_id_type = fc::sha256; - typedef fc::fixed_string<> account_name; - typedef fc::fixed_string<> permission_name; - typedef fc::fixed_string<> message_type; - //using account_name = std::string; - //using message_type = std::string; - //using permission_name = std::string; + + using eos::AccountName; + using account_name = eos::AccountName; + using eos::PermissionName; + using permission_name = eos::PermissionName; + using message_name = eos::TypeName; + using message_type = message_name; + /** * List all object types from all namespaces here so they can @@ -150,40 +153,11 @@ namespace eos { namespace chain { using signature_type = fc::ecc::compact_signature; using weight_type = uint16_t; - struct public_key_type - { - struct binary_key - { - binary_key() {} - uint32_t check = 0; - fc::ecc::public_key_data data; - }; - fc::ecc::public_key_data key_data; - public_key_type(); - public_key_type( const fc::ecc::public_key_data& data ); - public_key_type( const fc::ecc::public_key& pubkey ); - explicit public_key_type( const std::string& base58str ); - operator fc::ecc::public_key_data() const; - operator fc::ecc::public_key() const; - explicit operator std::string() const; - friend bool operator == ( const public_key_type& p1, const fc::ecc::public_key& p2); - friend bool operator == ( const public_key_type& p1, const public_key_type& p2); - friend bool operator != ( const public_key_type& p1, const public_key_type& p2); - friend bool operator < ( const public_key_type& p1, const public_key_type& p2); - bool is_valid_v1( const std::string& base58str ); - }; - + using public_key_type = eos::PublicKey; } } // eos::chain -namespace fc -{ - void to_variant( const eos::chain::public_key_type& var, fc::variant& vo ); - void from_variant( const fc::variant& var, eos::chain::public_key_type& vo ); -} -FC_REFLECT( eos::chain::public_key_type, (key_data) ) -FC_REFLECT( eos::chain::public_key_type::binary_key, (data)(check) ) FC_REFLECT(eos::chain::account_id_type, (_id)) FC_REFLECT(eos::chain::producer_id_type, (_id)) diff --git a/libraries/chain/transaction.cpp b/libraries/chain/transaction.cpp index 441de72129b7a071a6f6492b135eb6fde1a4007d..9139536b4f6b9d1011c95cd3da23c212e0759bfc 100644 --- a/libraries/chain/transaction.cpp +++ b/libraries/chain/transaction.cpp @@ -62,10 +62,6 @@ signature_type eos::chain::signed_transaction::sign(const private_key_type& key, return key.sign_compact(enc.result()); } -void transaction::set_expiration( fc::time_point_sec expiration_time ) { - expiration = expiration_time; -} - void transaction::set_reference_block(const block_id_type& reference_block) { ref_block_num = fc::endian_reverse_u32(reference_block._hash[0]); ref_block_prefix = reference_block._hash[1]; diff --git a/libraries/chain/types.cpp b/libraries/chain/types.cpp deleted file mode 100644 index bea7650f3094e570d91965fd70c407e3e9f3dfed..0000000000000000000000000000000000000000 --- a/libraries/chain/types.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (c) 2017, Respective Authors. - * - * The MIT License - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include -#include - -#include -#include -#include -#include - -namespace eos { namespace chain { - - public_key_type::public_key_type():key_data(){}; - - public_key_type::public_key_type(const fc::ecc::public_key_data& data) - :key_data( data ) {}; - - public_key_type::public_key_type(const fc::ecc::public_key& pubkey) - :key_data( pubkey ) {}; - - public_key_type::public_key_type(const std::string& base58str) - { - // TODO: Refactor syntactic checks into static is_valid() - // to make public_key_type API more similar to address API - std::string prefix( config::KeyPrefix ); - - const size_t prefix_len = prefix.size(); - FC_ASSERT(base58str.size() > prefix_len); - FC_ASSERT(base58str.substr(0, prefix_len) == prefix , "", ("base58str", base58str)); - auto bin = fc::from_base58(base58str.substr(prefix_len)); - auto bin_key = fc::raw::unpack(bin); - key_data = bin_key.data; - FC_ASSERT(fc::ripemd160::hash(key_data.data, key_data.size())._hash[0] == bin_key.check); - } - - public_key_type::operator fc::ecc::public_key_data() const - { - return key_data; - }; - - public_key_type::operator fc::ecc::public_key() const - { - return fc::ecc::public_key(key_data); - }; - - public_key_type::operator std::string() const - { - binary_key k; - k.data = key_data; - k.check = fc::ripemd160::hash( k.data.data, k.data.size() )._hash[0]; - auto data = fc::raw::pack( k ); - return config::KeyPrefix + fc::to_base58( data.data(), data.size() ); - } - - bool operator == (const public_key_type& p1, const fc::ecc::public_key& p2) - { - return p1.key_data == p2.serialize(); - } - - bool operator == (const public_key_type& p1, const public_key_type& p2) - { - return p1.key_data == p2.key_data; - } - - bool operator != (const public_key_type& p1, const public_key_type& p2) - { - return p1.key_data != p2.key_data; - } - - bool operator <(const public_key_type& p1, const public_key_type& p2) - { - return p1.key_data < p2.key_data; - }; - -} } // eos::chain - -namespace fc -{ - using namespace std; - void to_variant(const eos::chain::public_key_type& var, fc::variant& vo) - { - vo = std::string(var); - } - - void from_variant(const fc::variant& var, eos::chain::public_key_type& vo) - { - vo = eos::chain::public_key_type(var.as_string()); - } - -} // fc diff --git a/libraries/fc b/libraries/fc index 1f41494f360a1800fc967d647631cf64fdc549ee..cadc521fe41282487e047cc3030b2eb8231202c2 160000 --- a/libraries/fc +++ b/libraries/fc @@ -1 +1 @@ -Subproject commit 1f41494f360a1800fc967d647631cf64fdc549ee +Subproject commit cadc521fe41282487e047cc3030b2eb8231202c2 diff --git a/libraries/net/CMakeLists.txt b/libraries/net/CMakeLists.txt index 607eed85fc7c61975e4782812d7315d7707daa1c..30091e004e0d6f2f44a94942862a4c9c87333bef 100644 --- a/libraries/net/CMakeLists.txt +++ b/libraries/net/CMakeLists.txt @@ -10,7 +10,8 @@ set(SOURCES node.cpp add_library( eos_net ${SOURCES} ${HEADERS} ) target_link_libraries( eos_net - PUBLIC fc chainbase appbase ) + PUBLIC fc chainbase appbase eos_types) + target_include_directories( eos_net PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" PRIVATE "${CMAKE_SOURCE_DIR}/libraries/chain/include" diff --git a/libraries/types/Asset.cpp b/libraries/types/Asset.cpp index becc9244141c53abb6de0a87d2b23e8565cae35a..5216f9be941e905aac570a10dcca950805a9ab14 100644 --- a/libraries/types/Asset.cpp +++ b/libraries/types/Asset.cpp @@ -1,8 +1,9 @@ #include #include #include +#include -namespace EOS { +namespace eos { typedef boost::multiprecision::int128_t int128_t; uint8_t Asset::decimals()const { @@ -14,7 +15,7 @@ namespace EOS { a[0] = d; } - std::string Asset::symbol_name()const { + String Asset::symbol_name()const { auto a = (const char*)&symbol; assert( a[7] == 0 ); return &a[1]; @@ -32,21 +33,21 @@ namespace EOS { return table[ decimals() ]; } - string Asset::toString()const { - string result = fc::to_string(amount.value / precision()); + String Asset::toString()const { + String result = fc::to_string( static_cast(amount) / precision()); if( decimals() ) { - auto fract = amount.value % precision(); + auto fract = static_cast(amount) % precision(); result += "." + fc::to_string(precision() + fract).erase(0,1); } return result + " " + symbol_name(); } - Asset Asset::fromString( const string& from ) + Asset Asset::fromString( const String& from ) { try { - string s = fc::trim( from ); + String s = fc::trim( from ); auto space_pos = s.find( " " ); auto dot_pos = s.find( "." ); @@ -57,15 +58,15 @@ namespace EOS { auto intpart = s.substr( 0, dot_pos ); result.amount = fc::to_int64(intpart); - std::string fractpart; - if( dot_pos != std::string::npos ) + String fractpart; + if( dot_pos != String::npos ) { auto fractpart = "1" + s.substr( dot_pos + 1, space_pos - dot_pos - 1 ); result.set_decimals( fractpart.size() - 1 ); - result.amount.value *= result.precision(); - result.amount.value += fc::to_int64(fractpart); - result.amount.value -= result.precision(); + result.amount *= Int64(result.precision()); + result.amount += Int64(fc::to_int64(fractpart)); + result.amount -= Int64(result.precision()); } auto symbol = s.substr( space_pos + 1 ); @@ -82,8 +83,8 @@ namespace EOS { if( std::tie( a.base.symbol, a.quote.symbol ) != std::tie( b.base.symbol, b.quote.symbol ) ) return false; - const auto amult = uint128_t( b.quote.amount.value ) * a.base.amount.value; - const auto bmult = uint128_t( a.quote.amount.value ) * b.base.amount.value; + const auto amult = UInt128( b.quote.amount ) * UInt128(a.base.amount); + const auto bmult = UInt128( a.quote.amount ) * UInt128(b.base.amount); return amult == bmult; } @@ -95,8 +96,8 @@ namespace EOS { if( a.quote.symbol < b.quote.symbol ) return true; if( a.quote.symbol > b.quote.symbol ) return false; - const auto amult = uint128_t( b.quote.amount.value ) * a.base.amount.value; - const auto bmult = uint128_t( a.quote.amount.value ) * b.base.amount.value; + const auto amult = UInt128( b.quote.amount ) * UInt128(a.base.amount); + const auto bmult = UInt128( a.quote.amount ) * UInt128(b.base.amount); return amult < bmult; } @@ -125,17 +126,16 @@ namespace EOS { { if( a.symbol_name() == b.base.symbol_name() ) { - FC_ASSERT( b.base.amount.value > 0 ); - uint128_t result = (uint128_t(a.amount.value) * b.quote.amount.value)/b.base.amount.value; - FC_ASSERT( result.hi == 0 ); - return Asset( result.to_uint64(), b.quote.symbol ); + FC_ASSERT( static_cast(b.base.amount) > 0 ); + auto result = (UInt128(a.amount) * UInt128(b.quote.amount))/UInt128(b.base.amount); + return Asset( Int64(result), b.quote.symbol ); } + else if( a.symbol_name() == b.quote.symbol_name() ) { - FC_ASSERT( b.quote.amount.value > 0 ); - uint128_t result = (uint128_t(a.amount.value) * b.base.amount.value)/b.quote.amount.value; - FC_ASSERT( result.hi == 0 ); - return Asset( result.to_uint64(), b.base.symbol ); + FC_ASSERT( static_cast(b.quote.amount) > 0 ); + auto result = (UInt128(a.amount) *UInt128(b.base.amount))/UInt128(b.quote.amount); + return Asset( Int64(result), b.base.symbol ); } FC_THROW_EXCEPTION( fc::assert_exception, "invalid Asset * Price", ("Asset",a)("Price",b) ); } @@ -146,8 +146,8 @@ namespace EOS { return Price{ base, quote }; } FC_CAPTURE_AND_RETHROW( (base)(quote) ) } - Price Price::max( SymbolType base, SymbolType quote ) { return Asset( ShareType(EOS_MAX_SHARE_SUPPLY), base ) / Asset( ShareType(1), quote); } - Price Price::min( SymbolType base, SymbolType quote ) { return Asset( 1, base ) / Asset( EOS_MAX_SHARE_SUPPLY, quote); } + Price Price::max( AssetSymbol base, AssetSymbol quote ) { return Asset( ShareType(EOS_MAX_SHARE_SUPPLY), base ) / Asset( ShareType(1), quote); } + Price Price::min( AssetSymbol base, AssetSymbol quote ) { return Asset( 1, base ) / Asset( EOS_MAX_SHARE_SUPPLY, quote); } bool Price::is_null() const { return *this == Price(); } @@ -159,4 +159,4 @@ namespace EOS { } FC_CAPTURE_AND_RETHROW( (base)(quote) ) } -} // EOS +} // eos diff --git a/libraries/types/CMakeLists.txt b/libraries/types/CMakeLists.txt index 038786593bec05e2ff6e708aa85ba6613913b61d..d1c969d5b80ba754d0ba97b6e8f9f0022c63283d 100644 --- a/libraries/types/CMakeLists.txt +++ b/libraries/types/CMakeLists.txt @@ -1,6 +1,8 @@ file(GLOB HEADERS "include/eos/types/*.hpp") add_library( eos_types + Asset.cpp + PublicKey.cpp TypeParser.cpp native.cpp ) @@ -10,13 +12,10 @@ target_link_libraries( eos_types fc ) add_executable( type_generator type_generator.cpp ) target_link_libraries( type_generator fc eos_types ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ) -add_custom_command( OUTPUT generated.cpp include/eos/types/generated.hpp - COMMAND ${CMAKE_CURRENT_BINARY_DIR}/type_generator ${CMAKE_CURRENT_SOURCE_DIR}/types.eos generated.cpp generated.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/eos/types/generated.hpp +add_custom_command( OUTPUT include/eos/types/generated.hpp + COMMAND ${CMAKE_CURRENT_BINARY_DIR}/type_generator ${CMAKE_CURRENT_SOURCE_DIR}/types.eos ${CMAKE_CURRENT_SOURCE_DIR}/include/eos/types/generated.hpp DEPENDS types.eos type_generator ) -add_library( eos_generated_types generated.cpp ) -target_link_libraries( eos_generated_types eos_types ) - -add_executable( types_test test.cpp ) -target_link_libraries( types_test fc eos_generated_types ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ) +add_executable( types_test test.cpp include/eos/types/generated.hpp ) +target_link_libraries( types_test eos_types fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} ) diff --git a/libraries/types/PublicKey.cpp b/libraries/types/PublicKey.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cd65a038b506f9412ca18391b7791f085d448d1e --- /dev/null +++ b/libraries/types/PublicKey.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +#define KEY_PREFIX "EOS" + +namespace eos { + + PublicKey::PublicKey():key_data(){}; + + PublicKey::PublicKey(const fc::ecc::public_key_data& data) + :key_data( data ) {}; + + PublicKey::PublicKey(const fc::ecc::public_key& pubkey) + :key_data( pubkey ) {}; + + PublicKey::PublicKey(const std::string& base58str) + { + // TODO: Refactor syntactic checks into static is_valid() + // to make PublicKey API more similar to address API + std::string prefix( KEY_PREFIX ); + + const size_t prefix_len = prefix.size(); + FC_ASSERT(base58str.size() > prefix_len); + FC_ASSERT(base58str.substr(0, prefix_len) == prefix , "", ("base58str", base58str)); + auto bin = fc::from_base58(base58str.substr(prefix_len)); + auto bin_key = fc::raw::unpack(bin); + key_data = bin_key.data; + FC_ASSERT(fc::ripemd160::hash(key_data.data, key_data.size())._hash[0] == bin_key.check); + } + + PublicKey::operator fc::ecc::public_key_data() const + { + return key_data; + }; + + PublicKey::operator fc::ecc::public_key() const + { + return fc::ecc::public_key(key_data); + }; + + PublicKey::operator std::string() const + { + BinaryKey k; + k.data = key_data; + k.check = fc::ripemd160::hash( k.data.data, k.data.size() )._hash[0]; + auto data = fc::raw::pack( k ); + return KEY_PREFIX + fc::to_base58( data.data(), data.size() ); + } + + bool operator == (const PublicKey& p1, const fc::ecc::public_key& p2) + { + return p1.key_data == p2.serialize(); + } + + bool operator == (const PublicKey& p1, const PublicKey& p2) + { + return p1.key_data == p2.key_data; + } + + bool operator != (const PublicKey& p1, const PublicKey& p2) + { + return p1.key_data != p2.key_data; + } + + bool operator <(const PublicKey& p1, const PublicKey& p2) + { + return p1.key_data < p2.key_data; + }; + +} // eos + +namespace fc +{ + using namespace std; + void to_variant(const eos::PublicKey& var, fc::variant& vo) + { + vo = std::string(var); + } + + void from_variant(const fc::variant& var, eos::PublicKey& vo) + { + vo = eos::PublicKey(var.as_string()); + } + +} // fc diff --git a/libraries/types/TypeParser.cpp b/libraries/types/TypeParser.cpp index 8c4ec16dbf2d69251faa7ee29ef5f187905fbd3e..b2f20ba9ab3b6e2027ca2cd43fd4ad3aa0701f3e 100644 --- a/libraries/types/TypeParser.cpp +++ b/libraries/types/TypeParser.cpp @@ -2,7 +2,7 @@ #include #include -namespace EOS { +namespace eos { void AbstractSymbolTable::parse( std::istream& in ) { vector line_tokens; @@ -13,7 +13,7 @@ namespace EOS { FC_ASSERT( current.fields.size() > 0, "A struct must specify at least one field" ); this->addType( current ); current.fields.clear(); - current.base.clear(); + current.base = TypeName(); in_struct = false; }; @@ -57,4 +57,4 @@ namespace EOS { } } -} // namespace EOS +} // namespace eos diff --git a/libraries/types/include/eos/types/Asset.hpp b/libraries/types/include/eos/types/Asset.hpp index 80dfb31f20686e9f2ab3ff8309c4a293cfb07ed7..8333561c0f2d12c5e08bec35e0e20b1a2e74bc11 100644 --- a/libraries/types/include/eos/types/Asset.hpp +++ b/libraries/types/include/eos/types/Asset.hpp @@ -1,23 +1,17 @@ #pragma once #include -#include -#include +#include -/// EOS with 8 digits of precision +/// eos with 8 digits of precision #define EOS_SYMBOL (uint64_t(8) | (uint64_t('E') << 8) | (uint64_t('O') << 16) | (uint64_t('S') << 24) ) -/// VOTE with 8 digits of precision -#define VOTE_SYMBOL (uint64_t(8) | (uint64_t('V') << 8) | (uint64_t('O') << 16) | (uint64_t('T') << 24) | (uint64_t('E') << 32) ) - /// Defined to be largest power of 10 that fits in 53 bits of precision #define EOS_MAX_SHARE_SUPPLY int64_t(1000000000000000ll) -namespace EOS { - - typedef uint64_t AssetSymbol; - typedef Int64 ShareType; - using std::string; +namespace eos { + using AssetSymbol = uint64_t; + using ShareType = Int64; struct Asset { @@ -27,17 +21,15 @@ namespace EOS { ShareType amount; AssetSymbol symbol; - double to_real()const { - return static_cast(amount) / precision(); - } + double to_real()const { return static_cast(amount) / precision(); } uint8_t decimals()const; - std::string symbol_name()const; + String symbol_name()const; int64_t precision()const; void set_decimals(uint8_t d); - static Asset fromString( const string& from ); - string toString()const; + static Asset fromString( const String& from ); + String toString()const; Asset& operator += ( const Asset& o ) { @@ -63,45 +55,33 @@ namespace EOS { FC_ASSERT( a.symbol == b.symbol ); return std::tie(a.amount,a.symbol) < std::tie(b.amount,b.symbol); } - friend bool operator <= ( const Asset& a, const Asset& b ) - { - return (a == b) || (a < b); - } + friend bool operator <= ( const Asset& a, const Asset& b ) { return (a == b) || (a < b); } + friend bool operator != ( const Asset& a, const Asset& b ) { return !(a == b); } + friend bool operator > ( const Asset& a, const Asset& b ) { return !(a <= b); } + friend bool operator >= ( const Asset& a, const Asset& b ) { return !(a < b); } - friend bool operator != ( const Asset& a, const Asset& b ) - { - return !(a == b); - } - friend bool operator > ( const Asset& a, const Asset& b ) - { - return !(a <= b); - } - friend bool operator >= ( const Asset& a, const Asset& b ) - { - return !(a < b); - } - - friend Asset operator - ( const Asset& a, const Asset& b ) - { + friend Asset operator - ( const Asset& a, const Asset& b ) { FC_ASSERT( a.symbol == b.symbol ); return Asset( a.amount - b.amount, a.symbol ); } - friend Asset operator + ( const Asset& a, const Asset& b ) - { + + friend Asset operator + ( const Asset& a, const Asset& b ) { FC_ASSERT( a.symbol == b.symbol ); return Asset( a.amount + b.amount, a.symbol ); } + friend std::ostream& operator << ( std::ostream& out, const Asset& a ) { return out << a.toString(); } + }; struct Price { - Price(const Asset& base = Asset(), const Asset quote = Asset()) - : base(base),quote(quote){} - Asset base; Asset quote; + Price(const Asset& base = Asset(), const Asset quote = Asset()) + :base(base),quote(quote){} + static Price max(AssetSymbol base, AssetSymbol quote ); static Price min(AssetSymbol base, AssetSymbol quote ); @@ -127,40 +107,13 @@ namespace EOS { bool operator != ( const Price& a, const Price& b ); Asset operator * ( const Asset& a, const Price& b ); - inline fc::variant toVariant( const Asset& v ) { return v.toString(); } - inline void fromVariant( Asset& v, const fc::variant& var ) { v = Asset::fromString( var.get_string() ); } - - template - void toBinary( Stream& stream, const Asset& t ) { - EOS::toBinary( stream, t.amount ); - EOS::toBinary( stream, t.symbol ); - } - - template - void fromBinary( Stream& stream, Asset& t ) { - EOS::fromBinary( stream, t.amount ); - EOS::fromBinary( stream, t.symbol ); - } - - template - void toBinary( Stream& stream, const Price& t ) { - EOS::toBinary( stream, t.base ); - EOS::toBinary( stream, t.quote ); - } - - template - void fromBinary( Stream& stream, Price& t ) { - EOS::fromBinary( stream, t.base ); - EOS::fromBinary( stream, t.quote ); - } - -} // namespace EOS +} // namespace eos namespace fc { - inline void to_variant( const EOS::Asset& var, fc::variant& vo ) { vo = var.toString(); } - inline void from_variant( const fc::variant& var, EOS::Asset& vo ) { vo = EOS::Asset::fromString( var.get_string() ); } + inline void to_variant( const eos::Asset& var, fc::variant& vo ) { vo = var.toString(); } + inline void from_variant( const fc::variant& var, eos::Asset& vo ) { vo = eos::Asset::fromString( var.get_string() ); } } -FC_REFLECT( EOS::Asset, (amount)(symbol) ) -FC_REFLECT( EOS::Price, (base)(quote) ) +FC_REFLECT( eos::Asset, (amount)(symbol) ) +FC_REFLECT( eos::Price, (base)(quote) ) diff --git a/libraries/types/include/eos/types/PublicKey.hpp b/libraries/types/include/eos/types/PublicKey.hpp new file mode 100644 index 0000000000000000000000000000000000000000..008cf7629fe3419cd364857dc2f153c58920fde5 --- /dev/null +++ b/libraries/types/include/eos/types/PublicKey.hpp @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include + +namespace eos { + struct PublicKey + { + struct BinaryKey + { + BinaryKey() {} + uint32_t check = 0; + fc::ecc::public_key_data data; + }; + fc::ecc::public_key_data key_data; + PublicKey(); + PublicKey( const fc::ecc::public_key_data& data ); + PublicKey( const fc::ecc::public_key& pubkey ); + explicit PublicKey( const std::string& base58str ); + operator fc::ecc::public_key_data() const; + operator fc::ecc::public_key() const; + explicit operator std::string() const; + friend bool operator == ( const PublicKey& p1, const fc::ecc::public_key& p2); + friend bool operator == ( const PublicKey& p1, const PublicKey& p2); + friend bool operator != ( const PublicKey& p1, const PublicKey& p2); + friend bool operator < ( const PublicKey& p1, const PublicKey& p2); + bool is_valid_v1( const std::string& base58str ); + }; +} + +namespace fc +{ + void to_variant( const eos::PublicKey& var, fc::variant& vo ); + void from_variant( const fc::variant& var, eos::PublicKey& vo ); +} + +FC_REFLECT( eos::PublicKey, (key_data) ) +FC_REFLECT( eos::PublicKey::BinaryKey, (data)(check) ) diff --git a/libraries/types/include/eos/types/TypeParser.hpp b/libraries/types/include/eos/types/TypeParser.hpp index 3ebe350af31e6cbb7d862f6dba00d28b9e16bbf2..3092407b174adcb159ba7ed77f14b24f593dab40 100644 --- a/libraries/types/include/eos/types/TypeParser.hpp +++ b/libraries/types/include/eos/types/TypeParser.hpp @@ -10,7 +10,7 @@ #include -namespace EOS { +namespace eos { using std::map; using std::set; using std::string; @@ -39,7 +39,7 @@ namespace EOS { class SimpleSymbolTable : public AbstractSymbolTable { public: SimpleSymbolTable(): - known( { "FixedString16", "FixedString32", + known( { "Field", "Struct", "Asset", "FixedString16", "FixedString32", "UInt8", "UInt16", "UInt32", "UInt64", "UInt128", "Checksum", "UInt256", "UInt512", "Int8", "Int16", "Int32", "Int64", @@ -65,16 +65,17 @@ namespace EOS { order.push_back(to); } FC_CAPTURE_AND_RETHROW( (from)(to) ) } - virtual bool isKnownType( const TypeName& name ) override { try { + virtual bool isKnownType( const TypeName& n ) override { try { + String name(n); FC_ASSERT( name.size() > 0 ); if( name.size() > 2 && name.back() == ']' ) { FC_ASSERT( name[name.size()-2] == '[' ); return isKnownType( name.substr( 0, name.size()-2 ) ); } - return known.find(name) != known.end() || - typedefs.find(name) != typedefs.end() || - structs.find(name) != structs.end(); - } FC_CAPTURE_AND_RETHROW( (name) ) } + return known.find(n) != known.end() || + typedefs.find(n) != typedefs.end() || + structs.find(n) != structs.end(); + } FC_CAPTURE_AND_RETHROW( (n) ) } virtual Struct getType( TypeName name )const override { name = resolveTypedef( name ); @@ -105,8 +106,6 @@ namespace EOS { }; -} // namespace EOS +} // namespace eos -FC_REFLECT( EOS::Field, (name)(type) ) -FC_REFLECT( EOS::Struct, (name)(base)(fields) ) -FC_REFLECT( EOS::SimpleSymbolTable, (typedefs)(structs) ) +FC_REFLECT( eos::SimpleSymbolTable, (typedefs)(structs) ) diff --git a/libraries/types/include/eos/types/ints.hpp b/libraries/types/include/eos/types/ints.hpp deleted file mode 100644 index afd62f70f66bc19f46793ffc572b0a7c67544987..0000000000000000000000000000000000000000 --- a/libraries/types/include/eos/types/ints.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#include -#include - -namespace EOS { - - using namespace boost::multiprecision; - - typedef number > UInt8; - typedef number > UInt16; - typedef number > UInt32; - typedef number > UInt64; - typedef boost::multiprecision::uint128_t UInt128; - typedef boost::multiprecision::uint256_t UInt256; - typedef number > Int8; - typedef number > Int16; - typedef number > Int32; - typedef number > Int64; - typedef boost::multiprecision::int128_t Int128; - typedef boost::multiprecision::int256_t Int256; - - - template - void fromBinary( Stream& st, boost::multiprecision::number& value ) { - unsigned char data[(std::numeric_limits::digits+1)/8]; - st.read( (char*)data, sizeof(data) ); - boost::multiprecision::import_bits( value, data, data + sizeof(data), 1 ); - } - template - void toBinary( Stream& st, const boost::multiprecision::number& value ) { - unsigned char data[(std::numeric_limits::digits+1)/8]; - boost::multiprecision::export_bits( value, data, 1 ); - st.write( (const char*)data, sizeof(data) ); - } -} // namespace EOS diff --git a/libraries/types/include/eos/types/native.hpp b/libraries/types/include/eos/types/native.hpp index bc004a8829418c68906df00729f20dca6e01ec70..691f3b4fae2f14a9ce0a6d942c9babdb42c66d76 100644 --- a/libraries/types/include/eos/types/native.hpp +++ b/libraries/types/include/eos/types/native.hpp @@ -1,269 +1,120 @@ #pragma once -#include -#include - #include #include #include #include +#include #include #include #include #include -#include +#include #include - -namespace EOS { - using std::vector; - using std::array; - using std::string; - - - template - struct FixedString { - array value; - static_assert( sizeof(value) == Size, "unexpected padding" ); - - FixedString(){ memset( &value, 0, sizeof(value) ); } - FixedString( const std::string& str ) { - memset( &value, 0, sizeof(value) ); - if( str.size() <= Size ) - memcpy( (char*)&value, str.c_str(), str.size() ); - else { - memcpy( (char*)&value, str.c_str(), Size ); - } - } - operator std::string()const { - const char* self = (const char*)&value; - return std::string( self, self + size() ); - } - - uint32_t size()const { - if( *(((const char*)&value)+ - 1) ) - return sizeof(value); - return strnlen( (const char*)&value, Size ); - } - - friend bool operator == ( const FixedString& a, const FixedString& b ) { - return a.value == b.value; - } - friend bool operator < ( const FixedString& a, const FixedString& b ) { - return a.value < b.value; - } - friend bool operator == ( const FixedString& a, const char* b ) { - const char* self = (const char*)&a.value; - return 0 == strncmp( self, b, Size ); - } - }; - typedef FixedString<16> FixedString16; - typedef FixedString<32> FixedString32; - - typedef string TypeName; - typedef string FieldName; +#include +#include + +#include + +namespace eos { + using namespace boost::multiprecision; + + template + using Vector = std::vector; + + template + using Array = std::array; + + using String = std::string; + using Time = fc::time_point_sec; + using Signature = fc::ecc::compact_signature; + using Checksum = fc::sha256; + using FieldName = fc::fixed_string<>; + using FixedString32 = fc::fixed_string>; + using FixedString16 = fc::fixed_string<>; + using TypeName = FixedString32; + using Bytes = Vector; + + template + using UInt = number >; + template + using Int = number >; + + using UInt8 = UInt<8>; + using UInt16 = UInt<16>; + using UInt32 = UInt<32>; + using UInt64 = UInt<64>; + using UInt128 = boost::multiprecision::uint128_t; + using UInt256 = boost::multiprecision::uint256_t; + using Int8 = int8_t;//Int<8>; these types are different sizes than native... + using Int16 = int16_t; //Int<16>; + using Int32 = int32_t; //Int<32>; + using Int64 = int64_t; //Int<64>; + using Int128 = boost::multiprecision::int128_t; + using Int256 = boost::multiprecision::int256_t; struct Field { FieldName name; TypeName type; }; - typedef fc::time_point_sec Time; - typedef fc::ecc::compact_signature Signature; - typedef string String; - typedef fc::sha256 Checksum; - typedef fc::ripemd160 BlockID; - typedef fc::ripemd160 TransactionID; - struct Struct { TypeName name; TypeName base; - vector fields; + Vector fields; }; - struct Bytes { - uint32_t size()const { return value.size(); } - const char* data()const { return value.data(); } - char* data() { return value.data(); } - void resize( uint32_t size ) { value.resize(size); } - - template - T as()const; - - vector value; - }; - - struct PublicKey - { - struct BinaryKey - { - BinaryKey() {} - uint32_t check = 0; - fc::ecc::public_key_data data; - }; - fc::ecc::public_key_data key_data; - - - PublicKey(); - PublicKey( const fc::ecc::public_key_data& data ); - PublicKey( const fc::ecc::public_key& pubkey ); - explicit PublicKey( const std::string& base58str ); - operator fc::ecc::public_key_data() const; - operator fc::ecc::public_key() const; - explicit operator std::string() const; - friend bool operator == ( const PublicKey& p1, const fc::ecc::public_key& p2); - friend bool operator == ( const PublicKey& p1, const PublicKey& p2); - friend bool operator < ( const PublicKey& p1, const PublicKey& p2) { return p1.key_data < p2.key_data; } - friend bool operator != ( const PublicKey& p1, const PublicKey& p2); - }; - - template - inline void toBinary( Stream& stream, const FixedString32& v ) { - stream.write( (const char*)&v.value, sizeof(v.value) ); - } - template - inline void fromBinary( Stream& stream, FixedString32& v ) { - stream.read( (char*)&v.value, sizeof(v.value) ); - } - template - inline void fromBinary( Stream& stream, FixedString16& v ) { - stream.read( (char*)&v.value, sizeof(v.value) ); - } - template - inline void toBinary( Stream& stream, const FixedString16& v ) { - stream.write( (const char*)&v.value, sizeof(v.value) ); - } - - template - inline void toBinary( Stream& stream, const Signature& v ) { - stream.write( (const char*)&v, sizeof(v) ); - } - template - inline void fromBinary( Stream& stream, Signature& v ) { - stream.read( (char*)&v, sizeof(v) ); - } - - - inline fc::variant toVariant( const Int8& v ) { return static_cast(v); } - inline fc::variant toVariant( const Int16& v ) { return static_cast(v); } - inline fc::variant toVariant( const Int32& v ) { return static_cast(v); } - inline fc::variant toVariant( const Int64& v ) { return static_cast(v); } - inline fc::variant toVariant( const UInt8& v ) { return static_cast(v); } - inline fc::variant toVariant( const UInt16& v ) { return static_cast(v); } - inline fc::variant toVariant( const UInt32& v ) { return static_cast(v); } - inline fc::variant toVariant( const UInt64& v ) { return static_cast(v); } - inline fc::variant toVariant( const FixedString16& v ) { return std::string(v); } - inline fc::variant toVariant( const FixedString32& v ) { return std::string(v); } - inline fc::variant toVariant( const PublicKey& v ) { return std::string(v); } - inline fc::variant toVariant( const String& v ) { return v; } - inline fc::variant toVariant( const Time& v ) { return fc::variant(v); } - inline fc::variant toVariant( const Checksum& v ) { return fc::to_hex( (const char*)&v, sizeof(v) ); } - inline fc::variant toVariant( const Signature& v ) { return fc::to_hex( (const char*)&v, sizeof(v) ); } - - inline void fromVariant( Int8& v, const fc::variant& var ) { v = static_cast(var.as_int64()); } - inline void fromVariant( Int16& v, const fc::variant& var ) { v = static_cast(var.as_int64()); } - inline void fromVariant( Int32& v, const fc::variant& var ) { v = static_cast(var.as_int64()); } - inline void fromVariant( Int64& v, const fc::variant& var ) { v = static_cast(var.as_int64()); } - inline void fromVariant( UInt8& v, const fc::variant& var ) { v = static_cast(var.as_uint64()); } - inline void fromVariant( UInt16& v, const fc::variant& var ) { v = static_cast(var.as_uint64()); } - inline void fromVariant( UInt32& v, const fc::variant& var ) { v = static_cast(var.as_uint64()); } - inline void fromVariant( UInt64& v, const fc::variant& var ) { v = static_cast(var.as_uint64()); } - inline void fromVariant( FixedString16& v, const fc::variant& var ) { v = FixedString16( var.as_string() ); } - inline void fromVariant( FixedString32& v, const fc::variant& var ) { v = FixedString32( var.as_string() ); } - inline void fromVariant( String& v, const fc::variant& var ) { v = var.as_string(); } - inline void fromVariant( Time& v, const fc::variant& var ) { v = var.as