Rework UTXO related functions
This commit is contained in:
parent
f4ae5168b0
commit
e89846d490
|
@ -106,8 +106,24 @@ func (bc *Blockchain) FindUnspentTransactions(address string) []*Transaction {
|
|||
return unspentTXs
|
||||
}
|
||||
|
||||
// FindUTXOs finds and returns unspend transaction outputs for the address
|
||||
func (bc *Blockchain) FindUTXOs(address string, amount int) (int, map[string][]int) {
|
||||
// FindUTXO finds and returns all unspent transaction outputs
|
||||
func (bc *Blockchain) FindUTXO(address string) []TXOutput {
|
||||
var UTXOs []TXOutput
|
||||
unspentTransactions := bc.FindUnspentTransactions(address)
|
||||
|
||||
for _, tx := range unspentTransactions {
|
||||
for _, out := range tx.Vout {
|
||||
if out.CanBeUnlockedWith(address) {
|
||||
UTXOs = append(UTXOs, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return UTXOs
|
||||
}
|
||||
|
||||
// FindSpendableOutputs finds and returns unspent outputs to reference in inputs
|
||||
func (bc *Blockchain) FindSpendableOutputs(address string, amount int) (int, map[string][]int) {
|
||||
unspentOutputs := make(map[string][]int)
|
||||
unspentTXs := bc.FindUnspentTransactions(address)
|
||||
accumulated := 0
|
||||
|
|
10
cli.go
10
cli.go
|
@ -22,14 +22,10 @@ func (cli *CLI) getBalance(address string) {
|
|||
defer bc.db.Close()
|
||||
|
||||
balance := 0
|
||||
utxs := bc.FindUnspentTransactions(address)
|
||||
UTXOs := bc.FindUTXO(address)
|
||||
|
||||
for _, tx := range utxs {
|
||||
for _, out := range tx.Vout {
|
||||
if out.CanBeUnlockedWith(address) {
|
||||
balance += out.Value
|
||||
}
|
||||
}
|
||||
for _, out := range UTXOs {
|
||||
balance += out.Value
|
||||
}
|
||||
|
||||
fmt.Printf("Balance of '%s': %d\n", address, balance)
|
||||
|
|
|
@ -79,7 +79,7 @@ func NewUTXOTransaction(from, to string, value int, bc *Blockchain) *Transaction
|
|||
var inputs []TXInput
|
||||
var outputs []TXOutput
|
||||
|
||||
acc, validOutputs := bc.FindUTXOs(from, value)
|
||||
acc, validOutputs := bc.FindSpendableOutputs(from, value)
|
||||
|
||||
if acc < value {
|
||||
log.Panic("ERROR: Not enough funds")
|
||||
|
|
Loading…
Reference in New Issue