fix blockchain_test

pull/3/head
Minh Doan 7 years ago
parent 83240f3506
commit c7ff14af3d
  1. 12
      blockchain/block.go
  2. 3
      blockchain/block_test.go
  3. 12
      blockchain/blockchain.go
  4. 2
      blockchain/blockchain_test.go
  5. 53
      blockchain/transaction.go

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/sha256"
"encoding/gob"
"fmt"
"log"
"time"
)
@ -38,6 +39,17 @@ func DeserializeBlock(d []byte) *Block {
return &block
}
// Used for debuging.
func (b *Block) String() string {
res := fmt.Sprintf("Block created at %v\n", b.Timestamp)
for id, tx := range b.Transactions {
res += fmt.Sprintf("Transaction %v: %v\n", id, *tx)
}
res += fmt.Sprintf("previous blockhash: %v\n", b.PrevBlockHash)
res += fmt.Sprintf("hash: %v\n", b.Hash)
return res
}
// HashTransactions returns a hash of the transactions in the block
func (b *Block) HashTransactions() []byte {
var txHashes [][]byte

@ -2,6 +2,7 @@ package blockchain
import (
"bytes"
"fmt"
"testing"
)
@ -23,4 +24,6 @@ func TestBlockSerialize(t *testing.T) {
if bytes.Compare(block.Hash, deserializedBlock.Hash) != 0 {
t.Errorf("Serialize or Deserialize incorrect at Hash.")
}
fmt.Println(block)
}

@ -19,11 +19,11 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
for index := len(bc.blocks) - 1; index >= 0; index-- {
block := bc.blocks[index]
BreakTransaction:
for _, tx := range block.Transactions {
txID := hex.EncodeToString(tx.ID)
idx := -1
// TODO(minhdoan): Optimize this.
if spentTXOs[txID] != nil {
idx = 0
}
@ -35,7 +35,15 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []Transaction {
if txOutput.Address == address {
unspentTXs = append(unspentTXs, *tx)
continue BreakTransaction
// Break because of the assumption that each address appears once in output.
break
}
}
for _, txInput := range tx.TxInput {
if address == txInput.Address {
ID := hex.EncodeToString(txInput.TxID)
spentTXOs[ID] = append(spentTXOs[ID], txInput.TxOutputIndex)
}
}
}

@ -47,10 +47,8 @@ func TestAddNewTransferAmount(t *testing.T) {
t.Error("Failed to add new transfer")
}
t.Log(len(bc.blocks))
bc = bc.AddNewTransferAmount("minh", "rj", DefaultCoinbaseValue-2)
t.Log(len(bc.blocks))
if bc != nil {
t.Error("minh should not have enough fun to make the transfer")
}

@ -60,38 +60,31 @@ func NewCoinbaseTX(to, data string) *Transaction {
return &tx
}
// NewUTXOTransaction creates a new transaction
func NewUTXOTransaction(from, to string, amount int, bc *Blockchain) *Transaction {
var inputs []TXInput
var outputs []TXOutput
acc, validOutputs := bc.FindSpendableOutputs(from, amount)
if acc < amount {
return nil
}
// Used for debuging.
func (txInput *TXInput) String() string {
res := fmt.Sprintf("TxID: %v, ", hex.EncodeToString(txInput.TxID))
res += fmt.Sprintf("TxOutputIndex: %v, ", txInput.TxOutputIndex)
res += fmt.Sprintf("Address: %v", txInput.Address)
return res
}
// Build a list of inputs
for txid, outs := range validOutputs {
txID, err := hex.DecodeString(txid)
if err != nil {
return nil
}
// Used for debuging.
func (txOutput *TXOutput) String() string {
res := fmt.Sprintf("Value: %v, ", txOutput.Value)
res += fmt.Sprintf("Address: %v", txOutput.Address)
return res
}
for _, out := range outs {
input := TXInput{txID, out, from}
inputs = append(inputs, input)
// Used for debuging.
func (tx *Transaction) String() string {
res := fmt.Sprintf("ID: %v\n", hex.EncodeToString(tx.ID))
res += fmt.Sprintf("TxInput:\n")
for id, value := range tx.TxInput {
res += fmt.Sprintf("%v: %v\n", id, value.String())
}
res += fmt.Sprintf("TxOutput:\n")
for id, value := range tx.TxOutput {
res += fmt.Sprintf("%v: %v\n", id, value.String())
}
// Build a list of outputs
outputs = append(outputs, TXOutput{amount, to})
if acc > amount {
outputs = append(outputs, TXOutput{acc - amount, from}) // a change
}
tx := Transaction{nil, inputs, outputs}
tx.SetID()
return &tx
return res
}

Loading…
Cancel
Save