-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauthority.cpp
More file actions
119 lines (104 loc) · 3.48 KB
/
authority.cpp
File metadata and controls
119 lines (104 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <steemit/protocol/authority.hpp>
namespace steemit { namespace protocol {
// authority methods
void authority::add_authority( const public_key_type& k, weight_type w )
{
key_auths[k] = w;
}
void authority::add_authority( const account_name_type& k, weight_type w )
{
account_auths[k] = w;
}
vector< public_key_type > authority::get_keys()const
{
vector< public_key_type > result;
result.reserve( key_auths.size() );
for( const auto& k : key_auths )
result.push_back(k.first);
return result;
}
bool authority::is_impossible()const
{
uint64_t auth_weights = 0;
for( const auto& item : account_auths ) auth_weights += item.second;
for( const auto& item : key_auths ) auth_weights += item.second;
return auth_weights < weight_threshold;
}
uint32_t authority::num_auths()const { return account_auths.size() + key_auths.size(); }
void authority::clear() { account_auths.clear(); key_auths.clear(); }
void authority::validate()const
{
for( const auto& item : account_auths )
{
FC_ASSERT( is_valid_account_name( item.first ) );
}
}
bool is_valid_account_name( const string& name )
{
#if STEEMIT_MIN_ACCOUNT_NAME_LENGTH < 3
#error This is_valid_account_name implementation implicitly enforces minimum name length of 3.
#endif
const size_t len = name.size();
if( len < STEEMIT_MIN_ACCOUNT_NAME_LENGTH )
return false;
if( len > STEEMIT_MAX_ACCOUNT_NAME_LENGTH )
return false;
size_t begin = 0;
while( true )
{
size_t end = name.find_first_of( '.', begin );
if( end == std::string::npos )
end = len;
if( end - begin < 3 )
return false;
switch( name[begin] )
{
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p':
case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
break;
default:
return false;
}
switch( name[end-1] )
{
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p':
case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
case '8': case '9':
break;
default:
return false;
}
for( size_t i=begin+1; i<end-1; i++ )
{
switch( name[i] )
{
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p':
case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x':
case 'y': case 'z':
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
case '8': case '9':
case '-':
break;
default:
return false;
}
}
if( end == len )
break;
begin = end+1;
}
return true;
}
bool operator == ( const authority& a, const authority& b )
{
return ( a.weight_threshold == b.weight_threshold ) &&
( a.account_auths == b.account_auths ) &&
( a.key_auths == b.key_auths );
}
} } // steemit::protocol