pull/5/merge
Minh Doan 7 years ago
parent 609a62aaa2
commit d47a497265
  1. 21
      blockchain/utxopool.go
  2. 7
      blockchain/utxopool_test.go

@ -2,6 +2,7 @@ package blockchain
import ( import (
"encoding/hex" "encoding/hex"
"fmt"
) )
const ( const (
@ -73,9 +74,6 @@ func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool {
index := in.TxOutputIndex index := in.TxOutputIndex
// Check if the transaction with the addres is spent or not. // Check if the transaction with the addres is spent or not.
if val, ok := utxoPool.utxo[in.Address][inTxID][index]; ok { if val, ok := utxoPool.utxo[in.Address][inTxID][index]; ok {
if spentTXOs[in.Address][inTxID][index] {
return false
}
inTotal += val inTotal += val
} else { } else {
return false return false
@ -87,6 +85,9 @@ func (utxoPool *UTXOPool) VerifyOneTransaction(tx *Transaction) bool {
if _, ok := spentTXOs[in.Address][inTxID]; !ok { if _, ok := spentTXOs[in.Address][inTxID]; !ok {
spentTXOs[in.Address][inTxID] = make(map[int]bool) spentTXOs[in.Address][inTxID] = make(map[int]bool)
} }
if spentTXOs[in.Address][inTxID][index] {
return false
}
spentTXOs[in.Address][inTxID][index] = true spentTXOs[in.Address][inTxID][index] = true
} }
@ -206,3 +207,17 @@ func (utxoPool *UTXOPool) SelectTransactionsForNewBlock(transactions []*Transact
} }
return selected, unselected 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
}

@ -16,8 +16,7 @@ func TestVerifyOneTransactionAndUpdate(t *testing.T) {
t.Error("failed to create a new transaction.") t.Error("failed to create a new transaction.")
} }
// if utxoPool.VerifyOneTransaction(tx) { if !utxoPool.VerifyOneTransaction(tx) {
// t.Error("failed to verify a valid transaction.") t.Error("failed to verify a valid transaction.")
// } }
} }

Loading…
Cancel
Save