ionio-lang / ivy-elements

Fork of Ivy Language to write Elements Smart Contracts
https://ionio-lang.org
MIT License
0 stars 3 forks source link

Add Bytes type #19

Open altafan opened 2 years ago

altafan commented 2 years ago

We need to add the built-in type for bytes to Ionio:

tiero commented 2 years ago

casting from smaller to bigger, should add 00 bytes, meanwhile the opposite should truncate?

altafan commented 2 years ago

yes

altafan commented 2 years ago

All these types were inspired from caschscript but the Bytes type is just enough for this fork of Ivy.

Casting to bytesN would just mean dropping some trailing bytes from a given one but still a Bytes type can be returned.

Adding BytesN types would mean giving more context to the literal type by making sure the inner value does not exceed a fixed length. I think the effort for this does not stand the real usefulness of these types.

@tiero @bordalix what do you think? We might want to close this for now?

tiero commented 2 years ago

there are occasions when you are doing a binaryExpression between an element pushed by an opcode and something on the stack (ie. provider by the developer)

Like OP_INSPECTVERSION pushes a a bytes4, passing 8 bytes in the stack and compare them (OP_EQUAL) will make to return false.

ie.

bytes(2) === tx.version

It should implicitly transform/truncate to 4 bytes, knowing the right operator result type.

So it's fine to only expose bytes to the develper, but internally we must known the lenght of the bytes to perform proper comparisons operations.

bordalix commented 2 years ago

I would postpone this, for several reasons:

  1. If the purpose of Ionio is to help people better understand Script, adding these types will increase the complexity for the end user. The beauty of it is that it has the bare minimum for people to experiment with Script. I would keep it as simple as possible.

  2. It not reflects the underlaying tech, since, from what I know from Script, the stack stores byte vectors of up to 520 bytes, but opcodes which take integers and bools off the stack require that they be no more than 4 bytes long. So it seems at the stack layer we only have bytes, integers and bools.

  3. While reading about Script to answer this, I found this, which means we would have to check if a bytes4 + bytes4 is still a bytes4 or a bytes8. Again, is adding complexity without obvious gains:

Screenshot 2022-01-11 at 15 41 09

Just my 2 satoshis.