@@ -129,77 +129,74 @@ bool database::push_block(const signed_block& new_block, uint32_t skip)
129
129
bool database::_push_block (const signed_block& new_block)
130
130
{ try {
131
131
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.
136
134
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 () )
140
142
{
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 )
144
155
{
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
+ }
200
196
}
201
- else return false ;
197
+ return true ;
202
198
}
199
+ else return false ;
203
200
}
204
201
205
202
try {
@@ -209,10 +206,7 @@ bool database::_push_block(const signed_block& new_block)
209
206
session.commit ();
210
207
} catch ( const fc::exception & e ) {
211
208
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 () );
216
210
throw ;
217
211
}
218
212
0 commit comments