Implment UTXOSet.FindUTXO

This commit is contained in:
Ivan Kuznetsov 2017-09-17 11:01:18 +07:00
parent b15e1117f9
commit c3aa678291
1 changed files with 28 additions and 0 deletions

View File

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