Add blockchain/utxopool to node struct

pull/5/merge
Rongjian Lan 6 years ago
parent 21153224ae
commit 4c11a13d95
  1. 16
      blockchain/blockchain.go
  2. 2
      blockchain/blockchain_test.go
  3. 2
      blockchain/utxopool.go
  4. 8
      node/node.go

@ -7,7 +7,7 @@ import (
// Blockchain keeps a sequence of Blocks
type Blockchain struct {
blocks []*Block
Blocks []*Block
}
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
@ -17,8 +17,8 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
var unspentTXs []Transaction
spentTXOs := make(map[string][]int)
for index := len(bc.blocks) - 1; index >= 0; index-- {
block := bc.blocks[index]
for index := len(bc.Blocks) - 1; index >= 0; index-- {
block := bc.Blocks[index]
for _, tx := range block.Transactions {
txID := hex.EncodeToString(tx.ID)
@ -135,7 +135,7 @@ func (bc *Blockchain) NewUTXOTransaction(from, to string, amount int) *Transacti
func (bc *Blockchain) AddNewUserTransfer(utxoPool *UTXOPool, from, to string, amount int) bool {
tx := bc.NewUTXOTransaction(from, to, amount)
if tx != nil {
newBlock := NewBlock([]*Transaction{tx}, bc.blocks[len(bc.blocks)-1].Hash)
newBlock := NewBlock([]*Transaction{tx}, bc.Blocks[len(bc.Blocks)-1].Hash)
if bc.VerifyNewBlockAndUpdate(utxoPool, newBlock) {
return true
}
@ -145,18 +145,18 @@ func (bc *Blockchain) AddNewUserTransfer(utxoPool *UTXOPool, from, to string, am
// VerifyNewBlockAndUpdate verifies if the new coming block is valid for the current blockchain.
func (bc *Blockchain) VerifyNewBlockAndUpdate(utxopool *UTXOPool, block *Block) bool {
length := len(bc.blocks)
if bytes.Compare(block.PrevBlockHash, bc.blocks[length-1].Hash) != 0 {
length := len(bc.Blocks)
if bytes.Compare(block.PrevBlockHash, bc.Blocks[length-1].Hash) != 0 {
return false
}
if block.Timestamp < bc.blocks[length-1].Timestamp {
if block.Timestamp < bc.Blocks[length-1].Timestamp {
return false
}
if utxopool != nil && !utxopool.VerifyAndUpdate(block.Transactions) {
return false
}
bc.blocks = append(bc.blocks, block)
bc.Blocks = append(bc.Blocks, block)
return true
}

@ -68,7 +68,7 @@ func TestVerifyNewBlock(t *testing.T) {
if tx == nil {
t.Error("failed to create a new transaction.")
}
newBlock := NewBlock([]*Transaction{tx}, bc.blocks[len(bc.blocks)-1].Hash)
newBlock := NewBlock([]*Transaction{tx}, bc.Blocks[len(bc.Blocks)-1].Hash)
if !bc.VerifyNewBlockAndUpdate(utxoPool, newBlock) {
t.Error("failed to add a new valid block.")

@ -191,7 +191,7 @@ func CreateUTXOPoolFromTransaction(tx *Transaction) *UTXOPool {
// CreateUTXOPoolFromGenesisBlockChain a utxo pool from a genesis blockchain.
func CreateUTXOPoolFromGenesisBlockChain(bc *Blockchain) *UTXOPool {
tx := bc.blocks[0].Transactions[0]
tx := bc.Blocks[0].Transactions[0]
return CreateUTXOPoolFromTransaction(tx)
}

@ -18,6 +18,8 @@ type Node struct {
consensus *consensus.Consensus
BlockChannel chan blockchain.Block
pendingTransactions []blockchain.Transaction
blockchain *blockchain.Blockchain
utxoPool *blockchain.UTXOPool
}
// Start a server and process the request by a handler.
@ -192,5 +194,11 @@ func NewNode(consensus *consensus.Consensus) Node {
node := Node{}
node.consensus = consensus
node.BlockChannel = make(chan blockchain.Block)
coinbaseTx := blockchain.NewCoinbaseTX("harmony", "1")
node.blockchain = &blockchain.Blockchain{}
node.blockchain.Blocks = make([]*blockchain.Block, 0)
node.blockchain.Blocks = append(node.blockchain.Blocks, blockchain.NewGenesisBlock(coinbaseTx))
node.utxoPool = blockchain.CreateUTXOPoolFromGenesisBlockChain(node.blockchain)
return node
}

Loading…
Cancel
Save