You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

503 lines
14 KiB

9 years ago
9 years ago
8 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
8 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
  1. package mempool
  2. import (
  3. "bytes"
  4. "container/list"
  5. "fmt"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "github.com/pkg/errors"
  10. abci "github.com/tendermint/abci/types"
  11. auto "github.com/tendermint/tmlibs/autofile"
  12. "github.com/tendermint/tmlibs/clist"
  13. cmn "github.com/tendermint/tmlibs/common"
  14. "github.com/tendermint/tmlibs/log"
  15. cfg "github.com/tendermint/tendermint/config"
  16. "github.com/tendermint/tendermint/proxy"
  17. "github.com/tendermint/tendermint/types"
  18. )
  19. /*
  20. The mempool pushes new txs onto the proxyAppConn.
  21. It gets a stream of (req, res) tuples from the proxy.
  22. The mempool stores good txs in a concurrent linked-list.
  23. Multiple concurrent go-routines can traverse this linked-list
  24. safely by calling .NextWait() on each element.
  25. So we have several go-routines:
  26. 1. Consensus calling Update() and Reap() synchronously
  27. 2. Many mempool reactor's peer routines calling CheckTx()
  28. 3. Many mempool reactor's peer routines traversing the txs linked list
  29. 4. Another goroutine calling GarbageCollectTxs() periodically
  30. To manage these goroutines, there are three methods of locking.
  31. 1. Mutations to the linked-list is protected by an internal mtx (CList is goroutine-safe)
  32. 2. Mutations to the linked-list elements are atomic
  33. 3. CheckTx() calls can be paused upon Update() and Reap(), protected by .proxyMtx
  34. Garbage collection of old elements from mempool.txs is handlde via
  35. the DetachPrev() call, which makes old elements not reachable by
  36. peer broadcastTxRoutine() automatically garbage collected.
  37. TODO: Better handle abci client errors. (make it automatically handle connection errors)
  38. */
  39. var (
  40. // ErrTxInCache is returned to the client if we saw tx earlier
  41. ErrTxInCache = errors.New("Tx already exists in cache")
  42. // ErrMempoolIsFull means Tendermint & an application can't handle that much load
  43. ErrMempoolIsFull = errors.New("Mempool is full")
  44. )
  45. // Mempool is an ordered in-memory pool for transactions before they are proposed in a consensus
  46. // round. Transaction validity is checked using the CheckTx abci message before the transaction is
  47. // added to the pool. The Mempool uses a concurrent list structure for storing transactions that
  48. // can be efficiently accessed by multiple concurrent readers.
  49. type Mempool struct {
  50. config *cfg.MempoolConfig
  51. proxyMtx sync.Mutex
  52. proxyAppConn proxy.AppConnMempool
  53. txs *clist.CList // concurrent linked-list of good txs
  54. counter int64 // simple incrementing counter
  55. height int64 // the last block Update()'d to
  56. rechecking int32 // for re-checking filtered txs on Update()
  57. recheckCursor *clist.CElement // next expected response
  58. recheckEnd *clist.CElement // re-checking stops here
  59. notifiedTxsAvailable bool
  60. txsAvailable chan int64 // fires the next height once for each height, when the mempool is not empty
  61. // Keep a cache of already-seen txs.
  62. // This reduces the pressure on the proxyApp.
  63. cache *txCache
  64. // A log of mempool txs
  65. wal *auto.AutoFile
  66. logger log.Logger
  67. }
  68. // NewMempool returns a new Mempool with the given configuration and connection to an application.
  69. func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool, height int64) *Mempool {
  70. mempool := &Mempool{
  71. config: config,
  72. proxyAppConn: proxyAppConn,
  73. txs: clist.New(),
  74. counter: 0,
  75. height: height,
  76. rechecking: 0,
  77. recheckCursor: nil,
  78. recheckEnd: nil,
  79. logger: log.NewNopLogger(),
  80. cache: newTxCache(config.CacheSize),
  81. }
  82. proxyAppConn.SetResponseCallback(mempool.resCb)
  83. return mempool
  84. }
  85. // EnableTxsAvailable initializes the TxsAvailable channel,
  86. // ensuring it will trigger once every height when transactions are available.
  87. // NOTE: not thread safe - should only be called once, on startup
  88. func (mem *Mempool) EnableTxsAvailable() {
  89. mem.txsAvailable = make(chan int64, 1)
  90. }
  91. // SetLogger sets the Logger.
  92. func (mem *Mempool) SetLogger(l log.Logger) {
  93. mem.logger = l
  94. }
  95. // CloseWAL closes and discards the underlying WAL file.
  96. // Any further writes will not be relayed to disk.
  97. func (mem *Mempool) CloseWAL() bool {
  98. if mem == nil {
  99. return false
  100. }
  101. mem.proxyMtx.Lock()
  102. defer mem.proxyMtx.Unlock()
  103. if mem.wal == nil {
  104. return false
  105. }
  106. if err := mem.wal.Close(); err != nil && mem.logger != nil {
  107. mem.logger.Error("Mempool.CloseWAL", "err", err)
  108. }
  109. mem.wal = nil
  110. return true
  111. }
  112. func (mem *Mempool) InitWAL() {
  113. walDir := mem.config.WalDir()
  114. if walDir != "" {
  115. err := cmn.EnsureDir(walDir, 0700)
  116. if err != nil {
  117. cmn.PanicSanity(errors.Wrap(err, "Error ensuring Mempool wal dir"))
  118. }
  119. af, err := auto.OpenAutoFile(walDir + "/wal")
  120. if err != nil {
  121. cmn.PanicSanity(errors.Wrap(err, "Error opening Mempool wal file"))
  122. }
  123. mem.wal = af
  124. }
  125. }
  126. // Lock locks the mempool. The consensus must be able to hold lock to safely update.
  127. func (mem *Mempool) Lock() {
  128. mem.proxyMtx.Lock()
  129. }
  130. // Unlock unlocks the mempool.
  131. func (mem *Mempool) Unlock() {
  132. mem.proxyMtx.Unlock()
  133. }
  134. // Size returns the number of transactions in the mempool.
  135. func (mem *Mempool) Size() int {
  136. return mem.txs.Len()
  137. }
  138. // Flushes the mempool connection to ensure async resCb calls are done e.g.
  139. // from CheckTx.
  140. func (mem *Mempool) FlushAppConn() error {
  141. return mem.proxyAppConn.FlushSync()
  142. }
  143. // Flush removes all transactions from the mempool and cache
  144. func (mem *Mempool) Flush() {
  145. mem.proxyMtx.Lock()
  146. defer mem.proxyMtx.Unlock()
  147. mem.cache.Reset()
  148. for e := mem.txs.Front(); e != nil; e = e.Next() {
  149. mem.txs.Remove(e)
  150. e.DetachPrev()
  151. }
  152. }
  153. // TxsFront returns the first transaction in the ordered list for peer
  154. // goroutines to call .NextWait() on.
  155. func (mem *Mempool) TxsFront() *clist.CElement {
  156. return mem.txs.Front()
  157. }
  158. // TxsWaitChan returns a channel to wait on transactions. It will be closed
  159. // once the mempool is not empty (ie. the internal `mem.txs` has at least one
  160. // element)
  161. func (mem *Mempool) TxsWaitChan() <-chan struct{} {
  162. return mem.txs.WaitChan()
  163. }
  164. // CheckTx executes a new transaction against the application to determine its validity
  165. // and whether it should be added to the mempool.
  166. // It blocks if we're waiting on Update() or Reap().
  167. // cb: A callback from the CheckTx command.
  168. // It gets called from another goroutine.
  169. // CONTRACT: Either cb will get called, or err returned.
  170. func (mem *Mempool) CheckTx(tx types.Tx, cb func(*abci.Response)) (err error) {
  171. mem.proxyMtx.Lock()
  172. defer mem.proxyMtx.Unlock()
  173. if mem.Size() >= mem.config.Size {
  174. return ErrMempoolIsFull
  175. }
  176. // CACHE
  177. if !mem.cache.Push(tx) {
  178. return ErrTxInCache
  179. }
  180. // END CACHE
  181. // WAL
  182. if mem.wal != nil {
  183. // TODO: Notify administrators when WAL fails
  184. _, err := mem.wal.Write([]byte(tx))
  185. if err != nil {
  186. mem.logger.Error("Error writing to WAL", "err", err)
  187. }
  188. _, err = mem.wal.Write([]byte("\n"))
  189. if err != nil {
  190. mem.logger.Error("Error writing to WAL", "err", err)
  191. }
  192. }
  193. // END WAL
  194. // NOTE: proxyAppConn may error if tx buffer is full
  195. if err = mem.proxyAppConn.Error(); err != nil {
  196. return err
  197. }
  198. reqRes := mem.proxyAppConn.CheckTxAsync(tx)
  199. if cb != nil {
  200. reqRes.SetCallback(cb)
  201. }
  202. return nil
  203. }
  204. // ABCI callback function
  205. func (mem *Mempool) resCb(req *abci.Request, res *abci.Response) {
  206. if mem.recheckCursor == nil {
  207. mem.resCbNormal(req, res)
  208. } else {
  209. mem.resCbRecheck(req, res)
  210. }
  211. }
  212. func (mem *Mempool) resCbNormal(req *abci.Request, res *abci.Response) {
  213. switch r := res.Value.(type) {
  214. case *abci.Response_CheckTx:
  215. tx := req.GetCheckTx().Tx
  216. if r.CheckTx.Code == abci.CodeTypeOK {
  217. mem.counter++
  218. memTx := &mempoolTx{
  219. counter: mem.counter,
  220. height: mem.height,
  221. tx: tx,
  222. }
  223. mem.txs.PushBack(memTx)
  224. mem.logger.Info("Added good transaction", "tx", fmt.Sprintf("%X", types.Tx(tx).Hash()), "res", r)
  225. mem.notifyTxsAvailable()
  226. } else {
  227. // ignore bad transaction
  228. mem.logger.Info("Rejected bad transaction", "tx", fmt.Sprintf("%X", types.Tx(tx).Hash()), "res", r)
  229. // remove from cache (it might be good later)
  230. mem.cache.Remove(tx)
  231. }
  232. default:
  233. // ignore other messages
  234. }
  235. }
  236. func (mem *Mempool) resCbRecheck(req *abci.Request, res *abci.Response) {
  237. switch r := res.Value.(type) {
  238. case *abci.Response_CheckTx:
  239. memTx := mem.recheckCursor.Value.(*mempoolTx)
  240. if !bytes.Equal(req.GetCheckTx().Tx, memTx.tx) {
  241. cmn.PanicSanity(cmn.Fmt("Unexpected tx response from proxy during recheck\n"+
  242. "Expected %X, got %X", r.CheckTx.Data, memTx.tx))
  243. }
  244. if r.CheckTx.Code == abci.CodeTypeOK {
  245. // Good, nothing to do.
  246. } else {
  247. // Tx became invalidated due to newly committed block.
  248. mem.txs.Remove(mem.recheckCursor)
  249. mem.recheckCursor.DetachPrev()
  250. // remove from cache (it might be good later)
  251. mem.cache.Remove(req.GetCheckTx().Tx)
  252. }
  253. if mem.recheckCursor == mem.recheckEnd {
  254. mem.recheckCursor = nil
  255. } else {
  256. mem.recheckCursor = mem.recheckCursor.Next()
  257. }
  258. if mem.recheckCursor == nil {
  259. // Done!
  260. atomic.StoreInt32(&mem.rechecking, 0)
  261. mem.logger.Info("Done rechecking txs")
  262. // incase the recheck removed all txs
  263. if mem.Size() > 0 {
  264. mem.notifyTxsAvailable()
  265. }
  266. }
  267. default:
  268. // ignore other messages
  269. }
  270. }
  271. // TxsAvailable returns a channel which fires once for every height,
  272. // and only when transactions are available in the mempool.
  273. // NOTE: the returned channel may be nil if EnableTxsAvailable was not called.
  274. func (mem *Mempool) TxsAvailable() <-chan int64 {
  275. return mem.txsAvailable
  276. }
  277. func (mem *Mempool) notifyTxsAvailable() {
  278. if mem.Size() == 0 {
  279. panic("notified txs available but mempool is empty!")
  280. }
  281. if mem.txsAvailable != nil && !mem.notifiedTxsAvailable {
  282. select {
  283. case mem.txsAvailable <- mem.height + 1:
  284. default:
  285. }
  286. mem.notifiedTxsAvailable = true
  287. }
  288. }
  289. // Reap returns a list of transactions currently in the mempool.
  290. // If maxTxs is -1, there is no cap on the number of returned transactions.
  291. func (mem *Mempool) Reap(maxTxs int) types.Txs {
  292. mem.proxyMtx.Lock()
  293. defer mem.proxyMtx.Unlock()
  294. for atomic.LoadInt32(&mem.rechecking) > 0 {
  295. // TODO: Something better?
  296. time.Sleep(time.Millisecond * 10)
  297. }
  298. txs := mem.collectTxs(maxTxs)
  299. return txs
  300. }
  301. // maxTxs: -1 means uncapped, 0 means none
  302. func (mem *Mempool) collectTxs(maxTxs int) types.Txs {
  303. if maxTxs == 0 {
  304. return []types.Tx{}
  305. } else if maxTxs < 0 {
  306. maxTxs = mem.txs.Len()
  307. }
  308. txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), maxTxs))
  309. for e := mem.txs.Front(); e != nil && len(txs) < maxTxs; e = e.Next() {
  310. memTx := e.Value.(*mempoolTx)
  311. txs = append(txs, memTx.tx)
  312. }
  313. return txs
  314. }
  315. // Update informs the mempool that the given txs were committed and can be discarded.
  316. // NOTE: this should be called *after* block is committed by consensus.
  317. // NOTE: unsafe; Lock/Unlock must be managed by caller
  318. func (mem *Mempool) Update(height int64, txs types.Txs) error {
  319. // First, create a lookup map of txns in new txs.
  320. txsMap := make(map[string]struct{})
  321. for _, tx := range txs {
  322. txsMap[string(tx)] = struct{}{}
  323. }
  324. // Set height
  325. mem.height = height
  326. mem.notifiedTxsAvailable = false
  327. // Remove transactions that are already in txs.
  328. goodTxs := mem.filterTxs(txsMap)
  329. // Recheck mempool txs if any txs were committed in the block
  330. // NOTE/XXX: in some apps a tx could be invalidated due to EndBlock,
  331. // so we really still do need to recheck, but this is for debugging
  332. if mem.config.Recheck && (mem.config.RecheckEmpty || len(goodTxs) > 0) {
  333. mem.logger.Info("Recheck txs", "numtxs", len(goodTxs), "height", height)
  334. mem.recheckTxs(goodTxs)
  335. // At this point, mem.txs are being rechecked.
  336. // mem.recheckCursor re-scans mem.txs and possibly removes some txs.
  337. // Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
  338. }
  339. return nil
  340. }
  341. func (mem *Mempool) filterTxs(blockTxsMap map[string]struct{}) []types.Tx {
  342. goodTxs := make([]types.Tx, 0, mem.txs.Len())
  343. for e := mem.txs.Front(); e != nil; e = e.Next() {
  344. memTx := e.Value.(*mempoolTx)
  345. // Remove the tx if it's alredy in a block.
  346. if _, ok := blockTxsMap[string(memTx.tx)]; ok {
  347. // remove from clist
  348. mem.txs.Remove(e)
  349. e.DetachPrev()
  350. // NOTE: we don't remove committed txs from the cache.
  351. continue
  352. }
  353. // Good tx!
  354. goodTxs = append(goodTxs, memTx.tx)
  355. }
  356. return goodTxs
  357. }
  358. // NOTE: pass in goodTxs because mem.txs can mutate concurrently.
  359. func (mem *Mempool) recheckTxs(goodTxs []types.Tx) {
  360. if len(goodTxs) == 0 {
  361. return
  362. }
  363. atomic.StoreInt32(&mem.rechecking, 1)
  364. mem.recheckCursor = mem.txs.Front()
  365. mem.recheckEnd = mem.txs.Back()
  366. // Push txs to proxyAppConn
  367. // NOTE: resCb() may be called concurrently.
  368. for _, tx := range goodTxs {
  369. mem.proxyAppConn.CheckTxAsync(tx)
  370. }
  371. mem.proxyAppConn.FlushAsync()
  372. }
  373. //--------------------------------------------------------------------------------
  374. // mempoolTx is a transaction that successfully ran
  375. type mempoolTx struct {
  376. counter int64 // a simple incrementing counter
  377. height int64 // height that this tx had been validated in
  378. tx types.Tx //
  379. }
  380. // Height returns the height for this transaction
  381. func (memTx *mempoolTx) Height() int64 {
  382. return atomic.LoadInt64(&memTx.height)
  383. }
  384. //--------------------------------------------------------------------------------
  385. // txCache maintains a cache of transactions.
  386. type txCache struct {
  387. mtx sync.Mutex
  388. size int
  389. map_ map[string]struct{}
  390. list *list.List // to remove oldest tx when cache gets too big
  391. }
  392. // newTxCache returns a new txCache.
  393. func newTxCache(cacheSize int) *txCache {
  394. return &txCache{
  395. size: cacheSize,
  396. map_: make(map[string]struct{}, cacheSize),
  397. list: list.New(),
  398. }
  399. }
  400. // Reset resets the txCache to empty.
  401. func (cache *txCache) Reset() {
  402. cache.mtx.Lock()
  403. cache.map_ = make(map[string]struct{}, cache.size)
  404. cache.list.Init()
  405. cache.mtx.Unlock()
  406. }
  407. // Push adds the given tx to the txCache. It returns false if tx is already in the cache.
  408. func (cache *txCache) Push(tx types.Tx) bool {
  409. cache.mtx.Lock()
  410. defer cache.mtx.Unlock()
  411. if _, exists := cache.map_[string(tx)]; exists {
  412. return false
  413. }
  414. if cache.list.Len() >= cache.size {
  415. popped := cache.list.Front()
  416. poppedTx := popped.Value.(types.Tx)
  417. // NOTE: the tx may have already been removed from the map
  418. // but deleting a non-existent element is fine
  419. delete(cache.map_, string(poppedTx))
  420. cache.list.Remove(popped)
  421. }
  422. cache.map_[string(tx)] = struct{}{}
  423. cache.list.PushBack(tx)
  424. return true
  425. }
  426. // Remove removes the given tx from the cache.
  427. func (cache *txCache) Remove(tx types.Tx) {
  428. cache.mtx.Lock()
  429. delete(cache.map_, string(tx))
  430. cache.mtx.Unlock()
  431. }