Use NewReader instead of NewBuffer.
NewReader is slightly faster than NewBuffer, but more importantly, ensures the data is read-only.
This commit is contained in:
parent
e2628dfc0d
commit
0dd81ad003
3 changed files with 21 additions and 21 deletions
|
@ -61,7 +61,7 @@ func (db *LevelDb) FetchBlockHeaderBySha(sha *btcwire.ShaHash) (bh *btcwire.Bloc
|
||||||
// Only deserialize the header portion and ensure the transaction count
|
// Only deserialize the header portion and ensure the transaction count
|
||||||
// is zero since this is a standalone header.
|
// is zero since this is a standalone header.
|
||||||
var blockHeader btcwire.BlockHeader
|
var blockHeader btcwire.BlockHeader
|
||||||
err = blockHeader.Deserialize(bytes.NewBuffer(buf))
|
err = blockHeader.Deserialize(bytes.NewReader(buf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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) {
|
func (db *LevelDb) setBlk(sha *btcwire.ShaHash, blkHeight int64, buf []byte) {
|
||||||
// serialize
|
// serialize
|
||||||
var lw [8]byte
|
var lw [8]byte
|
||||||
binary.LittleEndian.PutUint64(lw[:], uint64(blkHeight))
|
binary.LittleEndian.PutUint64(lw[0:8], uint64(blkHeight))
|
||||||
|
|
||||||
shaKey := shaBlkToKey(sha)
|
shaKey := shaBlkToKey(sha)
|
||||||
blkKey := int64ToKey(blkHeight)
|
blkKey := int64ToKey(blkHeight)
|
||||||
|
|
|
@ -649,10 +649,10 @@ func (db *LevelDb) processBatches() error {
|
||||||
for txSha, txU := range db.txUpdateMap {
|
for txSha, txU := range db.txUpdateMap {
|
||||||
key := shaTxToKey(&txSha)
|
key := shaTxToKey(&txSha)
|
||||||
if txU.delete {
|
if txU.delete {
|
||||||
log.Tracef("deleting tx %v", txSha)
|
//log.Tracef("deleting tx %v", txSha)
|
||||||
db.lbatch.Delete(key)
|
db.lbatch.Delete(key)
|
||||||
} else {
|
} else {
|
||||||
log.Tracef("inserting tx %v", txSha)
|
//log.Tracef("inserting tx %v", txSha)
|
||||||
txdat := db.formatTx(txU)
|
txdat := db.formatTx(txU)
|
||||||
db.lbatch.Put(key, txdat)
|
db.lbatch.Put(key, txdat)
|
||||||
}
|
}
|
||||||
|
@ -660,10 +660,10 @@ func (db *LevelDb) processBatches() error {
|
||||||
for txSha, txSu := range db.txSpentUpdateMap {
|
for txSha, txSu := range db.txSpentUpdateMap {
|
||||||
key := shaSpentTxToKey(&txSha)
|
key := shaSpentTxToKey(&txSha)
|
||||||
if txSu.delete {
|
if txSu.delete {
|
||||||
log.Tracef("deleting tx %v", txSha)
|
//log.Tracef("deleting tx %v", txSha)
|
||||||
db.lbatch.Delete(key)
|
db.lbatch.Delete(key)
|
||||||
} else {
|
} else {
|
||||||
log.Tracef("inserting tx %v", txSha)
|
//log.Tracef("inserting tx %v", txSha)
|
||||||
txdat := db.formatTxFullySpent(txSu.txl)
|
txdat := db.formatTxFullySpent(txSu.txl)
|
||||||
db.lbatch.Put(key, txdat)
|
db.lbatch.Put(key, txdat)
|
||||||
}
|
}
|
||||||
|
|
30
ldb/tx.go
30
ldb/tx.go
|
@ -67,9 +67,9 @@ func (db *LevelDb) formatTx(txu *txUpdateObj) []byte {
|
||||||
spentbuf := txu.spentData
|
spentbuf := txu.spentData
|
||||||
|
|
||||||
txW := make([]byte, 16+len(spentbuf))
|
txW := make([]byte, 16+len(spentbuf))
|
||||||
binary.LittleEndian.PutUint64(txW[:], blkHeight)
|
binary.LittleEndian.PutUint64(txW[0:8], blkHeight)
|
||||||
binary.LittleEndian.PutUint32(txW[8:], txOff)
|
binary.LittleEndian.PutUint32(txW[8:12], txOff)
|
||||||
binary.LittleEndian.PutUint32(txW[12:], txLen)
|
binary.LittleEndian.PutUint32(txW[12:16], txLen)
|
||||||
copy(txW[16:], spentbuf)
|
copy(txW[16:], spentbuf)
|
||||||
|
|
||||||
return txW[:]
|
return txW[:]
|
||||||
|
@ -82,9 +82,9 @@ func (db *LevelDb) getTxData(txsha *btcwire.ShaHash) (int64, int, int, []byte, e
|
||||||
return 0, 0, 0, nil, err
|
return 0, 0, 0, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
blkHeight := binary.LittleEndian.Uint64(buf)
|
blkHeight := binary.LittleEndian.Uint64(buf[0:8])
|
||||||
txOff := binary.LittleEndian.Uint32(buf[8:])
|
txOff := binary.LittleEndian.Uint32(buf[8:12])
|
||||||
txLen := binary.LittleEndian.Uint32(buf[12:])
|
txLen := binary.LittleEndian.Uint32(buf[12:16])
|
||||||
|
|
||||||
spentBuf := make([]byte, len(buf)-16)
|
spentBuf := make([]byte, len(buf)-16)
|
||||||
copy(spentBuf, buf[16:])
|
copy(spentBuf, buf[16:])
|
||||||
|
@ -109,10 +109,10 @@ func (db *LevelDb) getTxFullySpent(txsha *btcwire.ShaHash) ([]*spentTx, error) {
|
||||||
for i := range spentTxList {
|
for i := range spentTxList {
|
||||||
offset := i * 20
|
offset := i * 20
|
||||||
|
|
||||||
blkHeight := binary.LittleEndian.Uint64(buf[offset:])
|
blkHeight := binary.LittleEndian.Uint64(buf[offset:offset+8])
|
||||||
txOff := binary.LittleEndian.Uint32(buf[offset+8:])
|
txOff := binary.LittleEndian.Uint32(buf[offset+8:offset+12])
|
||||||
txLen := binary.LittleEndian.Uint32(buf[offset+12:])
|
txLen := binary.LittleEndian.Uint32(buf[offset+12:offset+16])
|
||||||
numTxO := binary.LittleEndian.Uint32(buf[offset+16:])
|
numTxO := binary.LittleEndian.Uint32(buf[offset+16:offset+20])
|
||||||
|
|
||||||
sTx := spentTx{
|
sTx := spentTx{
|
||||||
blkHeight: int64(blkHeight),
|
blkHeight: int64(blkHeight),
|
||||||
|
@ -137,10 +137,10 @@ func (db *LevelDb) formatTxFullySpent(sTxList []*spentTx) []byte {
|
||||||
numTxO := uint32(sTx.numTxO)
|
numTxO := uint32(sTx.numTxO)
|
||||||
offset := i * 20
|
offset := i * 20
|
||||||
|
|
||||||
binary.LittleEndian.PutUint64(txW[offset:], blkHeight)
|
binary.LittleEndian.PutUint64(txW[offset:offset+8], blkHeight)
|
||||||
binary.LittleEndian.PutUint32(txW[offset+8:], txOff)
|
binary.LittleEndian.PutUint32(txW[offset+8:offset+12], txOff)
|
||||||
binary.LittleEndian.PutUint32(txW[offset+12:], txLen)
|
binary.LittleEndian.PutUint32(txW[offset+12:offset+16], txLen)
|
||||||
binary.LittleEndian.PutUint32(txW[offset+16:], numTxO)
|
binary.LittleEndian.PutUint32(txW[offset+16:offset+20], numTxO)
|
||||||
}
|
}
|
||||||
|
|
||||||
return txW
|
return txW
|
||||||
|
@ -272,7 +272,7 @@ func (db *LevelDb) fetchTxDataByLoc(blkHeight int64, txOff int, txLen int, txspe
|
||||||
err = btcdb.TxShaMissing
|
err = btcdb.TxShaMissing
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rbuf := bytes.NewBuffer(blkbuf[txOff : txOff+txLen])
|
rbuf := bytes.NewReader(blkbuf[txOff : txOff+txLen])
|
||||||
|
|
||||||
var tx btcwire.MsgTx
|
var tx btcwire.MsgTx
|
||||||
err = tx.Deserialize(rbuf)
|
err = tx.Deserialize(rbuf)
|
||||||
|
|
Loading…
Reference in a new issue