vm: serialize zero Integer to an empty ByteArray

This commit is contained in:
Evgenii Stratonikov 2020-04-15 17:02:45 +03:00
parent 5dba30a49d
commit 93d2a3e031
3 changed files with 7 additions and 4 deletions

View file

@ -80,10 +80,13 @@ func getEffectiveSize(buf []byte, isNeg bool) int {
} }
// IntToBytes converts integer to a slice in little-endian format. // IntToBytes converts integer to a slice in little-endian format.
// Note: NEO3 serialization differs from default C# BigInteger.ToByteArray()
// when n == 0. For zero is equal to empty slice in NEO3.
// https://github.com/neo-project/neo-vm/blob/master/src/neo-vm/Types/Integer.cs#L16
func IntToBytes(n *big.Int) []byte { func IntToBytes(n *big.Int) []byte {
sign := n.Sign() sign := n.Sign()
if sign == 0 { if sign == 0 {
return []byte{0} return []byte{}
} }
var ws []big.Word var ws []big.Word

View file

@ -16,7 +16,7 @@ var testCases = []struct {
number int64 number int64
buf []byte buf []byte
}{ }{
{0, []byte{0}}, {0, []byte{}},
{1, []byte{1}}, {1, []byte{1}},
{-1, []byte{255}}, {-1, []byte{255}},
{2, []byte{2}}, {2, []byte{2}},
@ -52,7 +52,7 @@ var testCases = []struct {
{-9187484529235886209, []byte{127, 127, 127, 127, 127, 127, 127, 128}}, {-9187484529235886209, []byte{127, 127, 127, 127, 127, 127, 127, 128}},
// https://github.com/dotnet/runtime/blob/master/src/libraries/System.Runtime.Numerics/tests/BigInteger/ToByteArray.cs#L14 // https://github.com/dotnet/runtime/blob/master/src/libraries/System.Runtime.Numerics/tests/BigInteger/ToByteArray.cs#L14
{0, []byte{0x00}}, // {0, []byte{0x00}}, commented because this test check default `BigInteger` serialization, not NEO3 serialization.
{3, []byte{0x03}}, {3, []byte{0x03}},
{128, []byte{0x80, 0x00}}, {128, []byte{0x80, 0x00}},
{200, []byte{0xc8, 0x00}}, {200, []byte{0xc8, 0x00}},

View file

@ -2213,7 +2213,7 @@ func TestCATInt0ByteArray(t *testing.T) {
vm.estack.PushVal([]byte{}) vm.estack.PushVal([]byte{})
runVM(t, vm) runVM(t, vm)
assert.Equal(t, 1, vm.estack.Len()) assert.Equal(t, 1, vm.estack.Len())
assert.Equal(t, &ByteArrayItem{[]byte{0}}, vm.estack.Pop().value) assert.Equal(t, &ByteArrayItem{[]byte{}}, vm.estack.Pop().value)
} }
func TestCATByteArrayInt1(t *testing.T) { func TestCATByteArrayInt1(t *testing.T) {