tendermint/mempool/mempool.go

418 lines
11 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"
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"
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
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
2014-09-10 02:43:16 -07:00
}
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,
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
}
2016-10-17 16:54:51 -07:00
func (mem *Mempool) initWAL() {
2017-04-28 20:59:02 -07:00
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 {
log.Error("Error ensuring Mempool wal dir", "error", err)
2017-04-28 20:59:02 -07:00
cmn.PanicSanity(err)
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 {
2016-11-05 09:15:34 -07:00
log.Error("Error opening Mempool wal file", "error", err)
2017-04-28 20:59:02 -07:00
cmn.PanicSanity(err)
2016-10-17 16:54:51 -07:00
}
mem.wal = af
}
}
2016-07-05 11:41:50 -07:00
// consensus must be able to hold lock to safely update
func (mem *Mempool) Lock() {
mem.proxyMtx.Lock()
}
func (mem *Mempool) Unlock() {
mem.proxyMtx.Unlock()
}
2016-07-05 11:41:50 -07:00
// Number of transactions in the mempool clist
2016-03-07 15:38:05 -08:00
func (mem *Mempool) Size() int {
return mem.txs.Len()
}
2016-07-05 11:41:50 -07:00
// Remove all transactions from mempool and cache
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()
}
}
2015-12-01 20:12:01 -08:00
// Return the first element of mem.txs for peer goroutines to call .NextWait() on.
// Blocks until txs has elements.
func (mem *Mempool) TxsFrontWait() *clist.CElement {
return mem.txs.FrontWait()
2015-03-21 13:31:17 -07:00
}
2015-12-01 20:12:01 -08:00
// Try a new transaction in the mempool.
// Potentially blocking if we're blocking 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)
} else {
// ignore bad transaction
2016-06-23 17:53:11 -07:00
log.Info("Bad Transaction", "res", r)
// 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)
2016-04-12 07:38:54 -07:00
log.Info("Done rechecking txs")
2016-02-14 17:00:33 -08:00
}
default:
// ignore other messages
}
}
// Get the valid transactions remaining
2016-09-16 08:08:31 -07:00
// If maxTxs is -1, there is no cap on 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
}
// Tell mempool that these txs were committed.
// Mempool will discard these txs.
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
// 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) {
2016-04-12 07:38:54 -07:00
log.Info("Recheck txs", "numtxs", len(goodTxs))
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
//--------------------------------------------------------------------------------
// A transaction that successfully ran
type mempoolTx struct {
counter int64 // a simple incrementing counter
height int64 // height that this tx had been validated in
tx types.Tx //
}
func (memTx *mempoolTx) Height() int {
return int(atomic.LoadInt64(&memTx.height))
}
//--------------------------------------------------------------------------------
type txCache struct {
mtx sync.Mutex
size int
map_ map[string]struct{}
list *list.List // to remove oldest tx when cache gets too big
}
func newTxCache(cacheSize int) *txCache {
return &txCache{
size: cacheSize,
map_: make(map[string]struct{}, cacheSize),
list: list.New(),
}
}
func (cache *txCache) Reset() {
cache.mtx.Lock()
cache.map_ = make(map[string]struct{}, cacheSize)
cache.list.Init()
cache.mtx.Unlock()
}
func (cache *txCache) Exists(tx types.Tx) bool {
cache.mtx.Lock()
_, exists := cache.map_[string(tx)]
cache.mtx.Unlock()
return exists
}
// Returns false if tx is in 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
// but deleting a non-existant element is fine
delete(cache.map_, string(poppedTx))
cache.list.Remove(popped)
}
cache.map_[string(tx)] = struct{}{}
cache.list.PushBack(tx)
return true
}
func (cache *txCache) Remove(tx types.Tx) {
cache.mtx.Lock()
delete(cache.map_, string(tx))
cache.mtx.Unlock()
}