Skip to content

Commit

Permalink
Merge branch 'main' of github.com:AntelopeIO/leap into gh-672
Browse files Browse the repository at this point in the history
  • Loading branch information
greg7mdp committed Jun 12, 2023
2 parents 727175d + 6c5e156 commit 42ca516
Show file tree
Hide file tree
Showing 24 changed files with 310 additions and 277 deletions.
2 changes: 1 addition & 1 deletion libraries/appbase
1 change: 1 addition & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ add_library( eosio_chain
protocol_feature_manager.cpp
producer_schedule.cpp
genesis_intrinsics.cpp
symbol.cpp
whitelisted_intrinsics.cpp
thread_utils.cpp
platform_timer_accuracy.cpp
Expand Down
10 changes: 6 additions & 4 deletions libraries/chain/asset.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <string>
#include <eosio/chain/asset.hpp>
#include <boost/rational.hpp>
#include <boost/algorithm/string.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/safe.hpp>

Expand All @@ -22,24 +24,24 @@ int64_t asset::precision()const {
string asset::to_string()const {
string sign = amount < 0 ? "-" : "";
int64_t abs_amount = std::abs(amount);
string result = fc::to_string( static_cast<int64_t>(abs_amount) / precision());
string result = std::to_string( static_cast<int64_t>(abs_amount) / precision());
if( decimals() )
{
auto fract = static_cast<int64_t>(abs_amount) % precision();
result += "." + fc::to_string(precision() + fract).erase(0,1);
result += "." + std::to_string(precision() + fract).erase(0,1);
}
return sign + result + " " + symbol_name();
}

asset asset::from_string(const string& from)
{
try {
string s = fc::trim(from);
string s = boost::algorithm::trim_copy(from);

// Find space in order to split amount and symbol
auto space_pos = s.find(' ');
EOS_ASSERT((space_pos != string::npos), asset_type_exception, "Asset's amount and symbol should be separated with space");
auto symbol_str = fc::trim(s.substr(space_pos + 1));
auto symbol_str = boost::algorithm::trim_copy(s.substr(space_pos + 1));
auto amount_str = s.substr(0, space_pos);

// Ensure that if decimal point is used (.), decimal fraction is specified
Expand Down
22 changes: 4 additions & 18 deletions libraries/chain/include/eosio/chain/symbol.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once
#include <fc/exception/exception.hpp>
#include <eosio/chain/exceptions.hpp>
#include <eosio/chain/types.hpp>
#include <eosio/chain/core_symbol.hpp>
#include <string>
#include <functional>

namespace eosio {
namespace chain {
namespace eosio::chain {

/**
class symbol represents a token and contains precision and name.
Expand Down Expand Up @@ -65,20 +65,7 @@ namespace eosio {
explicit symbol(uint64_t v = CORE_SYMBOL): m_value(v) {
EOS_ASSERT(valid(), symbol_type_exception, "invalid symbol: ${name}", ("name",name()));
}
static symbol from_string(const string& from)
{
try {
string s = fc::trim(from);
EOS_ASSERT(!s.empty(), symbol_type_exception, "creating symbol from empty string");
auto comma_pos = s.find(',');
EOS_ASSERT(comma_pos != string::npos, symbol_type_exception, "missing comma in symbol");
auto prec_part = s.substr(0, comma_pos);
uint8_t p = fc::to_int64(prec_part);
string name_part = s.substr(comma_pos + 1);
EOS_ASSERT( p <= max_precision, symbol_type_exception, "precision ${p} should be <= 18", ("p", p));
return symbol(string_to_symbol(p, name_part.c_str()));
} FC_CAPTURE_LOG_AND_RETHROW((from))
}
static symbol from_string(const string& from);
uint64_t value() const { return m_value; }
bool valid() const
{
Expand Down Expand Up @@ -184,8 +171,7 @@ namespace eosio {
{
return std::tie(lhs.sym, lhs.contract) > std::tie(rhs.sym, rhs.contract);
}
} // namespace chain
} // namespace eosio
} // namespace eosio::chain

namespace fc {
inline void to_variant(const eosio::chain::symbol& var, fc::variant& vo) { vo = var.to_string(); }
Expand Down
21 changes: 21 additions & 0 deletions libraries/chain/symbol.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <eosio/chain/symbol.hpp>
#include <boost/algorithm/string.hpp>

namespace eosio::chain {

symbol symbol::from_string(const string& from)
{
try {
string s = boost::algorithm::trim_copy(from);
EOS_ASSERT(!s.empty(), symbol_type_exception, "creating symbol from empty string");
auto comma_pos = s.find(',');
EOS_ASSERT(comma_pos != string::npos, symbol_type_exception, "missing comma in symbol");
auto prec_part = s.substr(0, comma_pos);
uint8_t p = fc::to_int64(prec_part);
string name_part = s.substr(comma_pos + 1);
EOS_ASSERT( p <= max_precision, symbol_type_exception, "precision ${p} should be <= 18", ("p", p));
return symbol(string_to_symbol(p, name_part.c_str()));
} FC_CAPTURE_LOG_AND_RETHROW((from));
}

} // namespace eosio::chain
2 changes: 1 addition & 1 deletion libraries/libfc/include/fc/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace fc {
{
static const char* name()
{
static std::string _name = std::string("fc::array<")+std::string(fc::get_typename<T>::name())+","+ fc::to_string(N) + ">";
static std::string _name = std::string("fc::array<")+std::string(fc::get_typename<T>::name())+","+ std::to_string(N) + ">";
return _name.c_str();
}
};
Expand Down
5 changes: 3 additions & 2 deletions libraries/libfc/include/fc/reflect/reflect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <boost/preprocessor/stringize.hpp>
#include <stdint.h>
#include <string.h>
#include <string>

#include <fc/reflect/typename.hpp>

Expand Down Expand Up @@ -200,7 +201,7 @@ template<> struct reflector<ENUM> { \
switch( elem ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_STRING, ENUM, FIELDS ) \
default: \
fc::throw_bad_enum_cast( fc::to_string(int64_t(elem)).c_str(), BOOST_PP_STRINGIZE(ENUM) ); \
fc::throw_bad_enum_cast( std::to_string(int64_t(elem)).c_str(), BOOST_PP_STRINGIZE(ENUM) ); \
}\
return nullptr; \
} \
Expand All @@ -211,7 +212,7 @@ template<> struct reflector<ENUM> { \
switch( elem ) { \
BOOST_PP_SEQ_FOR_EACH( FC_REFLECT_ENUM_TO_FC_STRING, ENUM, FIELDS ) \
} \
return fc::to_string(int64_t(elem)); \
return std::to_string(int64_t(elem)); \
} \
static std::string to_fc_string(int64_t i) { \
return to_fc_string(ENUM(i)); \
Expand Down
10 changes: 0 additions & 10 deletions libraries/libfc/include/fc/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,9 @@ namespace fc
int64_t to_int64( const std::string& );
uint64_t to_uint64( const std::string& );
double to_double( const std::string& );
std::string to_string( double );
std::string to_string( uint64_t );
std::string to_string( int64_t );
std::string to_string( uint16_t );
inline std::string to_string( int32_t v ) { return to_string( int64_t(v) ); }
inline std::string to_string( uint32_t v ){ return to_string( uint64_t(v) ); }
#ifdef __APPLE__
inline std::string to_string( size_t s) { return to_string(uint64_t(s)); }
#endif

class variant_object;
std::string format_string( const std::string&, const variant_object&, bool minimize = false );
std::string trim( const std::string& );

/**
* Convert '\t', '\r', '\n', '\\' and '"' to "\t\r\n\\\"" if escape_ctrl == on
Expand Down
2 changes: 1 addition & 1 deletion libraries/libfc/src/log/console_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ namespace fc {
const log_context context = m.get_context();
std::string file_line = context.get_file().substr( 0, 22 );
file_line += ':';
file_line += fixed_size( 6, fc::to_string( context.get_line_number() ) );
file_line += fixed_size( 6, std::to_string( context.get_line_number() ) );

std::string line;
line.reserve( 256 );
Expand Down
2 changes: 1 addition & 1 deletion libraries/libfc/src/log/gelf_appender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace fc
gelf_message["_timestamp_ns"] = time_ns;

static uint64_t gelf_log_counter;
gelf_message["_log_id"] = fc::to_string(++gelf_log_counter);
gelf_message["_log_id"] = std::to_string(++gelf_log_counter);

switch (context.get_log_level())
{
Expand Down
2 changes: 1 addition & 1 deletion libraries/libfc/src/log/log_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace fc

std::string log_context::to_string()const
{
return my->thread_name + " " + my->file + ":" + fc::to_string(my->line) + " " + my->method;
return my->thread_name + " " + my->file + ":" + std::to_string(my->line) + " " + my->method;

}

Expand Down
28 changes: 0 additions & 28 deletions libraries/libfc/src/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <fc/io/json.hpp>
#include <fc/exception/exception.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>

#include <string>
#include <sstream>
Expand Down Expand Up @@ -60,33 +59,6 @@ namespace fc {
}
FC_RETHROW_EXCEPTIONS( warn, "${i} => double", ("i",i) )
}

std::string to_string(double d)
{
// +2 is required to ensure that the double is rounded correctly when read back in. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
std::stringstream ss;
ss << std::setprecision(std::numeric_limits<double>::digits10 + 2) << std::fixed << d;
return ss.str();
}

std::string to_string( uint64_t d)
{
return boost::lexical_cast<std::string>(d);
}

std::string to_string( int64_t d)
{
return boost::lexical_cast<std::string>(d);
}
std::string to_string( uint16_t d)
{
return boost::lexical_cast<std::string>(d);
}
std::string trim( const std::string& s )
{
return boost::algorithm::trim_copy(s);
}

std::pair<std::string&, bool> escape_str( std::string& str, escape_control_chars escape_ctrl,
std::size_t max_len, std::string_view add_truncate_str )
{
Expand Down
2 changes: 1 addition & 1 deletion libraries/libfc/src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace fc {
if (count >= 0) {
uint64_t secs = (uint64_t)count / 1000000ULL;
uint64_t msec = ((uint64_t)count % 1000000ULL) / 1000ULL;
std::string padded_ms = fc::to_string((uint64_t)(msec + 1000ULL)).substr(1);
std::string padded_ms = std::to_string((uint64_t)(msec + 1000ULL)).substr(1);
const auto ptime = boost::posix_time::from_time_t(time_t(secs));
return boost::posix_time::to_iso_extended_string(ptime) + "." + padded_ms;
} else {
Expand Down
14 changes: 11 additions & 3 deletions libraries/libfc/src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,18 +466,26 @@ bool variant::as_bool()const
}
}

static std::string s_fc_to_string(double d)
{
// +2 is required to ensure that the double is rounded correctly when read back in. http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
std::stringstream ss;
ss << std::setprecision(std::numeric_limits<double>::digits10 + 2) << std::fixed << d;
return ss.str();
}

std::string variant::as_string()const
{
switch( get_type() )
{
case string_type:
return **reinterpret_cast<const const_string_ptr*>(this);
case double_type:
return to_string(*reinterpret_cast<const double*>(this));
return s_fc_to_string(*reinterpret_cast<const double*>(this));
case int64_type:
return to_string(*reinterpret_cast<const int64_t*>(this));
return std::to_string(*reinterpret_cast<const int64_t*>(this));
case uint64_type:
return to_string(*reinterpret_cast<const uint64_t*>(this));
return std::to_string(*reinterpret_cast<const uint64_t*>(this));
case bool_type:
return *reinterpret_cast<const bool*>(this) ? "true" : "false";
case blob_type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ class read_write : public api_base {
// the following will convert the input to array of 2 uint128_t in little endian, i.e. 50f0fa8360ec998f4bb65b00c86282f5 fb54b91bfed2fe7fe39a92d999d002c5
// which is the format used by secondary index
chain::key256_t k;
uint8_t buffer[32];
uint8_t buffer[32] = {};
boost::multiprecision::export_bits(v, buffer, 8, false);
memcpy(&k[0], buffer + 16, 16);
memcpy(&k[1], buffer, 16);
Expand Down
Loading

0 comments on commit 42ca516

Please sign in to comment.