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.

617 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
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 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
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 years ago
max-bytes PR follow-up (#2318) * ReapMaxTxs: return all txs if max is negative this mirrors ReapMaxBytes behavior See https://github.com/tendermint/tendermint/pull/2184#discussion_r214439950 * increase MaxAminoOverheadForBlock tested with: ``` func TestMaxAminoOverheadForBlock(t *testing.T) { maxChainID := "" for i := 0; i < MaxChainIDLen; i++ { maxChainID += "𠜎" } h := Header{ ChainID: maxChainID, Height: 10, Time: time.Now().UTC(), NumTxs: 100, TotalTxs: 200, LastBlockID: makeBlockID(make([]byte, 20), 300, make([]byte, 20)), LastCommitHash: tmhash.Sum([]byte("last_commit_hash")), DataHash: tmhash.Sum([]byte("data_hash")), ValidatorsHash: tmhash.Sum([]byte("validators_hash")), NextValidatorsHash: tmhash.Sum([]byte("next_validators_hash")), ConsensusHash: tmhash.Sum([]byte("consensus_hash")), AppHash: tmhash.Sum([]byte("app_hash")), LastResultsHash: tmhash.Sum([]byte("last_results_hash")), EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: tmhash.Sum([]byte("proposer_address")), } b := Block{ Header: h, Data: Data{Txs: makeTxs(10000, 100)}, Evidence: EvidenceData{}, LastCommit: &Commit{}, } bz, err := cdc.MarshalBinary(b) require.NoError(t, err) assert.Equal(t, MaxHeaderBytes+MaxAminoOverheadForBlock-2, len(bz)-1000000-20000-1) } ``` * fix MaxYYY constants calculation by using math.MaxInt64 See https://github.com/tendermint/tendermint/pull/2184#discussion_r214444244 * pass mempool filter as an option See https://github.com/tendermint/tendermint/pull/2184#discussion_r214445869 * fixes after Dev's comments
6 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. "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. // WithFilter sets a filter for mempool to only accept txs for which f(tx)
  120. // returns true.
  121. func WithFilter(f func(types.Tx) bool) MempoolOption {
  122. return func(mem *Mempool) { mem.filter = f }
  123. }
  124. // WithMetrics sets the metrics.
  125. func WithMetrics(metrics *Metrics) MempoolOption {
  126. return func(mem *Mempool) { mem.metrics = metrics }
  127. }
  128. // CloseWAL closes and discards the underlying WAL file.
  129. // Any further writes will not be relayed to disk.
  130. func (mem *Mempool) CloseWAL() bool {
  131. if mem == nil {
  132. return false
  133. }
  134. mem.proxyMtx.Lock()
  135. defer mem.proxyMtx.Unlock()
  136. if mem.wal == nil {
  137. return false
  138. }
  139. if err := mem.wal.Close(); err != nil && mem.logger != nil {
  140. mem.logger.Error("Mempool.CloseWAL", "err", err)
  141. }
  142. mem.wal = nil
  143. return true
  144. }
  145. func (mem *Mempool) InitWAL() {
  146. walDir := mem.config.WalDir()
  147. if walDir != "" {
  148. err := cmn.EnsureDir(walDir, 0700)
  149. if err != nil {
  150. cmn.PanicSanity(errors.Wrap(err, "Error ensuring Mempool wal dir"))
  151. }
  152. af, err := auto.OpenAutoFile(walDir + "/wal")
  153. if err != nil {
  154. cmn.PanicSanity(errors.Wrap(err, "Error opening Mempool wal file"))
  155. }
  156. mem.wal = af
  157. }
  158. }
  159. // Lock locks the mempool. The consensus must be able to hold lock to safely update.
  160. func (mem *Mempool) Lock() {
  161. mem.proxyMtx.Lock()
  162. }
  163. // Unlock unlocks the mempool.
  164. func (mem *Mempool) Unlock() {
  165. mem.proxyMtx.Unlock()
  166. }
  167. // Size returns the number of transactions in the mempool.
  168. func (mem *Mempool) Size() int {
  169. return mem.txs.Len()
  170. }
  171. // Flushes the mempool connection to ensure async resCb calls are done e.g.
  172. // from CheckTx.
  173. func (mem *Mempool) FlushAppConn() error {
  174. return mem.proxyAppConn.FlushSync()
  175. }
  176. // Flush removes all transactions from the mempool and cache
  177. func (mem *Mempool) Flush() {
  178. mem.proxyMtx.Lock()
  179. defer mem.proxyMtx.Unlock()
  180. mem.cache.Reset()
  181. for e := mem.txs.Front(); e != nil; e = e.Next() {
  182. mem.txs.Remove(e)
  183. e.DetachPrev()
  184. }
  185. }
  186. // TxsFront returns the first transaction in the ordered list for peer
  187. // goroutines to call .NextWait() on.
  188. func (mem *Mempool) TxsFront() *clist.CElement {
  189. return mem.txs.Front()
  190. }
  191. // TxsWaitChan returns a channel to wait on transactions. It will be closed
  192. // once the mempool is not empty (ie. the internal `mem.txs` has at least one
  193. // element)
  194. func (mem *Mempool) TxsWaitChan() <-chan struct{} {
  195. return mem.txs.WaitChan()
  196. }
  197. // CheckTx executes a new transaction against the application to determine its validity
  198. // and whether it should be added to the mempool.
  199. // It blocks if we're waiting on Update() or Reap().
  200. // cb: A callback from the CheckTx command.
  201. // It gets called from another goroutine.
  202. // CONTRACT: Either cb will get called, or err returned.
  203. func (mem *Mempool) CheckTx(tx types.Tx, cb func(*abci.Response)) (err error) {
  204. mem.proxyMtx.Lock()
  205. defer mem.proxyMtx.Unlock()
  206. if mem.Size() >= mem.config.Size {
  207. return ErrMempoolIsFull
  208. }
  209. if mem.filter != nil && !mem.filter(tx) {
  210. return
  211. }
  212. // CACHE
  213. if !mem.cache.Push(tx) {
  214. return ErrTxInCache
  215. }
  216. // END CACHE
  217. // WAL
  218. if mem.wal != nil {
  219. // TODO: Notify administrators when WAL fails
  220. _, err := mem.wal.Write([]byte(tx))
  221. if err != nil {
  222. mem.logger.Error("Error writing to WAL", "err", err)
  223. }
  224. _, err = mem.wal.Write([]byte("\n"))
  225. if err != nil {
  226. mem.logger.Error("Error writing to WAL", "err", err)
  227. }
  228. }
  229. // END WAL
  230. // NOTE: proxyAppConn may error if tx buffer is full
  231. if err = mem.proxyAppConn.Error(); err != nil {
  232. return err
  233. }
  234. reqRes := mem.proxyAppConn.CheckTxAsync(tx)
  235. if cb != nil {
  236. reqRes.SetCallback(cb)
  237. }
  238. return nil
  239. }
  240. // ABCI callback function
  241. func (mem *Mempool) resCb(req *abci.Request, res *abci.Response) {
  242. if mem.recheckCursor == nil {
  243. mem.resCbNormal(req, res)
  244. } else {
  245. mem.resCbRecheck(req, res)
  246. }
  247. mem.metrics.Size.Set(float64(mem.Size()))
  248. }
  249. func (mem *Mempool) resCbNormal(req *abci.Request, res *abci.Response) {
  250. switch r := res.Value.(type) {
  251. case *abci.Response_CheckTx:
  252. tx := req.GetCheckTx().Tx
  253. if r.CheckTx.Code == abci.CodeTypeOK {
  254. mem.counter++
  255. memTx := &mempoolTx{
  256. counter: mem.counter,
  257. height: mem.height,
  258. gasWanted: r.CheckTx.GasWanted,
  259. tx: tx,
  260. }
  261. mem.txs.PushBack(memTx)
  262. mem.logger.Info("Added good transaction", "tx", TxID(tx), "res", r, "total", mem.Size())
  263. mem.notifyTxsAvailable()
  264. } else {
  265. // ignore bad transaction
  266. mem.logger.Info("Rejected bad transaction", "tx", TxID(tx), "res", r)
  267. // remove from cache (it might be good later)
  268. mem.cache.Remove(tx)
  269. }
  270. default:
  271. // ignore other messages
  272. }
  273. }
  274. func (mem *Mempool) resCbRecheck(req *abci.Request, res *abci.Response) {
  275. switch r := res.Value.(type) {
  276. case *abci.Response_CheckTx:
  277. memTx := mem.recheckCursor.Value.(*mempoolTx)
  278. if !bytes.Equal(req.GetCheckTx().Tx, memTx.tx) {
  279. cmn.PanicSanity(fmt.Sprintf("Unexpected tx response from proxy during recheck\n"+
  280. "Expected %X, got %X", r.CheckTx.Data, memTx.tx))
  281. }
  282. if r.CheckTx.Code == abci.CodeTypeOK {
  283. // Good, nothing to do.
  284. } else {
  285. // Tx became invalidated due to newly committed block.
  286. mem.txs.Remove(mem.recheckCursor)
  287. mem.recheckCursor.DetachPrev()
  288. // remove from cache (it might be good later)
  289. mem.cache.Remove(req.GetCheckTx().Tx)
  290. }
  291. if mem.recheckCursor == mem.recheckEnd {
  292. mem.recheckCursor = nil
  293. } else {
  294. mem.recheckCursor = mem.recheckCursor.Next()
  295. }
  296. if mem.recheckCursor == nil {
  297. // Done!
  298. atomic.StoreInt32(&mem.rechecking, 0)
  299. mem.logger.Info("Done rechecking txs")
  300. // incase the recheck removed all txs
  301. if mem.Size() > 0 {
  302. mem.notifyTxsAvailable()
  303. }
  304. }
  305. default:
  306. // ignore other messages
  307. }
  308. }
  309. // TxsAvailable returns a channel which fires once for every height,
  310. // and only when transactions are available in the mempool.
  311. // NOTE: the returned channel may be nil if EnableTxsAvailable was not called.
  312. func (mem *Mempool) TxsAvailable() <-chan struct{} {
  313. return mem.txsAvailable
  314. }
  315. func (mem *Mempool) notifyTxsAvailable() {
  316. if mem.Size() == 0 {
  317. panic("notified txs available but mempool is empty!")
  318. }
  319. if mem.txsAvailable != nil && !mem.notifiedTxsAvailable {
  320. // channel cap is 1, so this will send once
  321. mem.notifiedTxsAvailable = true
  322. select {
  323. case mem.txsAvailable <- struct{}{}:
  324. default:
  325. }
  326. }
  327. }
  328. // ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes bytes total
  329. // with the condition that the total gasWanted must be less than maxGas.
  330. // If both maxes are negative, there is no cap on the size of all returned
  331. // transactions (~ all available transactions).
  332. func (mem *Mempool) ReapMaxBytesMaxGas(maxBytes int, maxGas int64) 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 totalBytes int
  341. var totalGas int64
  342. // TODO: we will get a performance boost if we have a good estimate of avg
  343. // size per tx, and set the initial capacity based off of that.
  344. // txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), max/mem.avgTxSize))
  345. txs := make([]types.Tx, 0, mem.txs.Len())
  346. for e := mem.txs.Front(); e != nil; e = e.Next() {
  347. memTx := e.Value.(*mempoolTx)
  348. // Check total size requirement
  349. // amino.UvarintSize is not used here because it won't be possible to reuse buf
  350. aminoOverhead := binary.PutUvarint(buf[:], uint64(len(memTx.tx)))
  351. if maxBytes > -1 && totalBytes+len(memTx.tx)+aminoOverhead > maxBytes {
  352. return txs
  353. }
  354. totalBytes += len(memTx.tx) + aminoOverhead
  355. // Check total gas requirement
  356. if maxGas > -1 && totalGas+memTx.gasWanted > maxGas {
  357. return txs
  358. }
  359. totalGas += memTx.gasWanted
  360. txs = append(txs, memTx.tx)
  361. }
  362. return txs
  363. }
  364. // ReapMaxTxs reaps up to max transactions from the mempool.
  365. // If max is negative, there is no cap on the size of all returned
  366. // transactions (~ all available transactions).
  367. func (mem *Mempool) ReapMaxTxs(max int) types.Txs {
  368. mem.proxyMtx.Lock()
  369. defer mem.proxyMtx.Unlock()
  370. if max < 0 {
  371. max = mem.txs.Len()
  372. }
  373. for atomic.LoadInt32(&mem.rechecking) > 0 {
  374. // TODO: Something better?
  375. time.Sleep(time.Millisecond * 10)
  376. }
  377. txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), max))
  378. for e := mem.txs.Front(); e != nil && len(txs) <= max; e = e.Next() {
  379. memTx := e.Value.(*mempoolTx)
  380. txs = append(txs, memTx.tx)
  381. }
  382. return txs
  383. }
  384. // Update informs the mempool that the given txs were committed and can be discarded.
  385. // NOTE: this should be called *after* block is committed by consensus.
  386. // NOTE: unsafe; Lock/Unlock must be managed by caller
  387. func (mem *Mempool) Update(height int64, txs types.Txs, filter func(types.Tx) bool) error {
  388. // First, create a lookup map of txns in new txs.
  389. txsMap := make(map[string]struct{}, len(txs))
  390. for _, tx := range txs {
  391. txsMap[string(tx)] = struct{}{}
  392. }
  393. // Set height
  394. mem.height = height
  395. mem.notifiedTxsAvailable = false
  396. if filter != nil {
  397. mem.filter = filter
  398. }
  399. // Remove transactions that are already in txs.
  400. goodTxs := mem.filterTxs(txsMap)
  401. // Recheck mempool txs if any txs were committed in the block
  402. // NOTE/XXX: in some apps a tx could be invalidated due to EndBlock,
  403. // so we really still do need to recheck, but this is for debugging
  404. if mem.config.Recheck && (mem.config.RecheckEmpty || len(goodTxs) > 0) {
  405. mem.logger.Info("Recheck txs", "numtxs", len(goodTxs), "height", height)
  406. mem.recheckTxs(goodTxs)
  407. // At this point, mem.txs are being rechecked.
  408. // mem.recheckCursor re-scans mem.txs and possibly removes some txs.
  409. // Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
  410. }
  411. // Update metrics
  412. mem.metrics.Size.Set(float64(mem.Size()))
  413. return nil
  414. }
  415. func (mem *Mempool) filterTxs(blockTxsMap map[string]struct{}) []types.Tx {
  416. goodTxs := make([]types.Tx, 0, mem.txs.Len())
  417. for e := mem.txs.Front(); e != nil; e = e.Next() {
  418. memTx := e.Value.(*mempoolTx)
  419. // Remove the tx if it's alredy in a block.
  420. if _, ok := blockTxsMap[string(memTx.tx)]; ok {
  421. // remove from clist
  422. mem.txs.Remove(e)
  423. e.DetachPrev()
  424. // NOTE: we don't remove committed txs from the cache.
  425. continue
  426. }
  427. // Good tx!
  428. goodTxs = append(goodTxs, memTx.tx)
  429. }
  430. return goodTxs
  431. }
  432. // NOTE: pass in goodTxs because mem.txs can mutate concurrently.
  433. func (mem *Mempool) recheckTxs(goodTxs []types.Tx) {
  434. if len(goodTxs) == 0 {
  435. return
  436. }
  437. atomic.StoreInt32(&mem.rechecking, 1)
  438. mem.recheckCursor = mem.txs.Front()
  439. mem.recheckEnd = mem.txs.Back()
  440. // Push txs to proxyAppConn
  441. // NOTE: resCb() may be called concurrently.
  442. for _, tx := range goodTxs {
  443. mem.proxyAppConn.CheckTxAsync(tx)
  444. }
  445. mem.proxyAppConn.FlushAsync()
  446. }
  447. //--------------------------------------------------------------------------------
  448. // mempoolTx is a transaction that successfully ran
  449. type mempoolTx struct {
  450. counter int64 // a simple incrementing counter
  451. height int64 // height that this tx had been validated in
  452. gasWanted int64 // amount of gas this tx states it will require
  453. tx types.Tx //
  454. }
  455. // Height returns the height for this transaction
  456. func (memTx *mempoolTx) Height() int64 {
  457. return atomic.LoadInt64(&memTx.height)
  458. }
  459. //--------------------------------------------------------------------------------
  460. type txCache interface {
  461. Reset()
  462. Push(tx types.Tx) bool
  463. Remove(tx types.Tx)
  464. }
  465. // mapTxCache maintains a cache of transactions. This only stores
  466. // the hash of the tx, due to memory concerns.
  467. type mapTxCache struct {
  468. mtx sync.Mutex
  469. size int
  470. map_ map[[sha256.Size]byte]*list.Element
  471. list *list.List // to remove oldest tx when cache gets too big
  472. }
  473. var _ txCache = (*mapTxCache)(nil)
  474. // newMapTxCache returns a new mapTxCache.
  475. func newMapTxCache(cacheSize int) *mapTxCache {
  476. return &mapTxCache{
  477. size: cacheSize,
  478. map_: make(map[[sha256.Size]byte]*list.Element, cacheSize),
  479. list: list.New(),
  480. }
  481. }
  482. // Reset resets the cache to an empty state.
  483. func (cache *mapTxCache) Reset() {
  484. cache.mtx.Lock()
  485. cache.map_ = make(map[[sha256.Size]byte]*list.Element, cache.size)
  486. cache.list.Init()
  487. cache.mtx.Unlock()
  488. }
  489. // Push adds the given tx to the cache and returns true. It returns false if tx
  490. // is already in the cache.
  491. func (cache *mapTxCache) Push(tx types.Tx) bool {
  492. cache.mtx.Lock()
  493. defer cache.mtx.Unlock()
  494. // Use the tx hash in the cache
  495. txHash := sha256.Sum256(tx)
  496. if moved, exists := cache.map_[txHash]; exists {
  497. cache.list.MoveToFront(moved)
  498. return false
  499. }
  500. if cache.list.Len() >= cache.size {
  501. popped := cache.list.Front()
  502. poppedTxHash := popped.Value.([sha256.Size]byte)
  503. delete(cache.map_, poppedTxHash)
  504. if popped != nil {
  505. cache.list.Remove(popped)
  506. }
  507. }
  508. cache.list.PushBack(txHash)
  509. cache.map_[txHash] = cache.list.Back()
  510. return true
  511. }
  512. // Remove removes the given tx from the cache.
  513. func (cache *mapTxCache) Remove(tx types.Tx) {
  514. cache.mtx.Lock()
  515. txHash := sha256.Sum256(tx)
  516. popped := cache.map_[txHash]
  517. delete(cache.map_, txHash)
  518. if popped != nil {
  519. cache.list.Remove(popped)
  520. }
  521. cache.mtx.Unlock()
  522. }
  523. type nopTxCache struct{}
  524. var _ txCache = (*nopTxCache)(nil)
  525. func (nopTxCache) Reset() {}
  526. func (nopTxCache) Push(types.Tx) bool { return true }
  527. func (nopTxCache) Remove(types.Tx) {}