-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
127 lines (114 loc) · 2.95 KB
/
utils.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
package main
import (
"encoding/hex"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
)
// IntToHex converts an int64 to a byte array of length 8
//
func IntToHex(num int64) []byte {
bytesArr := make([]byte, 8)
hexadecimalValue := fmt.Sprintf("%016x", num)
bytesTempArr, _ := hex.DecodeString(hexadecimalValue)
copy(bytesArr, bytesTempArr) // To ensure the length of 8 bytes
return bytesArr
}
/**
Convert a byte array into a string of bytes
eg: 0x0 -> 00; 0x0a -> 0a, etc*/
func ByteArrToString(arr []byte) string {
return fmt.Sprintf("%x", arr)
}
/**
Convert a byte array into a string of its corresponding bits sequence
Eg: Input: 1234567895
Output: "00000000 00000000 00000000 00000000 01001001 10010110 00000010 11010111" */
func byteArrToStringBits(byteArr []byte) string {
bitsStr := fmt.Sprintf("%08b", byteArr) // [00000000 00101011 ...]
bitsStr = strings.ReplaceAll(bitsStr," ", "") // [0000000000101011 ...] (removed empty spaces)
bitsStr = strings.ReplaceAll(bitsStr,"[", "") // 0000000000101011 ...] (removed left barcket)
bitsStr = strings.ReplaceAll(bitsStr,"]", "") // 0000000000101011 ... (removed right barcket)
return bitsStr
}
// true if the hash starts with zeroBits zeros, note that the hash is
// a slice of *bytes* but we want zeroBits *bits* (a byte has 8 bits)
func StartsWithXZeros(hash []byte, zeroBits int) bool {
hashBitsStr := byteArrToStringBits(hash)
count := 0
i := 0 // iterate over the hash bits
for count < zeroBits && i < len(hashBitsStr) {
if hashBitsStr[i] != '0' {
return false
}
i ++
count ++
}
return count == zeroBits;
}
func EqualSlices(a, b []byte) bool {
if (len(a) != len(b)) {
return false
}
for i := 0; i< len(a); i++ {
if (a[i] != b[i]) {
return false
}
}
return true
}
func EqualMaps(a, b map[string]int) bool {
if (len(a) != len(b)) {
return false
}
for key, val := range(a) {
_, ok := b[key]
if (!ok) {
return false
}
if b[key] != val {
return false
}
}
return true
}
func EqualTransactions(a,b Transaction) bool{
return EqualSlices(a.Hash, b.Hash)
}
func EqualBlocks(a,b Block) bool{
return EqualSlices(a.Hash,b.Hash)
}
/**
Return the number of all elements in the given 2 dimensional array
*/
func numElements2DArr(arr2D [][]byte) int {
numElts := 0
for _, arr := range arr2D {
numElts += len(arr)
}
return numElts
}
// Serializes a slice of byte slices by converting it to a byte slice so
// needed to easily hash data
func Serialize(input [][]byte )[]byte {
numElts := numElements2DArr(input)
inputArr := make([]byte, numElts) // The final []byte array
it := 0 // iterator for inputArr
for _, arr := range input {
for i:=0; i<len(arr); i++ {
inputArr[it] = arr[i]
it ++;
}
}
return inputArr;
}
func BlockchainToMap(bc *Blockchain) map[string]interface{} {
bcMap := map[string]interface{}{}
err := mapstructure.Decode(bc, &bcMap)
if err != nil {
fmt.Println("Decode Failed !!!")
panic(err)
}
//fmt.Printf("%#v", bcMap)
return bcMap
}