Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Commit

Permalink
Compressed BigInteger format (NeoVM 3) (#173)
Browse files Browse the repository at this point in the history
* compressed biginteger

* PUSHBYTES0 testing

* closes #170

* simplify

* back to PUSH0

* cat int(0) with empty bytearray

* optimize
  • Loading branch information
igormcoelho authored and shargon committed Jun 15, 2019
1 parent 1282c92 commit b4382aa
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/neo-vm/OpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public enum OpCode : byte
{
// Constants
/// <summary>
/// An empty array of bytes is pushed onto the stack.
/// An empty array of bytes is pushed onto the stack.
/// This is equivalent to pushing Integer zero to the stack.
/// This is equivalent to pushing Boolean false to the stack.
/// </summary>
PUSH0 = 0x00,
PUSHF = PUSH0,
Expand Down
6 changes: 4 additions & 2 deletions src/neo-vm/Types/Integer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace Neo.VM.Types
{
public class Integer : StackItem
{
private static readonly byte[] ZeroBytes = new byte[0];

private BigInteger value;

public Integer(BigInteger value)
Expand Down Expand Up @@ -41,14 +43,14 @@ public override bool GetBoolean()

public override byte[] GetByteArray()
{
return value.ToByteArray();
return value.IsZero ? ZeroBytes : value.ToByteArray();
}

private int _length = -1;
public override int GetByteLength()
{
if (_length == -1)
_length = value.ToByteArray().Length;
_length = GetByteArray().Length;
return _length;
}
}
Expand Down
55 changes: 55 additions & 0 deletions tests/neo-vm.Tests/Tests/OpCodes/Push/PUSH0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"category": "Push",
"name": "PUSH0",
"tests":
[
{
"name": "Good definition",
"script": "0x00",
"steps":
[
{
"actions":
[
"StepInto"
],
"result":
{
"state": "Break",
"invocationStack":
[
{
"instructionPointer": 1,
"nextInstruction": "RET",
"evaluationStack":
[
{
"type": "ByteArray",
"value": ""
}
]
}
]
}
},
{
"actions":
[
"StepInto"
],
"result":
{
"state": "Halt",
"resultStack":
[
{
"type": "ByteArray",
"value": ""
}
]
}
}
]
}
]
}
54 changes: 54 additions & 0 deletions tests/neo-vm.Tests/Tests/OpCodes/Splice/CAT.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,60 @@
}
}
]
},
{
"name": "CAT int(0) with empty ByteArray",
"script": "0x518C007E",
"steps":
[
{
"actions":
[
"StepInto",
"StepInto",
"StepInto"
],
"result":
{
"state": "Break",
"invocationStack":
[
{
"instructionPointer": 3,
"nextInstruction": "CAT",
"evaluationStack":
[
{
"type": "ByteArray",
"value": ""
},
{
"type": "Integer",
"value": 0
}
]
}
]
}
},
{
"actions":
[
"Execute"
],
"result":
{
"state": "Halt",
"resultStack":
[
{
"type": "ByteArray",
"value": ""
}
]
}
}
]
}
]
}

0 comments on commit b4382aa

Please sign in to comment.