Merge pull request #2228 from nspcc-dev/additional-interop

Constants and ABORT in interops
This commit is contained in:
Roman Khimov 2021-10-22 21:45:57 +03:00 committed by GitHub
commit 6fe8f4565d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 50 additions and 2 deletions

View file

@ -17,7 +17,7 @@ var (
goBuiltins = []string{"len", "append", "panic", "make", "copy", "recover", "delete"}
// Custom builtin utility functions.
customBuiltins = []string{
"FromAddress", "Equals", "Remove",
"Abort", "FromAddress", "Equals", "Remove",
"ToBool", "ToBytes", "ToString", "ToInteger",
}
)

View file

@ -1724,6 +1724,8 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
c.emitStoreByIndex(varGlobal, c.exceptionIndex)
case "delete":
emit.Opcodes(c.prog.BinWriter, opcode.REMOVE)
case "Abort":
emit.Opcodes(c.prog.BinWriter, opcode.ABORT)
case "Remove":
if !isCompoundSlice(c.typeOf(expr.Args[0])) {
c.prog.Err = errors.New("`Remove` supports only non-byte slices")

View file

@ -116,6 +116,18 @@ func TestFromAddress(t *testing.T) {
})
}
func TestAbort(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/util"
func Main() int {
util.Abort()
return 1
}`
v := vmAndCompile(t, src)
require.Error(t, v.Run())
require.True(t, v.HasFailed())
}
func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM {
b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src))
require.NoError(t, err)

View file

@ -1,5 +1,10 @@
package neogointernal
// Opcode0 emits opcode without arguments.
func Opcode0(op string) interface{} {
return nil
}
// Opcode1 emits opcode with 1 argument.
func Opcode1(op string, arg interface{}) interface{} {
return nil

View file

@ -1,5 +1,25 @@
package interop
const (
// Hash160Len is the length of proper Hash160 in bytes, use it to
// sanitize input parameters.
Hash160Len = 20
// Hash256Len is the length of proper Hash256 in bytes, use it to
// sanitize input parameters.
Hash256Len = 32
// PublicKeyCompressedLen is the length of compressed public key (which
// is the most common public key type), use it to sanitize input
// parameters.
PublicKeyCompressedLen = 33
// PublicKeyUncompressedLen is the length of uncompressed public key
// (but you're not likely to ever encounter that), use it to sanitize
// input parameters.
PublicKeyUncompressedLen = 65
// SignatureLen is the length of standard signature, use it to sanitize
// input parameters.
SignatureLen = 64
)
// Signature represents 64-byte signature.
type Signature []byte

View file

@ -3,7 +3,16 @@ Package util contains some special useful functions that are provided by compile
*/
package util
import "github.com/nspcc-dev/neo-go/pkg/interop"
import (
"github.com/nspcc-dev/neo-go/pkg/interop"
"github.com/nspcc-dev/neo-go/pkg/interop/neogointernal"
)
// Abort terminates current execution, unlike exception throwing with panic() it
// can't be recovered from.
func Abort() {
_ = neogointernal.Opcode0("ABORT")
}
// FromAddress is an utility function that converts a Neo address to its hash
// (160 bit BE value in a 20 byte slice). It can only be used for strings known