From b780a64b4d2506ebf134c3bae767d45470023175 Mon Sep 17 00:00:00 2001 From: Evgeniy Stratonikov Date: Fri, 5 Mar 2021 14:16:34 +0300 Subject: [PATCH] emit: allow to emit `big.Int` --- pkg/vm/emit/emit.go | 19 ++++++++++++++----- pkg/vm/emit/emit_test.go | 10 +++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pkg/vm/emit/emit.go b/pkg/vm/emit/emit.go index 31ea60697..00f7c1b44 100644 --- a/pkg/vm/emit/emit.go +++ b/pkg/vm/emit/emit.go @@ -59,14 +59,21 @@ func Int(w *io.BinWriter, i int64) { val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i)) Opcodes(w, val) default: - buf := bigint.ToPreallocatedBytes(big.NewInt(i), make([]byte, 0, 32)) - // l != 0 becase of switch - padSize := byte(8 - bits.LeadingZeros8(byte(len(buf)-1))) - Opcodes(w, opcode.PUSHINT8+opcode.Opcode(padSize)) - w.WriteBytes(padRight(1<= 0; i-- { @@ -75,6 +82,8 @@ func Array(w *io.BinWriter, es ...interface{}) { Array(w, e...) case int64: Int(w, e) + case *big.Int: + bigInt(w, e) case string: String(w, e) case util.Uint160: diff --git a/pkg/vm/emit/emit_test.go b/pkg/vm/emit/emit_test.go index 7d1fd9aa2..ea5c996a1 100644 --- a/pkg/vm/emit/emit_test.go +++ b/pkg/vm/emit/emit_test.go @@ -3,6 +3,8 @@ package emit import ( "encoding/binary" "errors" + "math" + "math/big" "testing" "github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames" @@ -144,7 +146,10 @@ func TestBytes(t *testing.T) { func TestEmitArray(t *testing.T) { t.Run("good", func(t *testing.T) { buf := io.NewBufBinWriter() - Array(buf.BinWriter, []interface{}{int64(1), int64(2)}, nil, int64(1), "str", true, []byte{0xCA, 0xFE}) + veryBig := new(big.Int).SetUint64(math.MaxUint64) + veryBig.Add(veryBig, big.NewInt(1)) + Array(buf.BinWriter, big.NewInt(0), veryBig, + []interface{}{int64(1), int64(2)}, nil, int64(1), "str", true, []byte{0xCA, 0xFE}) require.NoError(t, buf.Err) res := buf.Bytes() @@ -163,6 +168,9 @@ func TestEmitArray(t *testing.T) { assert.EqualValues(t, opcode.PUSH1, res[15]) assert.EqualValues(t, opcode.PUSH2, res[16]) assert.EqualValues(t, opcode.PACK, res[17]) + assert.EqualValues(t, opcode.PUSHINT128, res[18]) + assert.EqualValues(t, veryBig, bigint.FromBytes(res[19:35])) + assert.EqualValues(t, opcode.PUSH0, res[35]) }) t.Run("empty", func(t *testing.T) {