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.
92 lines
2.5 KiB
92 lines
2.5 KiB
7 years ago
|
package utils
|
||
7 years ago
|
|
||
|
import (
|
||
7 years ago
|
"bytes"
|
||
|
"encoding/binary"
|
||
|
"log"
|
||
6 years ago
|
"os/exec"
|
||
6 years ago
|
"regexp"
|
||
7 years ago
|
"strconv"
|
||
6 years ago
|
|
||
6 years ago
|
"github.com/dedis/kyber"
|
||
|
"github.com/harmony-one/harmony/crypto"
|
||
|
"github.com/harmony-one/harmony/crypto/pki"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p"
|
||
7 years ago
|
)
|
||
|
|
||
7 years ago
|
// ConvertFixedDataIntoByteArray converts an empty interface data to a byte array
|
||
|
func ConvertFixedDataIntoByteArray(data interface{}) []byte {
|
||
7 years ago
|
buff := new(bytes.Buffer)
|
||
7 years ago
|
err := binary.Write(buff, binary.BigEndian, data)
|
||
7 years ago
|
if err != nil {
|
||
|
log.Panic(err)
|
||
|
}
|
||
|
return buff.Bytes()
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
6 years ago
|
// GetUniqueIDFromPeer ...
|
||
6 years ago
|
// TODO(minhdoan): this is probably a hack, probably needs some strong non-collision hash.
|
||
6 years ago
|
func GetUniqueIDFromPeer(peer p2p.Peer) uint32 {
|
||
6 years ago
|
return GetUniqueIDFromIPPort(peer.IP, peer.Port)
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// GetUniqueIDFromIPPort --
|
||
6 years ago
|
func GetUniqueIDFromIPPort(ip, port string) uint32 {
|
||
6 years ago
|
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)
|
||
6 years ago
|
return uint32(value)
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// RunCmd runs command `name` with arguments `args`
|
||
6 years ago
|
func RunCmd(name string, args ...string) error {
|
||
|
cmd := exec.Command(name, args...)
|
||
6 years ago
|
stderrBytes := &bytes.Buffer{}
|
||
|
cmd.Stderr = stderrBytes
|
||
6 years ago
|
if err := cmd.Start(); err != nil {
|
||
|
log.Fatal(err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Println("Command running", name, args)
|
||
|
go func() {
|
||
|
if err := cmd.Wait(); err != nil {
|
||
|
log.Printf("Command finished with error: %v", err)
|
||
6 years ago
|
log.Printf("Stderr: %v", string(stderrBytes.Bytes()))
|
||
6 years ago
|
} else {
|
||
|
log.Printf("Command finished successfully")
|
||
|
}
|
||
|
}()
|
||
|
return nil
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// GenKey generates a key given ip and port.
|
||
6 years ago
|
func GenKey(ip, port string) (kyber.Scalar, kyber.Point) {
|
||
6 years ago
|
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)
|
||
6 years ago
|
pubKey := pki.GetPublicKeyFromScalar(priKey)
|
||
|
|
||
|
return priKey, pubKey
|
||
|
}
|
||
6 years ago
|
|
||
|
// AllocateShard uses the number of current nodes and number of shards
|
||
6 years ago
|
// 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 {
|
||
6 years ago
|
return 1, true
|
||
|
}
|
||
6 years ago
|
return 1, false
|
||
6 years ago
|
}
|
||
6 years ago
|
if numOfAddedNodes > numOfShards {
|
||
|
shardNum := numOfAddedNodes % numOfShards
|
||
|
if shardNum == 0 {
|
||
|
return numOfShards, false
|
||
6 years ago
|
}
|
||
6 years ago
|
return shardNum, false
|
||
6 years ago
|
}
|
||
6 years ago
|
return numOfAddedNodes, true
|
||
6 years ago
|
}
|