Fix make_change to not create half-satoshis #4836

pull morcos wants to merge 1 commits into bitcoin:master from morcos:fix-make_change changing 1 files +6 −4
  1. morcos commented at 7:42 PM on September 3, 2014: member

    make_change would split up inputs regardless of whether they were an odd number of satoshis and therefore would create outputs with half-satoshi amounts (or 9 decimal places in the python code). This would cause JSONRPC error: 16: bad-txns-in-belowout (perhaps both outputs are being rounded up). This fix solves that problem in that if you make-change too many times that won't happen, but I could imagine there are other places in the code that we need to be careful representing satoshis as a decimal or float number of BTC.

  2. laanwj commented at 11:33 AM on September 4, 2014: member

    Ideally we wouldn't be using floats for monetary amounts at all, not in the test cases either. In Python this means either integers as satoshis or Decimal() with fixed 8 decimals.

  3. jgarzik commented at 12:33 PM on September 4, 2014: contributor

    @laanwj Seconded. This probably wants Decimal()

  4. gavinandresen commented at 1:22 PM on September 4, 2014: contributor

    I remember fixing this bug, but the fix is probably lost on some long-forgotten branch.

    Just using Decimal isn't good enough if I recall correctly, the default rounding mode will still give you two inputs that add up to one satoshi more than is correct.

  5. gavinandresen commented at 1:27 PM on September 4, 2014: contributor

    Ah, found it:

    def make_change(from_node, amount_in, amount_out, fee):
        """
        Create change output(s), return them
        """
        outputs = {}
        amount = amount_out+fee
        change = amount_in - amount
        if change > amount*2:
            # Create an extra change output to break up big inputs
            change_address = from_node.getnewaddress()
            # Split change in two, being careful of rounding:
            outputs[change_address] = Decimal(change/2).quantize(Decimal('0.00000001'), rounding=ROUND_DOWN)
            change = amount_in - amount - outputs[change_address]
        if change > 0:
            outputs[from_node.getnewaddress()] = change
        return outputs
    
  6. Fix make_change to not create half-satoshis 3a7c3483b6
  7. morcos force-pushed on Sep 5, 2014
  8. morcos commented at 1:39 PM on September 5, 2014: member

    OK, I updated the PR to use Gavin's fix instead. It fixes the test case that was breaking for me as well.

  9. BitcoinPullTester commented at 1:52 PM on September 5, 2014: none

    Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/p4836_3a7c3483b6df12e84aefb937b85afec746b06b5e/ for binaries and test log. This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/ Contact BlueMatt on freenode if something looks broken.

  10. gavinandresen referenced this in commit 6ee78938ee on Sep 5, 2014
  11. gavinandresen merged this on Sep 5, 2014
  12. gavinandresen closed this on Sep 5, 2014

  13. morcos deleted the branch on Nov 12, 2014
  14. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-22 12:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me