From 4c11a13d95e11dcb06133ab140f255ccd7408d8d Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Tue, 19 Jun 2018 12:49:30 -0700 Subject: [PATCH] Add blockchain/utxopool to node struct --- blockchain/blockchain.go | 16 ++++++++-------- blockchain/blockchain_test.go | 2 +- blockchain/utxopool.go | 2 +- node/node.go | 8 ++++++++ 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/blockchain/blockchain.go b/blockchain/blockchain.go index 44ab3d8bc..3fb89e0b3 100644 --- a/blockchain/blockchain.go +++ b/blockchain/blockchain.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 } diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index b4318367c..ed7c73652 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -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.") diff --git a/blockchain/utxopool.go b/blockchain/utxopool.go index cd647f416..7960745fd 100644 --- a/blockchain/utxopool.go +++ b/blockchain/utxopool.go @@ -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) } diff --git a/node/node.go b/node/node.go index 1f5bd37d7..7434c1c2f 100644 --- a/node/node.go +++ b/node/node.go @@ -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 }