OSDN Git Service

auth_verification_test (#1970)
[bytom/bytom.git] / protocol / vm / pushdata.go
1 package vm
2
3 import "encoding/binary"
4
5 func opFalse(vm *virtualMachine) error {
6         err := vm.applyCost(1)
7         if err != nil {
8                 return err
9         }
10         return vm.pushBool(false, false)
11 }
12
13 func opPushdata(vm *virtualMachine) error {
14         err := vm.applyCost(1)
15         if err != nil {
16                 return err
17         }
18         d := make([]byte, len(vm.data))
19         copy(d, vm.data)
20         return vm.push(d, false)
21 }
22
23 func op1Negate(vm *virtualMachine) error {
24         err := vm.applyCost(1)
25         if err != nil {
26                 return err
27         }
28         return vm.pushInt64(-1, false)
29 }
30
31 func opNop(vm *virtualMachine) error {
32         return vm.applyCost(1)
33 }
34
35 // PushDataBytes push bytes to stack
36 func PushDataBytes(in []byte) []byte {
37         l := len(in)
38         if l == 0 {
39                 return []byte{byte(OP_0)}
40         }
41         if l <= 75 {
42                 return append([]byte{byte(OP_DATA_1) + uint8(l) - 1}, in...)
43         }
44         if l < 1<<8 {
45                 return append([]byte{byte(OP_PUSHDATA1), uint8(l)}, in...)
46         }
47         if l < 1<<16 {
48                 var b [2]byte
49                 binary.LittleEndian.PutUint16(b[:], uint16(l))
50                 return append([]byte{byte(OP_PUSHDATA2), b[0], b[1]}, in...)
51         }
52         var b [4]byte
53         binary.LittleEndian.PutUint32(b[:], uint32(l))
54         return append([]byte{byte(OP_PUSHDATA4), b[0], b[1], b[2], b[3]}, in...)
55 }
56
57 // PushDataInt64 push int64 to stack
58 func PushDataInt64(n int64) []byte {
59         if n == 0 {
60                 return []byte{byte(OP_0)}
61         }
62         if n >= 1 && n <= 16 {
63                 return []byte{uint8(OP_1) + uint8(n) - 1}
64         }
65         return PushDataBytes(Int64Bytes(n))
66 }