naivecoin icon indicating copy to clipboard operation
naivecoin copied to clipboard

Invalid block balance weirdness

Open neurofluxation opened this issue 5 years ago • 4 comments

Hey there (yet again),

So I have been getting an invalid block balance when mining an empty block all of a sudden and I can't seem to work out why. Doesn't matter if I restart the blockchain or not. Seems to happen randomly.

This is the exact error: Invalid block balance: inputs sum '46.04300000000003', outputs sum '46.04300000000004

Obviously the amounts will differ, it's been bugging me for a while now.

Thanks!

Edit: Here's my config... https://i.imgur.com/FA0TWEt.png

neurofluxation avatar Jun 17 '19 11:06 neurofluxation

Bump. I have found out nothing more, this just seem to "happen" after running for a while...?

neurofluxation avatar Jun 26 '19 13:06 neurofluxation

This is due to the weirdness in JavaScript's float processing. Whatever you do, do NOT use floats in a blockchain / cryptocurrency, it's a road to doom haha. Instead, use full integers as "Satoshis". And on the interface of the wallet / terminal, display those divided by 100,000,000 (If you're wanting 8 decimal places, like Bitcoin). And also make sure to round to the nearest integer during operations like multiplication/division, juuust to ensure JS doesn't pull any tricks.

If you want a package designed for handling these kind of numbers, try "bignum" packages.

JSKitty avatar Oct 18 '19 23:10 JSKitty

Good Kitty - Thank you, solved!

neurofluxation avatar Nov 01 '19 15:11 neurofluxation

BigInt is a thing now so native JS should work.

NetOpWibby avatar Nov 22 '19 02:11 NetOpWibby