Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace smart_ref with shared_ptr #1556

Merged
merged 7 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libraries/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace detail {
auto nathan_key = fc::ecc::private_key::regenerate(fc::sha256::hash(string("nathan")));
dlog("Allocating all stake to ${key}", ("key", utilities::key_to_wif(nathan_key)));
graphene::chain::genesis_state_type initial_state;
initial_state.initial_parameters.current_fees = fee_schedule::get_default();//->set_all_fees(GRAPHENE_BLOCKCHAIN_PRECISION);
initial_state.initial_parameters.current_fees = std::make_shared<fee_schedule>(fee_schedule::get_default());
initial_state.initial_active_witnesses = GRAPHENE_DEFAULT_MIN_WITNESS_COUNT;
initial_state.initial_timestamp = time_point_sec(time_point::now().sec_since_epoch() /
initial_state.initial_parameters.block_interval *
Expand Down
1 change: 1 addition & 0 deletions libraries/chain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ add_library( graphene_chain
protocol/assert.cpp
protocol/account.cpp
protocol/transfer.cpp
protocol/chain_parameters.cpp
protocol/committee_member.cpp
protocol/witness.cpp
protocol/market.cpp
Expand Down
2 changes: 1 addition & 1 deletion libraries/chain/db_getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const dynamic_global_property_object& database::get_dynamic_global_properties()

const fee_schedule& database::current_fee_schedule()const
{
return get_global_properties().parameters.current_fees;
return *get_global_properties().parameters.current_fees;
}

time_point_sec database::head_block_time()const
Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/db_maint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,8 @@ void database::perform_chain_maintenance(const signed_block& next_block, const g
update_active_committee_members();
update_worker_votes();

const dynamic_global_property_object& dgpo = get_dynamic_global_properties();

const auto& dgpo = get_dynamic_global_properties();
modify(gpo, [&dgpo](global_property_object& p) {
// Remove scaling of account registration fee
p.parameters.current_fees->get<account_create_operation>().basic_fee >>= p.parameters.account_fee_scale_bitshifts *
Expand Down
3 changes: 0 additions & 3 deletions libraries/chain/genesis_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@

#include <graphene/chain/genesis_state.hpp>

// this is required to serialize a genesis_state
#include <graphene/chain/protocol/fee_schedule.hpp>

namespace graphene { namespace chain {

chain_id_type genesis_state_type::compute_chain_id() const
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
* THE SOFTWARE.
*/
#pragma once
#include <memory>
#include <graphene/chain/protocol/base.hpp>
#include <graphene/chain/protocol/types.hpp>
#include <fc/smart_ref_fwd.hpp>

namespace graphene { namespace chain { struct fee_schedule; } }

namespace graphene { namespace chain {

typedef static_variant<> parameter_extension;

struct fee_schedule;

struct chain_parameters
{
/** using a smart ref breaks the circular dependency created between operations and the fee schedule */
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
smart_ref<fee_schedule> current_fees; ///< current schedule of fees
std::shared_ptr<fee_schedule> current_fees; ///< current schedule of fees
uint8_t block_interval = GRAPHENE_DEFAULT_BLOCK_INTERVAL; ///< interval in seconds between blocks
uint32_t maintenance_interval = GRAPHENE_DEFAULT_MAINTENANCE_INTERVAL; ///< interval in sections between blockchain maintenance events
uint8_t maintenance_skip_slots = GRAPHENE_DEFAULT_MAINTENANCE_SKIP_SLOTS; ///< number of block_intervals to skip at maintenance time
Expand Down Expand Up @@ -67,6 +67,15 @@ namespace graphene { namespace chain {

/** defined in fee_schedule.cpp */
void validate()const;

chain_parameters();
chain_parameters(const chain_parameters& other);
chain_parameters(chain_parameters&& other);
chain_parameters& operator=(const chain_parameters& other);
chain_parameters& operator=(chain_parameters&& other);
pmconrad marked this conversation as resolved.
Show resolved Hide resolved

private:
static void safe_copy(chain_parameters& to, const chain_parameters& from);
};

} } // graphene::chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,17 @@ namespace graphene { namespace chain {
*/
flat_set<fee_parameters> parameters;
uint32_t scale = GRAPHENE_100_PERCENT; ///< fee * scale / GRAPHENE_100_PERCENT
private:
static void set_fee_parameters(fee_schedule& sched);
};

typedef fee_schedule fee_schedule_type;

} } // graphene::chain

namespace fc {
template<> struct get_typename<std::shared_ptr<graphene::chain::fee_schedule>> { static const char* name() { return "shared_ptr<fee_schedule>"; } };
}

FC_REFLECT_TYPENAME( graphene::chain::fee_parameters )
FC_REFLECT( graphene::chain::fee_schedule, (parameters)(scale) )
2 changes: 0 additions & 2 deletions libraries/chain/include/graphene/chain/protocol/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include <fc/io/raw.hpp>
#include <fc/uint128.hpp>
#include <fc/static_variant.hpp>
#include <fc/smart_ref_fwd.hpp>

#include <memory>
#include <vector>
Expand Down Expand Up @@ -68,7 +67,6 @@ namespace graphene { namespace chain {
using std::tie;
using std::make_pair;

using fc::smart_ref;
using fc::variant_object;
using fc::variant;
using fc::enum_type;
Expand Down
81 changes: 81 additions & 0 deletions libraries/chain/protocol/chain_parameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <graphene/chain/protocol/chain_parameters.hpp>
#include <graphene/chain/protocol/fee_schedule.hpp>

namespace graphene { namespace chain {
chain_parameters::chain_parameters() {
current_fees = std::make_shared<fee_schedule>();
}

// copy constructor
chain_parameters::chain_parameters(const chain_parameters& other)
{
current_fees = std::make_shared<fee_schedule>(*other.current_fees);
safe_copy(*this, other);
}

// copy assignment
chain_parameters& chain_parameters::operator=(const chain_parameters& other)
{
if (&other != this)
{
current_fees = std::make_shared<fee_schedule>(*other.current_fees);
safe_copy(*this, other);
}
return *this;
}

// copies the easy stuff
void chain_parameters::safe_copy(chain_parameters& to, const chain_parameters& from)
{
to.block_interval = from.block_interval;
to.maintenance_interval = from.maintenance_interval;
to.maintenance_skip_slots = from.maintenance_skip_slots;
to.committee_proposal_review_period = from.committee_proposal_review_period;
to.maximum_transaction_size = from.maximum_transaction_size;
to.maximum_block_size = from.maximum_block_size;
to.maximum_time_until_expiration = from.maximum_time_until_expiration;
to.maximum_proposal_lifetime = from.maximum_proposal_lifetime;
to.maximum_asset_whitelist_authorities = from.maximum_asset_whitelist_authorities;
to.maximum_asset_feed_publishers = from.maximum_asset_feed_publishers;
to.maximum_witness_count = from.maximum_witness_count;
to.maximum_committee_count = from.maximum_committee_count;
to.maximum_authority_membership = from.maximum_authority_membership;
to.reserve_percent_of_fee = from.reserve_percent_of_fee;
to.network_percent_of_fee = from.network_percent_of_fee;
to.lifetime_referrer_percent_of_fee = from.lifetime_referrer_percent_of_fee;
to.cashback_vesting_period_seconds = from.cashback_vesting_period_seconds;
to.cashback_vesting_threshold = from.cashback_vesting_threshold;
to.count_non_member_votes = from.count_non_member_votes;
to.allow_non_member_whitelists = from.allow_non_member_whitelists;
to.witness_pay_per_block = from.witness_pay_per_block;
to.witness_pay_vesting_seconds = from.witness_pay_vesting_seconds;
to.worker_budget_per_day = from.worker_budget_per_day;
to.max_predicate_opcode = from.max_predicate_opcode;
to.fee_liquidation_threshold = from.fee_liquidation_threshold;
to.accounts_per_fee_scale = from.accounts_per_fee_scale;
to.account_fee_scale_bitshifts = from.account_fee_scale_bitshifts;
to.max_authority_depth = from.max_authority_depth;
to.extensions = from.extensions;
}

// move constructor
chain_parameters::chain_parameters(chain_parameters&& other)
{
current_fees = other.current_fees;
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
other.current_fees = nullptr;
safe_copy(*this, other);
}

// move assignment
chain_parameters& chain_parameters::operator=(chain_parameters&& other)
{
if (&other != this)
{
current_fees = other.current_fees;
jmjatlanta marked this conversation as resolved.
Show resolved Hide resolved
other.current_fees = nullptr;
safe_copy(*this, other);
}
return *this;
}

}}
12 changes: 0 additions & 12 deletions libraries/chain/protocol/fee_schedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@
#include <algorithm>
#include <graphene/chain/protocol/fee_schedule.hpp>

namespace fc
{
// these are required on certain platforms in Release mode
template<>
bool smart_ref<graphene::chain::fee_schedule>::operator !()const
{
throw std::logic_error("Not Implemented");
}

template class smart_ref<graphene::chain::fee_schedule>;
}

#define MAX_FEE_STABILIZATION_ITERATION 4

namespace graphene { namespace chain {
Expand Down
9 changes: 4 additions & 5 deletions libraries/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,10 @@ class wallet_api_impl
return ob.template as<T>( GRAPHENE_MAX_NESTED_OBJECTS );
}

void set_operation_fees( signed_transaction& tx, const fee_schedule& s )
void set_operation_fees( signed_transaction& tx, const std::shared_ptr<fee_schedule> s )
{
for( auto& op : tx.operations )
s.set_fee(op);
s->set_fee(op);
}

variant info() const
Expand Down Expand Up @@ -1008,8 +1008,7 @@ class wallet_api_impl

tx.operations.push_back( account_create_op );

auto current_fees = _remote_db->get_global_properties().parameters.current_fees;
set_operation_fees( tx, current_fees );
set_operation_fees( tx, _remote_db->get_global_properties().parameters.current_fees );

vector<public_key_type> paying_keys = registrar_account_object.active.get_keys();

Expand Down Expand Up @@ -2541,7 +2540,7 @@ class wallet_api_impl
new_fees.scale = scale;

chain_parameters new_params = current_params;
new_params.current_fees = new_fees;
new_params.current_fees = std::make_shared<fee_schedule>(new_fees);

committee_member_update_global_parameters_operation update_op;
update_op.new_parameters = new_params;
Expand Down
9 changes: 0 additions & 9 deletions programs/js_operation_serializer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ struct js_name<fc::array<T,N>>
template<size_t N> struct js_name<fc::array<char,N>> { static std::string name(){ return "bytes "+ fc::to_string(N); }; };
template<size_t N> struct js_name<fc::array<uint8_t,N>> { static std::string name(){ return "bytes "+ fc::to_string(N); }; };
template<typename T> struct js_name< fc::optional<T> > { static std::string name(){ return "optional " + js_name<T>::name(); } };
template<typename T> struct js_name< fc::smart_ref<T> > { static std::string name(){ return js_name<T>::name(); } };
template<> struct js_name< object_id_type > { static std::string name(){ return "object_id_type"; } };
template<typename T> struct js_name< fc::flat_set<T> > { static std::string name(){ return "set " + js_name<T>::name(); } };
template<typename T> struct js_name< std::vector<T> > { static std::string name(){ return "array " + js_name<T>::name(); } };
Expand Down Expand Up @@ -236,14 +235,6 @@ struct serializer<std::vector<T>,false>
static void generate() {}
};

template<typename T>
struct serializer<fc::smart_ref<T>,false>
{
static void init() {
serializer<T>::init(); }
static void generate() {}
};

template<>
struct serializer<std::vector<operation>,false>
{
Expand Down
4 changes: 2 additions & 2 deletions tests/common/database_fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void database_fixture::change_fees(
new_fees.scale = new_scale;

chain_parameters new_chain_params = current_chain_params;
new_chain_params.current_fees = new_fees;
new_chain_params.current_fees = std::make_shared<fee_schedule>(new_fees);

db.modify(db.get_global_properties(), [&](global_property_object& p) {
p.parameters = new_chain_params;
Expand Down Expand Up @@ -991,7 +991,7 @@ void database_fixture::enable_fees()
{
db.modify(global_property_id_type()(db), [](global_property_object& gpo)
{
gpo.parameters.current_fees = fee_schedule::get_default();
gpo.parameters.current_fees = std::make_shared<fee_schedule>(fee_schedule::get_default());
});
}

Expand Down
17 changes: 9 additions & 8 deletions tests/tests/fee_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ BOOST_AUTO_TEST_CASE( cashback_test )
upgrade_to_lifetime_member(rog_id);

BOOST_TEST_MESSAGE("Enable fees");
const auto& fees = db.get_global_properties().parameters.current_fees;
const auto& fees = *db.get_global_properties().parameters.current_fees;

#define CustomRegisterActor(actor_name, registrar_name, referrer_name, referrer_rate) \
{ \
Expand All @@ -474,7 +474,7 @@ BOOST_AUTO_TEST_CASE( cashback_test )
op.options.memo_key = actor_name ## _private_key.get_public_key(); \
op.active = authority(1, public_key_type(actor_name ## _private_key.get_public_key()), 1); \
op.owner = op.active; \
op.fee = fees->calculate_fee(op); \
op.fee = fees.calculate_fee(op); \
trx.operations = {op}; \
sign( trx, registrar_name ## _private_key ); \
actor_name ## _id = PUSH_TX( db, trx ).operation_results.front().get<object_id_type>(); \
Expand All @@ -500,10 +500,10 @@ BOOST_AUTO_TEST_CASE( cashback_test )
CustomAuditActor( pleb ); \
}

int64_t reg_fee = fees->get< account_create_operation >().premium_fee;
int64_t xfer_fee = fees->get< transfer_operation >().fee;
int64_t upg_an_fee = fees->get< account_upgrade_operation >().membership_annual_fee;
int64_t upg_lt_fee = fees->get< account_upgrade_operation >().membership_lifetime_fee;
int64_t reg_fee = fees.get< account_create_operation >().premium_fee;
int64_t xfer_fee = fees.get< transfer_operation >().fee;
int64_t upg_an_fee = fees.get< account_upgrade_operation >().membership_annual_fee;
int64_t upg_lt_fee = fees.get< account_upgrade_operation >().membership_lifetime_fee;
// all percentages here are cut from whole pie!
uint64_t network_pct = 20 * P1;
uint64_t lt_pct = 375 * P100 / 1000;
Expand Down Expand Up @@ -709,7 +709,7 @@ BOOST_AUTO_TEST_CASE( account_create_fee_scaling )
auto accounts_per_scale = db.get_global_properties().parameters.accounts_per_fee_scale;
db.modify(global_property_id_type()(db), [](global_property_object& gpo)
{
gpo.parameters.current_fees = fee_schedule::get_default();
gpo.parameters.current_fees = std::make_shared<fee_schedule>(fee_schedule::get_default());
gpo.parameters.current_fees->get<account_create_operation>().basic_fee = 1;
});

Expand Down Expand Up @@ -3694,7 +3694,8 @@ BOOST_AUTO_TEST_CASE( issue_429_test )
// make sure the database requires our fee to be nonzero
enable_fees();

auto fees_to_pay = db.get_global_properties().parameters.current_fees->get<asset_create_operation>();
const auto& fees = *db.get_global_properties().parameters.current_fees;
auto fees_to_pay = fees.get<asset_create_operation>();

{
signed_transaction tx;
Expand Down