From c3aa6782911812ff0ff602dc4eb29c738f9cad4a Mon Sep 17 00:00:00 2001 From: Ivan Kuznetsov Date: Sun, 17 Sep 2017 11:01:18 +0700 Subject: [PATCH] Implment UTXOSet.FindUTXO --- utxo_set.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/utxo_set.go b/utxo_set.go index 49c4c9f..0ee851d 100644 --- a/utxo_set.go +++ b/utxo_set.go @@ -82,3 +82,31 @@ func (u UTXOSet) GetCount() int { return counter } + +// FindUTXO finds UTXO for a public key hash +func (u UTXOSet) FindUTXO(pubKeyHash []byte) []TXOutput { + var UTXOs []TXOutput + db := u.Blockchain.db + + err := db.View(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte(utxoBucket)) + c := b.Cursor() + + for k, v := c.First(); k != nil; k, v = c.Next() { + outs := DeserializeOutputs(v) + + for _, out := range outs.Outputs { + if out.IsLockedWithKey(pubKeyHash) { + UTXOs = append(UTXOs, out) + } + } + } + + return nil + }) + if err != nil { + log.Panic(err) + } + + return UTXOs +}