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.

417 lines
11 KiB

9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 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
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package mempool
  2. import (
  3. "bytes"
  4. "container/list"
  5. "sync"
  6. "sync/atomic"
  7. "time"
  8. abci "github.com/tendermint/abci/types"
  9. auto "github.com/tendermint/tmlibs/autofile"
  10. "github.com/tendermint/tmlibs/clist"
  11. cmn "github.com/tendermint/tmlibs/common"
  12. cfg "github.com/tendermint/tendermint/config"
  13. "github.com/tendermint/tendermint/proxy"
  14. "github.com/tendermint/tendermint/types"
  15. )
  16. /*
  17. The mempool pushes new txs onto the proxyAppConn.
  18. It gets a stream of (req, res) tuples from the proxy.
  19. The memool stores good txs in a concurrent linked-list.
  20. Multiple concurrent go-routines can traverse this linked-list
  21. safely by calling .NextWait() on each element.
  22. So we have several go-routines:
  23. 1. Consensus calling Update() and Reap() synchronously
  24. 2. Many mempool reactor's peer routines calling CheckTx()
  25. 3. Many mempool reactor's peer routines traversing the txs linked list
  26. 4. Another goroutine calling GarbageCollectTxs() periodically
  27. To manage these goroutines, there are three methods of locking.
  28. 1. Mutations to the linked-list is protected by an internal mtx (CList is goroutine-safe)
  29. 2. Mutations to the linked-list elements are atomic
  30. 3. CheckTx() calls can be paused upon Update() and Reap(), protected by .proxyMtx
  31. Garbage collection of old elements from mempool.txs is handlde via
  32. the DetachPrev() call, which makes old elements not reachable by
  33. peer broadcastTxRoutine() automatically garbage collected.
  34. TODO: Better handle abci client errors. (make it automatically handle connection errors)
  35. */
  36. const cacheSize = 100000
  37. type Mempool struct {
  38. config *cfg.MempoolConfig
  39. proxyMtx sync.Mutex
  40. proxyAppConn proxy.AppConnMempool
  41. txs *clist.CList // concurrent linked-list of good txs
  42. counter int64 // simple incrementing counter
  43. height int // the last block Update()'d to
  44. rechecking int32 // for re-checking filtered txs on Update()
  45. recheckCursor *clist.CElement // next expected response
  46. recheckEnd *clist.CElement // re-checking stops here
  47. // Keep a cache of already-seen txs.
  48. // This reduces the pressure on the proxyApp.
  49. cache *txCache
  50. // A log of mempool txs
  51. wal *auto.AutoFile
  52. }
  53. func NewMempool(config *cfg.MempoolConfig, proxyAppConn proxy.AppConnMempool) *Mempool {
  54. mempool := &Mempool{
  55. config: config,
  56. proxyAppConn: proxyAppConn,
  57. txs: clist.New(),
  58. counter: 0,
  59. height: 0,
  60. rechecking: 0,
  61. recheckCursor: nil,
  62. recheckEnd: nil,
  63. cache: newTxCache(cacheSize),
  64. }
  65. mempool.initWAL()
  66. proxyAppConn.SetResponseCallback(mempool.resCb)
  67. return mempool
  68. }
  69. func (mem *Mempool) initWAL() {
  70. walDir := mem.config.WalDir()
  71. if walDir != "" {
  72. err := cmn.EnsureDir(walDir, 0700)
  73. if err != nil {
  74. log.Error("Error ensuring Mempool wal dir", "error", err)
  75. cmn.PanicSanity(err)
  76. }
  77. af, err := auto.OpenAutoFile(walDir + "/wal")
  78. if err != nil {
  79. log.Error("Error opening Mempool wal file", "error", err)
  80. cmn.PanicSanity(err)
  81. }
  82. mem.wal = af
  83. }
  84. }
  85. // consensus must be able to hold lock to safely update
  86. func (mem *Mempool) Lock() {
  87. mem.proxyMtx.Lock()
  88. }
  89. func (mem *Mempool) Unlock() {
  90. mem.proxyMtx.Unlock()
  91. }
  92. // Number of transactions in the mempool clist
  93. func (mem *Mempool) Size() int {
  94. return mem.txs.Len()
  95. }
  96. // Remove all transactions from mempool and cache
  97. func (mem *Mempool) Flush() {
  98. mem.proxyMtx.Lock()
  99. defer mem.proxyMtx.Unlock()
  100. mem.cache.Reset()
  101. for e := mem.txs.Front(); e != nil; e = e.Next() {
  102. mem.txs.Remove(e)
  103. e.DetachPrev()
  104. }
  105. }
  106. // Return the first element of mem.txs for peer goroutines to call .NextWait() on.
  107. // Blocks until txs has elements.
  108. func (mem *Mempool) TxsFrontWait() *clist.CElement {
  109. return mem.txs.FrontWait()
  110. }
  111. // Try a new transaction in the mempool.
  112. // Potentially blocking if we're blocking on Update() or Reap().
  113. // cb: A callback from the CheckTx command.
  114. // It gets called from another goroutine.
  115. // CONTRACT: Either cb will get called, or err returned.
  116. func (mem *Mempool) CheckTx(tx types.Tx, cb func(*abci.Response)) (err error) {
  117. mem.proxyMtx.Lock()
  118. defer mem.proxyMtx.Unlock()
  119. // CACHE
  120. if mem.cache.Exists(tx) {
  121. if cb != nil {
  122. cb(&abci.Response{
  123. Value: &abci.Response_CheckTx{
  124. &abci.ResponseCheckTx{
  125. Code: abci.CodeType_BadNonce, // TODO or duplicate tx
  126. Log: "Duplicate transaction (ignored)",
  127. },
  128. },
  129. })
  130. }
  131. return nil
  132. }
  133. mem.cache.Push(tx)
  134. // END CACHE
  135. // WAL
  136. if mem.wal != nil {
  137. // TODO: Notify administrators when WAL fails
  138. mem.wal.Write([]byte(tx))
  139. mem.wal.Write([]byte("\n"))
  140. }
  141. // END WAL
  142. // NOTE: proxyAppConn may error if tx buffer is full
  143. if err = mem.proxyAppConn.Error(); err != nil {
  144. return err
  145. }
  146. reqRes := mem.proxyAppConn.CheckTxAsync(tx)
  147. if cb != nil {
  148. reqRes.SetCallback(cb)
  149. }
  150. return nil
  151. }
  152. // ABCI callback function
  153. func (mem *Mempool) resCb(req *abci.Request, res *abci.Response) {
  154. if mem.recheckCursor == nil {
  155. mem.resCbNormal(req, res)
  156. } else {
  157. mem.resCbRecheck(req, res)
  158. }
  159. }
  160. func (mem *Mempool) resCbNormal(req *abci.Request, res *abci.Response) {
  161. switch r := res.Value.(type) {
  162. case *abci.Response_CheckTx:
  163. if r.CheckTx.Code == abci.CodeType_OK {
  164. mem.counter++
  165. memTx := &mempoolTx{
  166. counter: mem.counter,
  167. height: int64(mem.height),
  168. tx: req.GetCheckTx().Tx,
  169. }
  170. mem.txs.PushBack(memTx)
  171. } else {
  172. // ignore bad transaction
  173. log.Info("Bad Transaction", "res", r)
  174. // remove from cache (it might be good later)
  175. mem.cache.Remove(req.GetCheckTx().Tx)
  176. // TODO: handle other retcodes
  177. }
  178. default:
  179. // ignore other messages
  180. }
  181. }
  182. func (mem *Mempool) resCbRecheck(req *abci.Request, res *abci.Response) {
  183. switch r := res.Value.(type) {
  184. case *abci.Response_CheckTx:
  185. memTx := mem.recheckCursor.Value.(*mempoolTx)
  186. if !bytes.Equal(req.GetCheckTx().Tx, memTx.tx) {
  187. cmn.PanicSanity(cmn.Fmt("Unexpected tx response from proxy during recheck\n"+
  188. "Expected %X, got %X", r.CheckTx.Data, memTx.tx))
  189. }
  190. if r.CheckTx.Code == abci.CodeType_OK {
  191. // Good, nothing to do.
  192. } else {
  193. // Tx became invalidated due to newly committed block.
  194. mem.txs.Remove(mem.recheckCursor)
  195. mem.recheckCursor.DetachPrev()
  196. // remove from cache (it might be good later)
  197. mem.cache.Remove(req.GetCheckTx().Tx)
  198. }
  199. if mem.recheckCursor == mem.recheckEnd {
  200. mem.recheckCursor = nil
  201. } else {
  202. mem.recheckCursor = mem.recheckCursor.Next()
  203. }
  204. if mem.recheckCursor == nil {
  205. // Done!
  206. atomic.StoreInt32(&mem.rechecking, 0)
  207. log.Info("Done rechecking txs")
  208. }
  209. default:
  210. // ignore other messages
  211. }
  212. }
  213. // Get the valid transactions remaining
  214. // If maxTxs is -1, there is no cap on returned transactions.
  215. func (mem *Mempool) Reap(maxTxs int) types.Txs {
  216. mem.proxyMtx.Lock()
  217. defer mem.proxyMtx.Unlock()
  218. for atomic.LoadInt32(&mem.rechecking) > 0 {
  219. // TODO: Something better?
  220. time.Sleep(time.Millisecond * 10)
  221. }
  222. txs := mem.collectTxs(maxTxs)
  223. return txs
  224. }
  225. // maxTxs: -1 means uncapped, 0 means none
  226. func (mem *Mempool) collectTxs(maxTxs int) types.Txs {
  227. if maxTxs == 0 {
  228. return []types.Tx{}
  229. } else if maxTxs < 0 {
  230. maxTxs = mem.txs.Len()
  231. }
  232. txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), maxTxs))
  233. for e := mem.txs.Front(); e != nil && len(txs) < maxTxs; e = e.Next() {
  234. memTx := e.Value.(*mempoolTx)
  235. txs = append(txs, memTx.tx)
  236. }
  237. return txs
  238. }
  239. // Tell mempool that these txs were committed.
  240. // Mempool will discard these txs.
  241. // NOTE: this should be called *after* block is committed by consensus.
  242. // NOTE: unsafe; Lock/Unlock must be managed by caller
  243. func (mem *Mempool) Update(height int, txs types.Txs) {
  244. // TODO: check err ?
  245. mem.proxyAppConn.FlushSync() // To flush async resCb calls e.g. from CheckTx
  246. // First, create a lookup map of txns in new txs.
  247. txsMap := make(map[string]struct{})
  248. for _, tx := range txs {
  249. txsMap[string(tx)] = struct{}{}
  250. }
  251. // Set height
  252. mem.height = height
  253. // Remove transactions that are already in txs.
  254. goodTxs := mem.filterTxs(txsMap)
  255. // Recheck mempool txs if any txs were committed in the block
  256. // NOTE/XXX: in some apps a tx could be invalidated due to EndBlock,
  257. // so we really still do need to recheck, but this is for debugging
  258. if mem.config.Recheck && (mem.config.RecheckEmpty || len(txs) > 0) {
  259. log.Info("Recheck txs", "numtxs", len(goodTxs))
  260. mem.recheckTxs(goodTxs)
  261. // At this point, mem.txs are being rechecked.
  262. // mem.recheckCursor re-scans mem.txs and possibly removes some txs.
  263. // Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
  264. }
  265. }
  266. func (mem *Mempool) filterTxs(blockTxsMap map[string]struct{}) []types.Tx {
  267. goodTxs := make([]types.Tx, 0, mem.txs.Len())
  268. for e := mem.txs.Front(); e != nil; e = e.Next() {
  269. memTx := e.Value.(*mempoolTx)
  270. // Remove the tx if it's alredy in a block.
  271. if _, ok := blockTxsMap[string(memTx.tx)]; ok {
  272. // remove from clist
  273. mem.txs.Remove(e)
  274. e.DetachPrev()
  275. // NOTE: we don't remove committed txs from the cache.
  276. continue
  277. }
  278. // Good tx!
  279. goodTxs = append(goodTxs, memTx.tx)
  280. }
  281. return goodTxs
  282. }
  283. // NOTE: pass in goodTxs because mem.txs can mutate concurrently.
  284. func (mem *Mempool) recheckTxs(goodTxs []types.Tx) {
  285. if len(goodTxs) == 0 {
  286. return
  287. }
  288. atomic.StoreInt32(&mem.rechecking, 1)
  289. mem.recheckCursor = mem.txs.Front()
  290. mem.recheckEnd = mem.txs.Back()
  291. // Push txs to proxyAppConn
  292. // NOTE: resCb() may be called concurrently.
  293. for _, tx := range goodTxs {
  294. mem.proxyAppConn.CheckTxAsync(tx)
  295. }
  296. mem.proxyAppConn.FlushAsync()
  297. }
  298. //--------------------------------------------------------------------------------
  299. // A transaction that successfully ran
  300. type mempoolTx struct {
  301. counter int64 // a simple incrementing counter
  302. height int64 // height that this tx had been validated in
  303. tx types.Tx //
  304. }
  305. func (memTx *mempoolTx) Height() int {
  306. return int(atomic.LoadInt64(&memTx.height))
  307. }
  308. //--------------------------------------------------------------------------------
  309. type txCache struct {
  310. mtx sync.Mutex
  311. size int
  312. map_ map[string]struct{}
  313. list *list.List // to remove oldest tx when cache gets too big
  314. }
  315. func newTxCache(cacheSize int) *txCache {
  316. return &txCache{
  317. size: cacheSize,
  318. map_: make(map[string]struct{}, cacheSize),
  319. list: list.New(),
  320. }
  321. }
  322. func (cache *txCache) Reset() {
  323. cache.mtx.Lock()
  324. cache.map_ = make(map[string]struct{}, cacheSize)
  325. cache.list.Init()
  326. cache.mtx.Unlock()
  327. }
  328. func (cache *txCache) Exists(tx types.Tx) bool {
  329. cache.mtx.Lock()
  330. _, exists := cache.map_[string(tx)]
  331. cache.mtx.Unlock()
  332. return exists
  333. }
  334. // Returns false if tx is in cache.
  335. func (cache *txCache) Push(tx types.Tx) bool {
  336. cache.mtx.Lock()
  337. defer cache.mtx.Unlock()
  338. if _, exists := cache.map_[string(tx)]; exists {
  339. return false
  340. }
  341. if cache.list.Len() >= cache.size {
  342. popped := cache.list.Front()
  343. poppedTx := popped.Value.(types.Tx)
  344. // NOTE: the tx may have already been removed from the map
  345. // but deleting a non-existant element is fine
  346. delete(cache.map_, string(poppedTx))
  347. cache.list.Remove(popped)
  348. }
  349. cache.map_[string(tx)] = struct{}{}
  350. cache.list.PushBack(tx)
  351. return true
  352. }
  353. func (cache *txCache) Remove(tx types.Tx) {
  354. cache.mtx.Lock()
  355. delete(cache.map_, string(tx))
  356. cache.mtx.Unlock()
  357. }