-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathranking.cpp
More file actions
146 lines (118 loc) · 5.3 KB
/
ranking.cpp
File metadata and controls
146 lines (118 loc) · 5.3 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <sysio/crypto.hpp>
#include <sysio/datastream.hpp>
#include <sysio/sysio.hpp>
#include <sysio/multi_index.hpp>
#include <sysio/permission.hpp>
#include <sysio/privileged.hpp>
#include <sysio/serialize.hpp>
#include <sysio/singleton.hpp>
#include <sysio.system/sysio.system.hpp>
#include <sysio.token/sysio.token.hpp>
#include <type_traits>
#include <limits>
#include <set>
#include <algorithm>
#include <cmath>
namespace sysiosystem {
using sysio::const_mem_fun;
using sysio::current_time_point;
using sysio::indexed_by;
using sysio::microseconds;
using sysio::singleton;
void system_contract::register_producer( const name& producer, const sysio::block_signing_authority& producer_authority, const std::string& url, uint16_t location ) {
auto prod = _producers.find( producer.value );
const auto ct = current_time_point();
sysio::public_key producer_key{};
std::visit( [&](auto&& auth ) {
if( auth.keys.size() == 1 ) {
// if the producer_authority consists of a single key, use that key in the legacy producer_key field
producer_key = auth.keys[0].key;
}
for (const auto& kw : auth.keys) {
check( kw.key.index() < 2, "Only K1 & R1 keys allowed" );
}
}, producer_authority );
if ( prod != _producers.end() ) {
_producers.modify( prod, get_self(), [&]( producer_info& info ){
info.producer_key = producer_key;
info.is_active = true;
info.url = url;
info.location = location;
info.producer_authority = producer_authority;
if ( info.last_claim_time == time_point() )
info.last_claim_time = ct;
});
} else {
_producers.emplace( get_self(), [&]( producer_info& info ){
info.owner = producer;
info.producer_key = producer_key;
info.is_active = true;
info.url = url;
info.location = location;
info.last_claim_time = ct;
info.producer_authority = producer_authority;
});
}
}
void system_contract::regproducer( const name& producer, const sysio::public_key& producer_key, const std::string& url, uint16_t location ) {
require_auth( producer );
check( url.size() < 512, "url too long" );
register_producer( producer, convert_to_block_signing_authority( producer_key ), url, location );
}
void system_contract::regproducer2( const name& producer, const sysio::block_signing_authority& producer_authority, const std::string& url, uint16_t location ) {
require_auth( producer );
check( url.size() < 512, "url too long" );
std::visit( [&](auto&& auth ) {
check( auth.is_valid(), "invalid producer authority" );
}, producer_authority );
register_producer( producer, producer_authority, url, location );
}
void system_contract::unregprod( const name& producer ) {
require_auth( producer );
const auto& prod = _producers.get( producer.value, "producer not found" );
_producers.modify( prod, get_self(), [&]( producer_info& info ){
info.deactivate();
});
}
void system_contract::update_ranked_producers( const block_timestamp& block_time ) {
_gstate.last_producer_schedule_update = block_time;
auto idx = _producers.get_index<"prodrank"_n>();
using value_type = std::pair<sysio::producer_authority, uint16_t>;
std::vector< value_type > top_producers;
std::vector< finalizer_auth_info > proposed_finalizers;
top_producers.reserve(max_producers);
proposed_finalizers.reserve(max_producers);
for( auto it = idx.cbegin(); it != idx.cend() && top_producers.size() < max_producers; ++it ) {
if( it->rank > max_producers ) break; // no more ranked producers
if( !it->active() ) continue;
// Require active finalizer key for all scheduled producers
auto finalizer = _finalizers.find( it->owner.value );
if( finalizer == _finalizers.end() || finalizer->active_key_binary.empty() ) {
continue;
}
proposed_finalizers.emplace_back(*finalizer);
top_producers.emplace_back(
sysio::producer_authority{
.producer_name = it->owner,
.authority = it->get_producer_authority()
},
it->location
);
}
if( top_producers.empty() || top_producers.size() < _gstate.last_producer_schedule_size ) {
return;
}
// Sort by producer name for deterministic ordering
std::sort( top_producers.begin(), top_producers.end(), []( const value_type& lhs, const value_type& rhs ) {
return lhs.first.producer_name < rhs.first.producer_name;
} );
std::vector<sysio::producer_authority> producers;
producers.reserve(top_producers.size());
for( auto& item : top_producers )
producers.push_back( std::move(item.first) );
if( set_proposed_producers( producers ) >= 0 ) {
_gstate.last_producer_schedule_size = static_cast<decltype(_gstate.last_producer_schedule_size)>( producers.size() );
}
set_proposed_finalizers( std::move(proposed_finalizers) );
}
} /// namespace sysiosystem