From 0dd81ad003c805dde3470ed6424b0ba3f4dd5805 Mon Sep 17 00:00:00 2001 From: David Hill Date: Thu, 5 Jun 2014 13:45:43 -0400 Subject: [PATCH] Use NewReader instead of NewBuffer. NewReader is slightly faster than NewBuffer, but more importantly, ensures the data is read-only. --- ldb/block.go | 4 ++-- ldb/leveldb.go | 8 ++++---- ldb/tx.go | 30 +++++++++++++++--------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ldb/block.go b/ldb/block.go index 5d4ff22f..25e4c1af 100644 --- a/ldb/block.go +++ b/ldb/block.go @@ -61,7 +61,7 @@ func (db *LevelDb) FetchBlockHeaderBySha(sha *btcwire.ShaHash) (bh *btcwire.Bloc // Only deserialize the header portion and ensure the transaction count // is zero since this is a standalone header. var blockHeader btcwire.BlockHeader - err = blockHeader.Deserialize(bytes.NewBuffer(buf)) + err = blockHeader.Deserialize(bytes.NewReader(buf)) if err != nil { return nil, err } @@ -125,7 +125,7 @@ func (db *LevelDb) getBlk(sha *btcwire.ShaHash) (rblkHeight int64, rbuf []byte, func (db *LevelDb) setBlk(sha *btcwire.ShaHash, blkHeight int64, buf []byte) { // serialize var lw [8]byte - binary.LittleEndian.PutUint64(lw[:], uint64(blkHeight)) + binary.LittleEndian.PutUint64(lw[0:8], uint64(blkHeight)) shaKey := shaBlkToKey(sha) blkKey := int64ToKey(blkHeight) diff --git a/ldb/leveldb.go b/ldb/leveldb.go index 1e797a5a..2d7a6e4a 100644 --- a/ldb/leveldb.go +++ b/ldb/leveldb.go @@ -649,10 +649,10 @@ func (db *LevelDb) processBatches() error { for txSha, txU := range db.txUpdateMap { key := shaTxToKey(&txSha) if txU.delete { - log.Tracef("deleting tx %v", txSha) + //log.Tracef("deleting tx %v", txSha) db.lbatch.Delete(key) } else { - log.Tracef("inserting tx %v", txSha) + //log.Tracef("inserting tx %v", txSha) txdat := db.formatTx(txU) db.lbatch.Put(key, txdat) } @@ -660,10 +660,10 @@ func (db *LevelDb) processBatches() error { for txSha, txSu := range db.txSpentUpdateMap { key := shaSpentTxToKey(&txSha) if txSu.delete { - log.Tracef("deleting tx %v", txSha) + //log.Tracef("deleting tx %v", txSha) db.lbatch.Delete(key) } else { - log.Tracef("inserting tx %v", txSha) + //log.Tracef("inserting tx %v", txSha) txdat := db.formatTxFullySpent(txSu.txl) db.lbatch.Put(key, txdat) } diff --git a/ldb/tx.go b/ldb/tx.go index c010998f..ef7ec77b 100644 --- a/ldb/tx.go +++ b/ldb/tx.go @@ -67,9 +67,9 @@ func (db *LevelDb) formatTx(txu *txUpdateObj) []byte { spentbuf := txu.spentData txW := make([]byte, 16+len(spentbuf)) - binary.LittleEndian.PutUint64(txW[:], blkHeight) - binary.LittleEndian.PutUint32(txW[8:], txOff) - binary.LittleEndian.PutUint32(txW[12:], txLen) + binary.LittleEndian.PutUint64(txW[0:8], blkHeight) + binary.LittleEndian.PutUint32(txW[8:12], txOff) + binary.LittleEndian.PutUint32(txW[12:16], txLen) copy(txW[16:], spentbuf) return txW[:] @@ -82,9 +82,9 @@ func (db *LevelDb) getTxData(txsha *btcwire.ShaHash) (int64, int, int, []byte, e return 0, 0, 0, nil, err } - blkHeight := binary.LittleEndian.Uint64(buf) - txOff := binary.LittleEndian.Uint32(buf[8:]) - txLen := binary.LittleEndian.Uint32(buf[12:]) + blkHeight := binary.LittleEndian.Uint64(buf[0:8]) + txOff := binary.LittleEndian.Uint32(buf[8:12]) + txLen := binary.LittleEndian.Uint32(buf[12:16]) spentBuf := make([]byte, len(buf)-16) copy(spentBuf, buf[16:]) @@ -109,10 +109,10 @@ func (db *LevelDb) getTxFullySpent(txsha *btcwire.ShaHash) ([]*spentTx, error) { for i := range spentTxList { offset := i * 20 - blkHeight := binary.LittleEndian.Uint64(buf[offset:]) - txOff := binary.LittleEndian.Uint32(buf[offset+8:]) - txLen := binary.LittleEndian.Uint32(buf[offset+12:]) - numTxO := binary.LittleEndian.Uint32(buf[offset+16:]) + blkHeight := binary.LittleEndian.Uint64(buf[offset:offset+8]) + txOff := binary.LittleEndian.Uint32(buf[offset+8:offset+12]) + txLen := binary.LittleEndian.Uint32(buf[offset+12:offset+16]) + numTxO := binary.LittleEndian.Uint32(buf[offset+16:offset+20]) sTx := spentTx{ blkHeight: int64(blkHeight), @@ -137,10 +137,10 @@ func (db *LevelDb) formatTxFullySpent(sTxList []*spentTx) []byte { numTxO := uint32(sTx.numTxO) offset := i * 20 - binary.LittleEndian.PutUint64(txW[offset:], blkHeight) - binary.LittleEndian.PutUint32(txW[offset+8:], txOff) - binary.LittleEndian.PutUint32(txW[offset+12:], txLen) - binary.LittleEndian.PutUint32(txW[offset+16:], numTxO) + binary.LittleEndian.PutUint64(txW[offset:offset+8], blkHeight) + binary.LittleEndian.PutUint32(txW[offset+8:offset+12], txOff) + binary.LittleEndian.PutUint32(txW[offset+12:offset+16], txLen) + binary.LittleEndian.PutUint32(txW[offset+16:offset+20], numTxO) } return txW @@ -272,7 +272,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe err = btcdb.TxShaMissing return } - rbuf := bytes.NewBuffer(blkbuf[txOff : txOff+txLen]) + rbuf := bytes.NewReader(blkbuf[txOff : txOff+txLen]) var tx btcwire.MsgTx err = tx.Deserialize(rbuf)