Skip to content

Commit

Permalink
Merge pull request cryptonomex#437 from pmconrad/issue_433_429
Browse files Browse the repository at this point in the history
Fix issues 433 + 429
  • Loading branch information
pmconrad committed Oct 23, 2017
2 parents 274aaf7 + 39f533a commit 1b8f9c6
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 6 deletions.
26 changes: 23 additions & 3 deletions libraries/chain/asset_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ void_result asset_create_evaluator::do_evaluate( const asset_create_operation& o
wlog( "Asset ${s} has a name which requires hardfork 385", ("s",op.symbol) );
}

core_fee_paid -= core_fee_paid.value/2;

if( op.bitasset_opts )
{
const asset_object& backing = op.bitasset_opts->short_backing_asset(d);
Expand All @@ -116,16 +114,38 @@ void_result asset_create_evaluator::do_evaluate( const asset_create_operation& o
FC_ASSERT( op.precision == op.bitasset_opts->short_backing_asset(d).precision );
}

if( d.head_block_time() <= HARDFORK_CORE_429_TIME )
{ // TODO: remove after HARDFORK_CORE_429_TIME has passed
graphene::chain::impl::hf_429_visitor hf_429;
hf_429( op );
}

return void_result();
} FC_CAPTURE_AND_RETHROW( (op) ) }

void asset_create_evaluator::pay_fee()
{
fee_is_odd = core_fee_paid.value & 1;
core_fee_paid -= core_fee_paid.value/2;
generic_evaluator::pay_fee();
}

object_id_type asset_create_evaluator::do_apply( const asset_create_operation& op )
{ try {
bool hf_429 = fee_is_odd && db().head_block_time() > HARDFORK_CORE_429_TIME;

const asset_dynamic_data_object& dyn_asset =
db().create<asset_dynamic_data_object>( [&]( asset_dynamic_data_object& a ) {
a.current_supply = 0;
a.fee_pool = core_fee_paid; //op.calculate_fee(db().current_fee_schedule()).value / 2;
a.fee_pool = core_fee_paid - (hf_429 ? 1 : 0);
});
if( fee_is_odd && !hf_429 )
{
const auto& core_dd = db().get<asset_object>( asset_id_type() ).dynamic_data( db() );
db().modify( core_dd, [=]( asset_dynamic_data_object& dd ) {
dd.current_supply++;
});
}

asset_bitasset_data_id_type bit_asset_id;
if( op.bitasset_opts.valid() )
Expand Down
4 changes: 4 additions & 0 deletions libraries/chain/hardfork.d/CORE_429.hf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// bitshares-core #429 rounding issue when creating assets
#ifndef HARDFORK_CORE_429_TIME
#define HARDFORK_CORE_429_TIME (fc::time_point_sec( 1511793600 ))
#endif
25 changes: 25 additions & 0 deletions libraries/chain/include/graphene/chain/asset_evaluator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ namespace graphene { namespace chain {

void_result do_evaluate( const asset_create_operation& o );
object_id_type do_apply( const asset_create_operation& o );

/** override the default behavior defined by generic_evalautor which is to
* post the fee to fee_paying_account_stats.pending_fees
*/
virtual void pay_fee() override;
private:
bool fee_is_odd;
};

class asset_issue_evaluator : public evaluator<asset_issue_evaluator>
Expand Down Expand Up @@ -145,4 +152,22 @@ namespace graphene { namespace chain {
void_result do_apply( const asset_claim_fees_operation& o );
};

namespace impl { // TODO: remove after HARDFORK_CORE_429_TIME has passed
class hf_429_visitor {
public:
typedef void result_type;

template<typename T>
void operator()( const T& v )const {}

void operator()( const graphene::chain::asset_create_operation& v )const {
FC_ASSERT( v.fee.asset_id == asset_id_type(), "Can only pay fee in BTS since block #21040000" );
}

void operator()( const graphene::chain::proposal_create_operation& v )const {
for( const op_wrapper& op : v.proposed_ops )
op.op.visit( *this );
}
};
}
} } // graphene::chain
8 changes: 8 additions & 0 deletions libraries/chain/proposal_evaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <graphene/chain/asset_evaluator.hpp>
#include <graphene/chain/proposal_evaluator.hpp>
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/protocol/fee_schedule.hpp>
#include <graphene/chain/exceptions.hpp>
#include <graphene/chain/hardfork.hpp>

#include <fc/smart_ref_impl.hpp>

Expand Down Expand Up @@ -75,6 +77,12 @@ void_result proposal_create_evaluator::do_evaluate(const proposal_create_operati
_proposed_trx.operations.push_back(op.op);
_proposed_trx.validate();

if( d.head_block_time() <= HARDFORK_CORE_429_TIME )
{ // TODO: remove after HARDFORK_CORE_429_TIME has passed
graphene::chain::impl::hf_429_visitor hf_429;
hf_429( o );
}

return void_result();
} FC_CAPTURE_AND_RETHROW( (o) ) }

Expand Down
3 changes: 2 additions & 1 deletion libraries/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ add_subdirectory( witness )
add_subdirectory( account_history )
add_subdirectory( market_history )
add_subdirectory( delayed_node )
add_subdirectory( debug_witness )
add_subdirectory( debug_witness )
add_subdirectory( snapshot )
17 changes: 17 additions & 0 deletions libraries/plugins/snapshot/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
file(GLOB HEADERS "include/graphene/snapshot/*.hpp")

add_library( graphene_snapshot
snapshot.cpp
)

target_link_libraries( graphene_snapshot graphene_chain graphene_app )
target_include_directories( graphene_snapshot
PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" )

install( TARGETS
graphene_snapshot

RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
56 changes: 56 additions & 0 deletions libraries/plugins/snapshot/include/graphene/snapshot/snapshot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2017 Peter Conrad, and contributors.
*
* 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.
*/
#pragma once

#include <graphene/app/plugin.hpp>
#include <graphene/chain/database.hpp>

#include <fc/time.hpp>

namespace graphene { namespace snapshot_plugin {

class snapshot_plugin : public graphene::app::plugin {
public:
~snapshot_plugin() {}

std::string plugin_name()const override;

virtual void plugin_set_program_options(
boost::program_options::options_description &command_line_options,
boost::program_options::options_description &config_file_options
) override;

virtual void plugin_initialize( const boost::program_options::variables_map& options ) override;
virtual void plugin_startup() override;
virtual void plugin_shutdown() override;

private:
void check_snapshot( const graphene::chain::signed_block& b);

uint32_t snapshot_block = -1, last_block = 0;
fc::time_point_sec snapshot_time = fc::time_point_sec::maximum(), last_time = fc::time_point_sec(1);
fc::path dest;
};

} } //graphene::snapshot_plugin
123 changes: 123 additions & 0 deletions libraries/plugins/snapshot/snapshot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2017 Peter Conrad, and contributors.
*
* 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 <graphene/snapshot/snapshot.hpp>

#include <graphene/chain/database.hpp>

#include <fc/io/fstream.hpp>

using namespace graphene::snapshot_plugin;
using std::string;
using std::vector;

namespace bpo = boost::program_options;

static const char* OPT_BLOCK_NUM = "snapshot-at-block";
static const char* OPT_BLOCK_TIME = "snapshot-at-time";
static const char* OPT_DEST = "snapshot-to";

void snapshot_plugin::plugin_set_program_options(
boost::program_options::options_description& command_line_options,
boost::program_options::options_description& config_file_options)
{
command_line_options.add_options()
(OPT_BLOCK_NUM, bpo::value<uint32_t>(), "Block number after which to do a snapshot")
(OPT_BLOCK_TIME, bpo::value<string>(), "Block time (ISO format) after which to do a snapshot")
(OPT_DEST, bpo::value<string>(), "Pathname of JSON file where to store the snapshot")
;
config_file_options.add(command_line_options);
}

std::string snapshot_plugin::plugin_name()const
{
return "snapshot";
}

void snapshot_plugin::plugin_initialize(const boost::program_options::variables_map& options)
{ try {
ilog("snapshot plugin: plugin_initialize() begin");

if( options.count(OPT_BLOCK_NUM) || options.count(OPT_BLOCK_TIME) )
{
FC_ASSERT( options.count(OPT_DEST), "Must specify snapshot-to in addition to snapshot-at-block or snapshot-at-time!" );
dest = options[OPT_DEST].as<std::string>();
if( options.count(OPT_BLOCK_NUM) )
snapshot_block = options[OPT_BLOCK_NUM].as<uint32_t>();
if( options.count(OPT_BLOCK_TIME) )
snapshot_time = fc::time_point_sec::from_iso_string( options[OPT_BLOCK_TIME].as<std::string>() );
database().applied_block.connect( [&]( const graphene::chain::signed_block& b ) {
check_snapshot( b );
});
}
else
FC_ASSERT( !options.count("snapshot-to"), "Must specify snapshot-at-block or snapshot-at-time in addition to snapshot-to!" );
ilog("snapshot plugin: plugin_initialize() end");
} FC_LOG_AND_RETHROW() }

void snapshot_plugin::plugin_startup() {}

void snapshot_plugin::plugin_shutdown() {}

static void create_snapshot( const graphene::chain::database& db, const fc::path& dest )
{
ilog("snapshot plugin: creating snapshot");
fc::ofstream out;
try
{
out.open( dest );
}
catch ( fc::exception e )
{
wlog( "Failed to open snapshot destination: ${ex}", ("ex",e) );
return;
}
for( uint32_t space_id = 0; space_id < 256; space_id++ )
for( uint32_t type_id = 0; type_id < 256; type_id++ )
{
try
{
db.get_index( (uint8_t)space_id, (uint8_t)type_id );
}
catch (fc::assert_exception e)
{
continue;
}
auto& index = db.get_index( (uint8_t)space_id, (uint8_t)type_id );
index.inspect_all_objects( [&out]( const graphene::db::object& o ) {
out << fc::json::to_string( o.to_variant() ) << '\n';
});
}
out.close();
ilog("snapshot plugin: created snapshot");
}

void snapshot_plugin::check_snapshot( const graphene::chain::signed_block& b )
{ try {
uint32_t current_block = b.block_num();
if( (last_block < snapshot_block && snapshot_block <= current_block)
|| (last_time < snapshot_time && snapshot_time <= b.timestamp) )
create_snapshot( database(), dest );
last_block = current_block;
last_time = b.timestamp;
} FC_LOG_AND_RETHROW() }
2 changes: 1 addition & 1 deletion programs/witness_node/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif()

# We have to link against graphene_debug_witness because deficiency in our API infrastructure doesn't allow plugins to be fully abstracted #246
target_link_libraries( witness_node
PRIVATE graphene_app graphene_delayed_node graphene_account_history graphene_market_history graphene_witness graphene_chain graphene_debug_witness graphene_egenesis_full fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )
PRIVATE graphene_app graphene_delayed_node graphene_account_history graphene_market_history graphene_witness graphene_chain graphene_debug_witness graphene_egenesis_full graphene_snapshot fc ${CMAKE_DL_LIBS} ${PLATFORM_SPECIFIC_LIBS} )

install( TARGETS
witness_node
Expand Down
4 changes: 3 additions & 1 deletion programs/witness_node/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015 Cryptonomex, Inc., and contributors.
* Copyright (c) 2015-2017 Cryptonomex, Inc., and contributors.
*
* The MIT License
*
Expand Down Expand Up @@ -28,6 +28,7 @@
#include <graphene/account_history/account_history_plugin.hpp>
#include <graphene/market_history/market_history_plugin.hpp>
#include <graphene/delayed_node/delayed_node_plugin.hpp>
#include <graphene/snapshot/snapshot.hpp>

#include <fc/exception/exception.hpp>
#include <fc/thread/thread.hpp>
Expand Down Expand Up @@ -186,6 +187,7 @@ int main(int argc, char** argv) {
auto history_plug = node->register_plugin<account_history::account_history_plugin>();
auto market_history_plug = node->register_plugin<market_history::market_history_plugin>();
auto delayed_plug = node->register_plugin<delayed_node::delayed_node_plugin>();
auto snapshot_plug = node->register_plugin<snapshot_plugin::snapshot_plugin>();

try
{
Expand Down
Loading

0 comments on commit 1b8f9c6

Please sign in to comment.