GithubHelp home page GithubHelp logo

Comments (5)

igormcoelho avatar igormcoelho commented on June 12, 2024 1

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.

ixje avatar ixje commented on June 12, 2024

@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 :)

"type": "Integer",
"value": 0

from neo-vm.

ixje avatar ixje commented on June 12, 2024

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

private static readonly byte[] FALSE = new byte[0];

from neo-vm.

shargon avatar shargon commented on June 12, 2024

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.

ixje avatar ixje commented on June 12, 2024

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

private static readonly byte[] FALSE = new byte[0];

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.