This is a patial backport of Core PR12257 : https://github.com/bitcoin/bitcoin/pull/12257/commits/87ebce25d66952f5ce565bb5130dcf5e24049872
Also includes the following bug fix:
- [wallet] correctly limit output group size
Extracted from Core PR13805 : https://github.com/bitcoin/bitcoin/pull/13805/commits/57ec1c97b2d0c0ba6056a43fa4177efc39567ae2