forked from TrueCloudLab/neoneo-go
core: remove System.Binary.[Base64*, Base58*] syscalls
And move their tests to native StdLib.
This commit is contained in:
parent
4d2ad4b9e2
commit
5c9c168ee5
7 changed files with 58 additions and 130 deletions
|
@ -61,10 +61,6 @@ func TestSyscallExecution(t *testing.T) {
|
|||
sigs := "[]interop.Signature{" + sig + "}"
|
||||
sctx := "storage.Context{}"
|
||||
interops := map[string]syscallTestCase{
|
||||
"binary.Base58Decode": {interopnames.SystemBinaryBase58Decode, []string{b}, false},
|
||||
"binary.Base58Encode": {interopnames.SystemBinaryBase58Encode, []string{b}, false},
|
||||
"binary.Base64Decode": {interopnames.SystemBinaryBase64Decode, []string{b}, false},
|
||||
"binary.Base64Encode": {interopnames.SystemBinaryBase64Encode, []string{b}, false},
|
||||
"binary.Deserialize": {interopnames.SystemBinaryDeserialize, []string{b}, false},
|
||||
"binary.Serialize": {interopnames.SystemBinarySerialize, []string{"10"}, false},
|
||||
"contract.Call": {interopnames.SystemContractCall, []string{u160, `"m"`, "1", "3"}, false},
|
||||
|
@ -203,20 +199,20 @@ func TestNotify(t *testing.T) {
|
|||
|
||||
func TestSyscallInGlobalInit(t *testing.T) {
|
||||
src := `package foo
|
||||
import "github.com/nspcc-dev/neo-go/pkg/interop/binary"
|
||||
var a = binary.Base58Decode([]byte("5T"))
|
||||
func Main() []byte {
|
||||
import "github.com/nspcc-dev/neo-go/pkg/interop/runtime"
|
||||
var a = runtime.CheckWitness([]byte("5T"))
|
||||
func Main() bool {
|
||||
return a
|
||||
}`
|
||||
v, s := vmAndCompileInterop(t, src)
|
||||
s.interops[interopnames.ToID([]byte(interopnames.SystemBinaryBase58Decode))] = func(v *vm.VM) error {
|
||||
s.interops[interopnames.ToID([]byte(interopnames.SystemRuntimeCheckWitness))] = func(v *vm.VM) error {
|
||||
s := v.Estack().Pop().Value().([]byte)
|
||||
require.Equal(t, "5T", string(s))
|
||||
v.Estack().PushVal([]byte{1, 2})
|
||||
v.Estack().PushVal(true)
|
||||
return nil
|
||||
}
|
||||
require.NoError(t, v.Run())
|
||||
require.Equal(t, []byte{1, 2}, v.Estack().Pop().Value())
|
||||
require.Equal(t, true, v.Estack().Pop().Value())
|
||||
}
|
||||
|
||||
func TestOpcode(t *testing.T) {
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package binary
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
)
|
||||
|
@ -17,41 +14,3 @@ func Serialize(ic *interop.Context) error {
|
|||
func Deserialize(ic *interop.Context) error {
|
||||
return vm.RuntimeDeserialize(ic.VM)
|
||||
}
|
||||
|
||||
// EncodeBase64 encodes top stack item into a base64 string.
|
||||
func EncodeBase64(ic *interop.Context) error {
|
||||
src := ic.VM.Estack().Pop().Bytes()
|
||||
result := base64.StdEncoding.EncodeToString(src)
|
||||
ic.VM.Estack().PushVal([]byte(result))
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeBase64 decodes top stack item from base64 string to byte array.
|
||||
func DecodeBase64(ic *interop.Context) error {
|
||||
src := ic.VM.Estack().Pop().String()
|
||||
result, err := base64.StdEncoding.DecodeString(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ic.VM.Estack().PushVal(result)
|
||||
return nil
|
||||
}
|
||||
|
||||
// EncodeBase58 encodes top stack item into a base58 string.
|
||||
func EncodeBase58(ic *interop.Context) error {
|
||||
src := ic.VM.Estack().Pop().Bytes()
|
||||
result := base58.Encode(src)
|
||||
ic.VM.Estack().PushVal([]byte(result))
|
||||
return nil
|
||||
}
|
||||
|
||||
// DecodeBase58 decodes top stack item from base58 string to byte array.
|
||||
func DecodeBase58(ic *interop.Context) error {
|
||||
src := ic.VM.Estack().Pop().String()
|
||||
result, err := base58.Decode(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ic.VM.Estack().PushVal(result)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,11 +1,9 @@
|
|||
package binary
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
|
@ -49,44 +47,3 @@ func TestRuntimeSerialize(t *testing.T) {
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestRuntimeEncodeDecode(t *testing.T) {
|
||||
original := []byte("my pretty string")
|
||||
encoded64 := base64.StdEncoding.EncodeToString(original)
|
||||
encoded58 := base58.Encode(original)
|
||||
v := vm.New()
|
||||
ic := &interop.Context{VM: v}
|
||||
|
||||
t.Run("Encode64", func(t *testing.T) {
|
||||
v.Estack().PushVal(original)
|
||||
require.NoError(t, EncodeBase64(ic))
|
||||
actual := v.Estack().Pop().Bytes()
|
||||
require.Equal(t, []byte(encoded64), actual)
|
||||
})
|
||||
t.Run("Encode58", func(t *testing.T) {
|
||||
v.Estack().PushVal(original)
|
||||
require.NoError(t, EncodeBase58(ic))
|
||||
actual := v.Estack().Pop().Bytes()
|
||||
require.Equal(t, []byte(encoded58), actual)
|
||||
})
|
||||
t.Run("Decode64/positive", func(t *testing.T) {
|
||||
v.Estack().PushVal(encoded64)
|
||||
require.NoError(t, DecodeBase64(ic))
|
||||
actual := v.Estack().Pop().Bytes()
|
||||
require.Equal(t, original, actual)
|
||||
})
|
||||
t.Run("Decode64/error", func(t *testing.T) {
|
||||
v.Estack().PushVal(encoded64 + "%")
|
||||
require.Error(t, DecodeBase64(ic))
|
||||
})
|
||||
t.Run("Decode58/positive", func(t *testing.T) {
|
||||
v.Estack().PushVal(encoded58)
|
||||
require.NoError(t, DecodeBase58(ic))
|
||||
actual := v.Estack().Pop().Bytes()
|
||||
require.Equal(t, original, actual)
|
||||
})
|
||||
t.Run("Decode58/error", func(t *testing.T) {
|
||||
v.Estack().PushVal(encoded58 + "%")
|
||||
require.Error(t, DecodeBase58(ic))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,10 +2,6 @@ package interopnames
|
|||
|
||||
// Names of all used interops.
|
||||
const (
|
||||
SystemBinaryBase58Decode = "System.Binary.Base58Decode"
|
||||
SystemBinaryBase58Encode = "System.Binary.Base58Encode"
|
||||
SystemBinaryBase64Decode = "System.Binary.Base64Decode"
|
||||
SystemBinaryBase64Encode = "System.Binary.Base64Encode"
|
||||
SystemBinaryDeserialize = "System.Binary.Deserialize"
|
||||
SystemBinarySerialize = "System.Binary.Serialize"
|
||||
SystemCallbackCreate = "System.Callback.Create"
|
||||
|
@ -50,10 +46,6 @@ const (
|
|||
)
|
||||
|
||||
var names = []string{
|
||||
SystemBinaryBase58Decode,
|
||||
SystemBinaryBase58Encode,
|
||||
SystemBinaryBase64Decode,
|
||||
SystemBinaryBase64Encode,
|
||||
SystemBinaryDeserialize,
|
||||
SystemBinarySerialize,
|
||||
SystemCallbackCreate,
|
||||
|
|
|
@ -31,10 +31,6 @@ func SpawnVM(ic *interop.Context) *vm.VM {
|
|||
|
||||
// All lists are sorted, keep 'em this way, please.
|
||||
var systemInterops = []interop.Function{
|
||||
{Name: interopnames.SystemBinaryBase58Decode, Func: binary.DecodeBase58, Price: 1 << 12, ParamCount: 1},
|
||||
{Name: interopnames.SystemBinaryBase58Encode, Func: binary.EncodeBase58, Price: 1 << 12, ParamCount: 1},
|
||||
{Name: interopnames.SystemBinaryBase64Decode, Func: binary.DecodeBase64, Price: 1 << 12, ParamCount: 1},
|
||||
{Name: interopnames.SystemBinaryBase64Encode, Func: binary.EncodeBase64, Price: 1 << 12, ParamCount: 1},
|
||||
{Name: interopnames.SystemBinaryDeserialize, Func: binary.Deserialize, Price: 1 << 14, ParamCount: 1},
|
||||
{Name: interopnames.SystemBinarySerialize, Func: binary.Serialize, Price: 1 << 12, ParamCount: 1},
|
||||
{Name: interopnames.SystemContractCall, Func: contract.Call, Price: 1 << 15,
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package native
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
"github.com/mr-tron/base58"
|
||||
"github.com/nspcc-dev/neo-go/pkg/core/interop"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
|
@ -138,3 +140,53 @@ func TestStdLibJSON(t *testing.T) {
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestStdLibEncodeDecode(t *testing.T) {
|
||||
s := newStd()
|
||||
original := []byte("my pretty string")
|
||||
encoded64 := base64.StdEncoding.EncodeToString(original)
|
||||
encoded58 := base58.Encode(original)
|
||||
ic := &interop.Context{VM: vm.New()}
|
||||
var actual stackitem.Item
|
||||
|
||||
t.Run("Encode64", func(t *testing.T) {
|
||||
require.NotPanics(t, func() {
|
||||
actual = s.base64Encode(ic, []stackitem.Item{stackitem.Make(original)})
|
||||
})
|
||||
require.Equal(t, stackitem.Make(encoded64), actual)
|
||||
})
|
||||
t.Run("Encode58", func(t *testing.T) {
|
||||
require.NotPanics(t, func() {
|
||||
actual = s.base58Encode(ic, []stackitem.Item{stackitem.Make(original)})
|
||||
})
|
||||
require.Equal(t, stackitem.Make(encoded58), actual)
|
||||
})
|
||||
t.Run("Decode64/positive", func(t *testing.T) {
|
||||
require.NotPanics(t, func() {
|
||||
actual = s.base64Decode(ic, []stackitem.Item{stackitem.Make(encoded64)})
|
||||
})
|
||||
require.Equal(t, stackitem.Make(original), actual)
|
||||
})
|
||||
t.Run("Decode64/error", func(t *testing.T) {
|
||||
require.Panics(t, func() {
|
||||
_ = s.base64Decode(ic, []stackitem.Item{stackitem.Make(encoded64 + "%")})
|
||||
})
|
||||
require.Panics(t, func() {
|
||||
_ = s.base64Decode(ic, []stackitem.Item{stackitem.NewInterop(nil)})
|
||||
})
|
||||
})
|
||||
t.Run("Decode58/positive", func(t *testing.T) {
|
||||
require.NotPanics(t, func() {
|
||||
actual = s.base58Decode(ic, []stackitem.Item{stackitem.Make(encoded58)})
|
||||
})
|
||||
require.Equal(t, stackitem.Make(original), actual)
|
||||
})
|
||||
t.Run("Decode58/error", func(t *testing.T) {
|
||||
require.Panics(t, func() {
|
||||
_ = s.base58Decode(ic, []stackitem.Item{stackitem.Make(encoded58 + "%")})
|
||||
})
|
||||
require.Panics(t, func() {
|
||||
_ = s.base58Decode(ic, []stackitem.Item{stackitem.NewInterop(nil)})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -20,27 +20,3 @@ func Serialize(item interface{}) []byte {
|
|||
func Deserialize(b []byte) interface{} {
|
||||
return neogointernal.Syscall1("System.Binary.Deserialize", b)
|
||||
}
|
||||
|
||||
// Base64Encode encodes given byte slice into a base64 string and returns byte
|
||||
// representation of this string. It uses `System.Binary.Base64Encode` interop.
|
||||
func Base64Encode(b []byte) string {
|
||||
return neogointernal.Syscall1("System.Binary.Base64Encode", b).(string)
|
||||
}
|
||||
|
||||
// Base64Decode decodes given base64 string represented as a byte slice into
|
||||
// byte slice. It uses `System.Binary.Base64Decode` interop.
|
||||
func Base64Decode(b []byte) []byte {
|
||||
return neogointernal.Syscall1("System.Binary.Base64Decode", b).([]byte)
|
||||
}
|
||||
|
||||
// Base58Encode encodes given byte slice into a base58 string and returns byte
|
||||
// representation of this string. It uses `System.Binary.Base58Encode` syscall.
|
||||
func Base58Encode(b []byte) string {
|
||||
return neogointernal.Syscall1("System.Binary.Base58Encode", b).(string)
|
||||
}
|
||||
|
||||
// Base58Decode decodes given base58 string represented as a byte slice into
|
||||
// a new byte slice. It uses `System.Binary.Base58Decode` syscall.
|
||||
func Base58Decode(b []byte) []byte {
|
||||
return neogointernal.Syscall1("System.Binary.Base58Decode", b).([]byte)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue