polish code

pull/8/head
Minh Doan 7 years ago
parent cf0ce3ba85
commit 31f111ed5d
  1. 60
      consensus/consensus.go
  2. 22
      node/node.go
  3. 17
      p2p/peer.go

@ -3,55 +3,35 @@ package consensus // consensus
import ( import (
"fmt" "fmt"
"harmony-benchmark/blockchain"
"harmony-benchmark/common" "harmony-benchmark/common"
"harmony-benchmark/log" "harmony-benchmark/log"
"harmony-benchmark/p2p" "harmony-benchmark/p2p"
"regexp" "regexp"
"strconv" "strconv"
"harmony-benchmark/blockchain"
) )
// Consensus data containing all info related to one consensus process // Consensus data containing all info related to one consensus process
type Consensus struct { type Consensus struct {
state ConsensusState state ConsensusState
// Signatures collected from validators commits map[string]string // Signatures collected from validators
commits map[string]string responses map[string]string // Signatures collected from validators
// Signatures collected from validators data string // Actual block data to reach consensus on
responses map[string]string validators []p2p.Peer // List of validators
// Actual block data to reach consensus on leader p2p.Peer // Leader
data string priKey string // private key of current node
// List of validators IsLeader bool // Whether I am leader. False means I am validator
validators []p2p.Peer nodeId uint16 // Leader or validator Id - 2 byte
// Leader consensusId uint32 // Consensus Id (View Id) - 4 byte
leader p2p.Peer blockHash [32]byte // Blockhash - 32 byte
// private key of current node blockHeader []byte // BlockHeader to run consensus on
priKey string ShardIDShardID uint32 // Shard Id which this node belongs to
// Whether I am leader. False means I am validator ReadySignal chan int // Signal channel for starting a new consensus process
IsLeader bool BlockVerifier func(*blockchain.Block) bool // The verifier func passed from Node object
// Leader or validator Id - 2 byte OnConsensusDone func(*blockchain.Block) // The post-consensus processing func passed from Node object. Called when consensus on a new block is done
nodeId uint16 msgCategory byte // Network related fields
// Consensus Id (View Id) - 4 byte actionType byte
consensusId uint32 Log log.Logger
// 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
} }
// Consensus state enum for both leader and validator // Consensus state enum for both leader and validator

@ -15,21 +15,13 @@ var pendingTxMutex = &sync.Mutex{}
// Node represents a program (machine) participating in the network // Node represents a program (machine) participating in the network
// TODO(minhdoan, rj): consider using BlockChannel *chan blockchain.Block for efficiency. // TODO(minhdoan, rj): consider using BlockChannel *chan blockchain.Block for efficiency.
type Node struct { type Node struct {
// Consensus object containing all consensus related data (e.g. committee members, signatures, commits) consensus *consensus.Consensus // Consensus object containing all consensus related data (e.g. committee members, signatures, commits)
consensus *consensus.Consensus BlockChannel chan blockchain.Block // The channel to receive new blocks from Node
// The channel to receive new blocks from Node pendingTransactions []*blockchain.Transaction // All the transactions received but not yet processed for consensus
BlockChannel chan blockchain.Block transactionInConsensus []*blockchain.Transaction // The transactions selected into the new block and under consensus process
// All the transactions received but not yet processed for consensus blockchain *blockchain.Blockchain // The blockchain for the shard where this node belongs
pendingTransactions []*blockchain.Transaction UtxoPool *blockchain.UTXOPool // The corresponding UTXO pool of the current blockchain
// The transactions selected into the new block and under consensus process log log.Logger // Log utility
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
} }
// Add new transactions to the pending transaction list // Add new transactions to the pending transaction list

@ -8,17 +8,14 @@ import (
"strings" "strings"
) )
// Object for a p2p peer (node) // Peer is the object for a p2p peer (node)
type Peer struct { type Peer struct {
// Ip address of the peer Ip string // Ip address of the peer
Ip string Port string // Port number of the peer
// Port number of the peer PubKey string // Public key of the peer
Port string
// Public key of the peer
PubKey string
} }
// Send the message to the peer // SendMessage sends the message to the peer
func SendMessage(peer Peer, msg []byte) { func SendMessage(peer Peer, msg []byte) {
// Construct normal p2p message // Construct normal p2p message
content := ConstructP2pMessage(byte(0), msg) content := ConstructP2pMessage(byte(0), msg)
@ -26,7 +23,7 @@ func SendMessage(peer Peer, msg []byte) {
send(peer.Ip, peer.Port, content) 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) { func BroadcastMessage(peers []Peer, msg []byte) {
// Construct broadcast p2p message // Construct broadcast p2p message
content := ConstructP2pMessage(byte(17), msg) 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 { func ConstructP2pMessage(msgType byte, content []byte) []byte {
firstByte := byte(17) // messageType 0x11 firstByte := byte(17) // messageType 0x11

Loading…
Cancel
Save