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/internal/utils/utils.go

122 lines
2.9 KiB

package utils
7 years ago
import (
6 years ago
"bytes"
"encoding/binary"
"encoding/json"
"io"
6 years ago
"log"
"os"
"regexp"
6 years ago
"strconv"
"sync"
"github.com/dedis/kyber"
"github.com/harmony-one/harmony/crypto"
"github.com/harmony-one/harmony/crypto/pki"
"github.com/harmony-one/harmony/p2p"
7 years ago
)
var lock sync.Mutex
// Unmarshal is a function that unmarshals the data from the
// reader into the specified value.
func Unmarshal(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}
// Marshal is a function that marshals the object into an
// io.Reader.
func Marshal(v interface{}) (io.Reader, error) {
b, err := json.MarshalIndent(v, "", "\t")
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
}
// 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
}
7 years ago
// GetUniqueIDFromPeer ...
6 years ago
// TODO(minhdoan): this is probably a hack, probably needs some strong non-collision hash.
func GetUniqueIDFromPeer(peer p2p.Peer) uint32 {
6 years ago
return GetUniqueIDFromIPPort(peer.IP, peer.Port)
}
// GetUniqueIDFromIPPort --
func GetUniqueIDFromIPPort(ip, port string) uint32 {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Panic("Regex Compilation Failed", "err", err)
}
6 years ago
socketID := reg.ReplaceAllString(ip+port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketID)
return uint32(value)
}
6 years ago
// GenKey generates a key given ip and port.
func GenKey(ip, port string) (kyber.Scalar, kyber.Point) {
priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIDFromIPPort(ip, port))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr)
pubKey := pki.GetPublicKeyFromScalar(priKey)
return priKey, pubKey
}
// AllocateShard uses the number of current nodes and number of shards
// to return the shardNum a new node belongs to, it also tells whether the node is a leader
func AllocateShard(numOfAddedNodes, numOfShards int) (int, bool) {
if numOfShards == 1 {
if numOfAddedNodes == 1 {
return 1, true
}
return 1, false
}
if numOfAddedNodes > numOfShards {
shardNum := numOfAddedNodes % numOfShards
if shardNum == 0 {
return numOfShards, false
}
return shardNum, false
}
return numOfAddedNodes, true
}
// Save saves a representation of v to the file at path.
func Save(path string, v interface{}) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
r, err := Marshal(v)
if err != nil {
return err
}
_, err = io.Copy(f, r)
return err
}
// Load loads the file at path into v.
func Load(path string, v interface{}) error {
lock.Lock()
defer lock.Unlock()
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
return err
}
}
defer f.Close()
return Unmarshal(f, v)
}