add VerifyNewBlock and its test

pull/5/head
Minh Doan 7 years ago
parent f2de27fa32
commit 4129232928
  1. 2
      blockchain/block.go
  2. 19
      blockchain/blockchain.go
  3. 16
      blockchain/blockchain_test.go

@ -62,7 +62,7 @@ func (b *Block) HashTransactions() []byte {
return txHash[:] return txHash[:]
} }
// NewBlock creates and returns Block. // NewBlock creates and returns a neew block.
func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block { func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}} block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}}
block.Hash = block.HashTransactions() block.Hash = block.HashTransactions()

@ -1,7 +1,9 @@
package blockchain package blockchain
import ( import (
"bytes"
"encoding/hex" "encoding/hex"
"fmt"
) )
// Blockchain keeps a sequence of Blocks // Blockchain keeps a sequence of Blocks
@ -134,13 +136,28 @@ func (bc *Blockchain) NewUTXOTransaction(from, to string, amount int) *Transacti
func (bc *Blockchain) AddNewTransferAmount(from, to string, amount int) *Blockchain { func (bc *Blockchain) AddNewTransferAmount(from, to string, amount int) *Blockchain {
tx := bc.NewUTXOTransaction(from, to, amount) tx := bc.NewUTXOTransaction(from, to, amount)
if tx != nil { if tx != nil {
newBlock := NewBlock([]*Transaction{tx}, bc.blocks[len(bc.blocks)-1].PrevBlockHash) newBlock := NewBlock([]*Transaction{tx}, bc.blocks[len(bc.blocks)-1].Hash)
bc.blocks = append(bc.blocks, newBlock) bc.blocks = append(bc.blocks, newBlock)
return bc return bc
} }
return nil return nil
} }
// VerifyNewBlock verifies if the new coming block is valid for the current blockchain.
func (bc *Blockchain) VerifyNewBlock(block *Block) bool {
length := len(bc.blocks)
if bytes.Compare(block.PrevBlockHash, bc.blocks[length-1].Hash) != 0 {
fmt.Println("MINh1")
return false
}
if block.Timestamp < bc.blocks[length-1].Timestamp {
fmt.Println("MINh2")
return false
}
// TODO(minhdoan): Check Transactions parts
return true
}
// CreateBlockchain creates a new blockchain DB // CreateBlockchain creates a new blockchain DB
func CreateBlockchain(address string) *Blockchain { func CreateBlockchain(address string) *Blockchain {
// TODO: We assume we have not created any blockchain before. // TODO: We assume we have not created any blockchain before.

@ -61,3 +61,19 @@ func TestAddNewTransferAmount(t *testing.T) {
t.Error("minh should not have enough fun to make the transfer") t.Error("minh should not have enough fun to make the transfer")
} }
} }
func TestVerifyNewBlock(t *testing.T) {
bc := CreateBlockchain("minh")
bc = bc.AddNewTransferAmount("minh", "alok", 3)
bc = bc.AddNewTransferAmount("minh", "rj", 100)
tx := bc.NewUTXOTransaction("minh", "mark", 10)
if tx == nil {
t.Error("failed to create a new transaction.")
}
newBlock := NewBlock([]*Transaction{tx}, bc.blocks[len(bc.blocks)-1].Hash)
if !bc.VerifyNewBlock(newBlock) {
t.Error("failed to add a new valid block.")
}
}

Loading…
Cancel
Save