-
Notifications
You must be signed in to change notification settings - Fork 199
/
converters.go
132 lines (112 loc) · 3.2 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
package core
import (
"encoding/base64"
"encoding/hex"
"fmt"
"math"
"strconv"
"strings"
"github.com/ElrondNetwork/elrond-go/hashing"
"github.com/ElrondNetwork/elrond-go/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)
}
// ToB64 encodes the given buff to base64
func ToB64(buff []byte) string {
if buff == nil {
return "<NIL>"
}
return base64.StdEncoding.EncodeToString(buff)
}
// ToHex encodes the given buff to hex
func ToHex(buff []byte) string {
if buff == nil {
return "<NIL>"
}
return hex.EncodeToString(buff)
}
// CalculateHash marshalizes the interface and calculates its hash
func CalculateHash(
marshalizer marshal.Marshalizer,
hasher hashing.Hasher,
object interface{},
) ([]byte, error) {
if marshalizer == nil || marshalizer.IsInterfaceNil() {
return nil, ErrNilMarshalizer
}
if hasher == nil || hasher.IsInterfaceNil() {
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)
}
// 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) == 0 || len(splitString[0]) == 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
}