Skip to content

Commit

Permalink
Merge pull request EOSIO#115 from enumivo/staging
Browse files Browse the repository at this point in the history
merge eosio master
  • Loading branch information
Enumivo authored May 16, 2018
2 parents 0c71acd + be86faa commit 4e15584
Show file tree
Hide file tree
Showing 36 changed files with 1,256 additions and 119 deletions.
97 changes: 97 additions & 0 deletions CMakeModules/FindSoci.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
###############################################################################
# CMake module to search for SOCI library
#
# WARNING: This module is experimental work in progress.
#
# This module defines:
# SOCI_INCLUDE_DIRS = include dirs to be used when using the soci library
# SOCI_LIBRARY = full path to the soci library
# SOCI_VERSION = the soci version found (not yet. soci does not provide that info.)
# SOCI_FOUND = true if soci was found
#
# This module respects:
# LIB_SUFFIX = (64|32|"") Specifies the suffix for the lib directory
#
# For each component you specify in find_package(), the following variables are set.
#
# SOCI_${COMPONENT}_PLUGIN = full path to the soci plugin
# SOCI_${COMPONENT}_FOUND
#
# Copyright (c) 2011 Michael Jansen <info@michael-jansen.biz>
#
# Redistribution and use is allowed according to the terms of the BSD license.
# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
#
###############################################################################
#
### Global Configuration Section
#
SET(_SOCI_ALL_PLUGINS mysql odbc postgresql sqlite3)
SET(_SOCI_REQUIRED_VARS SOCI_INCLUDE_DIR SOCI_LIBRARY)

#
### FIRST STEP: Find the soci headers.
#
FIND_PATH(
SOCI_INCLUDE_DIR soci.h
PATH "/usr/local"
PATH_SUFFIXES "" "soci"
DOC "Soci (http://soci.sourceforge.net) include directory")
MARK_AS_ADVANCED(SOCI_INCLUDE_DIR)

SET(SOCI_INCLUDE_DIRS ${SOCI_INCLUDE_DIR})

#
### SECOND STEP: Find the soci core library. Respect LIB_SUFFIX
#
FIND_LIBRARY(
SOCI_LIBRARY
NAMES soci_core
HINTS ${SOCI_INCLUDE_DIR}/..
PATH_SUFFIXES lib${LIB_SUFFIX})
MARK_AS_ADVANCED(SOCI_LIBRARY)

GET_FILENAME_COMPONENT(SOCI_LIBRARY_DIR ${SOCI_LIBRARY} PATH)
MARK_AS_ADVANCED(SOCI_LIBRARY_DIR)

#
### THIRD STEP: Find all installed plugins if the library was found
#
IF(SOCI_INCLUDE_DIR AND SOCI_LIBRARY)

MESSAGE(STATUS "Soci found: Looking for plugins")
FOREACH(plugin IN LISTS _SOCI_ALL_PLUGINS)

FIND_LIBRARY(
SOCI_${plugin}_PLUGIN
NAMES soci_${plugin}
HINTS ${SOCI_INCLUDE_DIR}/..
PATH_SUFFIXES lib${LIB_SUFFIX})
MARK_AS_ADVANCED(SOCI_${plugin}_PLUGIN)

IF(SOCI_${plugin}_PLUGIN)
MESSAGE(STATUS " * Plugin ${plugin} found ${SOCI_${plugin}_PLUGIN}.")
SET(SOCI_${plugin}_FOUND True)
ELSE()
MESSAGE(STATUS " * Plugin ${plugin} not found.")
SET(SOCI_${plugin}_FOUND False)
ENDIF()

ENDFOREACH()

#
### FOURTH CHECK: Check if the required components were all found
#
FOREACH(component ${Soci_FIND_COMPONENTS})
IF(NOT SOCI_${component}_FOUND)
MESSAGE(SEND_ERROR "Required component ${component} not found.")
ENDIF()
ENDFOREACH()

ENDIF()

#
### ADHERE TO STANDARDS
#
include(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(Soci DEFAULT_MSG ${_SOCI_REQUIRED_VARS})
15 changes: 7 additions & 8 deletions contracts/enumivo.coin/enumivo.coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ void token::create( account_name issuer,

void token::issue( account_name to, asset quantity, string memo )
{
print( "issue" );
auto sym = quantity.symbol;
eosio_assert( sym.is_valid(), "invalid symbol name" );

Expand Down Expand Up @@ -70,7 +69,6 @@ void token::transfer( account_name from,
asset quantity,
string /*memo*/ )
{
print( "transfer from ", eosio::name{from}, " to ", eosio::name{to}, " ", quantity, "\n" );
eosio_assert( from != to, "cannot transfer to self" );
require_auth( from );
eosio_assert( is_account( to ), "to account does not exist");
Expand Down Expand Up @@ -106,10 +104,13 @@ void token::sub_balance( account_name owner, asset value, const currency_stats&
eosio_assert( false, "insufficient authority" );
}

from_acnts.modify( from, owner, [&]( auto& a ) {
a.balance -= value;
print( eosio::name{owner}, " balance: ", a.balance, "\n" );
});
if( from.balance.amount == value.amount ) {
from_acnts.erase( from );
} else {
from_acnts.modify( from, owner, [&]( auto& a ) {
a.balance -= value;
});
}
}

void token::add_balance( account_name owner, asset value, const currency_stats& st, account_name ram_payer )
Expand All @@ -120,13 +121,11 @@ void token::add_balance( account_name owner, asset value, const currency_stats&
eosio_assert( !st.enforce_whitelist, "can only transfer to white listed accounts" );
to_acnts.emplace( ram_payer, [&]( auto& a ){
a.balance = value;
print( eosio::name{owner}, " balance: ", a.balance, "\n" );
});
} else {
eosio_assert( !st.enforce_whitelist || to->whitelist, "receiver requires whitelist by issuer" );
to_acnts.modify( to, 0, [&]( auto& a ) {
a.balance += value;
print( eosio::name{owner}, " balance: ", a.balance, "\n" );
});
}
}
Expand Down
34 changes: 17 additions & 17 deletions contracts/enumivo.system/delegate_bandwidth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ namespace eosiosystem {
*/
void system_contract::buyram( account_name payer, account_name receiver, asset quant )
{
print( "\n payer: ", eosio::name{payer}, " buys ram for ", eosio::name{receiver}, " with ", quant, "\n" );
// print( "\n payer: ", eosio::name{payer}, " buys ram for ", eosio::name{receiver}, " with ", quant, "\n" );
require_auth( payer );
eosio_assert( quant.amount > 0, "must purchase a positive amount" );

Expand All @@ -148,7 +148,7 @@ namespace eosiosystem {
{ payer, N(eosio), quant, std::string("buy ram") } );
}

print( "free ram: ", _gstate.free_ram(), "\n");
// print( "free ram: ", _gstate.free_ram(), "\n");

int64_t bytes_out;

Expand All @@ -157,7 +157,7 @@ namespace eosiosystem {
bytes_out = es.convert( quant, S(0,RAM) ).amount;
});

print( "ram bytes out: ", bytes_out, "\n" );
// print( "ram bytes out: ", bytes_out, "\n" );

eosio_assert( bytes_out > 0, "must reserve a positive amount" );

Expand Down Expand Up @@ -187,17 +187,19 @@ namespace eosiosystem {
*/
void system_contract::sellram( account_name account, uint64_t bytes ) {
require_auth( account );
int64_t ibytes = static_cast<int64_t>(bytes);

user_resources_table userres( _self, account );
auto res_itr = userres.find( account );
eosio_assert( res_itr != userres.end(), "no resource row" );
eosio_assert( res_itr->ram_bytes >= bytes, "insufficient quota" );
eosio_assert( res_itr->ram_bytes >= ibytes, "insufficient quota" );

asset tokens_out;
auto itr = _rammarket.find(S(4,RAMEOS));
_rammarket.modify( itr, 0, [&]( auto& es ) {
tokens_out = es.convert( asset(bytes,S(0,RAM)), S(4,EOS) );
print( "out: ", tokens_out, "\n" );
/// the cast to int64_t of bytes is safe because we certify bytes is <= quota which is limited by prior purchases
tokens_out = es.convert( asset(ibytes,S(0,RAM)), S(4,EOS) );
// print( "out: ", tokens_out, "\n" );
});

_gstate.total_ram_bytes_reserved -= bytes;
Expand All @@ -223,7 +225,7 @@ namespace eosiosystem {

{
require_auth( from );
print( "from: ", eosio::name{from}, " to: ", eosio::name{receiver}, " net: ", stake_net_quantity, " cpu: ", stake_cpu_quantity );
// print( "from: ", eosio::name{from}, " to: ", eosio::name{receiver}, " net: ", stake_net_quantity, " cpu: ", stake_cpu_quantity );

eosio_assert( stake_cpu_quantity >= asset(0), "must stake a positive amount" );
eosio_assert( stake_net_quantity >= asset(0), "must stake a positive amount" );
Expand Down Expand Up @@ -252,7 +254,6 @@ namespace eosiosystem {
});
}

print( "totals" );
user_resources_table totals_tbl( _self, receiver );
auto tot_itr = totals_tbl.find( receiver );
if( tot_itr == totals_tbl.end() ) {
Expand All @@ -275,23 +276,18 @@ namespace eosiosystem {
{ source_stake_from, N(eosio), asset(total_stake), std::string("stake bandwidth") } );
}

print( "voters \n" );
auto from_voter = _voters.find(from);
if( from_voter == _voters.end() ) {
print( " create voter \n" );
from_voter = _voters.emplace( from, [&]( auto& v ) {
v.owner = from;
v.staked = total_stake;
print( " vote weight: ", v.last_vote_weight, "\n" );
});
} else {
_voters.modify( from_voter, 0, [&]( auto& v ) {
v.staked += total_stake;
print( " vote weight: ", v.last_vote_weight, "\n" );
});
}

print( "voteproducer\n" );
if( from_voter->producers.size() || from_voter->proxy ) {
voteproducer( from, from_voter->proxy, from_voter->producers );
}
Expand Down Expand Up @@ -332,10 +328,14 @@ namespace eosiosystem {

eosio_assert( total_refund > 0, "must unstake a positive amount" );

del_tbl.modify( dbw, from, [&]( auto& dbo ){
dbo.net_weight -= unstake_net_quantity;
dbo.cpu_weight -= unstake_cpu_quantity;
});
if( dbw.net_weight == unstake_net_quantity && dbw.cpu_weight == unstake_cpu_quantity ) {
del_tbl.erase( dbw );
} else {
del_tbl.modify( dbw, from, [&]( auto& dbo ){
dbo.net_weight -= unstake_net_quantity;
dbo.cpu_weight -= unstake_cpu_quantity;
});
}

user_resources_table totals_tbl( _self, receiver );

Expand Down
8 changes: 5 additions & 3 deletions contracts/enumivo.system/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ namespace eosiosystem {
}

double stake2vote( int64_t staked ) {
double weight = int64_t(now() / (seconds_per_day * 7)) / double( 52 );
/// TODO subtract 2080 brings the large numbers closer to this decade
double weight = int64_t( (now() / (seconds_per_day * 7)) ) / double( 52 );
return double(staked) * std::pow( 2, weight );
}
/**
Expand Down Expand Up @@ -174,7 +175,7 @@ namespace eosiosystem {
}

boost::container::flat_map<account_name, pair<double, bool /*new*/> > producer_deltas;
if ( voter->last_vote_weight != 0 ) {
if ( voter->last_vote_weight > 0 ) {
if( voter->proxy ) {
auto old_proxy = _voters.find( voter->proxy );
eosio_assert( old_proxy != _voters.end(), "old proxy not found" ); //data corruption
Expand Down Expand Up @@ -271,7 +272,8 @@ namespace eosiosystem {
new_weight += voter.proxied_vote_weight;
}

if ( new_weight != voter.last_vote_weight ) {
/// don't propagate small changes (1 ~= epsilon)
if ( fabs( new_weight - voter.last_vote_weight ) > 1 ) {
if ( voter.proxy ) {
auto& proxy = _voters.get( voter.proxy, "proxy not found" ); //data corruption
_voters.modify( proxy, 0, [&]( auto& p ) {
Expand Down
12 changes: 6 additions & 6 deletions contracts/enumivolib/time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,17 @@ namespace eosio {

ENULIB_SERIALIZE( block_timestamp, (slot) )
private:
static constexpr uint32_t block_interval_ms = 500;
static constexpr uint64_t block_timestamp_epoch = 946684800000ll; // epoch is year 2000
static constexpr int32_t block_interval_ms = 500;
static constexpr int64_t block_timestamp_epoch = 946684800000ll; // epoch is year 2000

void set_time_point(const time_point& t) {
auto micro_since_epoch = t.time_since_epoch();
auto msec_since_epoch = micro_since_epoch.count() / 1000;
slot = uint32_t(( msec_since_epoch - block_timestamp_epoch ) / block_interval_ms);
int64_t micro_since_epoch = t.time_since_epoch().count();
int64_t msec_since_epoch = micro_since_epoch / 1000;
slot = uint32_t(( msec_since_epoch - block_timestamp_epoch ) / int64_t(block_interval_ms));
}

void set_time_point(const time_point_sec& t) {
uint64_t sec_since_epoch = t.sec_since_epoch();
int64_t sec_since_epoch = t.sec_since_epoch();
slot = uint32_t((sec_since_epoch * 1000 - block_timestamp_epoch) / block_interval_ms);
}
}; // block_timestamp
Expand Down
5 changes: 2 additions & 3 deletions contracts/enumivolib/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,15 @@ namespace eosio {
tmp >>= (i == 0 ? 4 : 5);
}

trim_right_if( str, []( char c ){ return c == '.'; } );
trim_right_dots( str );
return str;
}

friend bool operator==( const name& a, const name& b ) { return a.value == b.value; }
account_name value = 0;

private:
template<typename Lambda>
static void trim_right_if(std::string& str, Lambda&& l ) {
static void trim_right_dots(std::string& str ) {
const auto last = str.find_last_not_of('.');
if (last != std::string::npos)
str = str.substr(0, last + 1);
Expand Down
1 change: 1 addition & 0 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_subdirectory(txn_test_gen_plugin)
add_subdirectory(db_size_api_plugin)
#add_subdirectory(faucet_testnet_plugin)
#add_subdirectory(mongo_db_plugin)
add_subdirectory(sql_db_plugin)

# Forward variables to top level so packaging picks them up
set(CPACK_DEBIAN_PACKAGE_DEPENDS ${CPACK_DEBIAN_PACKAGE_DEPENDS} PARENT_SCOPE)
4 changes: 1 addition & 3 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ class producer_plugin_impl {

void on_incoming_block(const signed_block_ptr& block) {

if( block->timestamp > fc::time_point::now() ) {
FC_ASSERT( block->timestamp < fc::time_point::now(), "received a block from the future, ignoring it" );
}
FC_ASSERT( block->timestamp < (fc::time_point::now() + fc::seconds(1)), "received a block from the future, ignoring it" );


chain::controller& chain = app().get_plugin<chain_plugin>().chain();
Expand Down
28 changes: 28 additions & 0 deletions plugins/sql_db_plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
find_package(Soci)
if(NOT SOCI_FOUND)
message(STATUS "Database SQL plugin: disabled")
return()
endif()

message(STATUS "Database SQL plugin: enabled")

include_directories(${CMAKE_CURRENT_SOURCE_DIR} include db)

add_library(sql_db_plugin
db/database.cpp
db/accounts_table.cpp
db/transactions_table.cpp
db/blocks_table.cpp
db/actions_table.cpp
sql_db_plugin.cpp
irreversible_block_storage.cpp
block_storage.cpp
)

target_link_libraries(sql_db_plugin
chain_plugin
${SOCI_LIBRARY}
)

add_subdirectory(test)

11 changes: 11 additions & 0 deletions plugins/sql_db_plugin/block_storage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "block_storage.h"

namespace eosio {

void block_storage::consume(const std::vector<chain::block_state_ptr> &blocks)
{
for (const auto& block : blocks)
ilog(block->id.str());
}

}
Loading

0 comments on commit 4e15584

Please sign in to comment.