vm: move IntToBytes and BytesToInt to emit package

This commit is contained in:
Evgenii Stratonikov 2020-02-03 18:05:13 +03:00
parent 4d8a3a359b
commit c821e1c4c8
6 changed files with 15 additions and 7 deletions

View file

@ -1,4 +1,4 @@
package vm package emit
import ( import (
"encoding/binary" "encoding/binary"

View file

@ -1,4 +1,4 @@
package vm package emit
import ( import (
"math" "math"

View file

@ -13,6 +13,8 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/CityOfZion/neo-go/pkg/vm/emit"
"github.com/CityOfZion/neo-go/pkg/crypto/hash" "github.com/CityOfZion/neo-go/pkg/crypto/hash"
"github.com/CityOfZion/neo-go/pkg/util" "github.com/CityOfZion/neo-go/pkg/util"
"github.com/CityOfZion/neo-go/pkg/vm/opcode" "github.com/CityOfZion/neo-go/pkg/vm/opcode"
@ -196,7 +198,7 @@ func compareItems(t *testing.T, a, b StackItem) {
case *BigIntegerItem: case *BigIntegerItem:
require.Equal(t, val, ac.value.Int64()) require.Equal(t, val, ac.value.Int64())
case *ByteArrayItem: case *ByteArrayItem:
require.Equal(t, val, BytesToInt(ac.value).Int64()) require.Equal(t, val, emit.BytesToInt(ac.value).Int64())
case *BoolItem: case *BoolItem:
if ac.value { if ac.value {
require.Equal(t, val, int64(1)) require.Equal(t, val, int64(1))

View file

@ -3,6 +3,8 @@ package vm
import ( import (
"errors" "errors"
"github.com/CityOfZion/neo-go/pkg/vm/emit"
"github.com/CityOfZion/neo-go/pkg/io" "github.com/CityOfZion/neo-go/pkg/io"
) )
@ -48,7 +50,7 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
w.WriteBool(t.value) w.WriteBool(t.value)
case *BigIntegerItem: case *BigIntegerItem:
w.WriteBytes([]byte{byte(integerT)}) w.WriteBytes([]byte{byte(integerT)})
w.WriteVarBytes(IntToBytes(t.value)) w.WriteVarBytes(emit.IntToBytes(t.value))
case *InteropItem: case *InteropItem:
w.Err = errors.New("not supported") w.Err = errors.New("not supported")
case *ArrayItem, *StructItem: case *ArrayItem, *StructItem:
@ -106,7 +108,7 @@ func DecodeBinaryStackItem(r *io.BinReader) StackItem {
return NewBoolItem(b) return NewBoolItem(b)
case integerT: case integerT:
data := r.ReadVarBytes() data := r.ReadVarBytes()
num := BytesToInt(data) num := emit.BytesToInt(data)
return &BigIntegerItem{ return &BigIntegerItem{
value: num, value: num,
} }

View file

@ -5,6 +5,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"github.com/CityOfZion/neo-go/pkg/vm/emit"
) )
// Stack implementation for the neo-go virtual machine. The stack implements // Stack implementation for the neo-go virtual machine. The stack implements
@ -81,7 +83,7 @@ func (e *Element) BigInt() *big.Int {
return big.NewInt(0) return big.NewInt(0)
default: default:
b := t.Value().([]uint8) b := t.Value().([]uint8)
return BytesToInt(b) return emit.BytesToInt(b)
} }
} }

View file

@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"math/big" "math/big"
"reflect" "reflect"
"github.com/CityOfZion/neo-go/pkg/vm/emit"
) )
// A StackItem represents the "real" value that is pushed on the stack. // A StackItem represents the "real" value that is pushed on the stack.
@ -142,7 +144,7 @@ func NewBigIntegerItem(value int) *BigIntegerItem {
// Bytes converts i to a slice of bytes. // Bytes converts i to a slice of bytes.
func (i *BigIntegerItem) Bytes() []byte { func (i *BigIntegerItem) Bytes() []byte {
return IntToBytes(i.value) return emit.IntToBytes(i.value)
} }
// Value implements StackItem interface. // Value implements StackItem interface.