github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/vm/pushdata.go (about) 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 func PushdataBytes(in []byte) []byte { 36 l := len(in) 37 if l == 0 { 38 return []byte{byte(OP_0)} 39 } 40 if l <= 75 { 41 return append([]byte{byte(OP_DATA_1) + uint8(l) - 1}, in...) 42 } 43 if l < 1<<8 { 44 return append([]byte{byte(OP_PUSHDATA1), uint8(l)}, in...) 45 } 46 if l < 1<<16 { 47 var b [2]byte 48 binary.LittleEndian.PutUint16(b[:], uint16(l)) 49 return append([]byte{byte(OP_PUSHDATA2), b[0], b[1]}, in...) 50 } 51 var b [4]byte 52 binary.LittleEndian.PutUint32(b[:], uint32(l)) 53 return append([]byte{byte(OP_PUSHDATA4), b[0], b[1], b[2], b[3]}, in...) 54 } 55 56 func PushdataInt64(n int64) []byte { 57 if n == 0 { 58 return []byte{byte(OP_0)} 59 } 60 if n >= 1 && n <= 16 { 61 return []byte{uint8(OP_1) + uint8(n) - 1} 62 } 63 return PushdataBytes(Int64Bytes(n)) 64 }