Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debit/Credit Balance Scaling #986

Closed
sachnk opened this issue Jul 11, 2023 · 3 comments
Closed

Debit/Credit Balance Scaling #986

sachnk opened this issue Jul 11, 2023 · 3 comments

Comments

@sachnk
Copy link

sachnk commented Jul 11, 2023

Hey!

Big fan of the work you guys are doing.

So, I noticed in your docs that TigerBeetle leverages scaling 64-bit unsigned integers. I understand the rationale here, as doing operations on integers is quite fast. I've used similar techniques in the past.

Where I've run into problems with 10 ^ n integer scaling is when n is quite large, which isn't as uncommon as one might think. When n is large, you aren't left with sufficient room in the remaining bytes to represent the whole-number part. In trading systems, for instance, you can easily hit 10 ^ 10 to represent fractional traded prices. In crypto, the problem gets way worse... an extreme example would be a wei, which would need to be scaled by 10 ^ 18.

Concretely, if you need to scale by 10 ^ 10, then your whole-number part is 2 ^ 64 / 10 ^ 10 = 1,844,674,407, which isn't terribly large.

One solution here is to perhaps have generics to represent balance amounts, and then perhaps the type can be chosen via a build flag, so either 64-bit, 128-bit, arbitrary-decimal type, etc. An arbitrary-decimal type would give the most flexibility, with performance perhaps being a trade-off. For the use cases I'm thinking of, I'd happily make that trade-off.

I do think this problem could become a pretty big deal ultimately for various users who aren't just using TigerBeetle to represent fiat currencies in normal payment flows where n tends to be small.

@jorangreef
Copy link
Member

jorangreef commented Jul 12, 2023

Hey @sachnk!

Big fan of the work you guys are doing.

Awesome to hear that, thanks so much.

We've actually been thinking about 128-bit balances recently. This is something we may do. We had considered a build flag but perhaps we can do it without affecting performance.

It would be great to catch up and chat about this. Would you like to join our Slack and we can setup a time?

@PhakornKiong
Copy link

Hey!

Big fan of the work you guys are doing.

So, I noticed in your docs that TigerBeetle leverages scaling 64-bit unsigned integers. I understand the rationale here, as doing operations on integers is quite fast. I've used similar techniques in the past.

Where I've run into problems with 10 ^ n integer scaling is when n is quite large, which isn't as uncommon as one might think. When n is large, you aren't left with sufficient room in the remaining bytes to represent the whole-number part. In trading systems, for instance, you can easily hit 10 ^ 10 to represent fractional traded prices. In crypto, the problem gets way worse... an extreme example would be a wei, which would need to be scaled by 10 ^ 18.

Concretely, if you need to scale by 10 ^ 10, then your whole-number part is 2 ^ 64 / 10 ^ 10 = 1,844,674,407, which isn't terribly large.

One solution here is to perhaps have generics to represent balance amounts, and then perhaps the type can be chosen via a build flag, so either 64-bit, 128-bit, arbitrary-decimal type, etc. An arbitrary-decimal type would give the most flexibility, with performance perhaps being a trade-off. For the use cases I'm thinking of, I'd happily make that trade-off.

I do think this problem could become a pretty big deal ultimately for various users who aren't just using TigerBeetle to represent fiat currencies in normal payment flows where n tends to be small.

+1 on this. I think this is an issue when trying to use TigerBeetle with cryptocurrencies. 128-bit seems to be reasonably large enough.

@batiati
Copy link
Contributor

batiati commented Sep 17, 2023

Hi @sachnk and @PhakornKiong, thanks for your input!
We're happy to announce that #1157 just landed with support for 128-bit balances and amounts.

I'm closing this issue as completed, but please feel free to post further comments here or in our Slack channel.

@batiati batiati closed this as completed Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants