|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Blockchain Projects BV. |
| 3 | + * |
| 4 | + * The MIT License |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +#pragma once |
| 25 | +#include <boost/range/numeric.hpp> |
| 26 | +#include <boost/range/adaptor/map.hpp> |
| 27 | +#include <boost/container/flat_map.hpp> |
| 28 | +#include <boost/container/flat_set.hpp> |
| 29 | +#include <boost/multi_index/composite_key.hpp> |
| 30 | + |
| 31 | +#include <fc/optional.hpp> |
| 32 | + |
| 33 | +#include <graphene/chain/database.hpp> |
| 34 | +#include <graphene/voting_stat/voting_stat_plugin.hpp> |
| 35 | + |
| 36 | +#include <graphene/protocol/types.hpp> |
| 37 | +#include <graphene/protocol/vote.hpp> |
| 38 | + |
| 39 | +#include <graphene/db/generic_index.hpp> |
| 40 | + |
| 41 | +using graphene::chain::object_id_type; |
| 42 | +using graphene::chain::account_id_type; |
| 43 | +using graphene::chain::vote_id_type; |
| 44 | + |
| 45 | +using graphene::db::object; |
| 46 | +using graphene::db::abstract_object; |
| 47 | +using graphene::db::generic_index; |
| 48 | +using graphene::db::by_id; |
| 49 | + |
| 50 | +using namespace boost::multi_index; |
| 51 | +using boost::container::flat_map; |
| 52 | +using boost::container::flat_set; |
| 53 | + |
| 54 | +namespace graphene { namespace voting_stat { |
| 55 | + /** |
| 56 | + * @brief tracks the number maintenance interval occurences |
| 57 | + * @ingroup object |
| 58 | + * @ingroup voting_stat_plugin |
| 59 | + * |
| 60 | + * The number of maintenance intervals to be tracked is set in this object. Since a fork can occur during a |
| 61 | + * maintenance interval, it is not sufficient to track the number of intervals through a plugin internal |
| 62 | + * variable. In the case of a fork this object will be reverted together with the internal maintenance counter. |
| 63 | + * Through the lifetime of the plugin there will be only one of this objects. |
| 64 | + * |
| 65 | + * @note By default this object are not tracked, the voting_stat_plugin must be loaded for this object to |
| 66 | + * be maintained. |
| 67 | + */ |
| 68 | + class maintenance_counter_object : public abstract_object<maintenance_counter_object> |
| 69 | + { |
| 70 | + public: |
| 71 | + static const uint8_t space_id = VOTING_STAT_SPACE_ID; |
| 72 | + static const uint8_t type_id = voting_stat_object_type_ids::maintenance_counter_object_type_id; |
| 73 | + |
| 74 | + maintenance_counter_object(){} |
| 75 | + |
| 76 | + bool counter_reached( chain::database& db ) const |
| 77 | + { |
| 78 | + if( counter == max_counter ) |
| 79 | + { |
| 80 | + db.modify<maintenance_counter_object>( *this, [](maintenance_counter_object& o) { |
| 81 | + o.counter = 0; |
| 82 | + }); |
| 83 | + return true; |
| 84 | + } |
| 85 | + db.modify<maintenance_counter_object>( *this, [](maintenance_counter_object& o) { |
| 86 | + o.counter += 1; |
| 87 | + }); |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + uint16_t max_counter = 12; // every 12th maintenance interval vote*_objects will be created |
| 92 | + uint16_t counter = 12; |
| 93 | + }; |
| 94 | + |
| 95 | + typedef multi_index_container< maintenance_counter_object, |
| 96 | + indexed_by< |
| 97 | + ordered_unique< tag<by_id>, |
| 98 | + member< object, object_id_type, &object::id > |
| 99 | + > |
| 100 | + > |
| 101 | + > maintenance_counter_multi_index_type; |
| 102 | + |
| 103 | + typedef generic_index< |
| 104 | + maintenance_counter_object, maintenance_counter_multi_index_type > maintenance_counter_index; |
| 105 | + |
| 106 | +}} // graphene::chain |
| 107 | + |
| 108 | +FC_REFLECT_DERIVED( graphene::voting_stat::maintenance_counter_object, (graphene::chain::object), |
| 109 | + (max_counter)(counter) ) |
0 commit comments