From 31f111ed5d49c01a9c3a20e92e053016c05ebafc Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Wed, 20 Jun 2018 20:11:32 -0700 Subject: [PATCH] polish code --- consensus/consensus.go | 60 ++++++++++++++---------------------------- node/node.go | 22 +++++----------- p2p/peer.go | 17 +++++------- 3 files changed, 34 insertions(+), 65 deletions(-) diff --git a/consensus/consensus.go b/consensus/consensus.go index 6aad6d00d..bf2d1f005 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -3,55 +3,35 @@ package consensus // consensus import ( "fmt" + "harmony-benchmark/blockchain" "harmony-benchmark/common" "harmony-benchmark/log" "harmony-benchmark/p2p" "regexp" "strconv" - "harmony-benchmark/blockchain" ) // Consensus data containing all info related to one consensus process type Consensus struct { - state ConsensusState - // Signatures collected from validators - commits map[string]string - // Signatures collected from validators - responses map[string]string - // Actual block data to reach consensus on - data string - // List of validators - validators []p2p.Peer - // Leader - leader p2p.Peer - // private key of current node - priKey string - // Whether I am leader. False means I am validator - IsLeader bool - // Leader or validator Id - 2 byte - nodeId uint16 - // Consensus Id (View Id) - 4 byte - consensusId uint32 - // Blockhash - 32 byte - blockHash [32]byte - // BlockHeader to run consensus on - blockHeader []byte - // Shard Id which this node belongs to - ShardIDShardID uint32 - - // Signal channel for starting a new consensus process - ReadySignal chan int - // The verifier func passed from Node object - BlockVerifier func(*blockchain.Block)bool - // The post-consensus processing func passed from Node object - // Called when consensus on a new block is done - OnConsensusDone func(*blockchain.Block) - - //// Network related fields - msgCategory byte - actionType byte - - Log log.Logger + state ConsensusState + commits map[string]string // Signatures collected from validators + responses map[string]string // Signatures collected from validators + data string // Actual block data to reach consensus on + validators []p2p.Peer // List of validators + leader p2p.Peer // Leader + priKey string // private key of current node + IsLeader bool // Whether I am leader. False means I am validator + nodeId uint16 // Leader or validator Id - 2 byte + consensusId uint32 // Consensus Id (View Id) - 4 byte + blockHash [32]byte // Blockhash - 32 byte + blockHeader []byte // BlockHeader to run consensus on + ShardIDShardID uint32 // Shard Id which this node belongs to + ReadySignal chan int // Signal channel for starting a new consensus process + BlockVerifier func(*blockchain.Block) bool // The verifier func passed from Node object + OnConsensusDone func(*blockchain.Block) // The post-consensus processing func passed from Node object. Called when consensus on a new block is done + msgCategory byte // Network related fields + actionType byte + Log log.Logger } // Consensus state enum for both leader and validator diff --git a/node/node.go b/node/node.go index fd0367511..22c70a5a8 100644 --- a/node/node.go +++ b/node/node.go @@ -15,21 +15,13 @@ var pendingTxMutex = &sync.Mutex{} // Node represents a program (machine) participating in the network // TODO(minhdoan, rj): consider using BlockChannel *chan blockchain.Block for efficiency. type Node struct { - // Consensus object containing all consensus related data (e.g. committee members, signatures, commits) - consensus *consensus.Consensus - // The channel to receive new blocks from Node - BlockChannel chan blockchain.Block - // All the transactions received but not yet processed for consensus - pendingTransactions []*blockchain.Transaction - // The transactions selected into the new block and under consensus process - transactionInConsensus []*blockchain.Transaction - // The blockchain for the shard where this node belongs - blockchain *blockchain.Blockchain - // The corresponding UTXO pool of the current blockchain - UtxoPool *blockchain.UTXOPool - - // Log utility - log log.Logger + consensus *consensus.Consensus // Consensus object containing all consensus related data (e.g. committee members, signatures, commits) + BlockChannel chan blockchain.Block // The channel to receive new blocks from Node + pendingTransactions []*blockchain.Transaction // All the transactions received but not yet processed for consensus + transactionInConsensus []*blockchain.Transaction // The transactions selected into the new block and under consensus process + blockchain *blockchain.Blockchain // The blockchain for the shard where this node belongs + UtxoPool *blockchain.UTXOPool // The corresponding UTXO pool of the current blockchain + log log.Logger // Log utility } // Add new transactions to the pending transaction list diff --git a/p2p/peer.go b/p2p/peer.go index 5e705efef..1caf09a15 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -8,17 +8,14 @@ import ( "strings" ) -// Object for a p2p peer (node) +// Peer is the object for a p2p peer (node) type Peer struct { - // Ip address of the peer - Ip string - // Port number of the peer - Port string - // Public key of the peer - PubKey string + Ip string // Ip address of the peer + Port string // Port number of the peer + PubKey string // Public key of the peer } -// Send the message to the peer +// SendMessage sends the message to the peer func SendMessage(peer Peer, msg []byte) { // Construct normal p2p message content := ConstructP2pMessage(byte(0), msg) @@ -26,7 +23,7 @@ func SendMessage(peer Peer, msg []byte) { send(peer.Ip, peer.Port, content) } -// Send the message to a list of peers +// BroadcastMessage sends the message to a list of peers func BroadcastMessage(peers []Peer, msg []byte) { // Construct broadcast p2p message content := ConstructP2pMessage(byte(17), msg) @@ -36,7 +33,7 @@ func BroadcastMessage(peers []Peer, msg []byte) { } } -// Construct the p2p message as [messageType, contentSize, content] +// ConstructP2pMessage constructs the p2p message as [messageType, contentSize, content] func ConstructP2pMessage(msgType byte, content []byte) []byte { firstByte := byte(17) // messageType 0x11