tendermint/mempool/mempool.go

478 lines
13 KiB
Go
Raw Normal View History

package mempool
import (
2016-02-14 17:00:33 -08:00
"bytes"
"container/list"
"sync"
2015-12-01 20:12:01 -08:00
"sync/atomic"
2016-02-14 17:00:33 -08:00
"time"
2017-05-02 00:53:32 -07:00
"github.com/pkg/errors"
abci "github.com/tendermint/abci/types"
2017-04-08 19:04:06 -07:00
auto "github.com/tendermint/tmlibs/autofile"
"github.com/tendermint/tmlibs/clist"
2017-04-28 20:59:02 -07:00
cmn "github.com/tendermint/tmlibs/common"
2017-05-12 14:07:53 -07:00
"github.com/tendermint/tmlibs/log"
2017-04-28 20:59:02 -07:00
cfg "github.com/tendermint/tendermint/config"
2017-04-28 20:59:02 -07:00
"github.com/tendermint/tendermint/proxy"
"github.com/tendermint/tendermint/types"
)
2014-09-10 02:43:16 -07:00
2015-12-01 20:12:01 -08:00
/*
The mempool pushes new txs onto the proxyAppConn.
2015-12-01 20:12:01 -08:00
It gets a stream of (req, res) tuples from the proxy.
The memool stores good txs in a concurrent linked-list.
Multiple concurrent go-routines can traverse this linked-list
safely by calling .NextWait() on each element.
So we have several go-routines:
1. Consensus calling Update() and Reap() synchronously
2. Many mempool reactor's peer routines calling CheckTx()
2015-12-01 20:12:01 -08:00
3. Many mempool reactor's peer routines traversing the txs linked list
4. Another goroutine calling GarbageCollectTxs() periodically
To manage these goroutines, there are three methods of locking.
1. Mutations to the linked-list is protected by an internal mtx (CList is goroutine-safe)
2. Mutations to the linked-list elements are atomic
3. CheckTx() calls can be paused upon Update() and Reap(), protected by .proxyMtx
2015-12-01 20:12:01 -08:00
Garbage collection of old elements from mempool.txs is handlde via
the DetachPrev() call, which makes old elements not reachable by
peer broadcastTxRoutine() automatically garbage collected.
2017-01-12 12:53:32 -08:00
TODO: Better handle abci client errors. (make it automatically handle connection errors)
2016-02-14 17:00:33 -08:00
2015-12-01 20:12:01 -08:00
*/
const cacheSize = 100000
2017-07-13 10:07:04 -07:00
// Mempool is an ordered in-memory pool for transactions before they are proposed in a consensus round.
// Transaction validity is checked using the CheckTx abci message before the transaction is added to the pool.
// The Mempool uses a concurrent list structure for storing transactions that can be efficiently accessed by multiple concurrent readers.
2014-09-10 02:43:16 -07:00
type Mempool struct {
config *cfg.MempoolConfig
2016-05-08 15:00:58 -07:00
2016-02-14 17:00:33 -08:00
proxyMtx sync.Mutex
2016-08-17 19:28:08 -07:00
proxyAppConn proxy.AppConnMempool
2016-02-14 17:00:33 -08:00
txs *clist.CList // concurrent linked-list of good txs
counter int64 // simple incrementing counter
height int // the last block Update()'d to
rechecking int32 // for re-checking filtered txs on Update()
recheckCursor *clist.CElement // next expected response
recheckEnd *clist.CElement // re-checking stops here
// Keep a cache of already-seen txs.
// This reduces the pressure on the proxyApp.
cache *txCache
2016-10-17 16:54:51 -07:00
// A log of mempool txs
wal *auto.AutoFile
2017-05-02 00:53:32 -07:00
2017-07-11 22:02:16 -07:00
// fires once for each height, when the mempool is not empty
txsAvailable chan struct{}
notifiedTxsAvailable bool
2017-05-02 00:53:32 -07:00
logger log.Logger
2014-09-10 02:43:16 -07:00
}
2017-07-13 10:07:04 -07:00
// NewMempool returns a new Mempool with the given configuration and connection to an application.
func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool) *Mempool {
2015-12-01 20:12:01 -08:00
mempool := &Mempool{
2016-05-08 15:00:58 -07:00
config: config,
2016-02-14 17:00:33 -08:00
proxyAppConn: proxyAppConn,
txs: clist.New(),
counter: 0,
height: 0,
rechecking: 0,
recheckCursor: nil,
recheckEnd: nil,
2017-05-02 00:53:32 -07:00
logger: log.NewNopLogger(),
cache: newTxCache(cacheSize),
2014-09-10 02:43:16 -07:00
}
2016-10-17 16:54:51 -07:00
mempool.initWAL()
proxyAppConn.SetResponseCallback(mempool.resCb)
2015-12-01 20:12:01 -08:00
return mempool
2014-09-10 02:43:16 -07:00
}
2017-07-13 10:19:44 -07:00
// FireOnTxsAvailable initializes the TxsAvailable channel,
// ensuring it will trigger once every height when transactions are available.
func (mem *Mempool) FireOnTxsAvailable() {
mem.txsAvailable = make(chan struct{}, 1)
}
2017-07-13 10:07:04 -07:00
// SetLogger sets the Logger.
2017-05-02 00:53:32 -07:00
func (mem *Mempool) SetLogger(l log.Logger) {
mem.logger = l
}
2016-10-17 16:54:51 -07:00
func (mem *Mempool) initWAL() {
walDir := mem.config.WalDir()
2016-10-30 03:55:27 -07:00
if walDir != "" {
2017-04-28 20:59:02 -07:00
err := cmn.EnsureDir(walDir, 0700)
2016-11-05 09:15:34 -07:00
if err != nil {
2017-05-02 00:53:32 -07:00
cmn.PanicSanity(errors.Wrap(err, "Error ensuring Mempool wal dir"))
2016-11-05 09:15:34 -07:00
}
2016-10-30 03:55:27 -07:00
af, err := auto.OpenAutoFile(walDir + "/wal")
2016-10-17 16:54:51 -07:00
if err != nil {
2017-05-02 00:53:32 -07:00
cmn.PanicSanity(errors.Wrap(err, "Error opening Mempool wal file"))
2016-10-17 16:54:51 -07:00
}
mem.wal = af
}
}
2017-07-13 10:07:04 -07:00
// Lock locks the mempool. The consensus must be able to hold lock to safely update.
func (mem *Mempool) Lock() {
mem.proxyMtx.Lock()
}
2017-07-13 10:07:04 -07:00
// Unlock unlocks the mempool.
func (mem *Mempool) Unlock() {
mem.proxyMtx.Unlock()
}
2017-07-13 10:07:04 -07:00
// Size returns the number of transactions in the mempool.
2016-03-07 15:38:05 -08:00
func (mem *Mempool) Size() int {
return mem.txs.Len()
}
2017-07-13 10:07:04 -07:00
// Flush removes all transactions from the mempool and cache
2016-07-05 11:41:50 -07:00
func (mem *Mempool) Flush() {
mem.proxyMtx.Lock()
defer mem.proxyMtx.Unlock()
mem.cache.Reset()
2016-07-05 11:41:50 -07:00
for e := mem.txs.Front(); e != nil; e = e.Next() {
mem.txs.Remove(e)
e.DetachPrev()
}
}
2017-07-13 10:07:04 -07:00
// TxsFrontWait returns the first transaction in the ordered list for peer goroutines to call .NextWait() on.
// It blocks until the mempool is not empty (ie. until the internal `mem.txs` has at least one element)
2015-12-01 20:12:01 -08:00
func (mem *Mempool) TxsFrontWait() *clist.CElement {
return mem.txs.FrontWait()
2015-03-21 13:31:17 -07:00
}
2017-07-13 10:07:04 -07:00
// CheckTx executes a new transaction against the application to determine its validity
// and whether it should be added to the mempool.
// It blocks if we're waiting on Update() or Reap().
2016-02-08 00:48:58 -08:00
// cb: A callback from the CheckTx command.
// It gets called from another goroutine.
// CONTRACT: Either cb will get called, or err returned.
2017-01-12 12:53:32 -08:00
func (mem *Mempool) CheckTx(tx types.Tx, cb func(*abci.Response)) (err error) {
2015-12-01 20:12:01 -08:00
mem.proxyMtx.Lock()
defer mem.proxyMtx.Unlock()
2015-09-29 08:36:52 -07:00
// CACHE
if mem.cache.Exists(tx) {
if cb != nil {
2017-01-12 12:53:32 -08:00
cb(&abci.Response{
Value: &abci.Response_CheckTx{
&abci.ResponseCheckTx{
Code: abci.CodeType_BadNonce, // TODO or duplicate tx
2016-05-14 09:33:27 -07:00
Log: "Duplicate transaction (ignored)",
},
},
})
}
return nil
}
mem.cache.Push(tx)
// END CACHE
2016-10-17 16:54:51 -07:00
// WAL
if mem.wal != nil {
// TODO: Notify administrators when WAL fails
mem.wal.Write([]byte(tx))
mem.wal.Write([]byte("\n"))
}
// END WAL
2016-02-14 17:00:33 -08:00
// NOTE: proxyAppConn may error if tx buffer is full
if err = mem.proxyAppConn.Error(); err != nil {
return err
2014-09-10 02:43:16 -07:00
}
2016-02-08 00:48:58 -08:00
reqRes := mem.proxyAppConn.CheckTxAsync(tx)
if cb != nil {
reqRes.SetCallback(cb)
}
2015-12-01 20:12:01 -08:00
return nil
2014-09-10 02:43:16 -07:00
}
2017-01-12 12:53:32 -08:00
// ABCI callback function
func (mem *Mempool) resCb(req *abci.Request, res *abci.Response) {
2016-02-14 17:00:33 -08:00
if mem.recheckCursor == nil {
mem.resCbNormal(req, res)
} else {
mem.resCbRecheck(req, res)
}
}
2017-01-12 12:53:32 -08:00
func (mem *Mempool) resCbNormal(req *abci.Request, res *abci.Response) {
2016-05-14 09:33:27 -07:00
switch r := res.Value.(type) {
2017-01-12 12:53:32 -08:00
case *abci.Response_CheckTx:
if r.CheckTx.Code == abci.CodeType_OK {
mem.counter++
memTx := &mempoolTx{
counter: mem.counter,
height: int64(mem.height),
2016-05-14 09:33:27 -07:00
tx: req.GetCheckTx().Tx,
2015-12-01 20:12:01 -08:00
}
mem.txs.PushBack(memTx)
2017-07-11 22:02:16 -07:00
mem.alertIfTxsAvailable()
} else {
// ignore bad transaction
2017-05-02 00:53:32 -07:00
mem.logger.Info("Bad Transaction", "res", r)
2016-06-23 17:53:11 -07:00
// remove from cache (it might be good later)
mem.cache.Remove(req.GetCheckTx().Tx)
2016-06-23 17:53:11 -07:00
// TODO: handle other retcodes
2015-12-01 20:12:01 -08:00
}
default:
// ignore other messages
}
}
2017-01-12 12:53:32 -08:00
func (mem *Mempool) resCbRecheck(req *abci.Request, res *abci.Response) {
2016-05-14 09:33:27 -07:00
switch r := res.Value.(type) {
2017-01-12 12:53:32 -08:00
case *abci.Response_CheckTx:
2016-02-14 17:00:33 -08:00
memTx := mem.recheckCursor.Value.(*mempoolTx)
2016-05-14 09:33:27 -07:00
if !bytes.Equal(req.GetCheckTx().Tx, memTx.tx) {
2017-04-28 20:59:02 -07:00
cmn.PanicSanity(cmn.Fmt("Unexpected tx response from proxy during recheck\n"+
2016-05-14 09:33:27 -07:00
"Expected %X, got %X", r.CheckTx.Data, memTx.tx))
2016-02-14 17:00:33 -08:00
}
2017-01-12 12:53:32 -08:00
if r.CheckTx.Code == abci.CodeType_OK {
2016-02-14 17:00:33 -08:00
// Good, nothing to do.
} else {
// Tx became invalidated due to newly committed block.
mem.txs.Remove(mem.recheckCursor)
mem.recheckCursor.DetachPrev()
2016-06-23 17:53:11 -07:00
// remove from cache (it might be good later)
mem.cache.Remove(req.GetCheckTx().Tx)
2016-02-14 17:00:33 -08:00
}
if mem.recheckCursor == mem.recheckEnd {
mem.recheckCursor = nil
} else {
mem.recheckCursor = mem.recheckCursor.Next()
}
if mem.recheckCursor == nil {
// Done!
atomic.StoreInt32(&mem.rechecking, 0)
2017-05-02 00:53:32 -07:00
mem.logger.Info("Done rechecking txs")
2017-07-11 22:02:16 -07:00
mem.alertIfTxsAvailable()
2016-02-14 17:00:33 -08:00
}
default:
// ignore other messages
}
}
2017-07-11 22:02:16 -07:00
func (mem *Mempool) alertIfTxsAvailable() {
if !mem.notifiedTxsAvailable && mem.Size() > 0 {
mem.notifiedTxsAvailable = true
mem.txsAvailable <- struct{}{}
}
}
2017-07-13 10:19:44 -07:00
// TxsAvailable returns a channel which fires once for every height,
// and only when transactions are available in the mempool.
// XXX: Will panic if mem.FireOnTxsAvailable() has not been called.
2017-07-11 22:02:16 -07:00
func (mem *Mempool) TxsAvailable() chan struct{} {
2017-07-13 10:19:44 -07:00
if mem.txsAvailable == nil {
panic("mem.txsAvailable is nil")
}
2017-07-11 22:02:16 -07:00
return mem.txsAvailable
}
2017-07-13 10:19:44 -07:00
func (mem *Mempool) alertIfTxsAvailable() {
if mem.txsAvailable != nil &&
!mem.notifiedTxsAvailable && mem.Size() > 0 {
mem.notifiedTxsAvailable = true
mem.txsAvailable <- struct{}{}
}
}
2017-07-13 10:07:04 -07:00
// Reap returns a list of transactions currently in the mempool.
// If maxTxs is -1, there is no cap on the number of returned transactions.
func (mem *Mempool) Reap(maxTxs int) types.Txs {
2015-12-01 20:12:01 -08:00
mem.proxyMtx.Lock()
defer mem.proxyMtx.Unlock()
2016-02-14 17:00:33 -08:00
for atomic.LoadInt32(&mem.rechecking) > 0 {
// TODO: Something better?
time.Sleep(time.Millisecond * 10)
}
2015-12-01 20:12:01 -08:00
2016-03-06 15:08:32 -08:00
txs := mem.collectTxs(maxTxs)
2016-02-14 17:00:33 -08:00
return txs
2015-09-25 09:55:59 -07:00
}
2016-04-26 19:17:13 -07:00
// maxTxs: -1 means uncapped, 0 means none
func (mem *Mempool) collectTxs(maxTxs int) types.Txs {
2016-03-06 15:08:32 -08:00
if maxTxs == 0 {
2016-03-07 21:43:39 -08:00
return []types.Tx{}
2016-04-26 19:17:13 -07:00
} else if maxTxs < 0 {
maxTxs = mem.txs.Len()
2016-03-06 15:08:32 -08:00
}
2017-04-28 20:59:02 -07:00
txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), maxTxs))
2016-03-06 15:08:32 -08:00
for e := mem.txs.Front(); e != nil && len(txs) < maxTxs; e = e.Next() {
2015-12-01 20:12:01 -08:00
memTx := e.Value.(*mempoolTx)
txs = append(txs, memTx.tx)
}
return txs
2015-09-25 09:55:59 -07:00
}
2017-07-13 10:07:04 -07:00
// Update informs the mempool that the given txs were committed and can be discarded.
2015-12-01 20:12:01 -08:00
// NOTE: this should be called *after* block is committed by consensus.
// NOTE: unsafe; Lock/Unlock must be managed by caller
func (mem *Mempool) Update(height int, txs types.Txs) {
// TODO: check err ?
2016-03-25 08:29:03 -07:00
mem.proxyAppConn.FlushSync() // To flush async resCb calls e.g. from CheckTx
2015-12-01 20:12:01 -08:00
// First, create a lookup map of txns in new txs.
txsMap := make(map[string]struct{})
for _, tx := range txs {
txsMap[string(tx)] = struct{}{}
2015-12-01 20:12:01 -08:00
}
// Set height
mem.height = height
2017-07-11 22:02:16 -07:00
mem.notifiedTxsAvailable = false
// Remove transactions that are already in txs.
2016-02-14 17:00:33 -08:00
goodTxs := mem.filterTxs(txsMap)
2016-04-19 18:18:40 -07:00
// Recheck mempool txs if any txs were committed in the block
// NOTE/XXX: in some apps a tx could be invalidated due to EndBlock,
// so we really still do need to recheck, but this is for debugging
2017-04-28 20:59:02 -07:00
if mem.config.Recheck && (mem.config.RecheckEmpty || len(txs) > 0) {
2017-07-11 22:02:16 -07:00
mem.logger.Info("Recheck txs", "numtxs", len(goodTxs), "height", height)
mem.recheckTxs(goodTxs)
// At this point, mem.txs are being rechecked.
// mem.recheckCursor re-scans mem.txs and possibly removes some txs.
// Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
}
2015-09-25 09:55:59 -07:00
}
func (mem *Mempool) filterTxs(blockTxsMap map[string]struct{}) []types.Tx {
2015-12-01 20:12:01 -08:00
goodTxs := make([]types.Tx, 0, mem.txs.Len())
for e := mem.txs.Front(); e != nil; e = e.Next() {
memTx := e.Value.(*mempoolTx)
// Remove the tx if it's alredy in a block.
2015-12-01 20:12:01 -08:00
if _, ok := blockTxsMap[string(memTx.tx)]; ok {
2016-06-23 17:53:11 -07:00
// remove from clist
2015-12-01 20:12:01 -08:00
mem.txs.Remove(e)
e.DetachPrev()
2016-06-23 17:53:11 -07:00
// NOTE: we don't remove committed txs from the cache.
2015-12-01 20:12:01 -08:00
continue
}
// Good tx!
goodTxs = append(goodTxs, memTx.tx)
2015-09-25 09:55:59 -07:00
}
2015-12-01 20:12:01 -08:00
return goodTxs
2015-09-25 09:55:59 -07:00
}
2016-02-14 17:00:33 -08:00
// NOTE: pass in goodTxs because mem.txs can mutate concurrently.
func (mem *Mempool) recheckTxs(goodTxs []types.Tx) {
if len(goodTxs) == 0 {
return
}
atomic.StoreInt32(&mem.rechecking, 1)
mem.recheckCursor = mem.txs.Front()
mem.recheckEnd = mem.txs.Back()
// Push txs to proxyAppConn
// NOTE: resCb() may be called concurrently.
for _, tx := range goodTxs {
mem.proxyAppConn.CheckTxAsync(tx)
}
mem.proxyAppConn.FlushAsync()
}
2015-12-01 20:12:01 -08:00
//--------------------------------------------------------------------------------
2017-07-13 10:07:04 -07:00
// mempoolTx is a transaction that successfully ran
2015-12-01 20:12:01 -08:00
type mempoolTx struct {
counter int64 // a simple incrementing counter
height int64 // height that this tx had been validated in
tx types.Tx //
}
2017-07-13 10:07:04 -07:00
// Height returns the height for this transaction
2015-12-01 20:12:01 -08:00
func (memTx *mempoolTx) Height() int {
return int(atomic.LoadInt64(&memTx.height))
}
//--------------------------------------------------------------------------------
2017-07-13 10:07:04 -07:00
// txCache maintains a cache of transactions.
type txCache struct {
mtx sync.Mutex
size int
map_ map[string]struct{}
list *list.List // to remove oldest tx when cache gets too big
}
2017-07-13 10:07:04 -07:00
// newTxCache returns a new txCache.
func newTxCache(cacheSize int) *txCache {
return &txCache{
size: cacheSize,
map_: make(map[string]struct{}, cacheSize),
list: list.New(),
}
}
2017-07-13 10:07:04 -07:00
// Reset resets the txCache to empty.
func (cache *txCache) Reset() {
cache.mtx.Lock()
cache.map_ = make(map[string]struct{}, cacheSize)
cache.list.Init()
cache.mtx.Unlock()
}
2017-07-13 10:07:04 -07:00
// Exists returns true if the given tx is cached.
func (cache *txCache) Exists(tx types.Tx) bool {
cache.mtx.Lock()
_, exists := cache.map_[string(tx)]
cache.mtx.Unlock()
return exists
}
2017-07-13 10:07:04 -07:00
// Push adds the given tx to the txCache. It returns false if tx is already in the cache.
func (cache *txCache) Push(tx types.Tx) bool {
cache.mtx.Lock()
defer cache.mtx.Unlock()
if _, exists := cache.map_[string(tx)]; exists {
return false
}
if cache.list.Len() >= cache.size {
popped := cache.list.Front()
poppedTx := popped.Value.(types.Tx)
// NOTE: the tx may have already been removed from the map
2017-05-20 21:43:00 -07:00
// but deleting a non-existent element is fine
delete(cache.map_, string(poppedTx))
cache.list.Remove(popped)
}
cache.map_[string(tx)] = struct{}{}
cache.list.PushBack(tx)
return true
}
2017-07-13 10:07:04 -07:00
// Remove removes the given tx from the cache.
func (cache *txCache) Remove(tx types.Tx) {
cache.mtx.Lock()
delete(cache.map_, string(tx))
cache.mtx.Unlock()
}