github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/core/wavm/utils/utils.go (about) 1 // Copyright 2019 The go-vnt Authors 2 // This file is part of the go-vnt library. 3 // 4 // The go-vnt library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-vnt library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-vnt library. If not, see <http://www.gnu.org/licenses/>. 16 17 package utils 18 19 import ( 20 "encoding/binary" 21 "math" 22 "math/big" 23 "strings" 24 25 "github.com/vntchain/go-vnt/common" 26 m "github.com/vntchain/go-vnt/common/math" 27 "github.com/vntchain/go-vnt/crypto" 28 "github.com/vntchain/vnt-wasm/wasm" 29 ) 30 31 var endianess = binary.LittleEndian 32 33 func BytesToF32(bytes []byte) float32 { 34 bits := endianess.Uint32(bytes) 35 36 return math.Float32frombits(bits) 37 } 38 39 func BytesToF64(bytes []byte) float64 { 40 bits := endianess.Uint64(bytes) 41 return math.Float64frombits(bits) 42 } 43 44 func BytesToI64(bytes []byte) uint64 { 45 bits := endianess.Uint64(bytes) 46 return bits 47 } 48 49 func I32ToBytes(i32 uint32) []byte { 50 bytes := make([]byte, 4) 51 endianess.PutUint32(bytes, i32) 52 return bytes 53 } 54 55 func I64ToBytes(i64 uint64) []byte { 56 bytes := make([]byte, 8) 57 endianess.PutUint64(bytes, i64) 58 return bytes 59 } 60 func F32ToBytes(f32 float32) []byte { 61 bits := math.Float32bits(f32) 62 bytes := make([]byte, 4) 63 endianess.PutUint32(bytes, bits) 64 return bytes 65 } 66 67 func F64ToBytes(f64 float64) []byte { 68 bits := math.Float64bits(f64) 69 bytes := make([]byte, 8) 70 endianess.PutUint64(bytes, bits) 71 return bytes 72 } 73 74 func Split(data []byte) (int, [][]byte) { 75 l := len(data) 76 n := math.Ceil(float64(l) / 32.0) 77 h := make([]byte, int(n)*32) 78 copy(h[len(h)-len(data):], data) 79 res := [][]byte{} 80 for i := 0; i < int(n); i++ { 81 res = append(res, h[i*32:(i+1)*32]) 82 } 83 return int(n), res 84 } 85 86 func ArrayLengthKey(symbol common.Hash) common.Hash { 87 return common.BigToHash(new(big.Int).Add(symbol.Big(), common.BytesToHash([]byte("length")).Big())) 88 } 89 90 func MapLocation(sym, key []byte) common.Hash { 91 return crypto.Keccak256Hash(sym, key) 92 } 93 func ArrayLocation(sym string, index int64, length int64) {} 94 95 func GetU256(mem []byte) *big.Int { 96 bigint := new(big.Int) 97 var toStr string 98 if len(mem) == 0 { 99 toStr = "0" 100 } else { 101 toStr = string(mem) 102 } 103 _, success := bigint.SetString(toStr, 10) 104 if success == false { 105 panic("Illegal uint256 input " + toStr) 106 } 107 return m.U256(bigint) 108 } 109 110 func GetIndex(m *wasm.Module) (writeIndex int, readIndex int, gasIndex int) { 111 writeIndex = -1 112 readIndex = -1 113 gasIndex = -1 114 for i, v := range m.Import.Entries { 115 if v.FieldName == "WriteWithPointer" { 116 writeIndex = i 117 } else if v.FieldName == "ReadWithPointer" { 118 readIndex = i 119 } else if v.FieldName == "AddGas" { 120 gasIndex = i 121 } 122 if writeIndex != -1 && readIndex != -1 && gasIndex != -1 { 123 return 124 } 125 } 126 return 127 } 128 129 func IsAddress(data []byte) (bool, []byte) { 130 magic := "address1537182776" 131 if strings.HasPrefix(string(data), magic) { 132 return true, []byte(string(data)[len(magic):]) 133 } 134 return false, nil 135 } 136 137 func IsU256(data []byte) (bool, []byte) { 138 magic := "u2561537182776" 139 if strings.HasPrefix(string(data), magic) { 140 return true, []byte(string(data)[len(magic):]) 141 } 142 return false, nil 143 }