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) {
|
2022-02-09 12:13:15 +00:00
|
|
|
if smallInt(w, i) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bigInt(w, big.NewInt(i), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// BigInt emits big-integer to the given buffer.
|
|
|
|
func BigInt(w *io.BinWriter, n *big.Int) {
|
|
|
|
bigInt(w, n, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func smallInt(w *io.BinWriter, i int64) bool {
|
2020-02-03 14:46:51 +00:00
|
|
|
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:
|
2022-02-09 12:13:15 +00:00
|
|
|
val := opcode.Opcode(int(opcode.PUSH0) + int(i))
|
2020-10-02 08:30:15 +00:00
|
|
|
Opcodes(w, val)
|
2020-02-03 14:46:51 +00:00
|
|
|
default:
|
2022-02-09 12:13:15 +00:00
|
|
|
return false
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
2022-02-09 12:13:15 +00:00
|
|
|
return true
|
2020-02-03 14:24:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 12:13:15 +00:00
|
|
|
func bigInt(w *io.BinWriter, n *big.Int, trySmall bool) {
|
|
|
|
if w.Err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if trySmall && n.IsInt64() && smallInt(w, n.Int64()) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:16:34 +00:00
|
|
|
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{}) {
|
2022-01-14 14:21:14 +00:00
|
|
|
if len(es) == 0 {
|
|
|
|
Opcodes(w, opcode.NEWARRAY0)
|
|
|
|
return
|
|
|
|
}
|
2020-03-27 08:00:06 +00:00
|
|
|
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-12-13 12:28:08 +00:00
|
|
|
case int32:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case uint32:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case int16:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case uint16:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case int8:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case uint8:
|
|
|
|
Int(w, int64(e))
|
|
|
|
case int:
|
|
|
|
Int(w, int64(e))
|
2021-03-05 11:16:34 +00:00
|
|
|
case *big.Int:
|
2022-02-09 12:13:15 +00:00
|
|
|
BigInt(w, e)
|
2020-03-27 08:00:06 +00:00
|
|
|
case string:
|
|
|
|
String(w, e)
|
|
|
|
case util.Uint160:
|
|
|
|
Bytes(w, e.BytesBE())
|
2021-04-16 13:19:37 +00:00
|
|
|
case util.Uint256:
|
|
|
|
Bytes(w, e.BytesBE())
|
2020-03-27 08:00:06 +00:00
|
|
|
case []byte:
|
|
|
|
Bytes(w, e)
|
|
|
|
case bool:
|
|
|
|
Bool(w, e)
|
|
|
|
default:
|
2020-08-04 09:03:31 +00:00
|
|
|
if es[i] != nil {
|
2021-07-22 11:23:31 +00:00
|
|
|
w.Err = fmt.Errorf("unsupported type: %T", 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
|
|
|
}
|