Comments (5)
This is an interesting question @ixje, let me explain. The Boolean stackitem needs a definition for true/false, and personally I don't know which is the oldest version that was adopted, but like shargon said, we will need to preserve this on Neo 2.X, but we could improve for Neo 3.
When you code a smart contract that returns any int zero (like int i = 0;
), compiler will usually issue a PUSH0
. However, PUSH0 is not actually pushing the value zero to the stack, but an empty array. This is fine, because on BigInteger format, all left-zeros can be removed without issue until you get a byte with top bit set to one (the zero before this you cannot remove, otherwise you change sign). So, an empty array is in fact a value false (as usual, integer 0). However, I'm not sure its byte size would indeed be zero after conversion Int->ByteArray. But I'm sure that an empty bytearray is also zero (obviously every language would have somewhere at least size control that would make it not perfectly zero, but officially the byte array is zero length).
So: [ ]
is false. [ 1 ]
is true. [ 2 ]
should be true as well, and [ -1 ]
too. One thing I don't know, is that if [ 0 ]
is false too. Is it? I was hoping in fact that [ 0 0 0 0 0 ]
is also false. Is it? So, we could indeed improve this check, as long as it does not break any past compatibility.
from neo-vm.
@shargon what are your thoughts on this? The above issue got triggered by a recent test you added so I'm assuming you'll have an idea about it :)
neo-vm/tests/neo-vm.Tests/Tests/OpCodes/Splice/SIZE.json
Lines 173 to 174 in a3f9444
from neo-vm.
To elaborate; this is my expected behaviour but in a normal language like C#
bool boolValue = false;
Console.WriteLine("Bool value: " + boolValue.ToString());
byte[] bytes = BitConverter.GetBytes(boolValue);
Console.WriteLine("Byte array value: {0}", BitConverter.ToString(bytes));
Console.WriteLine("bytes len {0}", bytes.Length);
returns
Bool value: False
Byte array value: 00
bytes len 1
The neo-vm returns an empty array instead
neo-vm/src/neo-vm/Types/Boolean.cs
Line 9 in a3f9444
from neo-vm.
I just know that is the expected behaviour inside the virtual machine ... it could be strange, but new byte[]{ 0x00}
is false
too inside the virtual machine.
from neo-vm.
I understand that this is how the VM has been working up to now. I raise the question if that is actually what we want. I think
neo-vm/src/neo-vm/Types/Boolean.cs
Line 9 in a3f9444
should be
private static readonly byte[] FALSE = { 0x00 };
because then this
StackItem true_item = new Neo.VM.Types.Boolean(true);
StackItem false_item = new Neo.VM.Types.Boolean(false);
Console.WriteLine(true_item.GetByteLength());
Console.WriteLine(false_item.GetByteLength());
would not output
1
0
but
1
1
which is inline with how C#'s build in BitConverter
works and how many other languages convert.
Let's ask for more opinions: @igormcoelho @vncoelho
from neo-vm.
Related Issues (20)
- Shallow-copy problem for OrderedDictionary.CopyTo in case of Array & Map type HOT 6
- Add 'utf8Size' opcode HOT 4
- Script checks are incomplete
- Can I integrate this vm in to my custom projects? HOT 1
- Efficient way to pass Map type to contract method? OpCode.PACKMAP? HOT 1
- Adapt code documentation of UNPACK opcode
- ReferenceCounter can become negative HOT 18
- Is this `MEMCPY` expected to push nothing to the stack?
- ScriptBuilder should have an emitPush overload that takes a ReadOnlySpan<byte>
- How to customize an interface in interoperation service layer HOT 3
- Presssure test on v3.3.0 cause execution failed HOT 1
- v3.3.0 need more than 30 hours for syncing mainnet even it's just about 1.7million blocks HOT 2
- What is the purpose of the second assert of this test? HOT 1
- Instruction pointer reading PUSHDATA4 operand size as *signed* int32 HOT 1
- Missing push in APPEND?
- EmitPush(bool) should convert stackitem to right type
- Buffer to Integer conversion inconsistency HOT 1
- Override GetString for Buffer/ByteString ? HOT 4
- Set Operation in a Get Function HOT 2
- Add Equatable for StackItem
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from neo-vm.