Repository navigation
Description
Follow up issue after earlier gitter chat and implementers call today (ethereum/eth2.0-pm#29)
To pick the best solution, we need a more structural approach, and list the arguments for each "problem-class".
Note that I highlight Java and javascript as special considerations, due to the different support for integers than most other languages provide.
Go, Rust, Swift, Nim all support signed/unsigned 32/64 bit numbers.
Problem classes
- Slot numbers
- Validator indices
- Balances
Slot numbers
Range here: 1 slot per 6 seconds, for a few thousand years (could upgrade earlier...) = approx. 1000*365*24*60*60/6=5,256,000,000.
Signed numbers: No. However, there is a case for the first few epochs, where logic is looking back at old history, beyond the genesis. This could potentially result in an underflow.
Ways to catch the underflow:
x < 0: signed number checkx < genesis_slot, withgenesis_slot >> 0(i.e. sufficiently large offset from 0.) One potential offset is1 << 63, effectively simulating a signed number (but worse: sign bit inverted, if you think of it as two's complement), but not human readable in decimal base. If this becomes the "blocknumber" of the future, we want it to be readable, right?
If we don't care about very long-term consistency, we can go for 32 bits. It seems unnecessary however, as there's more range available to every language/platform. E.g. we could even opt for an imaginary 48 bit (un)signed integer.
Javascript
Highlight from earlier gitter chat:
Well, if we're not using the higher bits (i.e. [53, 63]) of the signed 64 bit number for the next ~ 800K years, javascript can just keep using 52 bit mantissa based numbers (the 11 unused bits being the exponent part of the float). And then we can choose for a signed 64 bit number. With genesis at a clean 0.
I.e. by exploiting the range of slot numbers, we could choose for a 64 bit number (signed or unsigned), and have it be compatible with the 52 bit (excl. sign bit) range of javascript float-based (i.e. mantissa) integers.
Alternative would be to use big-numbers, like the alternative that Java also has, see below.
Java
Java only supports signed 64 bit numbers ("long"). Of course, you could transport a unsigned 64 bit number over a signed number, as done previously in Guava and supported in Java 8. This does introduce other things to consider, please refer to comments: A, B.
Alternative would be Big integers. (something that looks like z.Set(y).Add(x) instead of z = y + x). Special limits have to be imposed to keep it at 64-bit big-numbers as well.
Please refer to comments below about the Pros and Cons of Java options A, B.
Validator indices
The approximate range here: 0...4,000,000 (worst case validator count).
Note that due to in-active validators, the list may actually be even bigger. But it's somewhere in this order, worst case.
Signed numbers: No. However, there could be a case for an indexOf function that returns a -1 when some validator is not included in a list. Common practice in quite a few languages.
Validator indices are realtively low, and would fit easily in 32 bits:
2**31 = 2,147,483,648
2**32 = 4,294,967,296
Now the questions here are:
- Do we want unnecessary but easy "consistency" by going for 64 bit numbers?
- Do we want signed numbers?
Javascript
Fits in a 52 bit mantissa. ES6 supports bitwise operations only for 32 bits. If we want to do bit-magic on indices, we may want to just go for 32 bits or less.
Java
Java only supports signed integers. "int": 32 bits, signed. docs
Balances
For balances there is a valid concern where we may not even want to use a 64 bit number, if we want precise/small balances.
Range: two options:
- Same as ETH 1.0, requires big-numbers in every language (some with more readable usage than others)
- Less resolution. Making it fit in a 64 bit number
Signed: No. However, there is a case for ease in math to consider that clients may want to convert to signed numbers internally. Signs are not necessary anywhere after being encoded.
Javascript / Java
We know the limitations of these by now. Balances are likely to require the most resolution in the near-term. No shortcuts with ranges (like with slots). Highly important to get right and prevent bugs.
Personally a fan of using big-ints here, and use safe-math.
I tried to outline the problem cases + options + considerations. Please comment if you think anything is missing, or want to make a strong case for any of the options.
Activity
protolambda commentedon Feb 14, 2019
Personal opinion:
Please share if you have a different view, and why.
Edit: changed opinion slightly, still same integer sizes, but open for either signed or unsigned ints. If I really had to make a choice at gunpoint, I think I would rather have unsigned ints (mostly because of simplicity of having no signs, although they could be just fine).
Edit 2: Engrish
mratsim commentedon Feb 14, 2019
Spec considerations
Beyond the implementers story there is also the spec side to consider.
Potentially the early slots become an edge case with special complex (?) treatment in the spec.
Ergonomics
As discussed during the implementer calls, using signed integers requires conversion at very specific boundaries:
However using unsigned integers requires:
My position
I do not support unsigned int because:
it's harder to use the natural programming language syntax because everyone will probably use something like
state.slots.at(idx)withathidingidx - GENESIS_START_SLOT.it's harder to write natural math because unsigned integers do not behave like intuitive math when substracting or comparing. We're actually doing math modulo 2^32 or 2^64.
If a value needs to be compared with another or substracted to another for processing (length, count, quantity), unsigned int tends to create more implementation bugs.
We had a lot of bugs due to unsigned memory addressing and length in EVM1 (see https://github.com/status-im/nimbus/pull/97) and this is exemplified by a number of test cases that ensure clients can handle under and overflow in the EVM:
While a VM is a special case, I'd like to avoid that in normal higher level code like the beacon chain.
In short, signed integers when we need to do math, logic and indexing and unsigned when we need control over memory representation (EVM, serialization).
arnetheduck commentedon Feb 14, 2019
An important distinction I'd add is to differentiate serialization and logic.
For slots etc, it's entirely reasonable that the serialization is
uint64while the logic is expressed in signed integer arithmetic that's natural to read (which also happened to be the status quo until the offset solution was introduced). Implementers can deal with that as they wish (and contribute back to the spec!), including proving parts of the spec as being unsigned-safe, turning subtractions into additions etc - as long as we establish "safe ranges" that are reasonable.I'd consider the offset solution impractical mainly because it encourages cutting corners on correctness to gain some performance in the near term - going for that smaller data type when you should be doing a bigint thus effectively penalizing languages that don't naturally support the given "reasonable" range and thus making the real-world deployment either more bug-prone or less rich in terms of implementation diversity.
As an aside, I also find signed integers to be more difficult from a serialization perspective - their byte encoding (now little-endian) is onerous to work with in general (parsing, debugging etc)
cleishm commentedon Feb 14, 2019
It's my opinion that the spec should use unsigned integers in all situations where the described value should never be negative. Additionally, the spec should use unsigned integers of the minimal bit length required for the given purpose, and explicitly define under/overflow behavior.
Implementors can then use signed or unsigned integers as they see fit, as long as the requirements of the specification are maintained.
atoulme commentedon Feb 17, 2019
All,
I believe it is important to properly support teams that work in Java/Kotlin and build Ethereum 2.0 clients. We (hat tip to @cleishm) have implemented an unsigned 64 bit number in Cava:
https://github.com/ConsenSys/cava/blob/master/units/src/main/java/net/consensys/cava/units/bigints/UInt64.java
I have prepared a commit for Artemis to change its logic to use it (I replaced all uses of UnsignedLong):
atoulme/artemis@eb4ef00
I believe this alleviates some of the pains, especially as UInt64 supports bit shifting, exact additions and subtractions, and more.
If I can supply an opinion - Kotlin offers the flexibility you seek if you're looking for a flexible DSL that allows overloading operators.
Cheers.
lookfirst commentedon Feb 17, 2019
@protolambda
"int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232-1. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers."
"long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long."
Totally subjective opinion and not enough empirical evidence to argue for a debate.
Have you tested the speed of this? This should get easily optimized in the JIT. Many large financial institutions use Java for HFT (high frequency trading), which requires insane performance with large numbers.
As for the 'unreadable' part... it is common to build an API to simplify it for your use case.
protolambda commentedon Feb 17, 2019
True, this is what I meant with "transporting over a signed int", because this "support" is completely artificial.
Why?
See above, it's really just the same data-type, with a hacky workaround to provide support for unsigned behavior. It's not completely native to the JVM bytecode afaik (please correct me if I'm wrong).
I literally wrote up an entire post to start a debate on considerations with integers, with special attestation to Java. And I'm familiar enough with the JVM and Java bytecode to work with JNI, reflection and know of awful hacks such as the lower integer cache. I tried my best documenting everything, and noted the transport-over-signed integer support, but yes I have my opinions.
I will edit it to not include "not nice to work with", and point out both of our comments. "not nice to work with" really was the nicest thing I could say about it.
Have not tested it myself, but there's plenty of other research/benchmarks into big-integers. And generally, the standard BigInteger is much slower than the one in Go. Now compare it to a native 64 bit integer with native operations, no boxing/allocations, and it's far behind.
Personally, I fundamentally dislike it because of the boxing (it's not necessary if you're on a 64 bit platform) and awful syntax. There's reasons where I would use it however, like safe-math, or > 64 bit integers.
Also, I don't care about "usage in HFT" when it's not the bottleneck of the actual example application. Streaming and distribution across compute are much more important in such a case afaik. As a side note; I wonder if we can make the beacon-chain processing itself more parallelized...
I will just edit this, if it's too subjective.
My proposed alternative, if you want to pursue JVM with less of the concerns that Java raises: Kotlin.
Generally, Kotlin does a much better job at implementing the same thing (although still "experimental" phase in 1.3), but with types, readable constants, and readable operators. Still the same JVM limitations tho, as far as I know (but Kotlin also has a native target as well). For reasons like these, I think it deserves a look to transition to as a JVM based client now that it's still a relatively early phase.
TLDR: avoid hacky pseudo unsigned 64 bit support if possible, i.e. see if just signed numbers would work first. And if you do, use annotations, enforce them, and document the dangers.
Edit: fix quoting markup
Edit 2: fix some typos
lookfirst commentedon Feb 17, 2019
This got me digging into the performance and I found this:
https://github.com/bwakell/Huldra
https://codeforces.com/blog/entry/17235
protolambda commentedon Feb 17, 2019
If we're comparing big-ints, take a look at http://www.wilfred.me.uk/blog/2014/10/20/the-fastest-bigint-in-the-west/
Some languages are just better suited for heavy optimization.
That said, I think we need to stay on topic, and discuss the benefits and drawbacks of all integer signs and sizes. Let's not get stuck talking about just big-ints, as clearly, we can do better.
The question is: what is the best choice for a solid and fast implementation, while still being reasonable to implement in all the involved languages? (x3, the different problems may require different types of solutions)
lookfirst commentedon Feb 18, 2019
@protolambda What is the relevance of quoting a blog post from 2014 with tests against Java 1.7?
In the case of ETH 2.0, which is still being heavily developed and worked on, shouldn't the focus be less on premature optimization and more on a solid implementation? Solid meaning that it passes all unit and integration tests.
Pretty and fast is optimizations that can be added later. Java makes profiling and refactoring quite straightforward.
Kotlin is a bike shed issue. Switching to a whole new language just over this issue seems like another premature optimization. It isn't trivial. Documentation, build systems, technical skills of the developers, etc... all need to be updated and changed. In other words, a whole number of risks for benefit of unknown percentage.
I'd just go with @atoulme's suggestion and use Cava's implementation of UInt64. Problem solved.
cleishm commentedon Feb 18, 2019
I agree that Java/Kotlin is a bit of a bike shed issue, much like big vs little endian, signed vs unsigned, etc. ;-)
That said, I disagree about the triviality. Kotlin is highly compatible with Java, you can mix it in the same source tree, and it all works in the same IDE. I'd argue it's more like a syntax variation than a different language. Indeed, much of Cava is implemented in Kotlin but consumed using each module's Java visible APIs. We do this because it's much easier to use Kotlin in many situations. Such a situation would be if there really is an extensive need to use extensively use unsigned integers - one can switch a bunch of classes into the Kotlin syntax and that problem is solved. Fairly trivially.
cleishm commentedon Feb 18, 2019
(BTW, Cava already provides an SSZ implementation using Kotlin unsigned integers: https://github.com/ConsenSys/cava/blob/master/ssz/src/main/kotlin/net/consensys/cava/ssz/experimental/SSZWriter.kt#L183. I expect other eth-2.0 related libraries in Cava will, where relevant, also expose Kotlin API using unsigned ints.)
lookfirst commentedon Feb 18, 2019
Ah, so more like TypeScript/JavaScript. Sweet. Maybe that is the answer here then.
cleishm commentedon Feb 18, 2019
That's a much more succinct way of explaining it! 😂
5 remaining items
cleishm commentedon Feb 19, 2019
Google C++ style guide may be fine for Google, but I don't think it's widely accepted as best practice, and it's focussed on implementation rather than on specifications.
For a specification, I still contend that it should describe the minimal requirement. If slots & epoch only need a range of 0 .. 2**52, then define it that way in the spec. If a bit-flag is needed to indicate an error, add that in the specification also. Implementations can then use whatever types their language of choice provides, as long as they can represent the required range and the implementation handles under/overflow as specified.
Reduce GENESIS_SLOT to 2**32
mkalinin commentedon Feb 20, 2019
My teammates and I totally agree with @benjaminion. We are another Java team working on beacon chain implementation.
protolambda commentedon Feb 20, 2019
Alternative proposal, the unsigned int one
Ok, so alternatively, to make the spec more "pure", we can say the types for slots/epochs and validator indices are unsigned. But with very clear wording on the allowed range, and encoding. This way, clients can safely use the native types that they like ("long" (java signed int64), "Number" (JS sign + mantissa)).
Below representation is big-endian, to make it easy to read. Little-endian does not change much, other than the actual memory order of the bytes.
S= Sign bit. Disallowed to be 1 in input/output, unless explicitly stated.Exceptions can be made for API design (e.g. -1 for slot of unknown block-root).
Note that it can be 1 in the internal processes, e.g. an implementation can choose to encode it as a signed-number to not have to work-around unsigned math.
A= Explicitly disallowed for now. Higher end of the encoding range for integers. Can soft-fork in a looong time from now to enable this range.B= Explicitly allowed, but subject to specification rules. I.e. slot must not be larger than X slots of latest finalized slot number to be accepted.52 bits should be plenty for slot/epoch numbers.
For validator numbers, we could use the same policy above for maximum capacity.
If you like this better than the earlier proposal, please add an emoji. If not, please show support for the earlier proposal, or write your own.
cleishm commentedon Feb 20, 2019
@protolambda So to paraphrase, are you saying that slot/epoch should be an unsigned 52-bit integer, but any part of the specifications that encodes these values should do so as 64-bits with the top 12 always set to zero?
Do we know all the places these values will be encoded?
protolambda commentedon Feb 20, 2019
@cleishm Not exactly.
Simplified:
What I'm saying is that we can loosen the design space by just stating that every client has to support 52 bits. Every platform can do this. This requirement is important, because if you transfer data through a peer, you want them to be able to handle it (without something alike to reduction in resolution), so that the data can propagate to other peers without problem.
And it's two's complement, so the sign bit will be left-most, next to the unused bits, and all will be 0 for our use case (positive numbers from 0 ... n, with n bounded by time or validator count).
And we encode it as 64 bits, because it's much more standard, and we can loosen up the range towards 64 bits as soon as the need arises (in millions of years with current slot time, or when ethereum validator count explodes beyond imagination...).
Also, being unsigned would just mean the spec has to be explicit on simple underflows (e.g. slot lookback early in the chain, looking for data before genesis). And clients can choose to handle it as signed numbers instead of extra if statements, if they want to.
The alternative-alternative proposal would be to just use
uint64, but that's status quo. Similar, but not good enough for client to use their native types safely, while they could if they wanted so. If we want to stick to unsigned, then better make a good and minimal (see proposal, not this 😂) statement on what to expect from clients, so that everyone can use their best integer choice for slots/epoch/indices.TLDR:
protolambda commentedon Feb 20, 2019
Also, if neither the Java nor the Javascript teams want to use their native types, we could just as well forget about all of this, and keep it at
uint64. But reverting that decision later may not be so easy (if not near-impossible), hence all of this (boring) effort to get it sorted.cleishm commentedon Feb 20, 2019
@protolambda Thanks for the clarification - I think that agrees with what I had in mind. Specify slot/epoch as an integer value in the range 0 .. 2^52, and specifying any encoding of that to use 64-bits (thus the most significant 12 bits must be zero). Additionally, define under/overflow semantics as wrap-around or error.
Implementations are then free, in their code, to treat slot/epoch as unsigned 52-bit integers, or 64-bit signed or unsigned values. They just have to make sure it encodes & decodes correctly, and handles under/overflow as specified.
JustinDrake commentedon Feb 21, 2019
#655 provides this flexibility for implementers :)
cleishm commentedon Feb 21, 2019
@JustinDrake That's cool. Do you think overflow needs to be addressed? I suspect it should be an error.
JustinDrake commentedon Feb 22, 2019
I wouldn't be opposed to adding something like "Any
uint64overflow should throw an error." assuming we makejustification_bitfielda byte array. (At the momentjustification_bitfieldis awkwardly auint64designed to overflow.)In practice,
uint64s should not overflow because of time and economic constraints. And if we miss an overflow edge case that can be triggered (e.g. by an attacker) then whatever the spec defines (wrap around or throw an error) it may not make a meaningful difference as in either case it would likely require manual intervention.protolambda commentedon Mar 1, 2019
After experimenting with SSZ encoding implementation and spec implementation more (to be published), I feel like the choice for unsigned integers works much better: it's not hard to deal with the unsigned math at all in practice, and exclusiveness of unsigned numbers reduces the complexity of encoding/decoding.
lookfirst commentedon Mar 2, 2019
@protolambda Got examples? Would love to see.
protolambda commentedon Mar 6, 2019
The complexity reduction I talked about is just not having to deal with two types of integers in your encoding/decoding functions. And the possibility of negative numbers in a communication space where there doesn't exist an idea of a negative number, makes no sense imho. By being clear about the range of unsigned 64 bit integers (i.e. for each of the tree classes: 1) slot/epoch, 2) val. indices, 3) balances, all clients should be able to support uint64.
Just to be clear:
Closing this issue now, it stays uint64, and we'll all try to be more clear about the usage of integers in future changes.