}
// 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
}
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)
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
}
}
// 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
}
}
// 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++
}
}
// 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)
}
}
}
// 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
}