Skip to content

Commit d2bf27e

Browse files
authored
Merge pull request #1527 from bitshares/jmj_1313
Remove use of skip_fork_db flag
2 parents 596d7e9 + 7462d39 commit d2bf27e

File tree

3 files changed

+66
-73
lines changed

3 files changed

+66
-73
lines changed

libraries/chain/db_block.cpp

+64-70
Original file line numberDiff line numberDiff line change
@@ -129,77 +129,74 @@ bool database::push_block(const signed_block& new_block, uint32_t skip)
129129
bool database::_push_block(const signed_block& new_block)
130130
{ try {
131131
uint32_t skip = get_node_properties().skip_flags;
132-
if( !(skip&skip_fork_db) )
133-
{
134-
/// TODO: if the block is greater than the head block and before the next maitenance interval
135-
// verify that the block signer is in the current set of active witnesses.
132+
// TODO: If the block is greater than the head block and before the next maintenance interval
133+
// verify that the block signer is in the current set of active witnesses.
136134

137-
shared_ptr<fork_item> new_head = _fork_db.push_block(new_block);
138-
//If the head block from the longest chain does not build off of the current head, we need to switch forks.
139-
if( new_head->data.previous != head_block_id() )
135+
shared_ptr<fork_item> new_head = _fork_db.push_block(new_block);
136+
//If the head block from the longest chain does not build off of the current head, we need to switch forks.
137+
if( new_head->data.previous != head_block_id() )
138+
{
139+
//If the newly pushed block is the same height as head, we get head back in new_head
140+
//Only switch forks if new_head is actually higher than head
141+
if( new_head->data.block_num() > head_block_num() )
140142
{
141-
//If the newly pushed block is the same height as head, we get head back in new_head
142-
//Only switch forks if new_head is actually higher than head
143-
if( new_head->data.block_num() > head_block_num() )
143+
wlog( "Switching to fork: ${id}", ("id",new_head->data.id()) );
144+
auto branches = _fork_db.fetch_branch_from(new_head->data.id(), head_block_id());
145+
146+
// pop blocks until we hit the forked block
147+
while( head_block_id() != branches.second.back()->data.previous )
148+
{
149+
ilog( "popping block #${n} ${id}", ("n",head_block_num())("id",head_block_id()) );
150+
pop_block();
151+
}
152+
153+
// push all blocks on the new fork
154+
for( auto ritr = branches.first.rbegin(); ritr != branches.first.rend(); ++ritr )
144155
{
145-
wlog( "Switching to fork: ${id}", ("id",new_head->data.id()) );
146-
auto branches = _fork_db.fetch_branch_from(new_head->data.id(), head_block_id());
147-
148-
// pop blocks until we hit the forked block
149-
while( head_block_id() != branches.second.back()->data.previous )
150-
{
151-
ilog( "popping block #${n} ${id}", ("n",head_block_num())("id",head_block_id()) );
152-
pop_block();
153-
}
154-
155-
// push all blocks on the new fork
156-
for( auto ritr = branches.first.rbegin(); ritr != branches.first.rend(); ++ritr )
157-
{
158-
ilog( "pushing block from fork #${n} ${id}", ("n",(*ritr)->data.block_num())("id",(*ritr)->id) );
159-
optional<fc::exception> except;
160-
try {
161-
undo_database::session session = _undo_db.start_undo_session();
162-
apply_block( (*ritr)->data, skip );
163-
_block_id_to_block.store( (*ritr)->id, (*ritr)->data );
164-
session.commit();
165-
}
166-
catch ( const fc::exception& e ) { except = e; }
167-
if( except )
168-
{
169-
wlog( "exception thrown while switching forks ${e}", ("e",except->to_detail_string() ) );
170-
// remove the rest of branches.first from the fork_db, those blocks are invalid
171-
while( ritr != branches.first.rend() )
172-
{
173-
ilog( "removing block from fork_db #${n} ${id}", ("n",(*ritr)->data.block_num())("id",(*ritr)->id) );
174-
_fork_db.remove( (*ritr)->id );
175-
++ritr;
176-
}
177-
_fork_db.set_head( branches.second.front() );
178-
179-
// pop all blocks from the bad fork
180-
while( head_block_id() != branches.second.back()->data.previous )
181-
{
182-
ilog( "popping block #${n} ${id}", ("n",head_block_num())("id",head_block_id()) );
183-
pop_block();
184-
}
185-
186-
ilog( "Switching back to fork: ${id}", ("id",branches.second.front()->data.id()) );
187-
// restore all blocks from the good fork
188-
for( auto ritr2 = branches.second.rbegin(); ritr2 != branches.second.rend(); ++ritr2 )
189-
{
190-
ilog( "pushing block #${n} ${id}", ("n",(*ritr2)->data.block_num())("id",(*ritr2)->id) );
191-
auto session = _undo_db.start_undo_session();
192-
apply_block( (*ritr2)->data, skip );
193-
_block_id_to_block.store( (*ritr2)->id, (*ritr2)->data );
194-
session.commit();
195-
}
196-
throw *except;
197-
}
198-
}
199-
return true;
156+
ilog( "pushing block from fork #${n} ${id}", ("n",(*ritr)->data.block_num())("id",(*ritr)->id) );
157+
optional<fc::exception> except;
158+
try {
159+
undo_database::session session = _undo_db.start_undo_session();
160+
apply_block( (*ritr)->data, skip );
161+
_block_id_to_block.store( (*ritr)->id, (*ritr)->data );
162+
session.commit();
163+
}
164+
catch ( const fc::exception& e ) { except = e; }
165+
if( except )
166+
{
167+
wlog( "exception thrown while switching forks ${e}", ("e",except->to_detail_string() ) );
168+
// remove the rest of branches.first from the fork_db, those blocks are invalid
169+
while( ritr != branches.first.rend() )
170+
{
171+
ilog( "removing block from fork_db #${n} ${id}", ("n",(*ritr)->data.block_num())("id",(*ritr)->id) );
172+
_fork_db.remove( (*ritr)->id );
173+
++ritr;
174+
}
175+
_fork_db.set_head( branches.second.front() );
176+
177+
// pop all blocks from the bad fork
178+
while( head_block_id() != branches.second.back()->data.previous )
179+
{
180+
ilog( "popping block #${n} ${id}", ("n",head_block_num())("id",head_block_id()) );
181+
pop_block();
182+
}
183+
184+
ilog( "Switching back to fork: ${id}", ("id",branches.second.front()->data.id()) );
185+
// restore all blocks from the good fork
186+
for( auto ritr2 = branches.second.rbegin(); ritr2 != branches.second.rend(); ++ritr2 )
187+
{
188+
ilog( "pushing block #${n} ${id}", ("n",(*ritr2)->data.block_num())("id",(*ritr2)->id) );
189+
auto session = _undo_db.start_undo_session();
190+
apply_block( (*ritr2)->data, skip );
191+
_block_id_to_block.store( (*ritr2)->id, (*ritr2)->data );
192+
session.commit();
193+
}
194+
throw *except;
195+
}
200196
}
201-
else return false;
197+
return true;
202198
}
199+
else return false;
203200
}
204201

205202
try {
@@ -209,10 +206,7 @@ bool database::_push_block(const signed_block& new_block)
209206
session.commit();
210207
} catch ( const fc::exception& e ) {
211208
elog("Failed to push new block:\n${e}", ("e", e.to_detail_string()));
212-
if( !(skip&skip_fork_db) )
213-
{
214-
_fork_db.remove( new_block.id() );
215-
}
209+
_fork_db.remove( new_block.id() );
216210
throw;
217211
}
218212

libraries/chain/include/graphene/chain/database.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ namespace graphene { namespace chain {
6666
skip_witness_signature = 1 << 0, ///< used while reindexing
6767
skip_transaction_signatures = 1 << 1, ///< used by non-witness nodes
6868
skip_transaction_dupe_check = 1 << 2, ///< used while reindexing
69-
skip_fork_db = 1 << 3, ///< used while reindexing
7069
skip_block_size_check = 1 << 4, ///< used when applying locally generated transactions
7170
skip_tapos_check = 1 << 5, ///< used while reindexing -- note this skips expiration check as well
7271
// skip_authority_check = 1 << 6, ///< removed because effectively identical to skip_transaction_signatures

tests/tests/history_api_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ BOOST_AUTO_TEST_CASE(track_account) {
421421
// dan makes 1 op
422422
create_bitasset("EUR", dan_id);
423423

424-
generate_block( ~database::skip_fork_db );
424+
generate_block();
425425

426426
// anything against account_id_type() should be {}
427427
vector<operation_history_object> histories = hist_api.get_account_history("1.2.0", operation_history_id_type(0), 10, operation_history_id_type(0));
@@ -449,7 +449,7 @@ BOOST_AUTO_TEST_CASE(track_account) {
449449
create_bitasset( "BTC", account_id_type() );
450450
create_bitasset( "GBP", dan_id );
451451

452-
generate_block( ~database::skip_fork_db );
452+
generate_block();
453453

454454
histories = hist_api.get_account_history("dan", operation_history_id_type(0), 10, operation_history_id_type(0));
455455
BOOST_CHECK_EQUAL(histories.size(), 3);

0 commit comments

Comments
 (0)