BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
custom_authority_object.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Contributors.
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
29 #include <graphene/chain/types.hpp>
30 #include <boost/multi_index/composite_key.hpp>
31 
32 namespace graphene { namespace chain {
33 
39  class custom_authority_object : public abstract_object<custom_authority_object,
40  protocol_ids, custom_authority_object_type>
41  {
44  mutable optional<restriction_predicate_function> predicate_cache;
45 
46  public:
47  account_id_type account;
48  bool enabled;
49  time_point_sec valid_from;
50  time_point_sec valid_to;
53  flat_map<uint16_t, restriction> restrictions;
54  uint16_t restriction_counter = 0;
55 
57  bool is_valid(time_point_sec now) const { return enabled && now >= valid_from && now < valid_to; }
58 
60  vector<restriction> get_restrictions() const {
61  vector<restriction> rs;
62  std::transform(restrictions.begin(), restrictions.end(),
63  std::back_inserter(rs), [](auto i) { return i.second; });
64  return rs;
65  }
68  if (!predicate_cache.valid())
70 
71  return *predicate_cache;
72  }
74  void update_predicate_cache() const {
75  predicate_cache = get_restriction_predicate(get_restrictions(), operation_type);
76  }
78  void clear_predicate_cache() { predicate_cache.reset(); }
79  };
80 
81  struct by_account_custom;
82  struct by_expiration;
83 
87  typedef multi_index_container<
89  indexed_by<
90  ordered_unique<tag<by_id>, member<object, object_id_type, &object::id>>,
91  ordered_unique<tag<by_account_custom>,
92  composite_key<custom_authority_object,
93  member<custom_authority_object, account_id_type, &custom_authority_object::account>,
94  member<custom_authority_object, unsigned_int, &custom_authority_object::operation_type>,
95  member<custom_authority_object, bool, &custom_authority_object::enabled>,
96  member<object, object_id_type, &object::id>
97  >>,
98  ordered_unique<tag<by_expiration>,
99  composite_key<custom_authority_object,
100  member<custom_authority_object, time_point_sec, &custom_authority_object::valid_to>,
101  member<object, object_id_type, &object::id>
102  >
103  >
104  >
106 
111 
112 } } // graphene::chain
113 
115 
117 
Identifies a weighted set of keys and accounts that must approve operations.
Definition: authority.hpp:34
Definition: api.cpp:48
restriction_predicate_function get_restriction_predicate(vector< restriction > rs, operation::tag_type op_type)
get_restriction_predicate Get a predicate function for the supplied restriction
#define MAP_OBJECT_ID_TO_TYPE(OBJECT)
Definition: object_id.hpp:93
void clear_predicate_cache()
Clear the cache of the predicate function.
Tracks account custom authorities.
FC_REFLECT_TYPENAME(fc::log_message)
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
std::function< predicate_result(const operation &)> restriction_predicate_function
A restriction predicate is a function accepting an operation and returning a predicate_result.
flat_map< uint16_t, restriction > restrictions
multi_index_container< custom_authority_object, indexed_by< ordered_unique< tag< by_id >, member< object, object_id_type, &object::id > >, ordered_unique< tag< by_account_custom >, composite_key< custom_authority_object, member< custom_authority_object, account_id_type, &custom_authority_object::account >, member< custom_authority_object, unsigned_int, &custom_authority_object::operation_type >, member< custom_authority_object, bool, &custom_authority_object::enabled >, member< object, object_id_type, &object::id > > >, ordered_unique< tag< by_expiration >, composite_key< custom_authority_object, member< custom_authority_object, time_point_sec, &custom_authority_object::valid_to >, member< object, object_id_type, &object::id > > > > > custom_authority_multi_index_type
typename impl::transform< List, Transformer >::type transform
Transform elements of a typelist.
Definition: typelist.hpp:170
restriction_predicate_function get_predicate() const
Get predicate, from cache if possible, and update cache if not (modifies const object!) ...
void update_predicate_cache() const
Regenerate predicate function and update predicate cache.
bool is_valid(time_point_sec now) const
Check whether the custom authority is valid.
vector< restriction > get_restrictions() const
Get the restrictions as a vector rather than a map.