-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
164 lines (136 loc) · 3.17 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package common
import (
"github.com/icloudland/starchain/common/log"
. "github.com/icloudland/starchain/errors"
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"github.com/golang/crypto/ripemd160"
"io"
"math/rand"
"os"
)
func ToCodeHash(code []byte) (Uint160, error) {
temp := sha256.Sum256(code)
md := ripemd160.New()
io.WriteString(md, string(temp[:]))
f := md.Sum(nil)
hash, err := Uint160ParseFromBytes(f)
if err != nil {
return Uint160{}, NewDetailErr(errors.New("[Common] , ToCodeHash err."), ErrNoCode, "")
}
return hash, nil
}
func GetNonce() uint64 {
log.NewLog().Debug()
// Fixme replace with the real random number generator
nonce := uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
return nonce
}
func IntToBytes(n int) []byte {
tmp := int32(n)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.LittleEndian, tmp)
return bytesBuffer.Bytes()
}
func BytesToInt16(b []byte) int16 {
bytesBuffer := bytes.NewBuffer(b)
var tmp int16
binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int16(tmp)
}
func BytesToInt(b [] byte) []int {
i := make([]int, len(b))
for k,v := range b {
i[k] = int(v)
}
return i
}
func IsEqualBytes(b1 []byte, b2 []byte) bool {
len1 := len(b1)
len2 := len(b2)
if len1 != len2 {
return false
}
for i := 0; i < len1; i++ {
if b1[i] != b2[i] {
return false
}
}
return true
}
func BytesToHexString(data []byte) string {
return hex.EncodeToString(data)
}
func HexStringToBytes(value string) ([]byte, error) {
return hex.DecodeString(value)
}
func ToArrayReverse(arr []byte) []byte {
l := len(arr)
x := make([]byte, 0)
for i := l - 1; i >= 0 ;i--{
x = append(x, arr[i])
}
return x
}
func BytesReverse(u []byte) []byte {
for i, j := 0, len(u)-1; i < j; i, j = i+1, j-1 {
u[i], u[j] = u[j], u[i]
}
return u
}
func HexStringToBytesReverse(value string) ([]byte, error) {
u, err := hex.DecodeString(value)
if err != nil {
return u, err
}
return BytesReverse(u), err
}
func ClearBytes(arr []byte, len int) {
for i := 0; i < len; i++ {
arr[i] = 0
}
}
func CompareHeight(blockHeight uint64, heights []uint64) bool {
for _, height := range heights {
if blockHeight < height {
return false
}
}
return true
}
func GetUint16Array(source []byte) ([]uint16, error) {
if source == nil {
return nil, NewDetailErr(errors.New("[Common] , GetUint16Array err, source = nil"), ErrNoCode, "")
}
if len(source)%2 != 0 {
return nil, NewDetailErr(errors.New("[Common] , GetUint16Array err, length of source is odd."), ErrNoCode, "")
}
dst := make([]uint16, len(source)/2)
for i := 0; i < len(source)/2; i++ {
dst[i] = uint16(source[i*2]) + uint16(source[i*2+1])*256
}
return dst, nil
}
func ToByteArray(source []uint16) []byte {
dst := make([]byte, len(source)*2)
for i := 0; i < len(source); i++ {
dst[i*2] = byte(source[i] % 256)
dst[i*2+1] = byte(source[i] / 256)
}
return dst
}
func SliceRemove(slice []uint32, h uint32) []uint32 {
for i, v := range slice {
if v == h {
return append(slice[:i], slice[i+1:]...)
}
}
return slice
}
func FileExisted(filename string) bool {
_, err := os.Stat(filename)
return err == nil || os.IsExist(err)
}