worker_object.hpp: Move worker objects into own header #466

This commit is contained in:
theoreticalbts 2016-01-08 11:39:01 -05:00
parent 07fb90a7ec
commit e9eb050c1e
8 changed files with 191 additions and 159 deletions

View file

@ -34,14 +34,14 @@
#include <graphene/chain/market_object.hpp>
#include <graphene/chain/transaction_object.hpp>
#include <graphene/chain/withdraw_permission_object.hpp>
#include <graphene/chain/worker_evaluator.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/confidential_evaluator.hpp>
#include <fc/crypto/hex.hpp>
#include <fc/smart_ref_impl.hpp>
namespace graphene { namespace app {
login_api::login_api(application& a)
:_app(a)
{

View file

@ -37,7 +37,7 @@
#include <graphene/chain/market_object.hpp>
#include <graphene/chain/operation_history_object.hpp>
#include <graphene/chain/proposal_object.hpp>
#include <graphene/chain/worker_evaluator.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/confidential_evaluator.hpp>

View file

@ -40,6 +40,7 @@
#include <graphene/chain/withdraw_permission_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/witness_schedule_object.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/account_evaluator.hpp>
#include <graphene/chain/asset_evaluator.hpp>

View file

@ -37,7 +37,7 @@
#include <graphene/chain/global_property_object.hpp>
#include <graphene/chain/vesting_balance_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/worker_evaluator.hpp>
#include <graphene/chain/worker_object.hpp>
namespace graphene { namespace chain {
@ -112,15 +112,15 @@ void database::pay_workers( share_type& budget )
get_index_type<worker_index>().inspect_all_objects([this, &active_workers](const object& o) {
const worker_object& w = static_cast<const worker_object&>(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;

View file

@ -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<worker_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<uint64_t>& 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<by_id>, member< object, object_id_type, &object::id > >,
ordered_non_unique< tag<by_account>, member< worker_object, account_id_type, &worker_object::worker_account > >,
ordered_unique< tag<by_vote_for>, member< worker_object, vote_id_type, &worker_object::vote_for > >,
ordered_unique< tag<by_vote_against>, member< worker_object, vote_id_type, &worker_object::vote_against > >
>
> worker_object_multi_index_type;
//typedef flat_index<worker_object> worker_index;
using worker_index = generic_index<worker_object, worker_object_multi_index_type>;
class worker_create_evaluator : public evaluator<worker_create_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)
)

View file

@ -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 <graphene/db/object.hpp>
#include <graphene/db/generic_index.hpp>
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<worker_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<by_id>, member< object, object_id_type, &object::id > >,
ordered_non_unique< tag<by_account>, member< worker_object, account_id_type, &worker_object::worker_account > >,
ordered_unique< tag<by_vote_for>, member< worker_object, vote_id_type, &worker_object::vote_for > >,
ordered_unique< tag<by_vote_against>, member< worker_object, vote_id_type, &worker_object::vote_against > >
>
> worker_object_multi_index_type;
//typedef flat_index<worker_object> worker_index;
using worker_index = generic_index<worker_object, worker_object_multi_index_type>;
} } // 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)
)

View file

@ -23,8 +23,11 @@
*/
#include <graphene/chain/database.hpp>
#include <graphene/chain/worker_evaluator.hpp>
#include <graphene/chain/vesting_balance_object.hpp>
#include <graphene/chain/account_object.hpp>
#include <graphene/chain/vesting_balance_object.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/protocol/vote.hpp>
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;

View file

@ -36,7 +36,7 @@
#include <graphene/chain/vesting_balance_object.hpp>
#include <graphene/chain/withdraw_permission_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/worker_evaluator.hpp>
#include <graphene/chain/worker_object.hpp>
#include <graphene/utilities/tempdir.hpp>