Skip to content

Commit

Permalink
maint counter object added, changed plugin accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimfred committed Oct 21, 2019
1 parent 72587d3 commit c4a679d
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2019 Blockchain Projects BV.
*
* 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 <boost/range/numeric.hpp>
#include <boost/range/adaptor/map.hpp>
#include <boost/container/flat_map.hpp>
#include <boost/container/flat_set.hpp>
#include <boost/multi_index/composite_key.hpp>

#include <fc/optional.hpp>

#include <graphene/chain/database.hpp>
#include <graphene/voting_stat/voting_stat_plugin.hpp>

#include <graphene/protocol/types.hpp>
#include <graphene/protocol/vote.hpp>

#include <graphene/db/generic_index.hpp>

using graphene::chain::object_id_type;
using graphene::chain::account_id_type;
using graphene::chain::vote_id_type;

using graphene::db::object;
using graphene::db::abstract_object;
using graphene::db::generic_index;
using graphene::db::by_id;

using namespace boost::multi_index;
using boost::container::flat_map;
using boost::container::flat_set;

namespace graphene { namespace voting_stat {
/**
* @brief tracks the number maintenance interval occurences
* @ingroup object
* @ingroup voting_stat_plugin
*
* The number of maintenance intervals to be tracked is set in this object. Since a fork can occur during a
* maintenance interval, it is not sufficient to track the number of intervals through a plugin internal
* variable. In the case of a fork this object will be reverted together with the internal maintenance counter.
* Through the lifetime of the plugin there will be only one of this objects.
*
* @note By default this object are not tracked, the voting_stat_plugin must be loaded for this object to
* be maintained.
*/
class maintenance_counter_object : public abstract_object<maintenance_counter_object>
{
public:
static const uint8_t space_id = VOTING_STAT_SPACE_ID;
static const uint8_t type_id = voting_stat_object_type_ids::maintenance_counter_object_type_id;

maintenance_counter_object(){}

bool counter_reached( chain::database& db ) const
{
if( counter == max_counter )
{
db.modify<maintenance_counter_object>( *this, [](maintenance_counter_object& o) {
o.counter = 0;
});
return true;
}
db.modify<maintenance_counter_object>( *this, [](maintenance_counter_object& o) {
o.counter += 1;
});
return false;
}

uint16_t max_counter = 12; // every 12th maintenance interval vote*_objects will be created
uint16_t counter = 12;
};

typedef multi_index_container< maintenance_counter_object,
indexed_by<
ordered_unique< tag<by_id>,
member< object, object_id_type, &object::id >
>
>
> maintenance_counter_multi_index_type;

typedef generic_index<
maintenance_counter_object, maintenance_counter_multi_index_type > maintenance_counter_index;

}} // graphene::chain

FC_REFLECT_DERIVED( graphene::voting_stat::maintenance_counter_object, (graphene::chain::object),
(max_counter)(counter) )
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ namespace graphene { namespace voting_stat {
enum voting_stat_object_type_ids
{
voting_statistics_object_type_id,
voteable_statistics_object_type_id
voteable_statistics_object_type_id,
maintenance_counter_object_type_id
};

namespace detail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace graphene { namespace voting_stat {
/**
* @brief tracks the history of the voting stake for an account
* @ingroup object
* @ingroup implementation
* @ingroup voting_stat
*
* The calculation of the voting stake, performed in the maintenance interval, results in the creation or,
* if present, in the update of a voting_statistics_object.
Expand Down
45 changes: 31 additions & 14 deletions libraries/plugins/voting_stat/voting_stat_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

#include <graphene/voting_stat/voting_statistics_object.hpp>
#include <graphene/voting_stat/voteable_statistics_object.hpp>
#include <graphene/voting_stat/maintenance_counter_object.hpp>

#include <graphene/chain/worker_object.hpp>
#include <graphene/chain/witness_object.hpp>
#include <graphene/chain/committee_member_object.hpp>
Expand Down Expand Up @@ -52,13 +54,10 @@ class voting_stat_plugin_impl
boost::signals2::connection _on_voting_stake_calc_conn;
std::unique_ptr<boost::signals2::shared_connection_block> _on_voting_stake_calc_block;

uint16_t _maint_counter = 0;

/**
* plugin parameters
*/
bool _keep_objects_in_db = true;
uint16_t _track_every_x_maint = 12;
bool _track_worker_votes = true;
bool _track_witness_votes = true;
bool _track_committee_votes = true;
Expand Down Expand Up @@ -108,14 +107,14 @@ void voting_stat_plugin_impl::on_maintenance_begin(uint32_t block_num)
if( !_keep_objects_in_db )
delete_all_statistics_objects();

if( _maint_counter == _track_every_x_maint )
auto& db = database();
const auto& maint_counter_obj = *db.get_index_type<maintenance_counter_index>().indices().get<by_id>().begin();
if( maint_counter_obj.counter_reached( db ) )
{
_on_voting_stake_calc_block->unblock();
_maint_counter = 0;
_maint_block = block_num;
_create_voteable = true;
}
++_maint_counter;
}

void voting_stat_plugin_impl::on_maintenance_end()
Expand Down Expand Up @@ -147,8 +146,6 @@ void voting_stat_plugin_impl::create_voteable_statistics_objects()
{
auto& db = database();

// TODO secondary index for workers where current_time < worker_end_time
// will reduce the iteration time
if( _track_worker_votes )
{
const auto& worker_idx = db.get_index_type<worker_index>().indices().get<by_id>();
Expand Down Expand Up @@ -326,13 +323,8 @@ void voting_stat_plugin::plugin_initialize(const boost::program_options::variabl
auto& db = database();
db.add_index< primary_index<voting_statistics_index> >();
db.add_index< primary_index<voteable_statistics_index> >();
db.add_index< primary_index<maintenance_counter_index> >();

if( options.count("voting-stat-track-every-x-maint") ){
my->_track_every_x_maint = options["voting-stat-track-every-x-maint"].as<uint16_t>();
if( my->_track_every_x_maint == 0 )
my->_track_every_x_maint = 1;
my->_maint_counter = my->_track_every_x_maint;
}
if( options.count("voting-stat-keep-objects-in-db") ){
my->_keep_objects_in_db = options["voting-stat-keep-objects-in-db"].as<bool>();
}
Expand All @@ -345,6 +337,31 @@ void voting_stat_plugin::plugin_initialize(const boost::program_options::variabl
if( options.count("voting-stat-track-committee-votes") ){
my->_track_committee_votes = options["voting-stat-track-committee-votes"].as<bool>();
}
uint16_t track_every_x_maint = 12;
if( options.count("voting-stat-track-every-x-maint") )
{
track_every_x_maint = options["voting-stat-track-every-x-maint"].as<uint16_t>();
if( track_every_x_maint == 0 )
track_every_x_maint = 1;
}
const auto& maint_counter_idx = db.get_index_type<maintenance_counter_index>().indices().get<by_id>();
if( maint_counter_idx.empty() )
{
db.create<maintenance_counter_object>( [track_every_x_maint]( maintenance_counter_object& o ) {
o.max_counter = track_every_x_maint;
o.counter = track_every_x_maint;
});
}
else
{
db.modify<maintenance_counter_object>( *maint_counter_idx.begin(),
[track_every_x_maint]( maintenance_counter_object& o )
{
o.max_counter = track_every_x_maint;
o.counter = track_every_x_maint;
}
);
}

my->_on_voting_stake_calc_conn = db.on_voting_stake_calculated.connect(
[&]( const account_object& stake_account, const account_object& proxy_account, const uint64_t stake ){
Expand Down

0 comments on commit c4a679d

Please sign in to comment.