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.

499 lines
14 KiB

9 years ago
9 years ago
7 years ago
8 years ago
9 years ago
8 years ago
7 years ago
9 years ago
9 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 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
7 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 // true if fired on txsAvailable for this height
  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. mem.notifiedTxsAvailable = true
  283. mem.txsAvailable <- mem.height + 1
  284. }
  285. }
  286. // Reap returns a list of transactions currently in the mempool.
  287. // If maxTxs is -1, there is no cap on the number of returned transactions.
  288. func (mem *Mempool) Reap(maxTxs int) types.Txs {
  289. mem.proxyMtx.Lock()
  290. defer mem.proxyMtx.Unlock()
  291. for atomic.LoadInt32(&mem.rechecking) > 0 {
  292. // TODO: Something better?
  293. time.Sleep(time.Millisecond * 10)
  294. }
  295. txs := mem.collectTxs(maxTxs)
  296. return txs
  297. }
  298. // maxTxs: -1 means uncapped, 0 means none
  299. func (mem *Mempool) collectTxs(maxTxs int) types.Txs {
  300. if maxTxs == 0 {
  301. return []types.Tx{}
  302. } else if maxTxs < 0 {
  303. maxTxs = mem.txs.Len()
  304. }
  305. txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), maxTxs))
  306. for e := mem.txs.Front(); e != nil && len(txs) < maxTxs; e = e.Next() {
  307. memTx := e.Value.(*mempoolTx)
  308. txs = append(txs, memTx.tx)
  309. }
  310. return txs
  311. }
  312. // Update informs the mempool that the given txs were committed and can be discarded.
  313. // NOTE: this should be called *after* block is committed by consensus.
  314. // NOTE: unsafe; Lock/Unlock must be managed by caller
  315. func (mem *Mempool) Update(height int64, txs types.Txs) error {
  316. // First, create a lookup map of txns in new txs.
  317. txsMap := make(map[string]struct{})
  318. for _, tx := range txs {
  319. txsMap[string(tx)] = struct{}{}
  320. }
  321. // Set height
  322. mem.height = height
  323. mem.notifiedTxsAvailable = false
  324. // Remove transactions that are already in txs.
  325. goodTxs := mem.filterTxs(txsMap)
  326. // Recheck mempool txs if any txs were committed in the block
  327. // NOTE/XXX: in some apps a tx could be invalidated due to EndBlock,
  328. // so we really still do need to recheck, but this is for debugging
  329. if mem.config.Recheck && (mem.config.RecheckEmpty || len(txs) > 0) {
  330. mem.logger.Info("Recheck txs", "numtxs", len(goodTxs), "height", height)
  331. mem.recheckTxs(goodTxs)
  332. // At this point, mem.txs are being rechecked.
  333. // mem.recheckCursor re-scans mem.txs and possibly removes some txs.
  334. // Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
  335. }
  336. return nil
  337. }
  338. func (mem *Mempool) filterTxs(blockTxsMap map[string]struct{}) []types.Tx {
  339. goodTxs := make([]types.Tx, 0, mem.txs.Len())
  340. for e := mem.txs.Front(); e != nil; e = e.Next() {
  341. memTx := e.Value.(*mempoolTx)
  342. // Remove the tx if it's alredy in a block.
  343. if _, ok := blockTxsMap[string(memTx.tx)]; ok {
  344. // remove from clist
  345. mem.txs.Remove(e)
  346. e.DetachPrev()
  347. // NOTE: we don't remove committed txs from the cache.
  348. continue
  349. }
  350. // Good tx!
  351. goodTxs = append(goodTxs, memTx.tx)
  352. }
  353. return goodTxs
  354. }
  355. // NOTE: pass in goodTxs because mem.txs can mutate concurrently.
  356. func (mem *Mempool) recheckTxs(goodTxs []types.Tx) {
  357. if len(goodTxs) == 0 {
  358. return
  359. }
  360. atomic.StoreInt32(&mem.rechecking, 1)
  361. mem.recheckCursor = mem.txs.Front()
  362. mem.recheckEnd = mem.txs.Back()
  363. // Push txs to proxyAppConn
  364. // NOTE: resCb() may be called concurrently.
  365. for _, tx := range goodTxs {
  366. mem.proxyAppConn.CheckTxAsync(tx)
  367. }
  368. mem.proxyAppConn.FlushAsync()
  369. }
  370. //--------------------------------------------------------------------------------
  371. // mempoolTx is a transaction that successfully ran
  372. type mempoolTx struct {
  373. counter int64 // a simple incrementing counter
  374. height int64 // height that this tx had been validated in
  375. tx types.Tx //
  376. }
  377. // Height returns the height for this transaction
  378. func (memTx *mempoolTx) Height() int64 {
  379. return atomic.LoadInt64(&memTx.height)
  380. }
  381. //--------------------------------------------------------------------------------
  382. // txCache maintains a cache of transactions.
  383. type txCache struct {
  384. mtx sync.Mutex
  385. size int
  386. map_ map[string]struct{}
  387. list *list.List // to remove oldest tx when cache gets too big
  388. }
  389. // newTxCache returns a new txCache.
  390. func newTxCache(cacheSize int) *txCache {
  391. return &txCache{
  392. size: cacheSize,
  393. map_: make(map[string]struct{}, cacheSize),
  394. list: list.New(),
  395. }
  396. }
  397. // Reset resets the txCache to empty.
  398. func (cache *txCache) Reset() {
  399. cache.mtx.Lock()
  400. cache.map_ = make(map[string]struct{}, cache.size)
  401. cache.list.Init()
  402. cache.mtx.Unlock()
  403. }
  404. // Push adds the given tx to the txCache. It returns false if tx is already in the cache.
  405. func (cache *txCache) Push(tx types.Tx) bool {
  406. cache.mtx.Lock()
  407. defer cache.mtx.Unlock()
  408. if _, exists := cache.map_[string(tx)]; exists {
  409. return false
  410. }
  411. if cache.list.Len() >= cache.size {
  412. popped := cache.list.Front()
  413. poppedTx := popped.Value.(types.Tx)
  414. // NOTE: the tx may have already been removed from the map
  415. // but deleting a non-existent element is fine
  416. delete(cache.map_, string(poppedTx))
  417. cache.list.Remove(popped)
  418. }
  419. cache.map_[string(tx)] = struct{}{}
  420. cache.list.PushBack(tx)
  421. return true
  422. }
  423. // Remove removes the given tx from the cache.
  424. func (cache *txCache) Remove(tx types.Tx) {
  425. cache.mtx.Lock()
  426. delete(cache.map_, string(tx))
  427. cache.mtx.Unlock()
  428. }