BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
authority.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and 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
27 
28 namespace graphene { namespace protocol {
29 
34  struct authority
35  {
37  template<class ...Args>
38  authority(uint32_t threshhold, Args... auths)
39  : weight_threshold(threshhold)
40  {
41  add_authorities(auths...);
42  }
43 
44  enum classification
45  {
47  owner = 0,
49  active = 1,
50  key = 2
51  };
53  {
54  key_auths[k] = w;
55  }
56  void add_authority( const address& k, weight_type w )
57  {
58  address_auths[k] = w;
59  }
60  void add_authority( account_id_type k, weight_type w )
61  {
62  account_auths[k] = w;
63  }
64  bool is_impossible()const
65  {
66  uint64_t auth_weights = 0;
67  for( const auto& item : account_auths ) auth_weights += item.second;
68  for( const auto& item : key_auths ) auth_weights += item.second;
69  for( const auto& item : address_auths ) auth_weights += item.second;
70  return auth_weights < weight_threshold;
71  }
72 
73  template<typename AuthType>
74  void add_authorities(AuthType k, weight_type w)
75  {
76  add_authority(k, w);
77  }
78  template<typename AuthType, class ...Args>
79  void add_authorities(AuthType k, weight_type w, Args... auths)
80  {
81  add_authority(k, w);
82  add_authorities(auths...);
83  }
84 
85  vector<public_key_type> get_keys() const
86  {
87  vector<public_key_type> result;
88  result.reserve( key_auths.size() );
89  for( const auto& k : key_auths )
90  result.push_back(k.first);
91  return result;
92  }
93  vector<address> get_addresses() const
94  {
95  vector<address> result;
96  result.reserve( address_auths.size() );
97  for( const auto& k : address_auths )
98  result.push_back(k.first);
99  return result;
100  }
101 
102 
103  friend bool operator == ( const authority& a, const authority& b )
104  {
105  return (a.weight_threshold == b.weight_threshold) &&
106  (a.account_auths == b.account_auths) &&
107  (a.key_auths == b.key_auths) &&
108  (a.address_auths == b.address_auths);
109  }
110  friend bool operator!= ( const authority& a, const authority& b ) { return !(a==b); }
111  uint32_t num_auths()const { return account_auths.size() + key_auths.size() + address_auths.size(); }
112  void clear() { account_auths.clear(); key_auths.clear(); address_auths.clear(); weight_threshold = 0; }
113 
115  {
116  return authority( 1, GRAPHENE_NULL_ACCOUNT, 1 );
117  }
118 
119  uint32_t weight_threshold = 0;
120  flat_map<account_id_type,weight_type> account_auths;
121  flat_map<public_key_type,weight_type> key_auths;
123  flat_map<address,weight_type> address_auths;
124  };
125 
130  flat_set<account_id_type>& result,
131  const authority& a
132  );
133 
134 } } // namespace graphene::protocol
135 
136 FC_REFLECT( graphene::protocol::authority, (weight_threshold)(account_auths)(key_auths)(address_auths) )
138 
graphene::protocol::authority::weight_threshold
uint32_t weight_threshold
Definition: authority.hpp:119
graphene::protocol::add_authority_accounts
void add_authority_accounts(flat_set< account_id_type > &result, const authority &a)
Definition: authority.cpp:31
graphene::protocol::authority::address_auths
flat_map< address, weight_type > address_auths
Definition: authority.hpp:123
graphene::protocol::authority::add_authority
void add_authority(account_id_type k, weight_type w)
Definition: authority.hpp:60
graphene::protocol::authority::key_auths
flat_map< public_key_type, weight_type > key_auths
Definition: authority.hpp:121
graphene::protocol::public_key_type
Definition: types.hpp:312
graphene::protocol::authority::account_auths
flat_map< account_id_type, weight_type > account_auths
Definition: authority.hpp:120
graphene::protocol::authority::operator!=
friend bool operator!=(const authority &a, const authority &b)
Definition: authority.hpp:110
types.hpp
graphene::protocol::authority::add_authority
void add_authority(const address &k, weight_type w)
Definition: authority.hpp:56
graphene::protocol::authority::get_keys
vector< public_key_type > get_keys() const
Definition: authority.hpp:85
address.hpp
GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:85
graphene::protocol::authority::null_authority
static authority null_authority()
Definition: authority.hpp:114
graphene::protocol::authority::is_impossible
bool is_impossible() const
Definition: authority.hpp:64
graphene::protocol::weight_type
uint16_t weight_type
Definition: types.hpp:310
graphene::protocol::authority::num_auths
uint32_t num_auths() const
Definition: authority.hpp:111
graphene::protocol::authority::authority
authority(uint32_t threshhold, Args... auths)
Definition: authority.hpp:38
graphene::protocol::authority::add_authority
void add_authority(const public_key_type &k, weight_type w)
Definition: authority.hpp:52
graphene::protocol::authority::clear
void clear()
Definition: authority.hpp:112
FC_REFLECT_ENUM
FC_REFLECT_ENUM(graphene::net::core_message_type_enum,(trx_message_type)(block_message_type)(core_message_type_first)(item_ids_inventory_message_type)(blockchain_item_ids_inventory_message_type)(fetch_blockchain_item_ids_message_type)(fetch_items_message_type)(item_not_available_message_type)(hello_message_type)(connection_accepted_message_type)(connection_rejected_message_type)(address_request_message_type)(address_message_type)(closing_connection_message_type)(current_time_request_message_type)(current_time_reply_message_type)(check_firewall_message_type)(check_firewall_reply_message_type)(get_current_connections_request_message_type)(get_current_connections_reply_message_type)(core_message_type_last))(different_chain)(already_connected)(connected_to_self)(not_accepting_connections)(blocked)(invalid_hello_message)(client_too_old))(inbound)(outbound))(firewalled)(not_firewalled))(unable_to_connect)(connection_successful)) namespace std
Definition: core_messages.hpp:404
graphene::protocol::authority::key
@ key
Definition: authority.hpp:50
graphene::protocol::authority::add_authorities
void add_authorities(AuthType k, weight_type w, Args... auths)
Definition: authority.hpp:79
graphene::protocol::authority::operator==
friend bool operator==(const authority &a, const authority &b)
Definition: authority.hpp:103
graphene::protocol::authority::authority
authority()
Definition: authority.hpp:36
graphene::protocol::authority::owner
@ owner
Definition: authority.hpp:47
graphene::protocol::authority::classification
classification
Definition: authority.hpp:44
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
GRAPHENE_NULL_ACCOUNT
#define GRAPHENE_NULL_ACCOUNT
Represents the canonical account with NO authority (nobody can access funds in null account)
Definition: config.hpp:146
graphene::protocol::authority::get_addresses
vector< address > get_addresses() const
Definition: authority.hpp:93
graphene::protocol::authority
Identifies a weighted set of keys and accounts that must approve operations.
Definition: authority.hpp:34
graphene::protocol::authority::active
@ active
Definition: authority.hpp:49
graphene::protocol::authority::add_authorities
void add_authorities(AuthType k, weight_type w)
Definition: authority.hpp:74
graphene
Definition: api.cpp:48
graphene::protocol::address
a 160 bit hash of a public key
Definition: address.hpp:44