The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/utils/utils.go

46 lines
1.0 KiB

package utils
7 years ago
import (
6 years ago
"bytes"
"encoding/binary"
"log"
"regexp"
6 years ago
"strconv"
"strings"
"github.com/simple-rules/harmony-benchmark/p2p"
7 years ago
)
// ConvertFixedDataIntoByteArray converts an empty interface data to a byte array
func ConvertFixedDataIntoByteArray(data interface{}) []byte {
6 years ago
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, data)
6 years ago
if err != nil {
log.Panic(err)
}
return buff.Bytes()
7 years ago
}
6 years ago
// ConvertIntoInts is to convert '1,2,3,4' into []int{1,2,3,4}.
6 years ago
func ConvertIntoInts(data string) []int {
6 years ago
var res = []int{}
items := strings.Split(data, ",")
6 years ago
for _, value := range items {
intValue, err := strconv.Atoi(value)
if err == nil {
res = append(res, intValue)
}
6 years ago
}
return res
6 years ago
}
func GetUniqueIdFromPeer(peer p2p.Peer) uint16 {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Panic("Regex Compilation Failed", "err", err)
}
socketId := reg.ReplaceAllString(peer.Ip+peer.Port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketId)
return uint16(value)
}