Port https://github.com/neo-project/neo/pull/4118 and https://github.com/neo-project/neo/pull/4119.