|
|
@ -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.
|
|
|
|