From e9eb050c1ef5e95075e8605d9c261fd785e143ba Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Fri, 8 Jan 2016 11:39:01 -0500 Subject: [PATCH] worker_object.hpp: Move worker objects into own header #466 --- libraries/app/api.cpp | 4 +- .../app/include/graphene/app/database_api.hpp | 2 +- libraries/chain/db_init.cpp | 1 + libraries/chain/db_maint.cpp | 8 +- .../graphene/chain/worker_evaluator.hpp | 149 --------------- .../include/graphene/chain/worker_object.hpp | 177 ++++++++++++++++++ libraries/chain/worker_evaluator.cpp | 7 +- tests/tests/operation_tests2.cpp | 2 +- 8 files changed, 191 insertions(+), 159 deletions(-) create mode 100644 libraries/chain/include/graphene/chain/worker_object.hpp diff --git a/libraries/app/api.cpp b/libraries/app/api.cpp index 451bc027..facb97c0 100644 --- a/libraries/app/api.cpp +++ b/libraries/app/api.cpp @@ -34,14 +34,14 @@ #include #include #include -#include +#include #include #include #include namespace graphene { namespace app { - + login_api::login_api(application& a) :_app(a) { diff --git a/libraries/app/include/graphene/app/database_api.hpp b/libraries/app/include/graphene/app/database_api.hpp index c3bb0325..c896ee9e 100644 --- a/libraries/app/include/graphene/app/database_api.hpp +++ b/libraries/app/include/graphene/app/database_api.hpp @@ -37,7 +37,7 @@ #include #include #include -#include +#include #include #include diff --git a/libraries/chain/db_init.cpp b/libraries/chain/db_init.cpp index ca5d0f18..359e29ec 100644 --- a/libraries/chain/db_init.cpp +++ b/libraries/chain/db_init.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include diff --git a/libraries/chain/db_maint.cpp b/libraries/chain/db_maint.cpp index bf5453fd..58ce0b7a 100644 --- a/libraries/chain/db_maint.cpp +++ b/libraries/chain/db_maint.cpp @@ -37,7 +37,7 @@ #include #include #include -#include +#include namespace graphene { namespace chain { @@ -112,15 +112,15 @@ void database::pay_workers( share_type& budget ) get_index_type().inspect_all_objects([this, &active_workers](const object& o) { const worker_object& w = static_cast(o); auto now = head_block_time(); - if( w.is_active(now) && w.approving_stake(_vote_tally_buffer) > 0 ) + if( w.is_active(now) && w.approving_stake() > 0 ) active_workers.emplace_back(w); }); // worker with more votes is preferred // if two workers exactly tie for votes, worker with lower ID is preferred std::sort(active_workers.begin(), active_workers.end(), [this](const worker_object& wa, const worker_object& wb) { - share_type wa_vote = wa.approving_stake(_vote_tally_buffer); - share_type wb_vote = wb.approving_stake(_vote_tally_buffer); + share_type wa_vote = wa.approving_stake(); + share_type wb_vote = wb.approving_stake(); if( wa_vote != wb_vote ) return wa_vote > wb_vote; return wa.id < wb.id; diff --git a/libraries/chain/include/graphene/chain/worker_evaluator.hpp b/libraries/chain/include/graphene/chain/worker_evaluator.hpp index 77d18c54..d64f62f4 100644 --- a/libraries/chain/include/graphene/chain/worker_evaluator.hpp +++ b/libraries/chain/include/graphene/chain/worker_evaluator.hpp @@ -26,137 +26,6 @@ namespace graphene { namespace chain { - /** - * @defgroup worker_types Implementations of the various worker types in the system - * - * The system has various worker types, which do different things with the money they are paid. These worker types - * and their semantics are specified here. - * - * All worker types exist as a struct containing the data this worker needs to evaluate, as well as a method - * pay_worker, which takes a pay amount and a non-const database reference, and applies the worker's specific pay - * semantics to the worker_type struct and/or the database. Furthermore, all worker types have an initializer, - * which is a struct containing the data needed to create that kind of worker. - * - * Each initializer type has a method, init, which takes a non-const database reference, a const reference to the - * worker object being created, and a non-const reference to the specific *_worker_type object to initialize. The - * init method creates any further objects, and initializes the worker_type object as necessary according to the - * semantics of that particular worker type. - * - * To create a new worker type, define a my_new_worker_type struct with a pay_worker method which updates the - * my_new_worker_type object and/or the database. Create a my_new_worker_type::initializer struct with an init - * method and any data members necessary to create a new worker of this type. Reflect my_new_worker_type and - * my_new_worker_type::initializer into FC's type system, and add them to @ref worker_type and @ref - * worker_initializer respectively. Make sure the order of types in @ref worker_type and @ref worker_initializer - * remains the same. - * @{ - */ - /** - * @brief A worker who returns all of his pay to the reserve - * - * This worker type pays everything he receives back to the network's reserve funds pool. - */ - struct refund_worker_type - { - /// Record of how much this worker has burned in his lifetime - share_type total_burned; - - void pay_worker(share_type pay, database&); - }; - - /** - * @brief A worker who sends his pay to a vesting balance - * - * This worker type takes all of his pay and places it into a vesting balance - */ - struct vesting_balance_worker_type - { - /// The balance this worker pays into - vesting_balance_id_type balance; - - void pay_worker(share_type pay, database& db); - }; - - /** - * @brief A worker who permanently destroys all of his pay - * - * This worker sends all pay he receives to the null account. - */ - struct burn_worker_type - { - /// Record of how much this worker has burned in his lifetime - share_type total_burned; - - void pay_worker(share_type pay, database&); - }; - ///@} - - // The ordering of types in these two static variants MUST be the same. - typedef static_variant< - refund_worker_type, - vesting_balance_worker_type, - burn_worker_type - > worker_type; - - - /** - * @brief Worker object contains the details of a blockchain worker. See @ref workers for details. - */ - class worker_object : public abstract_object - { - public: - static const uint8_t space_id = protocol_ids; - static const uint8_t type_id = worker_object_type; - - /// ID of the account which owns this worker - account_id_type worker_account; - /// Time at which this worker begins receiving pay, if elected - time_point_sec work_begin_date; - /// Time at which this worker will cease to receive pay. Worker will be deleted at this time - time_point_sec work_end_date; - /// Amount in CORE this worker will be paid each day - share_type daily_pay; - /// ID of this worker's pay balance - worker_type worker; - /// Human-readable name for the worker - string name; - /// URL to a web page representing this worker - string url; - - /// Voting ID which represents approval of this worker - vote_id_type vote_for; - /// Voting ID which represents disapproval of this worker - vote_id_type vote_against; - - uint64_t total_votes_for = 0; - uint64_t total_votes_against = 0; - - bool is_active(fc::time_point_sec now)const { - return now >= work_begin_date && now <= work_end_date; - } - - /// TODO: remove unused argument - share_type approving_stake(const vector& stake_vote_tallies)const { - return int64_t( total_votes_for ) - int64_t( total_votes_against ); - } - }; - - - struct by_account; - struct by_vote_for; - struct by_vote_against; - typedef multi_index_container< - worker_object, - indexed_by< - ordered_unique< tag, member< object, object_id_type, &object::id > >, - ordered_non_unique< tag, member< worker_object, account_id_type, &worker_object::worker_account > >, - ordered_unique< tag, member< worker_object, vote_id_type, &worker_object::vote_for > >, - ordered_unique< tag, member< worker_object, vote_id_type, &worker_object::vote_against > > - > - > worker_object_multi_index_type; - - //typedef flat_index worker_index; - using worker_index = generic_index; - class worker_create_evaluator : public evaluator { public: @@ -167,21 +36,3 @@ namespace graphene { namespace chain { }; } } // graphene::chain - -FC_REFLECT( graphene::chain::refund_worker_type, (total_burned) ) -FC_REFLECT( graphene::chain::vesting_balance_worker_type, (balance) ) -FC_REFLECT( graphene::chain::burn_worker_type, (total_burned) ) -FC_REFLECT_TYPENAME( graphene::chain::worker_type ) -FC_REFLECT_DERIVED( graphene::chain::worker_object, (graphene::db::object), - (worker_account) - (work_begin_date) - (work_end_date) - (daily_pay) - (worker) - (vote_for) - (vote_against) - (total_votes_for) - (total_votes_against) - (name) - (url) - ) diff --git a/libraries/chain/include/graphene/chain/worker_object.hpp b/libraries/chain/include/graphene/chain/worker_object.hpp new file mode 100644 index 00000000..1219fc1c --- /dev/null +++ b/libraries/chain/include/graphene/chain/worker_object.hpp @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., 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 +#include + +namespace graphene { namespace chain { + +/** + * @defgroup worker_types Implementations of the various worker types in the system + * + * The system has various worker types, which do different things with the money they are paid. These worker types + * and their semantics are specified here. + * + * All worker types exist as a struct containing the data this worker needs to evaluate, as well as a method + * pay_worker, which takes a pay amount and a non-const database reference, and applies the worker's specific pay + * semantics to the worker_type struct and/or the database. Furthermore, all worker types have an initializer, + * which is a struct containing the data needed to create that kind of worker. + * + * Each initializer type has a method, init, which takes a non-const database reference, a const reference to the + * worker object being created, and a non-const reference to the specific *_worker_type object to initialize. The + * init method creates any further objects, and initializes the worker_type object as necessary according to the + * semantics of that particular worker type. + * + * To create a new worker type, define a my_new_worker_type struct with a pay_worker method which updates the + * my_new_worker_type object and/or the database. Create a my_new_worker_type::initializer struct with an init + * method and any data members necessary to create a new worker of this type. Reflect my_new_worker_type and + * my_new_worker_type::initializer into FC's type system, and add them to @ref worker_type and @ref + * worker_initializer respectively. Make sure the order of types in @ref worker_type and @ref worker_initializer + * remains the same. + * @{ + */ +/** + * @brief A worker who returns all of his pay to the reserve + * + * This worker type pays everything he receives back to the network's reserve funds pool. + */ +struct refund_worker_type +{ + /// Record of how much this worker has burned in his lifetime + share_type total_burned; + + void pay_worker(share_type pay, database&); +}; + +/** + * @brief A worker who sends his pay to a vesting balance + * + * This worker type takes all of his pay and places it into a vesting balance + */ +struct vesting_balance_worker_type +{ + /// The balance this worker pays into + vesting_balance_id_type balance; + + void pay_worker(share_type pay, database& db); +}; + +/** + * @brief A worker who permanently destroys all of his pay + * + * This worker sends all pay he receives to the null account. + */ +struct burn_worker_type +{ + /// Record of how much this worker has burned in his lifetime + share_type total_burned; + + void pay_worker(share_type pay, database&); +}; +///@} + +// The ordering of types in these two static variants MUST be the same. +typedef static_variant< + refund_worker_type, + vesting_balance_worker_type, + burn_worker_type +> worker_type; + + +/** + * @brief Worker object contains the details of a blockchain worker. See @ref workers for details. + */ +class worker_object : public abstract_object +{ + public: + static const uint8_t space_id = protocol_ids; + static const uint8_t type_id = worker_object_type; + + /// ID of the account which owns this worker + account_id_type worker_account; + /// Time at which this worker begins receiving pay, if elected + time_point_sec work_begin_date; + /// Time at which this worker will cease to receive pay. Worker will be deleted at this time + time_point_sec work_end_date; + /// Amount in CORE this worker will be paid each day + share_type daily_pay; + /// ID of this worker's pay balance + worker_type worker; + /// Human-readable name for the worker + string name; + /// URL to a web page representing this worker + string url; + + /// Voting ID which represents approval of this worker + vote_id_type vote_for; + /// Voting ID which represents disapproval of this worker + vote_id_type vote_against; + + uint64_t total_votes_for = 0; + uint64_t total_votes_against = 0; + + bool is_active(fc::time_point_sec now)const { + return now >= work_begin_date && now <= work_end_date; + } + + share_type approving_stake()const { + return int64_t( total_votes_for ) - int64_t( total_votes_against ); + } +}; + +struct by_account; +struct by_vote_for; +struct by_vote_against; +typedef multi_index_container< + worker_object, + indexed_by< + ordered_unique< tag, member< object, object_id_type, &object::id > >, + ordered_non_unique< tag, member< worker_object, account_id_type, &worker_object::worker_account > >, + ordered_unique< tag, member< worker_object, vote_id_type, &worker_object::vote_for > >, + ordered_unique< tag, member< worker_object, vote_id_type, &worker_object::vote_against > > + > +> worker_object_multi_index_type; + +//typedef flat_index worker_index; +using worker_index = generic_index; + +} } // graphene::chain + +FC_REFLECT( graphene::chain::refund_worker_type, (total_burned) ) +FC_REFLECT( graphene::chain::vesting_balance_worker_type, (balance) ) +FC_REFLECT( graphene::chain::burn_worker_type, (total_burned) ) +FC_REFLECT_TYPENAME( graphene::chain::worker_type ) +FC_REFLECT_DERIVED( graphene::chain::worker_object, (graphene::db::object), + (worker_account) + (work_begin_date) + (work_end_date) + (daily_pay) + (worker) + (vote_for) + (vote_against) + (total_votes_for) + (total_votes_against) + (name) + (url) + ) diff --git a/libraries/chain/worker_evaluator.cpp b/libraries/chain/worker_evaluator.cpp index 9592f238..cf6f0e00 100644 --- a/libraries/chain/worker_evaluator.cpp +++ b/libraries/chain/worker_evaluator.cpp @@ -23,8 +23,11 @@ */ #include #include -#include + #include +#include +#include + #include namespace graphene { namespace chain { @@ -40,7 +43,7 @@ void_result worker_create_evaluator::do_evaluate(const worker_create_evaluator:: } FC_CAPTURE_AND_RETHROW( (o) ) } -struct worker_init_visitor +struct worker_init_visitor { typedef void result_type; diff --git a/tests/tests/operation_tests2.cpp b/tests/tests/operation_tests2.cpp index d7b316c6..e13eda19 100644 --- a/tests/tests/operation_tests2.cpp +++ b/tests/tests/operation_tests2.cpp @@ -36,7 +36,7 @@ #include #include #include -#include +#include #include