This repository was archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathblock_header_state.cpp
343 lines (282 loc) · 16 KB
/
block_header_state.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <eosio/chain/block_header_state.hpp>
#include <eosio/chain/exceptions.hpp>
#include <limits>
namespace eosio { namespace chain {
bool block_header_state::is_active_producer( account_name n )const {
return producer_to_last_produced.find(n) != producer_to_last_produced.end();
}
producer_key block_header_state::get_scheduled_producer( block_timestamp_type t )const {
auto index = t.slot % (active_schedule.producers.size() * config::producer_repetitions);
index /= config::producer_repetitions;
return active_schedule.producers[index];
}
uint32_t block_header_state::calc_dpos_last_irreversible( account_name producer_of_next_block )const {
vector<uint32_t> blocknums; blocknums.reserve( producer_to_last_implied_irb.size() );
for( auto& i : producer_to_last_implied_irb ) {
blocknums.push_back( (i.first == producer_of_next_block) ? dpos_proposed_irreversible_blocknum : i.second);
}
/// 2/3 must be greater, so if I go 1/3 into the list sorted from low to high, then 2/3 are greater
if( blocknums.size() == 0 ) return 0;
std::size_t index = (blocknums.size()-1) / 3;
std::nth_element( blocknums.begin(), blocknums.begin() + index, blocknums.end() );
return blocknums[ index ];
}
pending_block_header_state block_header_state::next( block_timestamp_type when,
uint16_t num_prev_blocks_to_confirm )const
{
pending_block_header_state result;
if( when != block_timestamp_type() ) {
EOS_ASSERT( when > header.timestamp, block_validate_exception, "next block must be in the future" );
} else {
(when = header.timestamp).slot++;
}
auto prokey = get_scheduled_producer(when);
auto itr = producer_to_last_produced.find( prokey.producer_name );
if( itr != producer_to_last_produced.end() ) {
EOS_ASSERT( itr->second < (block_num+1) - num_prev_blocks_to_confirm, producer_double_confirm,
"producer ${prod} double-confirming known range",
("prod", prokey.producer_name)("num", block_num+1)
("confirmed", num_prev_blocks_to_confirm)("last_produced", itr->second) );
}
result.block_num = block_num + 1;
result.previous = id;
result.timestamp = when;
result.confirmed = num_prev_blocks_to_confirm;
result.active_schedule_version = active_schedule.version;
result.prev_activated_protocol_features = activated_protocol_features;
result.block_signing_key = prokey.block_signing_key;
result.producer = prokey.producer_name;
result.blockroot_merkle = blockroot_merkle;
result.blockroot_merkle.append( id );
/// grow the confirmed count
static_assert(std::numeric_limits<uint8_t>::max() >= (config::max_producers * 2 / 3) + 1, "8bit confirmations may not be able to hold all of the needed confirmations");
// This uses the previous block active_schedule because thats the "schedule" that signs and therefore confirms _this_ block
auto num_active_producers = active_schedule.producers.size();
uint32_t required_confs = (uint32_t)(num_active_producers * 2 / 3) + 1;
if( confirm_count.size() < config::maximum_tracked_dpos_confirmations ) {
result.confirm_count.reserve( confirm_count.size() + 1 );
result.confirm_count = confirm_count;
result.confirm_count.resize( confirm_count.size() + 1 );
result.confirm_count.back() = (uint8_t)required_confs;
} else {
result.confirm_count.resize( confirm_count.size() );
memcpy( &result.confirm_count[0], &confirm_count[1], confirm_count.size() - 1 );
result.confirm_count.back() = (uint8_t)required_confs;
}
auto new_dpos_proposed_irreversible_blocknum = dpos_proposed_irreversible_blocknum;
int32_t i = (int32_t)(result.confirm_count.size() - 1);
uint32_t blocks_to_confirm = num_prev_blocks_to_confirm + 1; /// confirm the head block too
while( i >= 0 && blocks_to_confirm ) {
--result.confirm_count[i];
//idump((confirm_count[i]));
if( result.confirm_count[i] == 0 )
{
uint32_t block_num_for_i = result.block_num - (uint32_t)(result.confirm_count.size() - 1 - i);
new_dpos_proposed_irreversible_blocknum = block_num_for_i;
//idump((dpos2_lib)(block_num)(dpos_irreversible_blocknum));
if (i == static_cast<int32_t>(result.confirm_count.size() - 1)) {
result.confirm_count.resize(0);
} else {
memmove( &result.confirm_count[0], &result.confirm_count[i + 1], result.confirm_count.size() - i - 1);
result.confirm_count.resize( result.confirm_count.size() - i - 1 );
}
break;
}
--i;
--blocks_to_confirm;
}
result.dpos_proposed_irreversible_blocknum = new_dpos_proposed_irreversible_blocknum;
result.dpos_irreversible_blocknum = calc_dpos_last_irreversible( prokey.producer_name );
result.prev_pending_schedule = pending_schedule;
if( pending_schedule.schedule.producers.size() &&
result.dpos_irreversible_blocknum >= pending_schedule.schedule_lib_num )
{
result.active_schedule = pending_schedule.schedule;
flat_map<account_name,uint32_t> new_producer_to_last_produced;
for( const auto& pro : result.active_schedule.producers ) {
if( pro.producer_name == prokey.producer_name ) {
new_producer_to_last_produced[pro.producer_name] = result.block_num;
} else {
auto existing = producer_to_last_produced.find( pro.producer_name );
if( existing != producer_to_last_produced.end() ) {
new_producer_to_last_produced[pro.producer_name] = existing->second;
} else {
new_producer_to_last_produced[pro.producer_name] = result.dpos_irreversible_blocknum;
}
}
}
result.producer_to_last_produced = std::move( new_producer_to_last_produced );
flat_map<account_name,uint32_t> new_producer_to_last_implied_irb;
for( const auto& pro : result.active_schedule.producers ) {
if( pro.producer_name == prokey.producer_name ) {
new_producer_to_last_implied_irb[pro.producer_name] = dpos_proposed_irreversible_blocknum;
} else {
auto existing = producer_to_last_implied_irb.find( pro.producer_name );
if( existing != producer_to_last_implied_irb.end() ) {
new_producer_to_last_implied_irb[pro.producer_name] = existing->second;
} else {
new_producer_to_last_implied_irb[pro.producer_name] = result.dpos_irreversible_blocknum;
}
}
}
result.producer_to_last_implied_irb = std::move( new_producer_to_last_implied_irb );
result.was_pending_promoted = true;
} else {
result.active_schedule = active_schedule;
result.producer_to_last_produced = producer_to_last_produced;
result.producer_to_last_produced[prokey.producer_name] = block_num;
result.producer_to_last_implied_irb = producer_to_last_implied_irb;
result.producer_to_last_implied_irb[prokey.producer_name] = dpos_proposed_irreversible_blocknum;
}
return result;
}
signed_block_header pending_block_header_state::make_block_header(
const checksum256_type& transaction_mroot,
const checksum256_type& action_mroot,
optional<producer_schedule_type>&& new_producers,
vector<digest_type>&& new_protocol_feature_activations
)const
{
signed_block_header h;
h.timestamp = timestamp;
h.producer = producer;
h.confirmed = confirmed;
h.previous = previous;
h.transaction_mroot = transaction_mroot;
h.action_mroot = action_mroot;
h.schedule_version = active_schedule_version;
h.new_producers = std::move(new_producers);
if( new_protocol_feature_activations.size() > 0 ) {
h.header_extensions.emplace_back(
protocol_feature_activation::extension_id(),
fc::raw::pack( protocol_feature_activation{ std::move(new_protocol_feature_activations) } )
);
}
return h;
}
block_header_state pending_block_header_state::_finish_next(
const signed_block_header& h,
const std::function<void( block_timestamp_type,
const flat_set<digest_type>&,
const vector<digest_type>& )>& validator
)&&
{
EOS_ASSERT( h.timestamp == timestamp, block_validate_exception, "timestamp mismatch" );
EOS_ASSERT( h.previous == previous, unlinkable_block_exception, "previous mismatch" );
EOS_ASSERT( h.confirmed == confirmed, block_validate_exception, "confirmed mismatch" );
EOS_ASSERT( h.producer == producer, wrong_producer, "wrong producer specified" );
EOS_ASSERT( h.schedule_version == active_schedule_version, producer_schedule_exception, "schedule_version in signed block is corrupted" );
if( h.new_producers ) {
EOS_ASSERT( !was_pending_promoted, producer_schedule_exception, "cannot set pending producer schedule in the same block in which pending was promoted to active" );
EOS_ASSERT( h.new_producers->version == active_schedule.version + 1, producer_schedule_exception, "wrong producer schedule version specified" );
EOS_ASSERT( prev_pending_schedule.schedule.producers.size() == 0, producer_schedule_exception,
"cannot set new pending producers until last pending is confirmed" );
}
protocol_feature_activation_set_ptr new_activated_protocol_features;
auto exts = h.validate_and_extract_header_extensions();
{
if( exts.size() > 0 ) {
auto& new_protocol_features = exts.front().get<protocol_feature_activation>().protocol_features;
validator( timestamp, prev_activated_protocol_features->protocol_features, new_protocol_features );
new_activated_protocol_features = std::make_shared<protocol_feature_activation_set>(
*prev_activated_protocol_features,
std::move( new_protocol_features )
);
} else {
new_activated_protocol_features = std::move( prev_activated_protocol_features );
}
}
auto block_number = block_num;
block_header_state result( std::move( *static_cast<detail::block_header_state_common*>(this) ) );
result.id = h.id();
result.header = h;
result.header_exts = std::move(exts);
if( h.new_producers ) {
result.pending_schedule.schedule = *h.new_producers;
result.pending_schedule.schedule_hash = digest_type::hash( *h.new_producers );
result.pending_schedule.schedule_lib_num = block_number;
} else {
if( was_pending_promoted ) {
result.pending_schedule.schedule.version = prev_pending_schedule.schedule.version;
} else {
result.pending_schedule.schedule = std::move( prev_pending_schedule.schedule );
}
result.pending_schedule.schedule_hash = std::move( prev_pending_schedule.schedule_hash );
result.pending_schedule.schedule_lib_num = prev_pending_schedule.schedule_lib_num;
}
result.activated_protocol_features = std::move( new_activated_protocol_features );
return result;
}
block_header_state pending_block_header_state::finish_next(
const signed_block_header& h,
const std::function<void( block_timestamp_type,
const flat_set<digest_type>&,
const vector<digest_type>& )>& validator,
bool skip_validate_signee
)&&
{
auto result = std::move(*this)._finish_next( h, validator );
// ASSUMPTION FROM controller_impl::apply_block = all untrusted blocks will have their signatures pre-validated here
if( !skip_validate_signee ) {
result.verify_signee( result.signee() );
}
return result;
}
block_header_state pending_block_header_state::finish_next(
signed_block_header& h,
const std::function<void( block_timestamp_type,
const flat_set<digest_type>&,
const vector<digest_type>& )>& validator,
const std::function<signature_type(const digest_type&)>& signer
)&&
{
auto result = std::move(*this)._finish_next( h, validator );
result.sign( signer );
h.producer_signature = result.header.producer_signature;
return result;
}
/**
* Transitions the current header state into the next header state given the supplied signed block header.
*
* Given a signed block header, generate the expected template based upon the header time,
* then validate that the provided header matches the template.
*
* If the header specifies new_producers then apply them accordingly.
*/
block_header_state block_header_state::next(
const signed_block_header& h,
const std::function<void( block_timestamp_type,
const flat_set<digest_type>&,
const vector<digest_type>& )>& validator,
bool skip_validate_signee )const
{
return next( h.timestamp, h.confirmed ).finish_next( h, validator, skip_validate_signee );
}
digest_type block_header_state::sig_digest()const {
auto header_bmroot = digest_type::hash( std::make_pair( header.digest(), blockroot_merkle.get_root() ) );
return digest_type::hash( std::make_pair(header_bmroot, pending_schedule.schedule_hash) );
}
void block_header_state::sign( const std::function<signature_type(const digest_type&)>& signer ) {
auto d = sig_digest();
header.producer_signature = signer( d );
EOS_ASSERT( block_signing_key == fc::crypto::public_key( header.producer_signature, d ),
wrong_signing_key, "block is signed with unexpected key" );
}
public_key_type block_header_state::signee()const {
return fc::crypto::public_key( header.producer_signature, sig_digest(), true );
}
void block_header_state::verify_signee( const public_key_type& signee )const {
EOS_ASSERT( block_signing_key == signee, wrong_signing_key,
"block not signed by expected key",
("block_signing_key", block_signing_key)( "signee", signee ) );
}
/**
* Reference cannot outlive *this. Assumes header_exts is not mutated after instatiation.
*/
const vector<digest_type>& block_header_state::get_new_protocol_feature_activations()const {
static const vector<digest_type> no_activations{};
if( header_exts.size() == 0 || !header_exts.front().contains<protocol_feature_activation>() )
return no_activations;
return header_exts.front().get<protocol_feature_activation>().protocol_features;
}
} } /// namespace eosio::chain