vm: make IntToBytes, BytesToInt public
VM serialization format should be able to be reused.
This commit is contained in:
parent
9037cabe1e
commit
f15ceff592
6 changed files with 16 additions and 16 deletions
|
@ -9,9 +9,9 @@ import (
|
|||
// wordSizeBytes is a size of a big.Word (uint) in bytes.`
|
||||
const wordSizeBytes = bits.UintSize / 8
|
||||
|
||||
// bytesToInt converts data in little-endian format to
|
||||
// BytesToInt converts data in little-endian format to
|
||||
// an integer.
|
||||
func bytesToInt(data []byte) *big.Int {
|
||||
func BytesToInt(data []byte) *big.Int {
|
||||
n := new(big.Int)
|
||||
size := len(data)
|
||||
if size == 0 {
|
||||
|
@ -79,8 +79,8 @@ func getEffectiveSize(buf []byte, isNeg bool) int {
|
|||
return size
|
||||
}
|
||||
|
||||
// intToBytes converts integer to a slice in little-endian format.
|
||||
func intToBytes(n *big.Int) []byte {
|
||||
// IntToBytes converts integer to a slice in little-endian format.
|
||||
func IntToBytes(n *big.Int) []byte {
|
||||
sign := n.Sign()
|
||||
if sign == 0 {
|
||||
return []byte{0}
|
||||
|
|
|
@ -106,19 +106,19 @@ var testCases = []struct {
|
|||
|
||||
func TestIntToBytes(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
buf := intToBytes(big.NewInt(tc.number))
|
||||
buf := IntToBytes(big.NewInt(tc.number))
|
||||
assert.Equal(t, tc.buf, buf, "error while converting %d", tc.number)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBytesToInt(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
num := bytesToInt(tc.buf)
|
||||
num := BytesToInt(tc.buf)
|
||||
assert.Equal(t, tc.number, num.Int64(), "error while converting %d", tc.number)
|
||||
}
|
||||
|
||||
t.Run("empty array", func(t *testing.T) {
|
||||
require.EqualValues(t, 0, bytesToInt([]byte{}).Int64())
|
||||
require.EqualValues(t, 0, BytesToInt([]byte{}).Int64())
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -131,7 +131,7 @@ func TestEquivalentRepresentations(t *testing.T) {
|
|||
buf = append(buf, 0xFF, 0xFF, 0xFF)
|
||||
}
|
||||
|
||||
num := bytesToInt(buf)
|
||||
num := BytesToInt(buf)
|
||||
assert.Equal(t, tc.number, num.Int64(), "error while converting %d", tc.number)
|
||||
}
|
||||
}
|
||||
|
@ -170,16 +170,16 @@ func TestVeryBigInts(t *testing.T) {
|
|||
num, ok := new(big.Int).SetString(tc.numStr, 10)
|
||||
assert.True(t, ok)
|
||||
|
||||
result := bytesToInt(tc.buf)
|
||||
result := BytesToInt(tc.buf)
|
||||
assert.Equal(t, num, result, "error while converting %s from bytes", tc.numStr)
|
||||
assert.Equal(t, tc.buf, intToBytes(result), "error while converting %s to bytes", tc.numStr)
|
||||
assert.Equal(t, tc.buf, IntToBytes(result), "error while converting %s to bytes", tc.numStr)
|
||||
}
|
||||
|
||||
for _, tc := range stdlibCases {
|
||||
num, ok := new(big.Int).SetString(tc.numStr, 10)
|
||||
assert.True(t, ok)
|
||||
|
||||
result := bytesToInt(util.ArrayReverse(tc.buf))
|
||||
result := BytesToInt(util.ArrayReverse(tc.buf))
|
||||
assert.Equal(t, num, result, "error while converting %s from bytes", tc.numStr)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -196,7 +196,7 @@ func compareItems(t *testing.T, a, b StackItem) {
|
|||
case *BigIntegerItem:
|
||||
require.Equal(t, val, ac.value.Int64())
|
||||
case *ByteArrayItem:
|
||||
require.Equal(t, val, bytesToInt(ac.value).Int64())
|
||||
require.Equal(t, val, BytesToInt(ac.value).Int64())
|
||||
case *BoolItem:
|
||||
if ac.value {
|
||||
require.Equal(t, val, int64(1))
|
||||
|
|
|
@ -48,7 +48,7 @@ func serializeItemTo(item StackItem, w *io.BinWriter, seen map[StackItem]bool) {
|
|||
w.WriteBool(t.value)
|
||||
case *BigIntegerItem:
|
||||
w.WriteBytes([]byte{byte(integerT)})
|
||||
w.WriteVarBytes(intToBytes(t.value))
|
||||
w.WriteVarBytes(IntToBytes(t.value))
|
||||
case *InteropItem:
|
||||
w.Err = errors.New("not supported")
|
||||
case *ArrayItem, *StructItem:
|
||||
|
@ -106,7 +106,7 @@ func DecodeBinaryStackItem(r *io.BinReader) StackItem {
|
|||
return NewBoolItem(b)
|
||||
case integerT:
|
||||
data := r.ReadVarBytes()
|
||||
num := bytesToInt(data)
|
||||
num := BytesToInt(data)
|
||||
return &BigIntegerItem{
|
||||
value: num,
|
||||
}
|
||||
|
|
|
@ -81,7 +81,7 @@ func (e *Element) BigInt() *big.Int {
|
|||
return big.NewInt(0)
|
||||
default:
|
||||
b := t.Value().([]uint8)
|
||||
return bytesToInt(b)
|
||||
return BytesToInt(b)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -142,7 +142,7 @@ func NewBigIntegerItem(value int) *BigIntegerItem {
|
|||
|
||||
// Bytes converts i to a slice of bytes.
|
||||
func (i *BigIntegerItem) Bytes() []byte {
|
||||
return intToBytes(i.value)
|
||||
return IntToBytes(i.value)
|
||||
}
|
||||
|
||||
// Value implements StackItem interface.
|
||||
|
|
Loading…
Reference in a new issue