fork_database prune function exists condition that will never meet
I read the source code to understand the EOS. In libraries/chain/fork_database.cpp and the function fork_database::prune( const block_state_ptr& h ), I can't understand some code because I don't think ((*bni)->block_num < num) will meet the condition .
Because there are only to ways to call prune function: fork_database::close() and fork_database::add( block_state_ptr n ), and each function calls prune function like this:
auto lib = my->head->dpos_irreversible_blocknum;
auto oldest = *my->index.get<by_block_num>().begin();
if( oldest->block_num < lib ) {
prune( oldest );
}
And in prune function: auto num = h->block_num; auto& by_bn = my->index.get<by_block_num>(); auto bni = by_bn.begin(); while( bni != by_bn.end() && (*bni)->block_num < num ) { prune( *bni ); bni = by_bn.begin(); }
I think that (*bni)->block_num is the same of h->block_num, ((*bni)->block_num < num) condition will never be met.
i also think so
void fork_database::prune( const block_state_ptr& h ) { auto num = h->block_num;
auto& by_bn = my->index.get<by_block_num>();
auto bni = by_bn.begin();
while( bni != by_bn.end() && (*bni)->block_num < num ) {
prune( *bni );
bni = by_bn.begin();
}
}
here is not a effective code, because the index is not changed at this period.
The condition will meet most of the time. Fork database are supposed to keep all the block_ids which are reversible. Fork database pruning will happen whenever there a last irreversible number update. Consider the following cases:
case 1: no forks assume lib = 1, fork_db will looks like
1->2->3->4->5
in this case if lib advances to 3, fork_db will be
3->4->5 (1 & 2 are removed in prune)
case 2: soft forks:
1->2 ->3 -> 4
|
\ ->3b->4b->5b (longest head)
if lib advances to 3, then fork db becomes:
3 -> 4
3b->4b-> 5b
By this stage, 2 branches become totally separated. if lib advances to 4, then
4
4b -> 5b
and if lib advances to 5:
5b
You'll know the branches are finally removed in fork_db along with lib advancing.
2nd question: here is not a effective code, because the index is not changed at this period.
This is not correct, because bni = by_bn.begin(); already reset the index to the beginning.