github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/vm/pushdata.go (about)

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