-
Notifications
You must be signed in to change notification settings - Fork 144
BigInteger to Hex in most compressed format (NeoVM 3) #170
Comments
This is very useful for NEP-5... if you have Balance zero, it will make it an empty bytearray (which can be ripped out automatically, if other proposal is accepted on Neo #824). |
🤔 Looking at neo-vm/src/neo-vm/ExecutionEngine.cs Lines 220 to 224 in 30ec0a3
I don't think that's expected behaviour at all int x = 0;
Console.WriteLine(BitConverter.ToString(BitConverter.GetBytes(x))); // 00-00-00-00 python In [1]: x = 0
In [2]: x.to_bytes(4, 'little')
Out[2]: b'\x00\x00\x00\x00' Limiting it to a single byte ( I actually think we should fix it like we did here #132 for the If the instructions Here are 4 tests (json);
Number 4 is the test you described. In the current state 1-3 pass, 4 will fail (as you described). As seen above the current neo-vm/src/neo-vm/ExecutionEngine.cs Line 53 in 30ec0a3
change this to private static readonly byte[] EmptyBytes = { 0 }; and all 4 tests pass as should be expected and the naming convention of the opcodes actually becomes compliant. (We probably want to rename |
In fact, the C# BigInteger from numerics is the one we use as standard for integers (thats why I emphasized it as VM_Integer). Negative values are two complement, and sometimes an extra zero is required on the left to differentiate from negative values. My proposal just affects zero value. Some months ago I fixed python project, in the opposite direction... it considered zero as empty array, what I propose now for Neo3. |
@ixje this is the current standard for numbers:
What I want to do is to change: This will make it fully standard with the rule of being the most compressed possible, and will automatically help solving many interesting things. |
I took a look. I like having a clear However, I still don't think it is good practice to turn |
In fact,this is transparent for users and devs... the most affected ones its us, here, devs creating dev tools for the devs 😂 Its important to be space efficient, specially on a high performant blockchain. |
I'm trying to see the issue from your side but I just cannot see how this is more transparent than having a natural relationship of
In your case the specification must say something like.
We can drop the whole second line if we do not adopt this empty bytearray behaviour. What is the main reason behind this proposal? Reducing storage needs? In general |
this is incorrect. The current proposed behavior is: The other "family" of opcodes is: If you look into C# BigInteger, having zero as The magic on the storage is not the single byte, but the ability to garbage collect automatically every existing key with empty bytearray as value... in NEP-5 scenario, it means that it will be impossible to create empty addresses without assets, occupying a big space on the key side and all the key prefixes. Without this, it's not possible to implement automatic garbage collection. |
I don't even understand what the "automatic" in "automatic garbage" collection means in this case. I see nothing preventing some algorithm looking for keys with 1 |
Your answer is like a lesson, @igormcoelho. A true professor, brother, even without dominating C# you can think in all languages. |
@ixje this is still under discussion, topic is not closed: see PR #173. I asked your opinion here because it's valuable. In fact, I just retracted from the creation of PUSHBYTES0, because it could possibly break (or require name changes) on the tools of many people, including our neo-avm-optimizer, neo-one, perhaps neoscan avm parser, all blockchain explorers, and mainly neo-python. This is not a C# protocol, it is a language agnostic protocol. I have been implementing Neo on C++ and trying to help neo-python to fix bugs in the past, so I can guarantee to you that no decision is based on C# here. I'm not a C# developer, and when NEO3 Specification (https://github.com/neo-project/specification) is ready, there will be no references in it for the C# language. If you take any existing biginteger library, all of them will have the same or nearly the same behavior. Negative is two's complement, and positive byte array is pruned. Standard python would put biginteger 0 as empty array, so this change is beneficial to python and not beneficial to C# (but good to Neo3 protocol on general). |
A more uniform view of the number zero can be achieved by always represent it in its most compressed format. 0x0000 is zero, 0x00 is also zero, but 0x'' (empty array) is also zero. So we should adopt empty array as our new standard. Currently, the standard (by C# BigInteger) is "0x00" I think (although 0x"" is also recognized as zero...)
This helps in many things:
So, Number 0 as bytearray, will actually be an empty bytearray, as expected. Before this, I used to make some strange computations, like PUSH0 PUSH0 ADD, not being equals to PUSH0, for example. Now this wont happen anymore.
If someone wants the hexstring zero, it can do
PUSHBYTES1 00
, as always.The text was updated successfully, but these errors were encountered: