Take out transactions from pending Pool for new block

pull/3/head^2
Rongjian Lan 7 years ago
parent 79adb14279
commit d2927374b1
  1. 6
      aws-code/transaction_generator.go
  2. 1
      consensus/consensus_leader.go
  3. 14
      node/node.go

@ -22,15 +22,21 @@ func main() {
ip := flag.String("ip", "127.0.0.1", "IP of the leader")
port := flag.String("port", "9000", "port of the leader.")
txToSend := flag.Int("tx_count", 100, "number of transaction")
txs := make([]blockchain.Transaction, 10)
txCount := 0
for true {
if txCount >= *txToSend {
break
}
for i := range txs {
txs[i] = newRandTransaction()
}
msg := node.ConstructTransactionListMessage(txs)
p2p.SendMessage(p2p.Peer{*ip, *port, "n/a"}, msg)
txCount += len(txs)
time.Sleep(1 * time.Second) // 10 transactions per second
}
}

@ -18,7 +18,6 @@ var mutex = &sync.Mutex{}
func (consensus *Consensus) WaitForNewBlock(blockChannel chan blockchain.Block) {
for { // keep waiting for new blocks
newBlock := <- blockChannel
log.Println("got block.....")
// TODO: think about potential race condition
if consensus.state == READY {
consensus.startConsensus(&newBlock)

@ -122,19 +122,23 @@ func (node *Node) NodeHandler(conn net.Conn) {
func (node *Node) WaitForConsensusReady(readySignal chan int) {
for { // keep waiting for consensus ready
<- readySignal
log.Println("got ready signal.....")
// create a new block
newBlock := new(blockchain.Block)
for {
if len(node.pendingTransactions) >= 10 {
log.Println("creating new block")
// TODO: package actual transactions
newBlock = blockchain.NewGenesisBlock(blockchain.NewCoinbaseTX("x", "y"))
log.Println("Creating new block")
// TODO (Minh): package actual transactions
// For now, just take out 10 transactions
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
}
time.Sleep(1 * time.Second) // Periodically check whether we have enough transactions to package into block.
}
log.Println("sending new block to consensus")
node.BlockChannel <- *newBlock
}
}

Loading…
Cancel
Save