OSDN Git Service

Repalce tp with mp (#434)
authorBlockmeta-区块元 <blockmeta@8btc.com>
Sat, 17 Mar 2018 07:23:10 +0000 (15:23 +0800)
committerPaladz <yzhu101@uottawa.ca>
Sat, 17 Mar 2018 07:23:10 +0000 (15:23 +0800)
Standardize the naming rule

protocol/txpool.go

index 7fcb3dd..906d43e 100644 (file)
@@ -59,16 +59,16 @@ func NewTxPool() *TxPool {
 }
 
 // GetNewTxCh return a unconfirmed transaction feed channel
-func (mp *TxPool) GetNewTxCh() chan *types.Tx {
-       return mp.newTxCh
+func (tp *TxPool) GetNewTxCh() chan *types.Tx {
+       return tp.newTxCh
 }
 
 // AddTransaction add a verified transaction to pool
-func (mp *TxPool) AddTransaction(tx *types.Tx, gasOnlyTx bool, height, fee uint64) (*TxDesc, error) {
-       mp.mtx.Lock()
-       defer mp.mtx.Unlock()
+func (tp *TxPool) AddTransaction(tx *types.Tx, gasOnlyTx bool, height, fee uint64) (*TxDesc, error) {
+       tp.mtx.Lock()
+       defer tp.mtx.Unlock()
 
-       if len(mp.pool) >= maxNewTxNum {
+       if len(tp.pool) >= maxNewTxNum {
                return nil, ErrPoolIsFull
        }
 
@@ -81,8 +81,8 @@ func (mp *TxPool) AddTransaction(tx *types.Tx, gasOnlyTx bool, height, fee uint6
                FeePerKB: fee * 1000 / tx.TxHeader.SerializedSize,
        }
 
-       mp.pool[tx.Tx.ID] = txD
-       atomic.StoreInt64(&mp.lastUpdated, time.Now().Unix())
+       tp.pool[tx.Tx.ID] = txD
+       atomic.StoreInt64(&tp.lastUpdated, time.Now().Unix())
 
        for _, id := range tx.TxHeader.ResultIds {
                output, err := tx.Output(*id)
@@ -91,29 +91,29 @@ func (mp *TxPool) AddTransaction(tx *types.Tx, gasOnlyTx bool, height, fee uint6
                        continue
                }
                if !gasOnlyTx || *output.Source.Value.AssetId == *consensus.BTMAssetID {
-                       mp.utxo[*id] = tx.Tx.ID
+                       tp.utxo[*id] = tx.Tx.ID
                }
        }
 
-       mp.newTxCh <- tx
+       tp.newTxCh <- tx
        log.WithField("tx_id", tx.Tx.ID.String()).Info("Add tx to mempool")
        return txD, nil
 }
 
 // AddErrCache add a failed transaction record to lru cache
-func (mp *TxPool) AddErrCache(txHash *bc.Hash, err error) {
-       mp.mtx.Lock()
-       defer mp.mtx.Unlock()
+func (tp *TxPool) AddErrCache(txHash *bc.Hash, err error) {
+       tp.mtx.Lock()
+       defer tp.mtx.Unlock()
 
-       mp.errCache.Add(txHash, err)
+       tp.errCache.Add(txHash, err)
 }
 
 // GetErrCache return the error of the transaction
-func (mp *TxPool) GetErrCache(txHash *bc.Hash) error {
-       mp.mtx.Lock()
-       defer mp.mtx.Unlock()
+func (tp *TxPool) GetErrCache(txHash *bc.Hash) error {
+       tp.mtx.Lock()
+       defer tp.mtx.Unlock()
 
-       v, ok := mp.errCache.Get(txHash)
+       v, ok := tp.errCache.Get(txHash)
        if !ok {
                return nil
        }
@@ -121,30 +121,30 @@ func (mp *TxPool) GetErrCache(txHash *bc.Hash) error {
 }
 
 // RemoveTransaction remove a transaction from the pool
-func (mp *TxPool) RemoveTransaction(txHash *bc.Hash) {
-       mp.mtx.Lock()
-       defer mp.mtx.Unlock()
+func (tp *TxPool) RemoveTransaction(txHash *bc.Hash) {
+       tp.mtx.Lock()
+       defer tp.mtx.Unlock()
 
-       txD, ok := mp.pool[*txHash]
+       txD, ok := tp.pool[*txHash]
        if !ok {
                return
        }
 
        for _, output := range txD.Tx.TxHeader.ResultIds {
-               delete(mp.utxo, *output)
+               delete(tp.utxo, *output)
        }
-       delete(mp.pool, *txHash)
-       atomic.StoreInt64(&mp.lastUpdated, time.Now().Unix())
+       delete(tp.pool, *txHash)
+       atomic.StoreInt64(&tp.lastUpdated, time.Now().Unix())
 
        log.WithField("tx_id", txHash).Info("remove tx from mempool")
 }
 
 // GetTransaction return the TxDesc by hash
-func (mp *TxPool) GetTransaction(txHash *bc.Hash) (*TxDesc, error) {
-       mp.mtx.RLock()
-       defer mp.mtx.RUnlock()
+func (tp *TxPool) GetTransaction(txHash *bc.Hash) (*TxDesc, error) {
+       tp.mtx.RLock()
+       defer tp.mtx.RUnlock()
 
-       if txD, ok := mp.pool[*txHash]; ok {
+       if txD, ok := tp.pool[*txHash]; ok {
                return txD, nil
        }
 
@@ -152,13 +152,13 @@ func (mp *TxPool) GetTransaction(txHash *bc.Hash) (*TxDesc, error) {
 }
 
 // GetTransactions return all the transactions in the pool
-func (mp *TxPool) GetTransactions() []*TxDesc {
-       mp.mtx.RLock()
-       defer mp.mtx.RUnlock()
+func (tp *TxPool) GetTransactions() []*TxDesc {
+       tp.mtx.RLock()
+       defer tp.mtx.RUnlock()
 
-       txDs := make([]*TxDesc, len(mp.pool))
+       txDs := make([]*TxDesc, len(tp.pool))
        i := 0
-       for _, desc := range mp.pool {
+       for _, desc := range tp.pool {
                txDs[i] = desc
                i++
        }
@@ -166,13 +166,13 @@ func (mp *TxPool) GetTransactions() []*TxDesc {
 }
 
 // GetTransactionUTXO return unconfirmed utxo
-func (mp *TxPool) GetTransactionUTXO(tx *bc.Tx) *state.UtxoViewpoint {
-       mp.mtx.RLock()
-       defer mp.mtx.RUnlock()
+func (tp *TxPool) GetTransactionUTXO(tx *bc.Tx) *state.UtxoViewpoint {
+       tp.mtx.RLock()
+       defer tp.mtx.RUnlock()
 
        view := state.NewUtxoViewpoint()
        for _, prevout := range tx.SpentOutputIDs {
-               if _, ok := mp.utxo[prevout]; ok {
+               if _, ok := tp.utxo[prevout]; ok {
                        view.Entries[prevout] = storage.NewUtxoEntry(false, 0, false)
                }
        }
@@ -180,35 +180,35 @@ func (mp *TxPool) GetTransactionUTXO(tx *bc.Tx) *state.UtxoViewpoint {
 }
 
 // IsTransactionInPool check wheather a transaction in pool or not
-func (mp *TxPool) IsTransactionInPool(txHash *bc.Hash) bool {
-       mp.mtx.RLock()
-       defer mp.mtx.RUnlock()
+func (tp *TxPool) IsTransactionInPool(txHash *bc.Hash) bool {
+       tp.mtx.RLock()
+       defer tp.mtx.RUnlock()
 
-       if _, ok := mp.pool[*txHash]; ok {
+       if _, ok := tp.pool[*txHash]; ok {
                return true
        }
        return false
 }
 
 // IsTransactionInErrCache check wheather a transaction in errCache or not
-func (mp *TxPool) IsTransactionInErrCache(txHash *bc.Hash) bool {
-       mp.mtx.RLock()
-       defer mp.mtx.RUnlock()
+func (tp *TxPool) IsTransactionInErrCache(txHash *bc.Hash) bool {
+       tp.mtx.RLock()
+       defer tp.mtx.RUnlock()
 
-       _, ok := mp.errCache.Get(txHash)
+       _, ok := tp.errCache.Get(txHash)
        return ok
 }
 
 // HaveTransaction IsTransactionInErrCache check is  transaction in errCache or pool
-func (mp *TxPool) HaveTransaction(txHash *bc.Hash) bool {
-       return mp.IsTransactionInPool(txHash) || mp.IsTransactionInErrCache(txHash)
+func (tp *TxPool) HaveTransaction(txHash *bc.Hash) bool {
+       return tp.IsTransactionInPool(txHash) || tp.IsTransactionInErrCache(txHash)
 }
 
 // Count return number of transcation in pool
-func (mp *TxPool) Count() int {
-       mp.mtx.RLock()
-       defer mp.mtx.RUnlock()
+func (tp *TxPool) Count() int {
+       tp.mtx.RLock()
+       defer tp.mtx.RUnlock()
 
-       count := len(mp.pool)
+       count := len(tp.pool)
        return count
 }