blockchain_go/utxo_set.go

87 lines
1.4 KiB
Go
Raw Normal View History

2017-09-16 22:16:14 -05:00
package main
import (
"encoding/hex"
"log"
"github.com/boltdb/bolt"
)
const utxoBucket = "chainstate"
// UTXOSet represents UTXO set
type UTXOSet struct{}
// Reindex rebuilds the UTXO set
func (u UTXOSet) Reindex(bc *Blockchain) {
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
log.Panic(err)
}
err = db.Update(func(tx *bolt.Tx) error {
bucketName := []byte(utxoBucket)
b := tx.Bucket(bucketName)
if b != nil {
err := tx.DeleteBucket(bucketName)
if err != nil {
log.Panic(err)
}
}
_, err := tx.CreateBucket(bucketName)
if err != nil {
log.Panic(err)
}
return nil
})
if err != nil {
log.Panic(err)
}
UTXO := bc.FindAllUTXO()
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(utxoBucket))
for txID, outs := range UTXO {
key, err := hex.DecodeString(txID)
if err != nil {
log.Panic(err)
}
err = b.Put(key, outs.Serialize())
if err != nil {
log.Panic(err)
}
}
return nil
})
}
// GetCount returns the number of transactions in the UTXO set
func (u UTXOSet) GetCount() int {
counter := 0
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
log.Panic(err)
}
err = db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(utxoBucket))
c := b.Cursor()
for k, _ := c.First(); k != nil; k, _ = c.Next() {
counter++
}
return nil
})
return counter
}