@@ -94,7 +94,7 @@ namespace eosio::chain {
94
94
95
95
void open_impl ( const char * desc, const std::filesystem::path& fork_db_file, fc::cfile_datastream& ds, validator_t & validator );
96
96
void close_impl ( std::ofstream& out );
97
- bool add_impl ( const bsp_t & n, ignore_duplicate_t ignore_duplicate, bool validate, validator_t & validator );
97
+ fork_db_add_t add_impl ( const bsp_t & n, ignore_duplicate_t ignore_duplicate, bool validate, validator_t & validator );
98
98
bool is_valid () const ;
99
99
100
100
bsp_t get_block_impl ( const block_id_type& id, include_root_t include_root = include_root_t ::no ) const ;
@@ -192,10 +192,11 @@ namespace eosio::chain {
192
192
193
193
template <class BSP >
194
194
void fork_database_impl<BSP>::reset_root_impl( const bsp_t & root_bsp ) {
195
- index .clear ();
196
195
assert (root_bsp);
197
196
root = root_bsp;
198
197
root->set_valid (true );
198
+ pending_savanna_lib_id = block_id_type{};
199
+ index .clear ();
199
200
}
200
201
201
202
template <class BSP >
@@ -240,8 +241,8 @@ namespace eosio::chain {
240
241
}
241
242
242
243
template <class BSP >
243
- bool fork_database_impl<BSP>::add_impl(const bsp_t & n, ignore_duplicate_t ignore_duplicate,
244
- bool validate, validator_t & validator) {
244
+ fork_db_add_t fork_database_impl<BSP>::add_impl(const bsp_t & n, ignore_duplicate_t ignore_duplicate,
245
+ bool validate, validator_t & validator) {
245
246
EOS_ASSERT ( root, fork_database_exception, " root not yet set" );
246
247
EOS_ASSERT ( n, fork_database_exception, " attempt to add null block state" );
247
248
@@ -277,15 +278,25 @@ namespace eosio::chain {
277
278
EOS_RETHROW_EXCEPTIONS ( fork_database_exception, " serialized fork database is incompatible with configured protocol features" )
278
279
}
279
280
281
+ auto prev_head = head_impl (include_root_t ::yes);
282
+
280
283
auto inserted = index .insert (n);
281
284
EOS_ASSERT (ignore_duplicate == ignore_duplicate_t ::yes || inserted.second , fork_database_exception,
282
285
" duplicate block added: ${id}" , (" id" , n->id ()));
283
286
284
- return inserted.second && n == head_impl (include_root_t ::no);
287
+ if (!inserted.second )
288
+ return fork_db_add_t ::duplicate;
289
+ const bool new_head = n == head_impl (include_root_t ::no);
290
+ if (new_head && n->previous () == prev_head->id ())
291
+ return fork_db_add_t ::appended_to_head;
292
+ if (new_head)
293
+ return fork_db_add_t ::fork_switch;
294
+
295
+ return fork_db_add_t ::added;
285
296
}
286
297
287
298
template <class BSP >
288
- bool fork_database_t <BSP>::add( const bsp_t & n, ignore_duplicate_t ignore_duplicate ) {
299
+ fork_db_add_t fork_database_t <BSP>::add( const bsp_t & n, ignore_duplicate_t ignore_duplicate ) {
289
300
std::lock_guard g ( my->mtx );
290
301
return my->add_impl (n, ignore_duplicate, false ,
291
302
[](block_timestamp_type timestamp,
@@ -448,8 +459,8 @@ namespace eosio::chain {
448
459
BSP fork_database_impl<BSP>::search_on_branch_impl( const block_id_type& h, uint32_t block_num, include_root_t include_root ) const {
449
460
if (!root)
450
461
return {};
451
- if ( include_root == include_root_t ::yes && root->id () == h && root-> block_num () == block_num ) {
452
- return root;
462
+ if ( include_root == include_root_t ::yes && root->block_num () == block_num ) {
463
+ return root; // root is root of every branch, no need to check h
453
464
}
454
465
if (block_num <= root->block_num ())
455
466
return {};
@@ -582,7 +593,7 @@ namespace eosio::chain {
582
593
template <class BSP >
583
594
BSP fork_database_impl<BSP>::get_block_impl(const block_id_type& id,
584
595
include_root_t include_root /* = include_root_t::no */ ) const {
585
- if ( include_root == include_root_t ::yes && root->id () == id ) {
596
+ if ( include_root == include_root_t ::yes && root && root ->id () == id ) {
586
597
return root;
587
598
}
588
599
auto itr = index .find ( id );
0 commit comments