vm

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 26, 2022 License: AGPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAltStackUnderflow  = errors.New("alt stack underflow")
	ErrBadValue           = errors.New("bad value")
	ErrContext            = errors.New("wrong context")
	ErrDataStackUnderflow = errors.New("data stack underflow")
	ErrDisallowedOpcode   = errors.New("disallowed opcode")
	ErrDivZero            = errors.New("division by zero")
	ErrFalseVMResult      = errors.New("false VM result")
	ErrLongProgram        = errors.New("program size exceeds maxint32")
	ErrRange              = errors.New("range error")
	ErrReturn             = errors.New("RETURN executed")
	ErrRunLimitExceeded   = errors.New("run limit exceeded")
	ErrShortProgram       = errors.New("unexpected end of program")
	ErrToken              = errors.New("unrecognized token")
	ErrUnexpected         = errors.New("unexpected error")
	ErrUnsupportedVM      = errors.New("unsupported VM")
	ErrVerifyFailed       = errors.New("VERIFY failed")
)
View Source
var TraceOut io.Writer

TraceOut - if non-nil - will receive trace output during execution.

Functions

func AsBigInt

func AsBigInt(b []byte) (*uint256.Int, error)

AsBigInt conv little endian bytes to big int

func AsBool

func AsBool(bytes []byte) bool

AsBool convert bytes to bool

func Assemble

func Assemble(s string) (res []byte, err error)

Assemble converts a string like "2 3 ADD 5 NUMEQUAL" into 0x525393559c. The input should not include PUSHDATA (or OP_<num>) ops; those will be inferred. Input may include jump-target labels of the form $foo, which can then be used as JUMP:$foo or JUMPIF:$foo.

func BigIntBytes

func BigIntBytes(n *uint256.Int) []byte

BigIntBytes conv big int to little endian bytes, uint256 is version 1.1.1

func BoolBytes

func BoolBytes(b bool) (result []byte)

BoolBytes convert bool to bytes

func Disassemble

func Disassemble(prog []byte) (string, error)

func PushDataBytes

func PushDataBytes(in []byte) []byte

PushDataBytes push bytes to stack

func PushDataUint64

func PushDataUint64(n uint64) []byte

PushDataUint64 push int64 to stack

func Uint64Bytes

func Uint64Bytes(n uint64) []byte

Uint64Bytes convert uint64 to bytes in vm

func Verify

func Verify(context *Context, gasLimit int64) (gasLeft int64, err error)

Verify program by running VM

Types

type Context

type Context struct {
	VMVersion uint64
	Code      []byte
	StateData [][]byte
	Arguments [][]byte

	EntryID []byte

	// TxVersion must be present when verifying transaction components
	// (such as spends and issuances).
	TxVersion   *uint64
	BlockHeight *uint64

	NumResults    *uint64
	AssetID       *[]byte
	Amount        *uint64
	DestPos       *uint64
	SpentOutputID *[]byte

	TxSigHash   func() []byte
	CheckOutput func(index uint64, amount uint64, assetID []byte, vmVersion uint64, code []byte, state [][]byte, expansion bool) (bool, error)
}

Context contains the execution context for the virtual machine.

Most fields are pointers and are not required to be present in all cases. A nil pointer means the value is absent in that context. If an opcode executes that requires an absent field to be present, it will return ErrContext.

By convention, variables of this type have the name context, _not_ ctx (to avoid confusion with context.Context).

type Instruction

type Instruction struct {
	Op   Op
	Len  uint32
	Data []byte
}

func ParseOp

func ParseOp(prog []byte, pc uint32) (inst Instruction, err error)

ParseOp parses the op at position pc in prog, returning the parsed instruction (opcode plus any associated data).

func ParseProgram

func ParseProgram(prog []byte) ([]Instruction, error)

func (*Instruction) IsPushdata

func (inst *Instruction) IsPushdata() bool

IsPushdata judge instruction whether is a pushdata operation(include opFalse operation)

type Op

type Op uint8
const (
	OP_FALSE Op = 0x00
	OP_0     Op = 0x00 // synonym

	OP_1    Op = 0x51
	OP_TRUE Op = 0x51 // synonym

	OP_2  Op = 0x52
	OP_3  Op = 0x53
	OP_4  Op = 0x54
	OP_5  Op = 0x55
	OP_6  Op = 0x56
	OP_7  Op = 0x57
	OP_8  Op = 0x58
	OP_9  Op = 0x59
	OP_10 Op = 0x5a
	OP_11 Op = 0x5b
	OP_12 Op = 0x5c
	OP_13 Op = 0x5d
	OP_14 Op = 0x5e
	OP_15 Op = 0x5f
	OP_16 Op = 0x60

	OP_DATA_1  Op = 0x01
	OP_DATA_2  Op = 0x02
	OP_DATA_3  Op = 0x03
	OP_DATA_4  Op = 0x04
	OP_DATA_5  Op = 0x05
	OP_DATA_6  Op = 0x06
	OP_DATA_7  Op = 0x07
	OP_DATA_8  Op = 0x08
	OP_DATA_9  Op = 0x09
	OP_DATA_10 Op = 0x0a
	OP_DATA_11 Op = 0x0b
	OP_DATA_12 Op = 0x0c
	OP_DATA_13 Op = 0x0d
	OP_DATA_14 Op = 0x0e
	OP_DATA_15 Op = 0x0f
	OP_DATA_16 Op = 0x10
	OP_DATA_17 Op = 0x11
	OP_DATA_18 Op = 0x12
	OP_DATA_19 Op = 0x13
	OP_DATA_20 Op = 0x14
	OP_DATA_21 Op = 0x15
	OP_DATA_22 Op = 0x16
	OP_DATA_23 Op = 0x17
	OP_DATA_24 Op = 0x18
	OP_DATA_25 Op = 0x19
	OP_DATA_26 Op = 0x1a
	OP_DATA_27 Op = 0x1b
	OP_DATA_28 Op = 0x1c
	OP_DATA_29 Op = 0x1d
	OP_DATA_30 Op = 0x1e
	OP_DATA_31 Op = 0x1f
	OP_DATA_32 Op = 0x20
	OP_DATA_33 Op = 0x21
	OP_DATA_34 Op = 0x22
	OP_DATA_35 Op = 0x23
	OP_DATA_36 Op = 0x24
	OP_DATA_37 Op = 0x25
	OP_DATA_38 Op = 0x26
	OP_DATA_39 Op = 0x27
	OP_DATA_40 Op = 0x28
	OP_DATA_41 Op = 0x29
	OP_DATA_42 Op = 0x2a
	OP_DATA_43 Op = 0x2b
	OP_DATA_44 Op = 0x2c
	OP_DATA_45 Op = 0x2d
	OP_DATA_46 Op = 0x2e
	OP_DATA_47 Op = 0x2f
	OP_DATA_48 Op = 0x30
	OP_DATA_49 Op = 0x31
	OP_DATA_50 Op = 0x32
	OP_DATA_51 Op = 0x33
	OP_DATA_52 Op = 0x34
	OP_DATA_53 Op = 0x35
	OP_DATA_54 Op = 0x36
	OP_DATA_55 Op = 0x37
	OP_DATA_56 Op = 0x38
	OP_DATA_57 Op = 0x39
	OP_DATA_58 Op = 0x3a
	OP_DATA_59 Op = 0x3b
	OP_DATA_60 Op = 0x3c
	OP_DATA_61 Op = 0x3d
	OP_DATA_62 Op = 0x3e
	OP_DATA_63 Op = 0x3f
	OP_DATA_64 Op = 0x40
	OP_DATA_65 Op = 0x41
	OP_DATA_66 Op = 0x42
	OP_DATA_67 Op = 0x43
	OP_DATA_68 Op = 0x44
	OP_DATA_69 Op = 0x45
	OP_DATA_70 Op = 0x46
	OP_DATA_71 Op = 0x47
	OP_DATA_72 Op = 0x48
	OP_DATA_73 Op = 0x49
	OP_DATA_74 Op = 0x4a
	OP_DATA_75 Op = 0x4b

	OP_PUSHDATA1 Op = 0x4c
	OP_PUSHDATA2 Op = 0x4d
	OP_PUSHDATA4 Op = 0x4e
	OP_NOP       Op = 0x61

	OP_JUMP           Op = 0x63
	OP_JUMPIF         Op = 0x64
	OP_VERIFY         Op = 0x69
	OP_FAIL           Op = 0x6a
	OP_CHECKPREDICATE Op = 0xc0

	OP_TOALTSTACK   Op = 0x6b
	OP_FROMALTSTACK Op = 0x6c
	OP_2DROP        Op = 0x6d
	OP_2DUP         Op = 0x6e
	OP_3DUP         Op = 0x6f
	OP_2OVER        Op = 0x70
	OP_2ROT         Op = 0x71
	OP_2SWAP        Op = 0x72
	OP_IFDUP        Op = 0x73
	OP_DEPTH        Op = 0x74
	OP_DROP         Op = 0x75
	OP_DUP          Op = 0x76
	OP_NIP          Op = 0x77
	OP_OVER         Op = 0x78
	OP_PICK         Op = 0x79
	OP_ROLL         Op = 0x7a
	OP_ROT          Op = 0x7b
	OP_SWAP         Op = 0x7c
	OP_TUCK         Op = 0x7d

	OP_CAT         Op = 0x7e
	OP_SUBSTR      Op = 0x7f
	OP_LEFT        Op = 0x80
	OP_RIGHT       Op = 0x81
	OP_SIZE        Op = 0x82
	OP_CATPUSHDATA Op = 0x89

	OP_INVERT      Op = 0x83
	OP_AND         Op = 0x84
	OP_OR          Op = 0x85
	OP_XOR         Op = 0x86
	OP_EQUAL       Op = 0x87
	OP_EQUALVERIFY Op = 0x88

	OP_1ADD               Op = 0x8b
	OP_1SUB               Op = 0x8c
	OP_2MUL               Op = 0x8d
	OP_2DIV               Op = 0x8e
	OP_NOT                Op = 0x91
	OP_0NOTEQUAL          Op = 0x92
	OP_ADD                Op = 0x93
	OP_SUB                Op = 0x94
	OP_MUL                Op = 0x95
	OP_DIV                Op = 0x96
	OP_MOD                Op = 0x97
	OP_LSHIFT             Op = 0x98
	OP_RSHIFT             Op = 0x99
	OP_BOOLAND            Op = 0x9a
	OP_BOOLOR             Op = 0x9b
	OP_NUMEQUAL           Op = 0x9c
	OP_NUMEQUALVERIFY     Op = 0x9d
	OP_NUMNOTEQUAL        Op = 0x9e
	OP_LESSTHAN           Op = 0x9f
	OP_GREATERTHAN        Op = 0xa0
	OP_LESSTHANOREQUAL    Op = 0xa1
	OP_GREATERTHANOREQUAL Op = 0xa2
	OP_MIN                Op = 0xa3
	OP_MAX                Op = 0xa4
	OP_WITHIN             Op = 0xa5

	OP_SHA256        Op = 0xa8
	OP_SHA3          Op = 0xaa
	OP_HASH160       Op = 0xab
	OP_CHECKSIG      Op = 0xac
	OP_CHECKMULTISIG Op = 0xad
	OP_TXSIGHASH     Op = 0xae

	// // support gmsm
	OP_SM3              Op = 0xb0
	OP_CHECKSIGSM2      Op = 0xb1
	OP_CHECKMULTISIGSM2 Op = 0xb2

	OP_CHECKOUTPUT Op = 0xc1
	OP_ASSET       Op = 0xc2
	OP_AMOUNT      Op = 0xc3
	OP_PROGRAM     Op = 0xc4
	OP_INDEX       Op = 0xc9
	OP_ENTRYID     Op = 0xca
	OP_OUTPUTID    Op = 0xcb
	OP_BLOCKHEIGHT Op = 0xcd
)

func (Op) String

func (op Op) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL