2020-02-03 14:24:57 +00:00
|
|
|
package emit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-04-21 13:45:48 +00:00
|
|
|
"math/bits"
|
2020-02-03 14:24:57 +00:00
|
|
|
|
2020-08-13 07:41:33 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/interop/interopnames"
|
2020-06-04 18:11:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2020-12-29 10:44:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2020-06-03 18:16:19 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-02-03 14:24:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Instruction emits a VM Instruction with data to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Instruction(w *io.BinWriter, op opcode.Opcode, b []byte) {
|
|
|
|
w.WriteB(byte(op))
|
|
|
|
w.WriteBytes(b)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:30:15 +00:00
|
|
|
// Opcodes emits a single VM Instruction without arguments to the given buffer.
|
|
|
|
func Opcodes(w *io.BinWriter, ops ...opcode.Opcode) {
|
|
|
|
for _, op := range ops {
|
|
|
|
w.WriteB(byte(op))
|
|
|
|
}
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bool emits a bool type the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Bool(w *io.BinWriter, ok bool) {
|
2020-02-03 14:24:57 +00:00
|
|
|
if ok {
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PUSHT)
|
2021-03-09 10:17:21 +00:00
|
|
|
} else {
|
|
|
|
Opcodes(w, opcode.PUSHF)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-06-03 18:16:19 +00:00
|
|
|
Instruction(w, opcode.CONVERT, []byte{byte(stackitem.BooleanT)})
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 13:45:48 +00:00
|
|
|
func padRight(s int, buf []byte) []byte {
|
|
|
|
l := len(buf)
|
|
|
|
buf = buf[:s]
|
|
|
|
if buf[l-1]&0x80 != 0 {
|
|
|
|
for i := l; i < s; i++ {
|
|
|
|
buf[i] = 0xFF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2020-02-03 14:24:57 +00:00
|
|
|
// Int emits a int type to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Int(w *io.BinWriter, i int64) {
|
|
|
|
switch {
|
|
|
|
case i == -1:
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PUSHM1)
|
2020-04-21 13:45:48 +00:00
|
|
|
case i >= 0 && i < 16:
|
2020-02-03 14:24:57 +00:00
|
|
|
val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i))
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, val)
|
2020-02-03 14:46:51 +00:00
|
|
|
default:
|
2021-03-05 11:16:34 +00:00
|
|
|
bigInt(w, big.NewInt(i))
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:16:34 +00:00
|
|
|
func bigInt(w *io.BinWriter, n *big.Int) {
|
|
|
|
buf := bigint.ToPreallocatedBytes(n, make([]byte, 0, 32))
|
|
|
|
if len(buf) == 0 {
|
|
|
|
Opcodes(w, opcode.PUSH0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
padSize := byte(8 - bits.LeadingZeros8(byte(len(buf)-1)))
|
|
|
|
Opcodes(w, opcode.PUSHINT8+opcode.Opcode(padSize))
|
|
|
|
w.WriteBytes(padRight(1<<padSize, buf))
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:00:06 +00:00
|
|
|
// Array emits array of elements to the given buffer.
|
|
|
|
func Array(w *io.BinWriter, es ...interface{}) {
|
|
|
|
for i := len(es) - 1; i >= 0; i-- {
|
|
|
|
switch e := es[i].(type) {
|
2020-11-24 08:08:52 +00:00
|
|
|
case []interface{}:
|
|
|
|
Array(w, e...)
|
2020-03-27 08:00:06 +00:00
|
|
|
case int64:
|
|
|
|
Int(w, e)
|
2021-03-05 11:16:34 +00:00
|
|
|
case *big.Int:
|
|
|
|
bigInt(w, e)
|
2020-03-27 08:00:06 +00:00
|
|
|
case string:
|
|
|
|
String(w, e)
|
|
|
|
case util.Uint160:
|
|
|
|
Bytes(w, e.BytesBE())
|
|
|
|
case []byte:
|
|
|
|
Bytes(w, e)
|
|
|
|
case bool:
|
|
|
|
Bool(w, e)
|
|
|
|
default:
|
2020-08-04 09:03:31 +00:00
|
|
|
if es[i] != nil {
|
2021-03-24 14:05:05 +00:00
|
|
|
w.Err = fmt.Errorf("unsupported type: %v", e)
|
2020-08-04 09:03:31 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PUSHNULL)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Int(w, int64(len(es)))
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, opcode.PACK)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:24:57 +00:00
|
|
|
// String emits a string to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func String(w *io.BinWriter, s string) {
|
|
|
|
Bytes(w, []byte(s))
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bytes emits a byte array to the given buffer.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Bytes(w *io.BinWriter, b []byte) {
|
|
|
|
var n = len(b)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case n < 0x100:
|
|
|
|
Instruction(w, opcode.PUSHDATA1, []byte{byte(n)})
|
|
|
|
case n < 0x10000:
|
2020-02-03 14:24:57 +00:00
|
|
|
buf := make([]byte, 2)
|
|
|
|
binary.LittleEndian.PutUint16(buf, uint16(n))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.PUSHDATA2, buf)
|
|
|
|
default:
|
2020-02-03 14:24:57 +00:00
|
|
|
buf := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(buf, uint32(n))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.PUSHDATA4, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-02-03 14:46:51 +00:00
|
|
|
w.WriteBytes(b)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Syscall emits the syscall API to the given buffer.
|
|
|
|
// Syscall API string cannot be 0.
|
2020-02-03 14:46:51 +00:00
|
|
|
func Syscall(w *io.BinWriter, api string) {
|
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
} else if len(api) == 0 {
|
|
|
|
w.Err = errors.New("syscall api cannot be of length 0")
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-04-15 14:40:05 +00:00
|
|
|
buf := make([]byte, 4)
|
2020-08-13 07:41:33 +00:00
|
|
|
binary.LittleEndian.PutUint32(buf, interopnames.ToID([]byte(api)))
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, opcode.SYSCALL, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call emits a call Instruction with label to the given buffer.
|
2020-02-21 08:50:03 +00:00
|
|
|
func Call(w *io.BinWriter, op opcode.Opcode, label uint16) {
|
2020-02-03 14:46:51 +00:00
|
|
|
Jmp(w, op, label)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Jmp emits a jump Instruction along with label to the given buffer.
|
2020-02-21 08:50:03 +00:00
|
|
|
func Jmp(w *io.BinWriter, op opcode.Opcode, label uint16) {
|
2020-02-03 14:46:51 +00:00
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
} else if !isInstructionJmp(op) {
|
|
|
|
w.Err = fmt.Errorf("opcode %s is not a jump or call type", op.String())
|
|
|
|
return
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2020-04-23 08:56:36 +00:00
|
|
|
buf := make([]byte, 4)
|
2020-02-21 08:50:03 +00:00
|
|
|
binary.LittleEndian.PutUint16(buf, label)
|
2020-02-03 14:46:51 +00:00
|
|
|
Instruction(w, op, buf)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
// AppCallNoArgs emits call to provided contract.
|
|
|
|
func AppCallNoArgs(w *io.BinWriter, scriptHash util.Uint160, operation string, f callflag.CallFlag) {
|
|
|
|
Int(w, int64(f))
|
|
|
|
String(w, operation)
|
2020-05-07 11:38:19 +00:00
|
|
|
Bytes(w, scriptHash.BytesBE())
|
2020-08-14 10:50:52 +00:00
|
|
|
Syscall(w, interopnames.SystemContractCall)
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 10:44:07 +00:00
|
|
|
// AppCall emits an APPCALL with the default parameters given operation and arguments.
|
|
|
|
func AppCall(w *io.BinWriter, scriptHash util.Uint160, operation string, f callflag.CallFlag, args ...interface{}) {
|
2020-03-27 08:00:06 +00:00
|
|
|
Array(w, args...)
|
2020-12-29 10:44:07 +00:00
|
|
|
AppCallNoArgs(w, scriptHash, operation, f)
|
2020-03-27 08:00:06 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 14:24:57 +00:00
|
|
|
func isInstructionJmp(op opcode.Opcode) bool {
|
2020-08-20 05:34:14 +00:00
|
|
|
return opcode.JMP <= op && op <= opcode.CALLL || op == opcode.ENDTRYL
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|