From d47a497265d578d78fadeda9496074fd476f9e10 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Mon, 18 Jun 2018 23:14:37 -0700 Subject: [PATCH] fix tests --- blockchain/utxopool.go | 21 ++++++++++++++++++--- blockchain/utxopool_test.go | 7 +++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/blockchain/utxopool.go b/blockchain/utxopool.go index cac48da40..cd647f416 100644 --- a/blockchain/utxopool.go +++ b/blockchain/utxopool.go @@ -2,6 +2,7 @@ package blockchain import ( "encoding/hex" + "fmt" ) const ( @@ -73,9 +74,6 @@ func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { index := in.TxOutputIndex // Check if the transaction with the addres is spent or not. if val, ok := utxoPool.utxo[in.Address][inTxID][index]; ok { - if spentTXOs[in.Address][inTxID][index] { - return false - } inTotal += val } else { return false @@ -87,6 +85,9 @@ func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool { if _, ok := spentTXOs[in.Address][inTxID]; !ok { spentTXOs[in.Address][inTxID] = make(map[int]bool) } + if spentTXOs[in.Address][inTxID][index] { + return false + } spentTXOs[in.Address][inTxID][index] = true } @@ -206,3 +207,17 @@ func (utxoPool *UTXOPool) SelectTransactionsForNewBlock(transactions []*Transact } return selected, unselected } + +// Used for debugging. +func (utxoPool *UTXOPool) String() string { + res := "" + for address, v1 := range utxoPool.utxo { + for txid, v2 := range v1 { + for index, value := range v2 { + res += fmt.Sprintf("address: %v, tx id: %v, index: %v, value: %v\n", address, txid, index, value) + + } + } + } + return res +} diff --git a/blockchain/utxopool_test.go b/blockchain/utxopool_test.go index c47fb4b24..819e637e1 100644 --- a/blockchain/utxopool_test.go +++ b/blockchain/utxopool_test.go @@ -16,8 +16,7 @@ func TestVerifyOneTransactionAndUpdate(t *testing.T) { t.Error("failed to create a new transaction.") } - // if utxoPool.VerifyOneTransaction(tx) { - // t.Error("failed to verify a valid transaction.") - // } - + if !utxoPool.VerifyOneTransaction(tx) { + t.Error("failed to verify a valid transaction.") + } }