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.

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