Refactor something in UTXOSet

This commit is contained in:
Ivan Kuznetsov 2017-09-18 11:01:24 +07:00
parent 337a637825
commit 827f124c61
1 changed files with 8 additions and 12 deletions

View File

@ -98,19 +98,15 @@ func (u UTXOSet) CountTransactions() int {
// Reindex rebuilds the UTXO set // Reindex rebuilds the UTXO set
func (u UTXOSet) Reindex() { func (u UTXOSet) Reindex() {
db := u.Blockchain.db db := u.Blockchain.db
bucketName := []byte(utxoBucket)
err := db.Update(func(tx *bolt.Tx) error { err := db.Update(func(tx *bolt.Tx) error {
bucketName := []byte(utxoBucket)
b := tx.Bucket(bucketName)
if b != nil {
err := tx.DeleteBucket(bucketName) err := tx.DeleteBucket(bucketName)
if err != nil { if err != nil && err != bolt.ErrBucketNotFound {
log.Panic(err) log.Panic(err)
} }
}
_, err := tx.CreateBucket(bucketName) _, err = tx.CreateBucket(bucketName)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }
@ -124,7 +120,7 @@ func (u UTXOSet) Reindex() {
UTXO := u.Blockchain.FindUTXO() UTXO := u.Blockchain.FindUTXO()
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(utxoBucket)) b := tx.Bucket(bucketName)
for txID, outs := range UTXO { for txID, outs := range UTXO {
key, err := hex.DecodeString(txID) key, err := hex.DecodeString(txID)
@ -154,8 +150,8 @@ func (u UTXOSet) Update(block *Block) {
if tx.IsCoinbase() == false { if tx.IsCoinbase() == false {
for _, vin := range tx.Vin { for _, vin := range tx.Vin {
updatedOuts := TXOutputs{} updatedOuts := TXOutputs{}
data := b.Get(vin.Txid) outsBytes := b.Get(vin.Txid)
outs := DeserializeOutputs(data) outs := DeserializeOutputs(outsBytes)
for outIdx, out := range outs.Outputs { for outIdx, out := range outs.Outputs {
if outIdx != vin.Vout { if outIdx != vin.Vout {