BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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 
138 
friend bool operator==(const authority &a, const authority &b)
Definition: authority.hpp:103
Identifies a weighted set of keys and accounts that must approve operations.
Definition: authority.hpp:34
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
void add_authority_accounts(flat_set< account_id_type > &result, const authority &a)
Definition: authority.cpp:31
Definition: api.cpp:56
flat_map< address, weight_type > address_auths
Definition: authority.hpp:123
authority(uint32_t threshhold, Args...auths)
Definition: authority.hpp:38
static authority null_authority()
Definition: authority.hpp:114
friend bool operator!=(const authority &a, const authority &b)
Definition: authority.hpp:110
flat_map< public_key_type, weight_type > key_auths
Definition: authority.hpp:121
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
vector< address > get_addresses() const
Definition: authority.hpp:93
void add_authorities(AuthType k, weight_type w, Args...auths)
Definition: authority.hpp:79
#define GRAPHENE_DECLARE_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:73
void add_authority(const address &k, weight_type w)
Definition: authority.hpp:56
flat_map< account_id_type, weight_type > account_auths
Definition: authority.hpp:120
#define GRAPHENE_NULL_ACCOUNT
Represents the canonical account with NO authority (nobody can access funds in null account) ...
Definition: config.hpp:131
vector< public_key_type > get_keys() const
Definition: authority.hpp:85
void add_authority(const public_key_type &k, weight_type w)
Definition: authority.hpp:52
uint16_t weight_type
Definition: types.hpp:248
a 160 bit hash of a public key
Definition: address.hpp:44
void add_authorities(AuthType k, weight_type w)
Definition: authority.hpp:74
uint32_t num_auths() const
Definition: authority.hpp:111
void add_authority(account_id_type k, weight_type w)
Definition: authority.hpp:60