From b4382aa0fb36d831f70e9f13dca232e0f2caf914 Mon Sep 17 00:00:00 2001 From: Igor Machado Coelho Date: Sat, 15 Jun 2019 05:52:05 -0300 Subject: [PATCH] Compressed BigInteger format (NeoVM 3) (#173) * compressed biginteger * PUSHBYTES0 testing * closes #170 * simplify * back to PUSH0 * cat int(0) with empty bytearray * optimize --- src/neo-vm/OpCode.cs | 4 +- src/neo-vm/Types/Integer.cs | 6 +- .../Tests/OpCodes/Push/PUSH0.json | 55 +++++++++++++++++++ .../Tests/OpCodes/Splice/CAT.json | 54 ++++++++++++++++++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tests/neo-vm.Tests/Tests/OpCodes/Push/PUSH0.json diff --git a/src/neo-vm/OpCode.cs b/src/neo-vm/OpCode.cs index b7258ded..dfa8516b 100644 --- a/src/neo-vm/OpCode.cs +++ b/src/neo-vm/OpCode.cs @@ -4,7 +4,9 @@ public enum OpCode : byte { // Constants /// - /// 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. /// PUSH0 = 0x00, PUSHF = PUSH0, diff --git a/src/neo-vm/Types/Integer.cs b/src/neo-vm/Types/Integer.cs index b3f91ca0..25d03ced 100644 --- a/src/neo-vm/Types/Integer.cs +++ b/src/neo-vm/Types/Integer.cs @@ -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) @@ -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; } } diff --git a/tests/neo-vm.Tests/Tests/OpCodes/Push/PUSH0.json b/tests/neo-vm.Tests/Tests/OpCodes/Push/PUSH0.json new file mode 100644 index 00000000..12b65179 --- /dev/null +++ b/tests/neo-vm.Tests/Tests/OpCodes/Push/PUSH0.json @@ -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": "" + } + ] + } + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/neo-vm.Tests/Tests/OpCodes/Splice/CAT.json b/tests/neo-vm.Tests/Tests/OpCodes/Splice/CAT.json index cc8e7cb5..ace4910e 100644 --- a/tests/neo-vm.Tests/Tests/OpCodes/Splice/CAT.json +++ b/tests/neo-vm.Tests/Tests/OpCodes/Splice/CAT.json @@ -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": "" + } + ] + } + } + ] } ] } \ No newline at end of file