This is extracted from Core PR11403 : https://github.com/bitcoin/bitcoin/pull/11403/commits/0c8ea6380c9f402ed9777fd015b117ba13125a35
We don't have a witnessifier, so the usefulness is limited for us, but it'll help backport the relevent part of PR11403 and we can cleanup after the fact.