Two fixes

fix returning error on insertTx and doSpend.
clear the batch on failures as well.
This commit is contained in:
David Hill 2013-11-26 15:58:13 -05:00
parent fc11da9ca0
commit ca502abbf2

View file

@ -314,6 +314,8 @@ func (db *LevelDb) InsertBlock(block *btcutil.Block) (height int64, rerr error)
defer func() { defer func() {
if rerr == nil { if rerr == nil {
rerr = db.processBatches() rerr = db.processBatches()
} else {
db.lBatch().Reset()
} }
}() }()
@ -363,8 +365,7 @@ func (db *LevelDb) InsertBlock(block *btcutil.Block) (height int64, rerr error)
err = db.insertTx(txsha, newheight, txloc[txidx].TxStart, txloc[txidx].TxLen, spentbuf) err = db.insertTx(txsha, newheight, txloc[txidx].TxStart, txloc[txidx].TxLen, spentbuf)
if err != nil { if err != nil {
log.Warnf("block %v idx %v failed to insert tx %v %v err %v", blocksha, newheight, &txsha, txidx, err) log.Warnf("block %v idx %v failed to insert tx %v %v err %v", blocksha, newheight, &txsha, txidx, err)
return 0, err
return
} }
// Some old blocks contain duplicate transactions // Some old blocks contain duplicate transactions
@ -412,8 +413,7 @@ func (db *LevelDb) InsertBlock(block *btcutil.Block) (height int64, rerr error)
err = db.doSpend(tx) err = db.doSpend(tx)
if err != nil { if err != nil {
log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, txsha, txidx, err) log.Warnf("block %v idx %v failed to spend tx %v %v err %v", blocksha, newheight, txsha, txidx, err)
return 0, err
return
} }
} }
return newheight, nil return newheight, nil
@ -626,6 +626,8 @@ func (db *LevelDb) processBatches() error {
db.lbatch = new(leveldb.Batch) db.lbatch = new(leveldb.Batch)
} }
defer db.lbatch.Reset()
for txSha, txU := range db.txUpdateMap { for txSha, txU := range db.txUpdateMap {
key := shaTxToKey(&txSha) key := shaTxToKey(&txSha)
if txU.delete { if txU.delete {
@ -660,7 +662,6 @@ func (db *LevelDb) processBatches() error {
log.Tracef("batch failed %v\n", err) log.Tracef("batch failed %v\n", err)
return err return err
} }
db.lbatch.Reset()
db.txUpdateMap = map[btcwire.ShaHash]*txUpdateObj{} db.txUpdateMap = map[btcwire.ShaHash]*txUpdateObj{}
db.txSpentUpdateMap = make(map[btcwire.ShaHash]*spentTxUpdate) db.txSpentUpdateMap = make(map[btcwire.ShaHash]*spentTxUpdate)
} }