This is a partial backport of Core PR12257 : https://github.com/bitcoin/bitcoin/pull/12257/commits/0128121101fb3ee82f3abd3973a967a4226ffe0e
Depends on D3394
Also include the test for a later bugfix:
- [qa] Add test for too-large wallet output groups
This is extracted from Core PR13805 : https://github.com/bitcoin/bitcoin/pull/13805/commits/a13647b8bd667ca58d8e82682c1d46555dce42c9