conradoqg / naivecoin

A cryptocurrency implementation in less than 1500 lines of code
Apache License 2.0
1.23k stars 323 forks source link

Invalid block balance weirdness #60

Open neurofluxation opened 5 years ago

neurofluxation commented 5 years ago

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 commented 5 years ago

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

JSKitty commented 5 years ago

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.

neurofluxation commented 5 years ago

Good Kitty - Thank you, solved!

NetOpWibby commented 4 years ago

BigInt is a thing now so native JS should work.