pull/114/head
Minh Doan 6 years ago
parent 15fddd608f
commit 162866d1b4
  1. 9
      beaconchain/beaconchain.go
  2. 2
      beaconchain/beaconchain_handler.go
  3. 12
      crypto/pki/utils.go
  4. 8
      discovery/discovery.go
  5. 7
      node/worker/worker.go
  6. 8
      profiler/profiler.go

@ -29,7 +29,7 @@ type BeaconChain struct {
NumberOfLeadersAdded int
}
//Init
// New return BeaconChain.
func New(filename string) *BeaconChain {
idc := BeaconChain{}
//idc.NumberOfShards = readConfigFile(filename)
@ -49,7 +49,7 @@ func generateIDCKeys() kyber.Point {
return pubkey
}
//AcceptConnections welcomes new connections
// AcceptConnections welcomes new connections
func (IDC *BeaconChain) AcceptConnections(b []byte) {
NewNode := node.DeserializeNode(b)
fmt.Println(NewNode)
@ -62,10 +62,11 @@ func (IDC *BeaconChain) registerNode(Node *node.Node) {
return
}
func (IDC *BeaconChain) CommunicatePublicKeyToNode(Peer p2p.Peer) {
// CommunicatePublicKeyToNode communicates public key to node.
func (IDC *BeaconChain) CommunicatePublicKeyToNode(peer p2p.Peer) {
pbkey := pki.GetBytesFromPublicKey(IDC.PubKey)
msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Acknowledge, pbkey[:])
p2p.SendMessage(Peer, msgToSend)
p2p.SendMessage(peer, msgToSend)
}
//StartServer a server and process the request by a handler.

@ -10,7 +10,7 @@ import (
proto_identity "github.com/harmony-one/harmony/proto/identity"
)
//BeaconChainHandler handles registration of new Identities
// BeaconChainHandler handles registration of new Identities
// This could have been its seperate package like consensus, but am avoiding creating a lot of packages.
func (IDC *BeaconChain) BeaconChainHandler(conn net.Conn) {
content, err := p2p.ReadMessageContent(conn)

@ -8,6 +8,7 @@ import (
"github.com/harmony-one/harmony/log"
)
// GetAddressFromPublicKey returns address given a public key.
func GetAddressFromPublicKey(pubKey kyber.Point) [20]byte {
bytes, err := pubKey.MarshalBinary()
if err != nil {
@ -19,23 +20,27 @@ func GetAddressFromPublicKey(pubKey kyber.Point) [20]byte {
return address
}
// GetAddressFromPrivateKey returns address given a private key.
func GetAddressFromPrivateKey(priKey kyber.Scalar) [20]byte {
return GetAddressFromPublicKey(GetPublicKeyFromScalar(priKey))
}
// GetAddressFromPrivateKeyBytes returns address from private key in bytes.
func GetAddressFromPrivateKeyBytes(priKey [32]byte) [20]byte {
return GetAddressFromPublicKey(GetPublicKeyFromScalar(crypto.Ed25519Curve.Scalar().SetBytes(priKey[:])))
}
// Temporary helper function for benchmark use
// GetAddressFromInt is the temporary helper function for benchmark use
func GetAddressFromInt(value int) [20]byte {
return GetAddressFromPublicKey(GetPublicKeyFromScalar(GetPrivateKeyScalarFromInt(value)))
}
// GetPrivateKeyScalarFromInt return private key scalar.
func GetPrivateKeyScalarFromInt(value int) kyber.Scalar {
return crypto.Ed25519Curve.Scalar().SetInt64(int64(value))
}
// GetPrivateKeyFromInt returns private key in bytes given an interger.
func GetPrivateKeyFromInt(value int) [32]byte {
priKey, err := crypto.Ed25519Curve.Scalar().SetInt64(int64(value)).MarshalBinary()
priKeyBytes := [32]byte{}
@ -45,6 +50,7 @@ func GetPrivateKeyFromInt(value int) [32]byte {
return priKeyBytes
}
// GetPublicKeyFromPrivateKey return public key from private key.
func GetPublicKeyFromPrivateKey(priKey [32]byte) kyber.Point {
suite := crypto.Ed25519Curve
scalar := suite.Scalar()
@ -52,12 +58,12 @@ func GetPublicKeyFromPrivateKey(priKey [32]byte) kyber.Point {
return suite.Point().Mul(scalar, nil)
}
// Same as GetPublicKeyFromPrivateKey, but it directly works on kyber.Scalar object.
// GetPublicKeyFromScalar is the same as GetPublicKeyFromPrivateKey, but it directly works on kyber.Scalar object.
func GetPublicKeyFromScalar(priKey kyber.Scalar) kyber.Point {
return crypto.Ed25519Curve.Point().Mul(priKey, nil)
}
// Converts public key point to bytes
// GetBytesFromPublicKey converts public key point to bytes
func GetBytesFromPublicKey(pubKey kyber.Point) [32]byte {
bytes, err := pubKey.MarshalBinary()
result := [32]byte{}

@ -7,6 +7,7 @@ import (
"github.com/harmony-one/harmony/p2p"
)
// ConfigEntry is the config entry.
type ConfigEntry struct {
IP string
Port string
@ -24,6 +25,8 @@ func (config ConfigEntry) String() string {
return fmt.Sprintf("idc: %v:%v", config.IP, config.Port)
}
// New return new ConfigEntry.
// TODO: This should be change because this package is discovery and New here implies New Discovery.
func New(priK kyber.Scalar, pubK kyber.Point) *ConfigEntry {
var config ConfigEntry
config.priK = priK
@ -34,6 +37,7 @@ func New(priK kyber.Scalar, pubK kyber.Point) *ConfigEntry {
return &config
}
// StartClientMode starts client mode.
func (config *ConfigEntry) StartClientMode(idcIP, idcPort string) error {
config.IP = "myip"
config.Port = "myport"
@ -45,18 +49,22 @@ func (config *ConfigEntry) StartClientMode(idcIP, idcPort string) error {
return nil
}
// GetShardID ...
func (config *ConfigEntry) GetShardID() string {
return config.ShardID
}
// GetPeers ...
func (config *ConfigEntry) GetPeers() []p2p.Peer {
return config.peers
}
// GetLeader ...
func (config *ConfigEntry) GetLeader() p2p.Peer {
return config.leader
}
// GetSelfPeer ...
func (config *ConfigEntry) GetSelfPeer() p2p.Peer {
return config.self
}

@ -23,7 +23,7 @@ type environment struct {
receipts []*types.Receipt
}
// worker is the main object which takes care of submitting new work to consensus engine
// Worker is the main object which takes care of submitting new work to consensus engine
// and gathering the sealing result.
type Worker struct {
config *params.ChainConfig
@ -50,6 +50,7 @@ func (w *Worker) commitTransaction(tx *types.Transaction, coinbase common.Addres
return receipt.Logs, nil
}
// CommitTransactions commits transactions.
func (w *Worker) CommitTransactions(txs []*types.Transaction, coinbase common.Address) error {
snap := w.current.state.Snapshot()
@ -67,6 +68,7 @@ func (w *Worker) CommitTransactions(txs []*types.Transaction, coinbase common.Ad
return nil
}
// UpdateCurrent updates ...
func (w *Worker) UpdateCurrent() error {
parent := w.chain.CurrentBlock()
num := parent.Number()
@ -96,10 +98,12 @@ func (w *Worker) makeCurrent(parent *types.Block, header *types.Header) error {
return nil
}
// GetCurrentState ...
func (w *Worker) GetCurrentState() *state.StateDB {
return w.current.state
}
// Commit ...
func (w *Worker) Commit() (*types.Block, error) {
s := w.current.state.Copy()
block, err := w.engine.Finalize(w.chain, w.current.header, s, w.current.txs, w.current.receipts)
@ -109,6 +113,7 @@ func (w *Worker) Commit() (*types.Block, error) {
return block, nil
}
// New ...
func New(config *params.ChainConfig, chain *core.BlockChain, engine consensus.Engine) *Worker {
worker := &Worker{
config: config,

@ -12,6 +12,7 @@ import (
"github.com/shirou/gopsutil/process"
)
// Profiler is the profiler data structure.
type Profiler struct {
// parameters
logger log.Logger
@ -25,6 +26,8 @@ type Profiler struct {
var singleton *Profiler
var once sync.Once
// GetProfiler returns a pointer of Profiler.
// TODO: This should be a New method.
func GetProfiler() *Profiler {
once.Do(func() {
singleton = &Profiler{}
@ -32,6 +35,7 @@ func GetProfiler() *Profiler {
return singleton
}
// Config configurates Profiler.
func (profiler *Profiler) Config(logger log.Logger, shardID string, metricsReportURL string) {
profiler.logger = logger
profiler.pid = int32(os.Getpid())
@ -39,6 +43,7 @@ func (profiler *Profiler) Config(logger log.Logger, shardID string, metricsRepor
profiler.MetricsReportURL = metricsReportURL
}
// LogMemory logs memory.
func (profiler *Profiler) LogMemory() {
for {
// log mem usage
@ -50,6 +55,7 @@ func (profiler *Profiler) LogMemory() {
}
}
// LogCPU logs CPU metrics.
func (profiler *Profiler) LogCPU() {
for {
// log cpu usage
@ -61,6 +67,7 @@ func (profiler *Profiler) LogCPU() {
}
}
// LogMetrics logs metrics.
func (profiler *Profiler) LogMetrics(metrics map[string]interface{}) {
jsonValue, _ := json.Marshal(metrics)
rsp, err := http.Post(profiler.MetricsReportURL, "application/json", bytes.NewBuffer(jsonValue))
@ -69,6 +76,7 @@ func (profiler *Profiler) LogMetrics(metrics map[string]interface{}) {
}
}
// Start starts profiling.
func (profiler *Profiler) Start() {
profiler.proc, _ = process.NewProcess(profiler.pid)
go profiler.LogCPU()

Loading…
Cancel
Save