[refactor] put finality and maturity checking into a lambda
Summary:
No behavior change.
This is a partial backport of core#22677
https://github.com/bitcoin/bitcoin/pull/22677/commits/bb9078ed51159fa162484f16993313ed6cf980e3
Depends on D12441
Test Plan: ninja all check-all
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Differential Revision: https://reviews.bitcoinabc.org/D12443