External API functions need to grab locks before performing internal database operations.
Untangle internal and external functions, to prevent recursive locks
This commit is contained in:
parent
dc3a2dbac0
commit
f205ff5ac0
2 changed files with 13 additions and 2 deletions
|
@ -50,6 +50,12 @@ type blockCacheObj struct {
|
||||||
func (db *SqliteDb) FetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, err error) {
|
func (db *SqliteDb) FetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, err error) {
|
||||||
db.dbLock.Lock()
|
db.dbLock.Lock()
|
||||||
defer db.dbLock.Unlock()
|
defer db.dbLock.Unlock()
|
||||||
|
return db.fetchBlockBySha(sha)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetchBlockBySha - return a btcutil Block, object may be a cached.
|
||||||
|
// Must be called with db lock held.
|
||||||
|
func (db *SqliteDb) fetchBlockBySha(sha *btcwire.ShaHash) (blk *btcutil.Block, err error) {
|
||||||
|
|
||||||
blkcache, ok := db.fetchBlockCache(sha)
|
blkcache, ok := db.fetchBlockCache(sha)
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -128,6 +134,9 @@ func (db *SqliteDb) insertBlockCache(sha *btcwire.ShaHash, blk *btcutil.Block) {
|
||||||
// FetchTxByShaList given a array of ShaHash, look up the transactions
|
// FetchTxByShaList given a array of ShaHash, look up the transactions
|
||||||
// and return them in a TxListReply array.
|
// and return them in a TxListReply array.
|
||||||
func (db *SqliteDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListReply {
|
func (db *SqliteDb) FetchTxByShaList(txShaList []*btcwire.ShaHash) []*btcdb.TxListReply {
|
||||||
|
db.dbLock.Lock()
|
||||||
|
defer db.dbLock.Unlock()
|
||||||
|
|
||||||
var replies []*btcdb.TxListReply
|
var replies []*btcdb.TxListReply
|
||||||
for _, txsha := range txShaList {
|
for _, txsha := range txShaList {
|
||||||
tx, _, _, _, height, txspent, err := db.fetchTxDataBySha(txsha)
|
tx, _, _, _, height, txspent, err := db.fetchTxDataBySha(txsha)
|
||||||
|
@ -171,7 +180,7 @@ func (db *SqliteDb) fetchTxDataBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
blksha, err = db.FetchBlockShaByHeight(height)
|
blksha, err = db.fetchBlockShaByHeight(height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("block idx lookup %v to %v", height, err)
|
log.Warnf("block idx lookup %v to %v", height, err)
|
||||||
return
|
return
|
||||||
|
@ -179,7 +188,7 @@ func (db *SqliteDb) fetchTxDataBySha(txsha *btcwire.ShaHash) (rtx *btcwire.MsgTx
|
||||||
log.Tracef("transaction %v is at block %v %v tx %v",
|
log.Tracef("transaction %v is at block %v %v tx %v",
|
||||||
txsha, blksha, height, toff)
|
txsha, blksha, height, toff)
|
||||||
|
|
||||||
blk, err = db.FetchBlockBySha(blksha)
|
blk, err = db.fetchBlockBySha(blksha)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("unable to fetch block %v %v ",
|
log.Warnf("unable to fetch block %v %v ",
|
||||||
height, &blksha)
|
height, &blksha)
|
||||||
|
|
|
@ -120,6 +120,8 @@ func (db *SqliteDb) existsTxSha(txsha *btcwire.ShaHash) (exists bool) {
|
||||||
|
|
||||||
// FetchLocationBySha looks up the Tx sha information by name.
|
// FetchLocationBySha looks up the Tx sha information by name.
|
||||||
func (db *SqliteDb) FetchLocationBySha(txsha *btcwire.ShaHash) (blockidx int64, txoff int, txlen int, err error) {
|
func (db *SqliteDb) FetchLocationBySha(txsha *btcwire.ShaHash) (blockidx int64, txoff int, txlen int, err error) {
|
||||||
|
db.dbLock.Lock()
|
||||||
|
defer db.dbLock.Unlock()
|
||||||
return db.fetchLocationBySha(txsha)
|
return db.fetchLocationBySha(txsha)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue