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