pull/113/head
Minh Doan 6 years ago
parent b707b0d893
commit 7b37717e7e
  1. 5
      node/worker/worker.go
  2. 2
      utils/bytes.go
  3. 19
      utils/distribution_config.go
  4. 1
      utils/metrics.go
  5. 20
      utils/utils.go

@ -1,6 +1,9 @@
package worker
import (
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/harmony/consensus"
@ -8,8 +11,6 @@ import (
"github.com/harmony-one/harmony/core/state"
"github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/core/vm"
"math/big"
"time"
)
// environment is the worker's current environment and holds all of the current state information.

@ -111,7 +111,7 @@ func LeftPadBytes(slice []byte, l int) []byte {
return padded
}
// Parse the string representation of hex into 32 byte array
// Get32BytesFromString parses the string representation of hex into 32 byte array
func Get32BytesFromString(hashString string) ([32]byte, error) {
bytes, err := hex.DecodeString(hashString)
if err != nil {

@ -12,6 +12,7 @@ import (
"github.com/harmony-one/harmony/p2p"
)
// ConfigEntry is the config entry.
type ConfigEntry struct {
IP string
Port string
@ -20,17 +21,18 @@ type ConfigEntry struct {
ValidatorID int // Validator ID in its shard.
}
// DistributionConfig is the distribution config.
type DistributionConfig struct {
config []ConfigEntry
}
// done
// NewDistributionConfig creates new DistributionConfig
func NewDistributionConfig() *DistributionConfig {
config := DistributionConfig{}
return &config
}
// Gets all the leader peers and corresponding shard Ids
// GetLeadersAndShardIDs gets all the leader peers and corresponding shard Ids
func (config *DistributionConfig) GetLeadersAndShardIDs() ([]p2p.Peer, []uint32) {
var peerList []p2p.Peer
var shardIDs []uint32
@ -48,6 +50,7 @@ func (config *DistributionConfig) GetLeadersAndShardIDs() ([]p2p.Peer, []uint32)
return peerList, shardIDs
}
// GetClientPeer returns client peer.
func (config *DistributionConfig) GetClientPeer() *p2p.Peer {
for _, entry := range config.config {
if entry.Role != "client" {
@ -59,8 +62,7 @@ func (config *DistributionConfig) GetClientPeer() *p2p.Peer {
return nil
}
// done
// Gets the port of the client node in the config
// GetClientPort gets the port of the client node in the config
func (config *DistributionConfig) GetClientPort() string {
for _, entry := range config.config {
if entry.Role == "client" {
@ -70,8 +72,7 @@ func (config *DistributionConfig) GetClientPort() string {
return ""
}
// done
// Parse the config file and return a 2d array containing the file data
// ReadConfigFile parses the config file and return a 2d array containing the file data
func (config *DistributionConfig) ReadConfigFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
@ -123,7 +124,7 @@ func (config *DistributionConfig) GetPeers(ip, port, shardID string) []p2p.Peer
return peerList
}
// GetPeers Gets the validator list
// GetSelfPeer Gets the validator list
func (config *DistributionConfig) GetSelfPeer(ip, port, shardID string) p2p.Peer {
for _, entry := range config.config {
if entry.IP == ip && entry.Port == port && entry.ShardID == shardID {
@ -147,10 +148,12 @@ func (config *DistributionConfig) GetLeader(shardID string) p2p.Peer {
return leaderPeer
}
// GetConfigEntries returns a list of ConfigEntry.
func (config *DistributionConfig) GetConfigEntries() []ConfigEntry {
return config.config
}
// GetMyConfigEntry ...
func (config *DistributionConfig) GetMyConfigEntry(ip string, port string) *ConfigEntry {
if config.config == nil {
return nil
@ -165,6 +168,6 @@ func (config *DistributionConfig) GetMyConfigEntry(ip string, port string) *Conf
func setKey(peer *p2p.Peer) {
// Get public key deterministically based on ip and port
priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIdFromPeer(*peer))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr)
priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIDFromPeer(*peer))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr)
peer.PubKey = pki.GetPublicKeyFromScalar(priKey)
}

@ -1,5 +1,6 @@
package utils
// BToMb ...
func BToMb(b uint64) uint64 {
return b / 1024 / 1024
}

@ -24,28 +24,29 @@ func ConvertFixedDataIntoByteArray(data interface{}) []byte {
return buff.Bytes()
}
// TODO(minhdoan): this is probably a hack, probably needs some strong non-collision hash.
func GetUniqueIdFromPeer(peer p2p.Peer) uint16 {
// GetUniqueIDFromPeer returns unique id from peer.
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)
socketID := reg.ReplaceAllString(peer.IP+peer.Port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketID)
return uint16(value)
}
func GetUniqueIdFromIPPort(ip, port string) uint16 {
// GetUniqueIDFromIPPort return unique id from ip port.
func GetUniqueIDFromIPPort(ip, port string) uint16 {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Panic("Regex Compilation Failed", "err", err)
}
socketId := reg.ReplaceAllString(ip+port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketId)
socketID := reg.ReplaceAllString(ip+port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketID)
return uint16(value)
}
// RunCmd Runs command `name` with arguments `args`
// RunCmd runs command `name` with arguments `args`
func RunCmd(name string, args ...string) error {
cmd := exec.Command(name, args...)
if err := cmd.Start(); err != nil {
@ -64,8 +65,9 @@ func RunCmd(name string, args ...string) error {
return nil
}
// 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)
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

Loading…
Cancel
Save