Add option to disable txcache

This commit is contained in:
Martin Boehm 2018-05-14 15:49:08 +02:00
parent 993fe001e4
commit 7cb8c8d3f0
2 changed files with 29 additions and 15 deletions

View File

@ -63,6 +63,8 @@ var (
explorerURL = flag.String("explorer", "", "address of blockchain explorer") explorerURL = flag.String("explorer", "", "address of blockchain explorer")
coin = flag.String("coin", "btc", "coin name") coin = flag.String("coin", "btc", "coin name")
noTxCache = flag.Bool("notxcache", false, "disable tx cache")
) )
var ( var (
@ -156,7 +158,7 @@ func main() {
return return
} }
if txCache, err = db.NewTxCache(index, chain, metrics); err != nil { if txCache, err = db.NewTxCache(index, chain, metrics, !*noTxCache); err != nil {
glog.Error("txCache ", err) glog.Error("txCache ", err)
return return
} }

View File

@ -12,29 +12,39 @@ type TxCache struct {
db *RocksDB db *RocksDB
chain bchain.BlockChain chain bchain.BlockChain
metrics *common.Metrics metrics *common.Metrics
enabled bool
} }
// NewTxCache creates new TxCache interface and returns its handle // NewTxCache creates new TxCache interface and returns its handle
func NewTxCache(db *RocksDB, chain bchain.BlockChain, metrics *common.Metrics) (*TxCache, error) { func NewTxCache(db *RocksDB, chain bchain.BlockChain, metrics *common.Metrics, enabled bool) (*TxCache, error) {
if !enabled {
glog.Info("txcache: disabled")
}
return &TxCache{ return &TxCache{
db: db, db: db,
chain: chain, chain: chain,
metrics: metrics, metrics: metrics,
enabled: enabled,
}, nil }, nil
} }
// GetTransaction returns transaction either from RocksDB or if not present from blockchain // GetTransaction returns transaction either from RocksDB or if not present from blockchain
// it the transaction is confirmed, it is stored in the RocksDB // it the transaction is confirmed, it is stored in the RocksDB
func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, uint32, error) { func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, uint32, error) {
tx, h, err := c.db.GetTx(txid) var tx *bchain.Tx
if err != nil { var h uint32
return nil, 0, err var err error
} if c.enabled {
if tx != nil { tx, h, err = c.db.GetTx(txid)
// number of confirmations is not stored in cache, they change all the time if err != nil {
tx.Confirmations = bestheight - h + 1 return nil, 0, err
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "hit"}).Inc() }
return tx, h, nil if tx != nil {
// number of confirmations is not stored in cache, they change all the time
tx.Confirmations = bestheight - h + 1
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "hit"}).Inc()
return tx, h, nil
}
} }
tx, err = c.chain.GetTransaction(txid) tx, err = c.chain.GetTransaction(txid)
if err != nil { if err != nil {
@ -45,10 +55,12 @@ func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, ui
if tx.Confirmations > 0 { if tx.Confirmations > 0 {
// the transaction in the currently best block has 1 confirmation // the transaction in the currently best block has 1 confirmation
h = bestheight - tx.Confirmations + 1 h = bestheight - tx.Confirmations + 1
err = c.db.PutTx(tx, h, tx.Blocktime) if c.enabled {
// do not return caching error, only log it err = c.db.PutTx(tx, h, tx.Blocktime)
if err != nil { // do not return caching error, only log it
glog.Error("PutTx error ", err) if err != nil {
glog.Error("PutTx error ", err)
}
} }
} else { } else {
h = 0 h = 0