Optimize InsertBlock with cached tx hashes.

This commit optimizes InsertBlock slightly by using the cached transaction
hashes instead of recomputing them.  Also, while here, use the
ShaHash.IsEqual function to do comparisons for consistency.

This is ongoing work towards conformal/btcd#25.

ok @drahn.
This commit is contained in:
Dave Collins 2013-10-26 18:02:09 -05:00
parent 998682ec83
commit 19880b177e

View file

@ -289,11 +289,11 @@ func (db *LevelDb) DropAfterBlockBySha(sha *btcwire.ShaHash) (rerr error) {
}
}
// rather than iterate the list of tx backward, do it twice.
for _, tx := range blk.MsgBlock().Transactions {
txSha, _ := tx.TxSha()
txShas, _ := blk.TxShas()
for _, txSha := range txShas {
var txUo txUpdateObj
txUo.delete = true
db.txUpdateMap[txSha] = &txUo
db.txUpdateMap[*txSha] = &txUo
}
db.lBatch().Delete(shaBlkToKey(blksha))
db.lBatch().Delete(int64ToKey(height))
@ -347,8 +347,7 @@ func (db *LevelDb) InsertBlock(block *btcutil.Block) (height int64, rerr error)
// miners, the sha of the transaction exists in a previous block,
// detect this condition and 'accept' the block.
for txidx, tx := range mblock.Transactions {
var txsha btcwire.ShaHash
txsha, err = tx.TxSha()
txsha, err := block.TxSha(txidx)
if err != nil {
log.Warnf("failed to compute tx name block %v idx %v err %v", blocksha, txidx, err)
return 0, err
@ -362,7 +361,7 @@ func (db *LevelDb) InsertBlock(block *btcutil.Block) (height int64, rerr error)
if err != nil {
panic("invalid sha string in source")
}
if txsha == *dupsha {
if txsha.IsEqual(dupsha) {
//log.Tracef("skipping sha %v %v", dupsha, newheight)
continue
}
@ -372,7 +371,7 @@ func (db *LevelDb) InsertBlock(block *btcutil.Block) (height int64, rerr error)
if err != nil {
panic("invalid sha string in source")
}
if txsha == *dupsha {
if txsha.IsEqual(dupsha) {
//log.Tracef("skipping sha %v %v", dupsha, newheight)
continue
}
@ -385,15 +384,15 @@ 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 {
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
}
err = db.doSpend(tx)
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
}