MOVEONLY: add helper function for calculating ancestors and checking limits
Summary:
This is a backport of core#21800 [2/9]
https://github.com/bitcoin/bitcoin/pull/21800/commits/97dd1c729d2bbedf9527b914c0cc8267b8a7c21b
Test Plan: ninja all check-all
Reviewers: #bitcoin_abc, Fabien
Reviewed By: #bitcoin_abc, Fabien
Differential Revision: https://reviews.bitcoinabc.org/D12156