bundle block blockchain transaction into blockchain packagge. add tests for utils

pull/2/head
Minh Doan 7 years ago
parent 3d5c33ee14
commit 2eb9629202
  1. 6
      blockchain/block.go
  2. 26
      blockchain/block_test.go
  3. 15
      blockchain/blockchain.go
  4. 2
      blockchain/transaction.go
  5. 9
      blockchain/transaction_test.go
  6. 11
      utils/utils.go
  7. 17
      utils/utils_test.go
  8. 25
      utils_test.go

@ -1,4 +1,4 @@
package main
package blockchain
import (
"bytes"
@ -44,7 +44,7 @@ func (b *Block) HashTransactions() []byte {
var txHash [32]byte
for _, tx := range b.Transactions {
txHashes = append(txHashes, tx.ID)
txHashes = append(txHashes, tx.id)
}
txHash = sha256.Sum256(bytes.Join(txHashes, []byte{}))
return txHash[:]
@ -52,7 +52,7 @@ func (b *Block) HashTransactions() []byte {
// NewBlock creates and returns Block.
func NewBlock(transactions []*Transaction, prevBlockHash []byte) *Block {
block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}, 0}
block := &Block{time.Now().Unix(), transactions, prevBlockHash, []byte{}}
block.Hash = block.HashTransactions()
return block
}

@ -0,0 +1,26 @@
package blockchain
import (
"bytes"
"testing"
)
func TestBlockSerialize(t *testing.T) {
cbtx := NewCoinbaseTX("minh", genesisCoinbaseData)
block := NewGenesisBlock(cbtx)
serializedValue := block.Serialize()
deserializedBlock := DeserializeBlock(serializedValue)
if block.Timestamp != deserializedBlock.Timestamp {
t.Errorf("Serialize or Deserialize incorrect at TimeStamp.")
}
if bytes.Compare(block.PrevBlockHash, deserializedBlock.PrevBlockHash) != 0 {
t.Errorf("Serialize or Deserialize incorrect at PrevBlockHash.")
}
if bytes.Compare(block.Hash, deserializedBlock.Hash) != 0 {
t.Errorf("Serialize or Deserialize incorrect at Hash.")
}
}

@ -1,4 +1,4 @@
package main
package blockchain
import (
"encoding/hex"
@ -11,11 +11,6 @@ type Blockchain struct {
const genesisCoinbaseData = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks"
// NewBlockchain creates a new Blockchain with genesis Block
func NewBlockchain(address string) *Blockchain {
return &Blockchain{[]*Block{NewGenesisBlock()}}
}
// FindUnspentTransactions returns a list of transactions containing unspent outputs
func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
var unspentTXs []Transaction
@ -26,14 +21,14 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
BreakTransaction:
for _, tx := range block.Transactions {
txId := hex.EncodeToString(tx.Id)
txID := hex.EncodeToString(tx.id)
idx := -1
if spentTXOs[txId] != nil {
if spentTXOs[txID] != nil {
idx = 0
}
for outIdx, txOutput := range tx.txOutput {
if idx >= 0 && spentTXOs[txId][idx] == outIdx {
if idx >= 0 && spentTXOs[txID][idx] == outIdx {
idx++
continue
}
@ -73,7 +68,7 @@ func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map
Work:
for _, tx := range unspentTXs {
txID := hex.EncodeToString(tx.ID)
txID := hex.EncodeToString(tx.id)
for outIdx, txOutput := range tx.txOutput {
if txOutput.address == address && accumulated < amount {

@ -1,4 +1,4 @@
package main
package blockchain
import (
"bytes"

@ -0,0 +1,9 @@
package blockchain
import (
"testing"
)
func TestNewCoinbaseTX(t *testing.T) {
NewCoinbaseTX("minh", genesisCoinbaseData)
}

@ -1,4 +1,4 @@
package main
package utils
import (
"bytes"
@ -19,14 +19,15 @@ func IntToHex(num int64) []byte {
return buff.Bytes()
}
// Helper library to convert '1,2,3,4' into []int{1,2,3,4}.
// ConvertIntoInts is to convert '1,2,3,4' into []int{1,2,3,4}.
func ConvertIntoInts(data string) []int {
var res = []int{}
items := strings.Split(data, " ")
items := strings.Split(data, ",")
for _, value := range items {
intValue, err := strconv.Atoi(value)
checkError(err)
res = append(res, intValue)
if err == nil {
res = append(res, intValue)
}
}
return res
}

@ -0,0 +1,17 @@
package utils
import "testing"
func TestConvertIntoInts(t *testing.T) {
data := "1,2,3,4"
res := ConvertIntoInts(data)
if len(res) != 4 {
t.Errorf("Array length parsed incorrect.")
}
for id, value := range res {
if value != id+1 {
t.Errorf("Parsing incorrect.")
}
}
}

@ -1,25 +0,0 @@
package main
import "testing"
func TestConvertIntoMap(t *testing.T) {
data := "minh:3,mike:2"
res := ConvertIntoMap(data)
if len(res) != 2 {
t.Errorf("Result should have 2 pairs (key, value)")
}
if val, ok := res["minh"]; !ok {
t.Errorf("Result should contain key minh")
} else {
if res["minh"] != 3 {
t.Errorf("Value of minh should be 3")
}
}
if val, ok := res["mike"]; !ok {
t.Errorf("Result should contain key mike")
} else {
if res["minh"] != 3 {
t.Errorf("Value of minh should be 2")
}
}
}
Loading…
Cancel
Save