mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-26 09:42:22 +00:00
emit: use io.BinWriter instead of bytes.Buffer
This commit is contained in:
parent
698c647f07
commit
8243a8b3a7
6 changed files with 128 additions and 199 deletions
|
@ -1,7 +1,6 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
@ -1486,11 +1485,8 @@ func ScriptFromWitness(hash util.Uint160, witness *transaction.Witness) ([]byte,
|
||||||
verification := witness.VerificationScript
|
verification := witness.VerificationScript
|
||||||
|
|
||||||
if len(verification) == 0 {
|
if len(verification) == 0 {
|
||||||
bb := new(bytes.Buffer)
|
bb := io.NewBufBinWriter()
|
||||||
err := emit.AppCall(bb, hash, false)
|
emit.AppCall(bb.BinWriter, hash, false)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
verification = bb.Bytes()
|
verification = bb.Bytes()
|
||||||
} else if h := witness.ScriptHash(); hash != h {
|
} else if h := witness.ScriptHash(); hash != h {
|
||||||
return nil, errors.New("witness hash mismatch")
|
return nil, errors.New("witness hash mismatch")
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package rpc
|
package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -113,22 +112,12 @@ func GetInvocationScript(tx *transaction.Transaction, wif *keys.WIF) ([]byte, er
|
||||||
func CreateDeploymentScript(avm []byte, contract *ContractDetails) ([]byte, error) {
|
func CreateDeploymentScript(avm []byte, contract *ContractDetails) ([]byte, error) {
|
||||||
var props smartcontract.PropertyState
|
var props smartcontract.PropertyState
|
||||||
|
|
||||||
script := new(bytes.Buffer)
|
script := io.NewBufBinWriter()
|
||||||
if err := emit.Bytes(script, []byte(contract.Description)); err != nil {
|
emit.Bytes(script.BinWriter, []byte(contract.Description))
|
||||||
return nil, err
|
emit.Bytes(script.BinWriter, []byte(contract.Email))
|
||||||
}
|
emit.Bytes(script.BinWriter, []byte(contract.Author))
|
||||||
if err := emit.Bytes(script, []byte(contract.Email)); err != nil {
|
emit.Bytes(script.BinWriter, []byte(contract.Version))
|
||||||
return nil, err
|
emit.Bytes(script.BinWriter, []byte(contract.ProjectName))
|
||||||
}
|
|
||||||
if err := emit.Bytes(script, []byte(contract.Author)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := emit.Bytes(script, []byte(contract.Version)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := emit.Bytes(script, []byte(contract.ProjectName)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if contract.HasStorage {
|
if contract.HasStorage {
|
||||||
props |= smartcontract.HasStorage
|
props |= smartcontract.HasStorage
|
||||||
}
|
}
|
||||||
|
@ -138,31 +127,21 @@ func CreateDeploymentScript(avm []byte, contract *ContractDetails) ([]byte, erro
|
||||||
if contract.IsPayable {
|
if contract.IsPayable {
|
||||||
props |= smartcontract.IsPayable
|
props |= smartcontract.IsPayable
|
||||||
}
|
}
|
||||||
if err := emit.Int(script, int64(props)); err != nil {
|
emit.Int(script.BinWriter, int64(props))
|
||||||
return nil, err
|
emit.Int(script.BinWriter, int64(contract.ReturnType))
|
||||||
}
|
|
||||||
if err := emit.Int(script, int64(contract.ReturnType)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
params := make([]byte, len(contract.Parameters))
|
params := make([]byte, len(contract.Parameters))
|
||||||
for k := range contract.Parameters {
|
for k := range contract.Parameters {
|
||||||
params[k] = byte(contract.Parameters[k])
|
params[k] = byte(contract.Parameters[k])
|
||||||
}
|
}
|
||||||
if err := emit.Bytes(script, params); err != nil {
|
emit.Bytes(script.BinWriter, params)
|
||||||
return nil, err
|
emit.Bytes(script.BinWriter, avm)
|
||||||
}
|
emit.Syscall(script.BinWriter, "Neo.Contract.Create")
|
||||||
if err := emit.Bytes(script, avm); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := emit.Syscall(script, "Neo.Contract.Create"); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return script.Bytes(), nil
|
return script.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// expandArrayIntoScript pushes all FuncParam parameters from the given array
|
// expandArrayIntoScript pushes all FuncParam parameters from the given array
|
||||||
// into the given buffer in reverse order.
|
// into the given buffer in reverse order.
|
||||||
func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
|
func expandArrayIntoScript(script *io.BinWriter, slice []Param) error {
|
||||||
for j := len(slice) - 1; j >= 0; j-- {
|
for j := len(slice) - 1; j >= 0; j-- {
|
||||||
fp, err := slice[j].GetFuncParam()
|
fp, err := slice[j].GetFuncParam()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -174,33 +153,25 @@ func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := emit.Bytes(script, str); err != nil {
|
emit.Bytes(script, str)
|
||||||
return err
|
|
||||||
}
|
|
||||||
case String:
|
case String:
|
||||||
str, err := fp.Value.GetString()
|
str, err := fp.Value.GetString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := emit.String(script, str); err != nil {
|
emit.String(script, str)
|
||||||
return err
|
|
||||||
}
|
|
||||||
case Hash160:
|
case Hash160:
|
||||||
hash, err := fp.Value.GetUint160FromHex()
|
hash, err := fp.Value.GetUint160FromHex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := emit.Bytes(script, hash.BytesBE()); err != nil {
|
emit.Bytes(script, hash.BytesBE())
|
||||||
return err
|
|
||||||
}
|
|
||||||
case Hash256:
|
case Hash256:
|
||||||
hash, err := fp.Value.GetUint256()
|
hash, err := fp.Value.GetUint256()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := emit.Bytes(script, hash.BytesBE()); err != nil {
|
emit.Bytes(script, hash.BytesBE())
|
||||||
return err
|
|
||||||
}
|
|
||||||
case PublicKey:
|
case PublicKey:
|
||||||
str, err := fp.Value.GetString()
|
str, err := fp.Value.GetString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -210,17 +181,13 @@ func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := emit.Bytes(script, key.Bytes()); err != nil {
|
emit.Bytes(script, key.Bytes())
|
||||||
return err
|
|
||||||
}
|
|
||||||
case Integer:
|
case Integer:
|
||||||
val, err := fp.Value.GetInt()
|
val, err := fp.Value.GetInt()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := emit.Int(script, int64(val)); err != nil {
|
emit.Int(script, int64(val))
|
||||||
return err
|
|
||||||
}
|
|
||||||
case Boolean:
|
case Boolean:
|
||||||
str, err := fp.Value.GetString()
|
str, err := fp.Value.GetString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -228,14 +195,11 @@ func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
|
||||||
}
|
}
|
||||||
switch str {
|
switch str {
|
||||||
case "true":
|
case "true":
|
||||||
err = emit.Int(script, 1)
|
emit.Int(script, 1)
|
||||||
case "false":
|
case "false":
|
||||||
err = emit.Int(script, 0)
|
emit.Int(script, 0)
|
||||||
default:
|
default:
|
||||||
err = errors.New("wrong boolean value")
|
return errors.New("wrong boolean value")
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
return fmt.Errorf("parameter type %v is not supported", fp.Type)
|
||||||
|
@ -247,44 +211,32 @@ func expandArrayIntoScript(script *bytes.Buffer, slice []Param) error {
|
||||||
// CreateFunctionInvocationScript creates a script to invoke given contract with
|
// CreateFunctionInvocationScript creates a script to invoke given contract with
|
||||||
// given parameters.
|
// given parameters.
|
||||||
func CreateFunctionInvocationScript(contract util.Uint160, params Params) ([]byte, error) {
|
func CreateFunctionInvocationScript(contract util.Uint160, params Params) ([]byte, error) {
|
||||||
script := new(bytes.Buffer)
|
script := io.NewBufBinWriter()
|
||||||
for i := len(params) - 1; i >= 0; i-- {
|
for i := len(params) - 1; i >= 0; i-- {
|
||||||
switch params[i].Type {
|
switch params[i].Type {
|
||||||
case stringT:
|
case stringT:
|
||||||
if err := emit.String(script, params[i].String()); err != nil {
|
emit.String(script.BinWriter, params[i].String())
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
case numberT:
|
case numberT:
|
||||||
num, err := params[i].GetInt()
|
num, err := params[i].GetInt()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err := emit.String(script, strconv.Itoa(num)); err != nil {
|
emit.String(script.BinWriter, strconv.Itoa(num))
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
case arrayT:
|
case arrayT:
|
||||||
slice, err := params[i].GetArray()
|
slice, err := params[i].GetArray()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = expandArrayIntoScript(script, slice)
|
err = expandArrayIntoScript(script.BinWriter, slice)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = emit.Int(script, int64(len(slice)))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = emit.Opcode(script, opcode.PACK)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
emit.Int(script.BinWriter, int64(len(slice)))
|
||||||
|
emit.Opcode(script.BinWriter, opcode.PACK)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := emit.AppCall(script, contract, false); err != nil {
|
emit.AppCall(script.BinWriter, contract, false)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return script.Bytes(), nil
|
return script.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,13 +245,11 @@ func CreateFunctionInvocationScript(contract util.Uint160, params Params) ([]byt
|
||||||
// expects one array of FuncParams and expands it onto the stack as independent
|
// expects one array of FuncParams and expands it onto the stack as independent
|
||||||
// elements.
|
// elements.
|
||||||
func CreateInvocationScript(contract util.Uint160, funcParams []Param) ([]byte, error) {
|
func CreateInvocationScript(contract util.Uint160, funcParams []Param) ([]byte, error) {
|
||||||
script := new(bytes.Buffer)
|
script := io.NewBufBinWriter()
|
||||||
err := expandArrayIntoScript(script, funcParams)
|
err := expandArrayIntoScript(script.BinWriter, funcParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = emit.AppCall(script, contract, false); err != nil {
|
emit.AppCall(script.BinWriter, contract, false)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return script.Bytes(), nil
|
return script.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
package smartcontract
|
package smartcontract
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/io"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/emit"
|
"github.com/CityOfZion/neo-go/pkg/vm/emit"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
||||||
)
|
)
|
||||||
|
@ -22,22 +22,14 @@ func CreateMultiSigRedeemScript(m int, publicKeys keys.PublicKeys) ([]byte, erro
|
||||||
return nil, fmt.Errorf("public key count %d exceeds maximum of length 1024", len(publicKeys))
|
return nil, fmt.Errorf("public key count %d exceeds maximum of length 1024", len(publicKeys))
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
if err := emit.Int(buf, int64(m)); err != nil {
|
emit.Int(buf.BinWriter, int64(m))
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
sort.Sort(publicKeys)
|
sort.Sort(publicKeys)
|
||||||
for _, pubKey := range publicKeys {
|
for _, pubKey := range publicKeys {
|
||||||
if err := emit.Bytes(buf, pubKey.Bytes()); err != nil {
|
emit.Bytes(buf.BinWriter, pubKey.Bytes())
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err := emit.Int(buf, int64(len(publicKeys))); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err := emit.Opcode(buf, opcode.CHECKMULTISIG); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
emit.Int(buf.BinWriter, int64(len(publicKeys)))
|
||||||
|
emit.Opcode(buf.BinWriter, opcode.CHECKMULTISIG)
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
return buf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,146 +1,135 @@
|
||||||
package emit
|
package emit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
|
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/io"
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Instruction emits a VM Instruction with data to the given buffer.
|
// Instruction emits a VM Instruction with data to the given buffer.
|
||||||
func Instruction(w *bytes.Buffer, op opcode.Opcode, b []byte) error {
|
func Instruction(w *io.BinWriter, op opcode.Opcode, b []byte) {
|
||||||
if err := w.WriteByte(byte(op)); err != nil {
|
w.WriteB(byte(op))
|
||||||
return err
|
w.WriteBytes(b)
|
||||||
}
|
|
||||||
_, err := w.Write(b)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Opcode emits a single VM Instruction without arguments to the given buffer.
|
// Opcode emits a single VM Instruction without arguments to the given buffer.
|
||||||
func Opcode(w io.ByteWriter, op opcode.Opcode) error {
|
func Opcode(w *io.BinWriter, op opcode.Opcode) {
|
||||||
return w.WriteByte(byte(op))
|
w.WriteB(byte(op))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bool emits a bool type the given buffer.
|
// Bool emits a bool type the given buffer.
|
||||||
func Bool(w io.ByteWriter, ok bool) error {
|
func Bool(w *io.BinWriter, ok bool) {
|
||||||
if ok {
|
if ok {
|
||||||
return Opcode(w, opcode.PUSHT)
|
Opcode(w, opcode.PUSHT)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return Opcode(w, opcode.PUSHF)
|
Opcode(w, opcode.PUSHF)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int emits a int type to the given buffer.
|
// Int emits a int type to the given buffer.
|
||||||
func Int(w *bytes.Buffer, i int64) error {
|
func Int(w *io.BinWriter, i int64) {
|
||||||
if i == -1 {
|
switch {
|
||||||
return Opcode(w, opcode.PUSHM1)
|
case i == -1:
|
||||||
}
|
Opcode(w, opcode.PUSHM1)
|
||||||
if i == 0 {
|
case i == 0:
|
||||||
return Opcode(w, opcode.PUSHF)
|
Opcode(w, opcode.PUSHF)
|
||||||
}
|
case i > 0 && i < 16:
|
||||||
if i > 0 && i < 16 {
|
|
||||||
val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i))
|
val := opcode.Opcode(int(opcode.PUSH1) - 1 + int(i))
|
||||||
return Opcode(w, val)
|
Opcode(w, val)
|
||||||
}
|
default:
|
||||||
|
|
||||||
bInt := big.NewInt(i)
|
bInt := big.NewInt(i)
|
||||||
val := IntToBytes(bInt)
|
val := IntToBytes(bInt)
|
||||||
return Bytes(w, val)
|
Bytes(w, val)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// String emits a string to the given buffer.
|
// String emits a string to the given buffer.
|
||||||
func String(w *bytes.Buffer, s string) error {
|
func String(w *io.BinWriter, s string) {
|
||||||
return Bytes(w, []byte(s))
|
Bytes(w, []byte(s))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes emits a byte array to the given buffer.
|
// Bytes emits a byte array to the given buffer.
|
||||||
func Bytes(w *bytes.Buffer, b []byte) error {
|
func Bytes(w *io.BinWriter, b []byte) {
|
||||||
var (
|
var n = len(b)
|
||||||
err error
|
|
||||||
n = len(b)
|
|
||||||
)
|
|
||||||
|
|
||||||
if n <= int(opcode.PUSHBYTES75) {
|
switch {
|
||||||
return Instruction(w, opcode.Opcode(n), b)
|
case n <= int(opcode.PUSHBYTES75):
|
||||||
} else if n < 0x100 {
|
Instruction(w, opcode.Opcode(n), b)
|
||||||
err = Instruction(w, opcode.PUSHDATA1, []byte{byte(n)})
|
return
|
||||||
} else if n < 0x10000 {
|
case n < 0x100:
|
||||||
|
Instruction(w, opcode.PUSHDATA1, []byte{byte(n)})
|
||||||
|
case n < 0x10000:
|
||||||
buf := make([]byte, 2)
|
buf := make([]byte, 2)
|
||||||
binary.LittleEndian.PutUint16(buf, uint16(n))
|
binary.LittleEndian.PutUint16(buf, uint16(n))
|
||||||
err = Instruction(w, opcode.PUSHDATA2, buf)
|
Instruction(w, opcode.PUSHDATA2, buf)
|
||||||
} else {
|
default:
|
||||||
buf := make([]byte, 4)
|
buf := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(buf, uint32(n))
|
binary.LittleEndian.PutUint32(buf, uint32(n))
|
||||||
err = Instruction(w, opcode.PUSHDATA4, buf)
|
Instruction(w, opcode.PUSHDATA4, buf)
|
||||||
}
|
}
|
||||||
if err != nil {
|
w.WriteBytes(b)
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = w.Write(b)
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Syscall emits the syscall API to the given buffer.
|
// Syscall emits the syscall API to the given buffer.
|
||||||
// Syscall API string cannot be 0.
|
// Syscall API string cannot be 0.
|
||||||
func Syscall(w *bytes.Buffer, api string) error {
|
func Syscall(w *io.BinWriter, api string) {
|
||||||
if len(api) == 0 {
|
if w.Err != nil {
|
||||||
return errors.New("syscall api cannot be of length 0")
|
return
|
||||||
|
} else if len(api) == 0 {
|
||||||
|
w.Err = errors.New("syscall api cannot be of length 0")
|
||||||
|
return
|
||||||
}
|
}
|
||||||
buf := make([]byte, len(api)+1)
|
buf := make([]byte, len(api)+1)
|
||||||
buf[0] = byte(len(api))
|
buf[0] = byte(len(api))
|
||||||
copy(buf[1:], api)
|
copy(buf[1:], api)
|
||||||
return Instruction(w, opcode.SYSCALL, buf)
|
Instruction(w, opcode.SYSCALL, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call emits a call Instruction with label to the given buffer.
|
// Call emits a call Instruction with label to the given buffer.
|
||||||
func Call(w *bytes.Buffer, op opcode.Opcode, label int16) error {
|
func Call(w *io.BinWriter, op opcode.Opcode, label int16) {
|
||||||
return Jmp(w, op, label)
|
Jmp(w, op, label)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Jmp emits a jump Instruction along with label to the given buffer.
|
// Jmp emits a jump Instruction along with label to the given buffer.
|
||||||
func Jmp(w *bytes.Buffer, op opcode.Opcode, label int16) error {
|
func Jmp(w *io.BinWriter, op opcode.Opcode, label int16) {
|
||||||
if !isInstructionJmp(op) {
|
if w.Err != nil {
|
||||||
return fmt.Errorf("opcode %s is not a jump or call type", op.String())
|
return
|
||||||
|
} else if !isInstructionJmp(op) {
|
||||||
|
w.Err = fmt.Errorf("opcode %s is not a jump or call type", op.String())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
buf := make([]byte, 2)
|
buf := make([]byte, 2)
|
||||||
binary.LittleEndian.PutUint16(buf, uint16(label))
|
binary.LittleEndian.PutUint16(buf, uint16(label))
|
||||||
return Instruction(w, op, buf)
|
Instruction(w, op, buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppCall emits an appcall, if tailCall is true, tailCall opcode will be
|
// AppCall emits an appcall, if tailCall is true, tailCall opcode will be
|
||||||
// emitted instead.
|
// emitted instead.
|
||||||
func AppCall(w *bytes.Buffer, scriptHash util.Uint160, tailCall bool) error {
|
func AppCall(w *io.BinWriter, scriptHash util.Uint160, tailCall bool) {
|
||||||
op := opcode.APPCALL
|
op := opcode.APPCALL
|
||||||
if tailCall {
|
if tailCall {
|
||||||
op = opcode.TAILCALL
|
op = opcode.TAILCALL
|
||||||
}
|
}
|
||||||
return Instruction(w, op, scriptHash.BytesBE())
|
Instruction(w, op, scriptHash.BytesBE())
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppCallWithOperationAndData emits an appcall with the given operation and data.
|
// AppCallWithOperationAndData emits an appcall with the given operation and data.
|
||||||
func AppCallWithOperationAndData(w *bytes.Buffer, scriptHash util.Uint160, operation string, data []byte) error {
|
func AppCallWithOperationAndData(w *io.BinWriter, scriptHash util.Uint160, operation string, data []byte) {
|
||||||
if err := Bytes(w, data); err != nil {
|
Bytes(w, data)
|
||||||
return err
|
String(w, operation)
|
||||||
}
|
AppCall(w, scriptHash, false)
|
||||||
if err := String(w, operation); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return AppCall(w, scriptHash, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppCallWithOperation emits an appcall with the given operation.
|
// AppCallWithOperation emits an appcall with the given operation.
|
||||||
func AppCallWithOperation(w *bytes.Buffer, scriptHash util.Uint160, operation string) error {
|
func AppCallWithOperation(w *io.BinWriter, scriptHash util.Uint160, operation string) {
|
||||||
if err := Bool(w, false); err != nil {
|
Bool(w, false)
|
||||||
return err
|
String(w, operation)
|
||||||
}
|
AppCall(w, scriptHash, false)
|
||||||
if err := String(w, operation); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return AppCall(w, scriptHash, false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isInstructionJmp(op opcode.Opcode) bool {
|
func isInstructionJmp(op opcode.Opcode) bool {
|
||||||
|
|
|
@ -1,33 +1,33 @@
|
||||||
package emit
|
package emit
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/io"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEmitInt(t *testing.T) {
|
func TestEmitInt(t *testing.T) {
|
||||||
t.Run("1-byte int", func(t *testing.T) {
|
t.Run("1-byte int", func(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
Int(buf, 10)
|
Int(buf.BinWriter, 10)
|
||||||
result := buf.Bytes()
|
result := buf.Bytes()
|
||||||
assert.EqualValues(t, opcode.PUSH10, result[0])
|
assert.EqualValues(t, opcode.PUSH10, result[0])
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("2-byte int", func(t *testing.T) {
|
t.Run("2-byte int", func(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
Int(buf, 100)
|
Int(buf.BinWriter, 100)
|
||||||
result := buf.Bytes()
|
result := buf.Bytes()
|
||||||
assert.EqualValues(t, opcode.PUSHBYTES1, result[0])
|
assert.EqualValues(t, opcode.PUSHBYTES1, result[0])
|
||||||
assert.EqualValues(t, 100, result[1])
|
assert.EqualValues(t, 100, result[1])
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("4-byte int", func(t *testing.T) {
|
t.Run("4-byte int", func(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
Int(buf, 1000)
|
Int(buf.BinWriter, 1000)
|
||||||
result := buf.Bytes()
|
result := buf.Bytes()
|
||||||
assert.EqualValues(t, opcode.PUSHBYTES2, result[0])
|
assert.EqualValues(t, opcode.PUSHBYTES2, result[0])
|
||||||
assert.EqualValues(t, 1000, binary.LittleEndian.Uint16(result[1:3]))
|
assert.EqualValues(t, 1000, binary.LittleEndian.Uint16(result[1:3]))
|
||||||
|
@ -35,18 +35,18 @@ func TestEmitInt(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmitBool(t *testing.T) {
|
func TestEmitBool(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
Bool(buf, true)
|
Bool(buf.BinWriter, true)
|
||||||
Bool(buf, false)
|
Bool(buf.BinWriter, false)
|
||||||
result := buf.Bytes()
|
result := buf.Bytes()
|
||||||
assert.Equal(t, opcode.Opcode(result[0]), opcode.PUSH1)
|
assert.Equal(t, opcode.Opcode(result[0]), opcode.PUSH1)
|
||||||
assert.Equal(t, opcode.Opcode(result[1]), opcode.PUSH0)
|
assert.Equal(t, opcode.Opcode(result[1]), opcode.PUSH0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmitString(t *testing.T) {
|
func TestEmitString(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
str := "City Of Zion"
|
str := "City Of Zion"
|
||||||
String(buf, str)
|
String(buf.BinWriter, str)
|
||||||
assert.Equal(t, buf.Len(), len(str)+1)
|
assert.Equal(t, buf.Len(), len(str)+1)
|
||||||
assert.Equal(t, buf.Bytes()[1:], []byte(str))
|
assert.Equal(t, buf.Bytes()[1:], []byte(str))
|
||||||
}
|
}
|
||||||
|
@ -58,9 +58,9 @@ func TestEmitSyscall(t *testing.T) {
|
||||||
"Neo.Runtime.Whatever",
|
"Neo.Runtime.Whatever",
|
||||||
}
|
}
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
for _, syscall := range syscalls {
|
for _, syscall := range syscalls {
|
||||||
Syscall(buf, syscall)
|
Syscall(buf.BinWriter, syscall)
|
||||||
result := buf.Bytes()
|
result := buf.Bytes()
|
||||||
assert.Equal(t, opcode.Opcode(result[0]), opcode.SYSCALL)
|
assert.Equal(t, opcode.Opcode(result[0]), opcode.SYSCALL)
|
||||||
assert.Equal(t, result[1], uint8(len(syscall)))
|
assert.Equal(t, result[1], uint8(len(syscall)))
|
||||||
|
@ -70,8 +70,8 @@ func TestEmitSyscall(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmitCall(t *testing.T) {
|
func TestEmitCall(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
Call(buf, opcode.JMP, 100)
|
Call(buf.BinWriter, opcode.JMP, 100)
|
||||||
result := buf.Bytes()
|
result := buf.Bytes()
|
||||||
assert.Equal(t, opcode.Opcode(result[0]), opcode.JMP)
|
assert.Equal(t, opcode.Opcode(result[0]), opcode.JMP)
|
||||||
label := binary.LittleEndian.Uint16(result[1:3])
|
label := binary.LittleEndian.Uint16(result[1:3])
|
||||||
|
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/CityOfZion/neo-go/pkg/io"
|
||||||
|
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
"github.com/CityOfZion/neo-go/pkg/crypto/hash"
|
||||||
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
||||||
"github.com/CityOfZion/neo-go/pkg/util"
|
"github.com/CityOfZion/neo-go/pkg/util"
|
||||||
|
@ -31,9 +33,9 @@ func TestInteropHook(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.RegisterInteropGetter(fooInteropGetter)
|
v.RegisterInteropGetter(fooInteropGetter)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
emit.Syscall(buf, "foo")
|
emit.Syscall(buf.BinWriter, "foo")
|
||||||
emit.Opcode(buf, opcode.RET)
|
emit.Opcode(buf.BinWriter, opcode.RET)
|
||||||
v.Load(buf.Bytes())
|
v.Load(buf.Bytes())
|
||||||
runVM(t, v)
|
runVM(t, v)
|
||||||
assert.Equal(t, 1, v.estack.Len())
|
assert.Equal(t, 1, v.estack.Len())
|
||||||
|
@ -44,12 +46,12 @@ func TestInteropHookViaID(t *testing.T) {
|
||||||
v := New()
|
v := New()
|
||||||
v.RegisterInteropGetter(fooInteropGetter)
|
v.RegisterInteropGetter(fooInteropGetter)
|
||||||
|
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
fooid := InteropNameToID([]byte("foo"))
|
fooid := InteropNameToID([]byte("foo"))
|
||||||
var id = make([]byte, 4)
|
var id = make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(id, fooid)
|
binary.LittleEndian.PutUint32(id, fooid)
|
||||||
_ = emit.Syscall(buf, string(id))
|
emit.Syscall(buf.BinWriter, string(id))
|
||||||
_ = emit.Opcode(buf, opcode.RET)
|
emit.Opcode(buf.BinWriter, opcode.RET)
|
||||||
v.Load(buf.Bytes())
|
v.Load(buf.Bytes())
|
||||||
runVM(t, v)
|
runVM(t, v)
|
||||||
assert.Equal(t, 1, v.estack.Len())
|
assert.Equal(t, 1, v.estack.Len())
|
||||||
|
@ -131,10 +133,10 @@ func TestBytesToPublicKey(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPushBytes1to75(t *testing.T) {
|
func TestPushBytes1to75(t *testing.T) {
|
||||||
buf := new(bytes.Buffer)
|
buf := io.NewBufBinWriter()
|
||||||
for i := 1; i <= 75; i++ {
|
for i := 1; i <= 75; i++ {
|
||||||
b := randomBytes(i)
|
b := randomBytes(i)
|
||||||
emit.Bytes(buf, b)
|
emit.Bytes(buf.BinWriter, b)
|
||||||
vm := load(buf.Bytes())
|
vm := load(buf.Bytes())
|
||||||
err := vm.Step()
|
err := vm.Step()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
Loading…
Reference in a new issue