-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconverters.go
179 lines (149 loc) · 4.49 KB
/
converters.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package core
import (
"fmt"
"math"
"math/big"
"strconv"
"strings"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go-core/hashing"
"github.com/ElrondNetwork/elrond-go-core/marshal"
)
// ConvertBytes converts the input bytes in a readable string using multipliers (k, M, G)
func ConvertBytes(bytes uint64) string {
if bytes < 1024 {
return fmt.Sprintf("%d B", bytes)
}
if bytes < 1024*1024 {
return fmt.Sprintf("%.2f KB", float64(bytes)/1024.0)
}
if bytes < 1024*1024*1024 {
return fmt.Sprintf("%.2f MB", float64(bytes)/1024.0/1024.0)
}
return fmt.Sprintf("%.2f GB", float64(bytes)/1024.0/1024.0/1024.0)
}
// CalculateHash marshalizes the interface and calculates its hash
func CalculateHash(
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
object interface{},
) ([]byte, error) {
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
if check.IfNil(hasher) {
return nil, ErrNilHasher
}
mrsData, err := marshalizer.Marshal(object)
if err != nil {
return nil, err
}
hash := hasher.Compute(string(mrsData))
return hash, nil
}
func plural(count int, singular string) (result string) {
if count < 2 {
result = strconv.Itoa(count) + " " + singular + " "
} else {
result = strconv.Itoa(count) + " " + singular + "s "
}
return
}
// SecondsToHourMinSec transform seconds input in a human friendly format
func SecondsToHourMinSec(input int) string {
numSecondsInAMinute := 60
numMinutesInAHour := 60
numSecondsInAHour := numSecondsInAMinute * numMinutesInAHour
result := ""
hours := math.Floor(float64(input) / float64(numSecondsInAMinute) / float64(numMinutesInAHour))
seconds := input % (numSecondsInAHour)
minutes := math.Floor(float64(seconds) / float64(numSecondsInAMinute))
seconds = input % numSecondsInAMinute
if hours > 0 {
result = plural(int(hours), "hour")
}
if minutes > 0 {
result += plural(int(minutes), "minute")
}
if seconds > 0 {
result += plural(seconds, "second")
}
return result
}
// GetShardIDString will return the string representation of the shard id
func GetShardIDString(shardID uint32) string {
if shardID == math.MaxUint32 {
return "metachain"
}
return fmt.Sprintf("%d", shardID)
}
// ConvertShardIDToUint32 converts shard id from string to uint32
func ConvertShardIDToUint32(shardIDStr string) (uint32, error) {
if shardIDStr == "metachain" {
return MetachainShardId, nil
}
shardID, err := strconv.ParseInt(shardIDStr, 10, 64)
if err != nil {
return 0, err
}
return uint32(shardID), nil
}
// EpochStartIdentifier returns the string for the epoch start identifier
func EpochStartIdentifier(epoch uint32) string {
return fmt.Sprintf("epochStartBlock_%d", epoch)
}
// IsUnknownEpochIdentifier return if the epoch identifier represents unknown epoch
func IsUnknownEpochIdentifier(identifier []byte) (bool, error) {
splitString := strings.Split(string(identifier), "_")
if len(splitString) < 2 || len(splitString[1]) == 0 {
return false, ErrInvalidIdentifierForEpochStartBlockRequest
}
epoch, err := strconv.ParseUint(splitString[1], 10, 32)
if err != nil {
return false, ErrInvalidIdentifierForEpochStartBlockRequest
}
if epoch == math.MaxUint32 {
return true, nil
}
return false, nil
}
// CommunicationIdentifierBetweenShards is used to generate the identifier between shardID1 and shardID2
// identifier is generated such as the first shard from identifier is always smaller or equal than the last
func CommunicationIdentifierBetweenShards(shardId1 uint32, shardId2 uint32) string {
if shardId1 == AllShardId || shardId2 == AllShardId {
return ShardIdToString(AllShardId)
}
if shardId1 == shardId2 {
return ShardIdToString(shardId1)
}
if shardId1 < shardId2 {
return ShardIdToString(shardId1) + ShardIdToString(shardId2)
}
return ShardIdToString(shardId2) + ShardIdToString(shardId1)
}
// ShardIdToString returns the string according to the shard id
func ShardIdToString(shardId uint32) string {
if shardId == MetachainShardId {
return "_META"
}
if shardId == AllShardId {
return "_ALL"
}
return fmt.Sprintf("_%d", shardId)
}
// ConvertToEvenHex converts the provided value in a hex string, even number of characters
func ConvertToEvenHex(value int) string {
str := fmt.Sprintf("%x", value)
if len(str)%2 != 0 {
str = "0" + str
}
return str
}
// ConvertToEvenHexBigInt converts the provided value in a hex string, even number of characters
func ConvertToEvenHexBigInt(value *big.Int) string {
str := value.Text(16)
if len(str)%2 != 0 {
str = "0" + str
}
return str
}