mempool: unexport the mutex

callers should not need to lock/unlock the mempool themselves.
This commit is contained in:
David Hill 2016-08-23 14:59:48 -04:00
parent 641182b2ad
commit a109bea3f1

View file

@ -118,7 +118,7 @@ type TxPool struct {
// The following variables must only be used atomically. // The following variables must only be used atomically.
lastUpdated int64 // last time pool was updated lastUpdated int64 // last time pool was updated
sync.RWMutex mtx sync.RWMutex
cfg Config cfg Config
pool map[chainhash.Hash]*TxDesc pool map[chainhash.Hash]*TxDesc
orphans map[chainhash.Hash]*btcutil.Tx orphans map[chainhash.Hash]*btcutil.Tx
@ -165,9 +165,9 @@ func (mp *TxPool) removeOrphan(txHash *chainhash.Hash) {
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) RemoveOrphan(txHash *chainhash.Hash) { func (mp *TxPool) RemoveOrphan(txHash *chainhash.Hash) {
mp.Lock() mp.mtx.Lock()
mp.removeOrphan(txHash) mp.removeOrphan(txHash)
mp.Unlock() mp.mtx.Unlock()
} }
// limitNumOrphans limits the number of orphan transactions by evicting a random // limitNumOrphans limits the number of orphan transactions by evicting a random
@ -277,8 +277,8 @@ func (mp *TxPool) isTransactionInPool(hash *chainhash.Hash) bool {
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) IsTransactionInPool(hash *chainhash.Hash) bool { func (mp *TxPool) IsTransactionInPool(hash *chainhash.Hash) bool {
// Protect concurrent access. // Protect concurrent access.
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
return mp.isTransactionInPool(hash) return mp.isTransactionInPool(hash)
} }
@ -301,8 +301,8 @@ func (mp *TxPool) isOrphanInPool(hash *chainhash.Hash) bool {
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) IsOrphanInPool(hash *chainhash.Hash) bool { func (mp *TxPool) IsOrphanInPool(hash *chainhash.Hash) bool {
// Protect concurrent access. // Protect concurrent access.
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
return mp.isOrphanInPool(hash) return mp.isOrphanInPool(hash)
} }
@ -321,8 +321,8 @@ func (mp *TxPool) haveTransaction(hash *chainhash.Hash) bool {
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) HaveTransaction(hash *chainhash.Hash) bool { func (mp *TxPool) HaveTransaction(hash *chainhash.Hash) bool {
// Protect concurrent access. // Protect concurrent access.
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
return mp.haveTransaction(hash) return mp.haveTransaction(hash)
} }
@ -368,8 +368,8 @@ func (mp *TxPool) removeTransaction(tx *btcutil.Tx, removeRedeemers bool) {
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) RemoveTransaction(tx *btcutil.Tx, removeRedeemers bool) { func (mp *TxPool) RemoveTransaction(tx *btcutil.Tx, removeRedeemers bool) {
// Protect concurrent access. // Protect concurrent access.
mp.Lock() mp.mtx.Lock()
defer mp.Unlock() defer mp.mtx.Unlock()
mp.removeTransaction(tx, removeRedeemers) mp.removeTransaction(tx, removeRedeemers)
} }
@ -383,8 +383,8 @@ func (mp *TxPool) RemoveTransaction(tx *btcutil.Tx, removeRedeemers bool) {
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) RemoveDoubleSpends(tx *btcutil.Tx) { func (mp *TxPool) RemoveDoubleSpends(tx *btcutil.Tx) {
// Protect concurrent access. // Protect concurrent access.
mp.Lock() mp.mtx.Lock()
defer mp.Unlock() defer mp.mtx.Unlock()
for _, txIn := range tx.MsgTx().TxIn { for _, txIn := range tx.MsgTx().TxIn {
if txRedeemer, ok := mp.outpoints[txIn.PreviousOutPoint]; ok { if txRedeemer, ok := mp.outpoints[txIn.PreviousOutPoint]; ok {
@ -475,8 +475,8 @@ func (mp *TxPool) fetchInputUtxos(tx *btcutil.Tx) (*blockchain.UtxoViewpoint, er
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) FetchTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error) { func (mp *TxPool) FetchTransaction(txHash *chainhash.Hash) (*btcutil.Tx, error) {
// Protect concurrent access. // Protect concurrent access.
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
if txDesc, exists := mp.pool[*txHash]; exists { if txDesc, exists := mp.pool[*txHash]; exists {
return txDesc.Tx, nil return txDesc.Tx, nil
@ -754,8 +754,8 @@ func (mp *TxPool) maybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool)
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) MaybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool) ([]*chainhash.Hash, error) { func (mp *TxPool) MaybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit bool) ([]*chainhash.Hash, error) {
// Protect concurrent access. // Protect concurrent access.
mp.Lock() mp.mtx.Lock()
defer mp.Unlock() defer mp.mtx.Unlock()
return mp.maybeAcceptTransaction(tx, isNew, rateLimit) return mp.maybeAcceptTransaction(tx, isNew, rateLimit)
} }
@ -856,9 +856,9 @@ func (mp *TxPool) processOrphans(hash *chainhash.Hash) []*btcutil.Tx {
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) ProcessOrphans(hash *chainhash.Hash) []*btcutil.Tx { func (mp *TxPool) ProcessOrphans(hash *chainhash.Hash) []*btcutil.Tx {
mp.Lock() mp.mtx.Lock()
acceptedTxns := mp.processOrphans(hash) acceptedTxns := mp.processOrphans(hash)
mp.Unlock() mp.mtx.Unlock()
return acceptedTxns return acceptedTxns
} }
@ -876,8 +876,8 @@ func (mp *TxPool) ProcessOrphans(hash *chainhash.Hash) []*btcutil.Tx {
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) ProcessTransaction(tx *btcutil.Tx, allowOrphan, rateLimit bool) ([]*btcutil.Tx, error) { func (mp *TxPool) ProcessTransaction(tx *btcutil.Tx, allowOrphan, rateLimit bool) ([]*btcutil.Tx, error) {
// Protect concurrent access. // Protect concurrent access.
mp.Lock() mp.mtx.Lock()
defer mp.Unlock() defer mp.mtx.Unlock()
log.Tracef("Processing transaction %v", tx.Hash()) log.Tracef("Processing transaction %v", tx.Hash())
@ -935,8 +935,8 @@ func (mp *TxPool) ProcessTransaction(tx *btcutil.Tx, allowOrphan, rateLimit bool
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) Count() int { func (mp *TxPool) Count() int {
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
return len(mp.pool) return len(mp.pool)
} }
@ -946,8 +946,8 @@ func (mp *TxPool) Count() int {
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) TxHashes() []*chainhash.Hash { func (mp *TxPool) TxHashes() []*chainhash.Hash {
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
hashes := make([]*chainhash.Hash, len(mp.pool)) hashes := make([]*chainhash.Hash, len(mp.pool))
i := 0 i := 0
@ -965,8 +965,8 @@ func (mp *TxPool) TxHashes() []*chainhash.Hash {
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) TxDescs() []*TxDesc { func (mp *TxPool) TxDescs() []*TxDesc {
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
descs := make([]*TxDesc, len(mp.pool)) descs := make([]*TxDesc, len(mp.pool))
i := 0 i := 0
@ -984,8 +984,8 @@ func (mp *TxPool) TxDescs() []*TxDesc {
// This is part of the mining.TxSource interface implementation and is safe for // This is part of the mining.TxSource interface implementation and is safe for
// concurrent access as required by the interface contract. // concurrent access as required by the interface contract.
func (mp *TxPool) MiningDescs() []*mining.TxDesc { func (mp *TxPool) MiningDescs() []*mining.TxDesc {
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
descs := make([]*mining.TxDesc, len(mp.pool)) descs := make([]*mining.TxDesc, len(mp.pool))
i := 0 i := 0
@ -1002,8 +1002,8 @@ func (mp *TxPool) MiningDescs() []*mining.TxDesc {
// //
// This function is safe for concurrent access. // This function is safe for concurrent access.
func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetRawMempoolVerboseResult { func (mp *TxPool) RawMempoolVerbose() map[string]*btcjson.GetRawMempoolVerboseResult {
mp.RLock() mp.mtx.RLock()
defer mp.RUnlock() defer mp.mtx.RUnlock()
result := make(map[string]*btcjson.GetRawMempoolVerboseResult, result := make(map[string]*btcjson.GetRawMempoolVerboseResult,
len(mp.pool)) len(mp.pool))