Synchronize the logic for pendingTransactions, and use utxoPool's tx selection logic

pull/7/head
Rongjian Lan 7 years ago
parent 91258d9b05
commit f09332d5b5
  1. 20
      node/node.go
  2. 16
      node/node_handler.go

@ -6,18 +6,36 @@ import (
"harmony-benchmark/log" "harmony-benchmark/log"
"net" "net"
"os" "os"
"sync"
) )
var pendingTxMutex = &sync.Mutex{}
// A node represents a program (machine) participating in the network // A node represents a program (machine) participating in the network
type Node struct { type Node struct {
consensus *consensus.Consensus consensus *consensus.Consensus
BlockChannel chan blockchain.Block BlockChannel chan blockchain.Block
pendingTransactions []blockchain.Transaction pendingTransactions []*blockchain.Transaction
transactionInConsensus []*blockchain.Transaction
blockchain *blockchain.Blockchain blockchain *blockchain.Blockchain
utxoPool *blockchain.UTXOPool utxoPool *blockchain.UTXOPool
log log.Logger log log.Logger
} }
func (node *Node) addPendingTransactions(newTxs []*blockchain.Transaction) {
pendingTxMutex.Lock()
node.pendingTransactions = append(node.pendingTransactions, newTxs...)
pendingTxMutex.Unlock()
}
func (node *Node) getTransactionsForNewBlock() []*blockchain.Transaction {
pendingTxMutex.Lock()
selected, unselected := node.utxoPool.SelectTransactionsForNewBlock(node.pendingTransactions)
node.pendingTransactions = unselected
pendingTxMutex.Unlock()
return selected
}
// Start a server and process the request by a handler. // Start a server and process the request by a handler.
func (node *Node) StartServer(port string) { func (node *Node) StartServer(port string) {
node.listenOnPort(port) node.listenOnPort(port)

@ -76,12 +76,12 @@ func (node *Node) transactionMessageHandler(msgPayload []byte) {
case SEND: case SEND:
txDecoder := gob.NewDecoder(bytes.NewReader(msgPayload[1:])) // skip the SEND messge type txDecoder := gob.NewDecoder(bytes.NewReader(msgPayload[1:])) // skip the SEND messge type
txList := new([]blockchain.Transaction) txList := new([]*blockchain.Transaction)
err := txDecoder.Decode(&txList) err := txDecoder.Decode(&txList)
if err != nil { if err != nil {
node.log.Error("Failed deserializing transaction list", "node", node) node.log.Error("Failed deserializing transaction list", "node", node)
} }
node.pendingTransactions = append(node.pendingTransactions, *txList...) node.addPendingTransactions(*txList)
case REQUEST: case REQUEST:
reader := bytes.NewBuffer(msgPayload[1:]) reader := bytes.NewBuffer(msgPayload[1:])
var txIds map[[32]byte]bool var txIds map[[32]byte]bool
@ -95,7 +95,7 @@ func (node *Node) transactionMessageHandler(msgPayload []byte) {
txIds[getFixedByteTxId(txId)] = true txIds[getFixedByteTxId(txId)] = true
} }
var txToReturn []blockchain.Transaction var txToReturn []*blockchain.Transaction
for _, tx := range node.pendingTransactions { for _, tx := range node.pendingTransactions {
if txIds[getFixedByteTxId(tx.ID)] { if txIds[getFixedByteTxId(tx.ID)] {
txToReturn = append(txToReturn, tx) txToReturn = append(txToReturn, tx)
@ -125,14 +125,8 @@ func (node *Node) WaitForConsensusReady(readySignal chan int) {
for { for {
if len(node.pendingTransactions) >= 10 { if len(node.pendingTransactions) >= 10 {
node.log.Debug("Creating new block", "node", node) node.log.Debug("Creating new block", "node", node)
// TODO (Minh): package actual transactions selectedTxs := node.getTransactionsForNewBlock()
// For now, just take out 10 transactions newBlock = blockchain.NewBlock(selectedTxs, []byte{})
var txList []*blockchain.Transaction
for _, tx := range node.pendingTransactions[0:10] {
txList = append(txList, &tx)
}
node.pendingTransactions = node.pendingTransactions[10:]
newBlock = blockchain.NewBlock(txList, []byte{})
break break
} }
time.Sleep(1 * time.Second) // Periodically check whether we have enough transactions to package into block. time.Sleep(1 * time.Second) // Periodically check whether we have enough transactions to package into block.

Loading…
Cancel
Save