blockbook/db/txcache.go

103 lines
2.6 KiB
Go
Raw Permalink Normal View History

2018-03-06 04:36:24 -07:00
package db
import (
"github.com/golang/glog"
"github.com/juju/errors"
2020-02-26 09:17:43 -07:00
"github.com/trezor/blockbook/bchain"
"github.com/trezor/blockbook/bchain/coins/eth"
"github.com/trezor/blockbook/common"
2018-03-06 04:36:24 -07:00
)
// TxCache is handle to TxCacheServer
type TxCache struct {
db *RocksDB
chain bchain.BlockChain
metrics *common.Metrics
is *common.InternalState
enabled bool
chainType bchain.ChainType
2018-03-06 04:36:24 -07:00
}
// NewTxCache creates new TxCache interface and returns its handle
2018-10-08 06:55:21 -06:00
func NewTxCache(db *RocksDB, chain bchain.BlockChain, metrics *common.Metrics, is *common.InternalState, enabled bool) (*TxCache, error) {
2018-05-14 07:49:08 -06:00
if !enabled {
glog.Info("txcache: disabled")
}
2018-03-06 04:36:24 -07:00
return &TxCache{
db: db,
chain: chain,
metrics: metrics,
is: is,
enabled: enabled,
chainType: chain.GetChainParser().GetChainType(),
2018-03-06 04:36:24 -07:00
}, 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) (*bchain.Tx, int, error) {
2018-05-14 07:49:08 -06:00
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
2018-10-08 06:55:21 -06:00
_, bestheight, _ := c.is.GetSyncState()
2018-05-14 07:49:08 -06:00
tx.Confirmations = bestheight - h + 1
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "hit"}).Inc()
return tx, int(h), nil
2018-05-14 07:49:08 -06:00
}
2018-03-06 04:36:24 -07:00
}
tx, err = c.chain.GetTransaction(txid)
if err != nil {
2018-04-06 09:13:38 -06:00
return nil, 0, err
2018-03-06 04:36:24 -07:00
}
2018-03-13 04:34:49 -06:00
c.metrics.TxCacheEfficiency.With(common.Labels{"status": "miss"}).Inc()
2018-10-08 06:55:21 -06:00
// cache only confirmed transactions
2018-03-06 04:36:24 -07:00
if tx.Confirmations > 0 {
if c.chainType == bchain.ChainBitcoinType {
ta, err := c.db.GetTxAddresses(txid)
if err != nil {
return nil, 0, err
}
switch {
case ta == nil:
// the transaction may not yet be indexed, in that case:
if tx.BlockHeight > 0 {
// Check if the tx height value is set.
h = tx.BlockHeight
} else {
// Get the height from the backend's bestblock.
h, err = c.chain.GetBestBlockHeight()
if err != nil {
return nil, 0, err
}
}
default:
h = ta.Height
}
} else if c.chainType == bchain.ChainEthereumType {
h, err = eth.GetHeightFromTx(tx)
2018-10-08 06:55:21 -06:00
if err != nil {
return nil, 0, err
}
} else {
return nil, 0, errors.New("Unknown chain type")
2018-10-08 06:55:21 -06:00
}
2018-05-14 07:49:08 -06:00
if c.enabled {
err = c.db.PutTx(tx, h, tx.Blocktime)
// do not return caching error, only log it
if err != nil {
2020-05-21 14:43:18 -06:00
glog.Warning("PutTx ", tx.Txid, ",error ", err)
2018-05-14 07:49:08 -06:00
}
2018-03-06 04:36:24 -07:00
}
2018-04-06 09:13:38 -06:00
} else {
return tx, -1, nil
2018-03-06 04:36:24 -07:00
}
return tx, int(h), nil
2018-03-06 04:36:24 -07:00
}