@@ -199,17 +199,19 @@ void CLASS::do_organize(typename Block::cptr block,
199199 bool strong{};
200200 uint256_t work{};
201201 hashes tree_branch{};
202- height_t branch_point{};
203- header_links store_branch{};
204-
205- if (!get_branch_work (work, branch_point, tree_branch, store_branch, header))
202+ header_states store_branch{};
203+ if (!get_branch_work (work, tree_branch, store_branch, header))
206204 {
207205 handler (fault (error::organize2), height);
208206 return ;
209207 }
210208
209+ // New height is one above branch_point if the tree/store branch is empty.
210+ const auto branch_size = add1 (tree_branch.size () + store_branch.size ());
211+ const auto branch_point = height - branch_size;
212+
211213 // branch_point is the highest tree-candidate common block.
212- if (!get_is_strong (strong, work, branch_point))
214+ if (!query. get_strong_branch (strong, work, branch_point))
213215 {
214216 handler (fault (error::organize3), height);
215217 return ;
@@ -257,9 +259,9 @@ void CLASS::do_organize(typename Block::cptr block,
257259 }
258260
259261 // Push stored strong headers to candidate chain.
260- for (const auto & link : std::views::reverse (store_branch))
262+ for (const auto & stored : std::views::reverse (store_branch))
261263 {
262- if (!set_organized (link, ++top))
264+ if (!set_organized (stored. link , ++top))
263265 {
264266 handler (fault (error::organize6), height);
265267 return ;
@@ -553,67 +555,45 @@ CLASS::chain_state::cptr CLASS::get_chain_state(
553555// Also obtains branch point for work summation termination.
554556// Also obtains ordered branch identifiers for subsequent reorg.
555557TEMPLATE
556- bool CLASS::get_branch_work (uint256_t & work, size_t & branch_point,
557- system::hashes& tree_branch, header_links & store_branch,
558+ bool CLASS::get_branch_work (uint256_t & work,
559+ system::hashes& tree_branch, header_states & store_branch,
558560 const system::chain::header& header) const NOEXCEPT
559561{
562+ using namespace system ;
563+ const auto & query = archive ();
564+
560565 // Use pointer to avoid const/copy.
561566 auto previous = &header.previous_block_hash ();
562- const auto & query = archive ();
563567 work = header.proof ();
564568
565- // Sum all branch work from tree.
566- for (auto it = tree_.find (system::hash_cref (*previous)); it != tree_.end ();
567- it = tree_.find (system::hash_cref (*previous)))
568- {
569- const auto & next = get_header (*it->second .block );
570- previous = &next.previous_block_hash ();
571- tree_branch.push_back (next.hash ());
572- work += next.proof ();
573- }
574-
575- // Sum branch work from store.
576- database::height_link link{};
577- for (link = query.to_header (*previous); !query.is_candidate_header (link);
578- link = query.to_parent (link))
569+ // Get portion of branch from tree and sum its work.
570+ auto it = tree_.find (hash_cref (*previous));
571+ while (it != tree_.end ());
579572 {
580- uint32_t bits{};
581- if (link.is_terminal () || !query.get_bits (bits, link))
582- return false ;
573+ // Iterate.
574+ const auto & head = get_header (*it->second .block );
575+ previous = &head.previous_block_hash ();
576+ it = tree_.find (hash_cref (*previous));
583577
584- store_branch.push_back (link);
585- work += system::chain::header::proof (bits);
578+ // Accumulate.
579+ tree_branch.push_back (head.hash ());
580+ work += head.proof ();
586581 }
587582
588- // Height of the highest candidate header is the branch point.
589- return query.get_height (branch_point, link);
590- }
591-
592- // A branch with greater work will cause candidate reorganization.
593- TEMPLATE
594- bool CLASS::get_is_strong (bool & strong, const uint256_t & branch_work,
595- size_t branch_point) const NOEXCEPT
596- {
597- uint256_t candidate_work{};
598- const auto & query = archive ();
583+ // Get portion of branch that is already stored.
584+ if (!query.get_branch (store_branch, *previous))
585+ return false ;
599586
600- for ( auto height = query. get_top_candidate (); height > branch_point;
601- --height )
587+ // If store_branch is empty then previous is candidate/ branch_point.
588+ if (!store_branch. empty () )
602589 {
603- uint32_t bits {};
604- if (!query.get_bits (bits, query. to_candidate (height) ))
590+ uint256_t store_work {};
591+ if (!query.get_work (store_work, store_branch ))
605592 return false ;
606593
607- // Not strong when candidate_work equals or exceeds branch_work.
608- candidate_work += system::chain::header::proof (bits);
609- if (candidate_work >= branch_work)
610- {
611- strong = false ;
612- return true ;
613- }
594+ work += store_work;
614595 }
615596
616- strong = true ;
617597 return true ;
618598}
619599
0 commit comments