-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
103 lines (93 loc) · 3.02 KB
/
util.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
/*
* Copyright (C) 2021 Zilliqa
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package core
import (
"bytes"
"encoding/binary"
"math/big"
"net"
"os"
"testing"
)
// place a number into the destination byte stream at the specified offset
// caller should make sure that the value of numericTypeLen following rules
// Uint128 -> 16 (bytes)
// Uint64 -> 8 (bytes)
// Uint32 -> 4 (bytes)
func UintToByteArray(dst []byte, offset uint, num *big.Int, numericTypeLen uint) []byte {
lengthAvailable := uint(len(dst)) - offset
if lengthAvailable < numericTypeLen {
newDst := make([]byte, uint(len(dst))+numericTypeLen-lengthAvailable)
copy(newDst, dst)
dst = newDst
}
rightShift := (numericTypeLen - 1) * 8
ff, _ := new(big.Int).SetString("FF", 16)
for i := uint(0); i < numericTypeLen; i++ {
shifted := new(big.Int).Rsh(num, rightShift)
anded := new(big.Int).And(shifted, ff)
dst[offset+i] = byte(anded.Int64())
rightShift -= 8
}
return dst
}
// extract a number from the source byte stream at the specific offset.
// Uint128 -> 16 (bytes)
// Uint64 -> 8 (bytes)
// Uint32 -> 4 (bytes)
func ByteArrayToUint(src []byte, offset uint, numericTypeLen uint) *big.Int {
resultNum := new(big.Int)
if offset+numericTypeLen <= uint(len(src)) {
leftShift := (numericTypeLen - 1) * 8
for i := uint(0); i < numericTypeLen; i++ {
tmp := new(big.Int).SetBytes(src[offset+i : offset+i+1])
tmp = new(big.Int).Lsh(tmp, leftShift)
resultNum = new(big.Int).And(resultNum, tmp)
leftShift -= 8
}
}
return resultNum
}
func IP2Long(ip string) uint32 {
var long uint32
binary.Read(bytes.NewBuffer(net.ParseIP(ip).To4()), binary.LittleEndian, &long)
return long
}
const ScillaIndexSeparator = 0x16
func GenerateStorageKey(addr, vname string, indices []string) []byte {
var result []byte
result = mergeTwoBytes(result, []byte(addr))
result = mergeTwoBytes(result, []byte{ScillaIndexSeparator})
result = mergeTwoBytes(result, []byte(vname))
result = mergeTwoBytes(result, []byte{ScillaIndexSeparator})
for _, indice := range indices {
bs := mergeTwoBytes([]byte("\""+indice+"\""), []byte{ScillaIndexSeparator})
result = mergeTwoBytes(result, bs)
}
return result
}
func mergeTwoBytes(byte1, byte2 []byte) []byte {
ret := make([]byte, len(byte1)+len(byte2))
copy(ret[0:len(byte1)], byte1)
copy(ret[len(byte1):], byte2)
return ret
}
func SkipIfCI(t *testing.T) {
if os.Getenv("CI") != "" {
t.Skip("Skipping testing in CI environment")
}
}