Add option to disable txcache
parent
993fe001e4
commit
7cb8c8d3f0
|
@ -63,6 +63,8 @@ var (
|
|||
explorerURL = flag.String("explorer", "", "address of blockchain explorer")
|
||||
|
||||
coin = flag.String("coin", "btc", "coin name")
|
||||
|
||||
noTxCache = flag.Bool("notxcache", false, "disable tx cache")
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -156,7 +158,7 @@ func main() {
|
|||
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)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -12,29 +12,39 @@ type TxCache struct {
|
|||
db *RocksDB
|
||||
chain bchain.BlockChain
|
||||
metrics *common.Metrics
|
||||
enabled bool
|
||||
}
|
||||
|
||||
// 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{
|
||||
db: db,
|
||||
chain: chain,
|
||||
metrics: metrics,
|
||||
enabled: enabled,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetTransaction returns transaction either from RocksDB or if not present from blockchain
|
||||
// it the transaction is confirmed, it is stored in the RocksDB
|
||||
func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, uint32, error) {
|
||||
tx, h, err := c.db.GetTx(txid)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
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
|
||||
var tx *bchain.Tx
|
||||
var h uint32
|
||||
var err error
|
||||
if c.enabled {
|
||||
tx, h, err = c.db.GetTx(txid)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
|
@ -45,10 +55,12 @@ func (c *TxCache) GetTransaction(txid string, bestheight uint32) (*bchain.Tx, ui
|
|||
if tx.Confirmations > 0 {
|
||||
// the transaction in the currently best block has 1 confirmation
|
||||
h = bestheight - tx.Confirmations + 1
|
||||
err = c.db.PutTx(tx, h, tx.Blocktime)
|
||||
// do not return caching error, only log it
|
||||
if err != nil {
|
||||
glog.Error("PutTx error ", err)
|
||||
if c.enabled {
|
||||
err = c.db.PutTx(tx, h, tx.Blocktime)
|
||||
// do not return caching error, only log it
|
||||
if err != nil {
|
||||
glog.Error("PutTx error ", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
h = 0
|
||||
|
|
Loading…
Reference in New Issue