From 162866d1b4465eed88a110429a4b039328807fd8 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Thu, 29 Nov 2018 22:05:47 -0800 Subject: [PATCH] fix golint --- beaconchain/beaconchain.go | 9 +++++---- beaconchain/beaconchain_handler.go | 2 +- crypto/pki/utils.go | 12 +++++++++--- discovery/discovery.go | 8 ++++++++ node/worker/worker.go | 7 ++++++- profiler/profiler.go | 8 ++++++++ 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/beaconchain/beaconchain.go b/beaconchain/beaconchain.go index b3eaddb43..77a2ebc68 100644 --- a/beaconchain/beaconchain.go +++ b/beaconchain/beaconchain.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. diff --git a/beaconchain/beaconchain_handler.go b/beaconchain/beaconchain_handler.go index 39f5f3ae2..5ed1c105f 100644 --- a/beaconchain/beaconchain_handler.go +++ b/beaconchain/beaconchain_handler.go @@ -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) diff --git a/crypto/pki/utils.go b/crypto/pki/utils.go index 1ef2be212..c508c51e7 100644 --- a/crypto/pki/utils.go +++ b/crypto/pki/utils.go @@ -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{} diff --git a/discovery/discovery.go b/discovery/discovery.go index 7ed3f1023..1ad881454 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -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 } diff --git a/node/worker/worker.go b/node/worker/worker.go index 0906ed1cd..a8b066b80 100644 --- a/node/worker/worker.go +++ b/node/worker/worker.go @@ -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, diff --git a/profiler/profiler.go b/profiler/profiler.go index fee3c8ab3..4d34e909f 100644 --- a/profiler/profiler.go +++ b/profiler/profiler.go @@ -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()