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 (
"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

@ -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

@ -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

Loading…
Cancel
Save