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.

702 lines
20 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
abci: localClient improvements & bugfixes & pubsub Unsubscribe issues (#2748) * use READ lock/unlock in ConsensusState#GetLastHeight Refs #2721 * do not use defers when there's no need * fix peer formatting (output its address instead of the pointer) ``` [54310]: E[11-02|11:59:39.851] Connection failed @ sendRoutine module=p2p peer=0xb78f00 conn=MConn{74.207.236.148:26656} err="pong timeout" ``` https://github.com/tendermint/tendermint/issues/2721#issuecomment-435326581 * panic if peer has no state https://github.com/tendermint/tendermint/issues/2721#issuecomment-435347165 It's confusing that sometimes we check if peer has a state, but most of the times we expect it to be there 1. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/mempool/reactor.go#L138 2. https://github.com/tendermint/tendermint/blob/add79700b5fe84417538202b6c927c8cc5383672/rpc/core/consensus.go#L196 (edited) I will change everything to always assume peer has a state and panic otherwise that should help identify issues earlier * abci/localclient: extend lock on app callback App callback should be protected by lock as well (note this was already done for InitChainAsync, why not for others???). Otherwise, when we execute the block, tx might come in and call the callback in the same time we're updating it in execBlockOnProxyApp => DATA RACE Fixes #2721 Consensus state is locked ``` goroutine 113333 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00180009c, 0xc0000c7e00) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*RWMutex).RLock(0xc001800090) /usr/local/go/src/sync/rwmutex.go:50 +0x4e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).GetRoundState(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:218 +0x46 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).queryMaj23Routine(0xc0017def80, 0x11104a0, 0xc0072488f0, 0xc007248 9c0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:735 +0x16d created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusReactor).AddPeer /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/reactor.go:172 +0x236 ``` because localClient is locked ``` goroutine 1899 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0xc0000cb500) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).SetResponseCallback(0xc0001fb560, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:32 +0x33 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnConsensus).SetResponseCallback(0xc00002f750, 0xc007868540) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:57 +0x40 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.execBlockOnProxyApp(0x1104e20, 0xc002ca0ba0, 0x11092a0, 0xc00002f750, 0xc0001fe960, 0xc000bfc660, 0x110cfe0, 0xc000090330, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:230 +0x1fd github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state.(*BlockExecutor).ApplyBlock(0xc002c2a230, 0x7, 0x0, 0xc000eae880, 0x6, 0xc002e52c60, 0x16, 0x1f927, 0xc9d12, 0xc000d9d5a0, ...) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/state/execution.go:96 +0x142 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).finalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1339 +0xa3e github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryFinalizeCommit(0xc001800000, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1270 +0x451 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit.func1(0xc001800000, 0x0, 0x1f928) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1218 +0x90 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).enterCommit(0xc001800000, 0x1f928, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1247 +0x6b8 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).addVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xc003bc7ad0, 0xc003bc7b10) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1659 +0xbad github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).tryAddVote(0xc001800000, 0xc003d8dea0, 0xc000cf4cc0, 0x28, 0xf1, 0xf1, 0xf1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:1517 +0x59 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).handleMsg(0xc001800000, 0xd98200, 0xc0070dbed0, 0xc000cf4cc0, 0x28) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:660 +0x64b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).receiveRoutine(0xc001800000, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:617 +0x670 created by github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus.(*ConsensusState).OnStart /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/consensus/state.go:311 +0x132 ``` tx comes in and CheckTx is executed right when we execute the block ``` goroutine 111044 [semacquire, 309 minutes]: sync.runtime_SemacquireMutex(0xc00003363c, 0x0) /usr/local/go/src/runtime/sema.go:71 +0x3d sync.(*Mutex).Lock(0xc000033638) /usr/local/go/src/sync/mutex.go:134 +0xff github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client.(*localClient).CheckTxAsync(0xc0001fb0e0, 0xc002d94500, 0x13f, 0x280, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/abci/client/local_client.go:85 +0x47 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy.(*appConnMempool).CheckTxAsync(0xc00002f720, 0xc002d94500, 0x13f, 0x280, 0x1) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/proxy/app_conn.go:114 +0x51 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool.(*Mempool).CheckTx(0xc002d3a320, 0xc002d94500, 0x13f, 0x280, 0xc0072355f0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/mempool/mempool.go:316 +0x17b github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core.BroadcastTxSync(0xc002d94500, 0x13f, 0x280, 0x0, 0x0, 0x0) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/core/mempool.go:93 +0xb8 reflect.Value.call(0xd85560, 0x10326c0, 0x13, 0xec7b8b, 0x4, 0xc00663f180, 0x1, 0x1, 0xc00663f180, 0xc00663f188, ...) /usr/local/go/src/reflect/value.go:447 +0x449 reflect.Value.Call(0xd85560, 0x10326c0, 0x13, 0xc00663f180, 0x1, 0x1, 0x0, 0x0, 0xc005cc9344) /usr/local/go/src/reflect/value.go:308 +0xa4 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.makeHTTPHandler.func2(0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/handlers.go:269 +0x188 net/http.HandlerFunc.ServeHTTP(0xc002c81f20, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.(*ServeMux).ServeHTTP(0xc002c81b60, 0x1102060, 0xc00663f100, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2361 +0x127 github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.maxBytesHandler.ServeHTTP(0x10f8a40, 0xc002c81b60, 0xf4240, 0x1102060, 0xc00663f100, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:219 +0xcf github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server.RecoverAndLogHandler.func1(0x1103220, 0xc00121e620, 0xc0082d7900) /root/go/src/github.com/MinterTeam/minter-go-node/vendor/github.com/tendermint/tendermint/rpc/lib/server/http_server.go:192 +0x394 net/http.HandlerFunc.ServeHTTP(0xc002c06ea0, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:1964 +0x44 net/http.serverHandler.ServeHTTP(0xc001a1aa90, 0x1103220, 0xc00121e620, 0xc0082d7900) /usr/local/go/src/net/http/server.go:2741 +0xab net/http.(*conn).serve(0xc00785a3c0, 0x11041a0, 0xc000f844c0) /usr/local/go/src/net/http/server.go:1847 +0x646 created by net/http.(*Server).Serve /usr/local/go/src/net/http/server.go:2851 +0x2f5 ``` * consensus: use read lock in Receive#VoteMessage * use defer to unlock mutex because application might panic * use defer in every method of the localClient * add a changelog entry * drain channels before Unsubscribe(All) Read https://github.com/tendermint/tendermint/blob/55362ed76630f3e1ebec159a598f6a9fb5892cb1/libs/pubsub/pubsub.go#L13 for the detailed explanation of the issue. We'll need to fix it someday. Make sure to keep an eye on https://github.com/tendermint/tendermint/blob/master/docs/architecture/adr-033-pubsub.md * retry instead of panic when peer has no state in reactors other than consensus in /dump_consensus_state RPC endpoint, skip a peer with no state * rpc/core/mempool: simplify error messages * rpc/core/mempool: use time.After instead of timer also, do not log DeliverTx result (to be consistent with other memthods) * unlock before calling the callback in reqRes#SetCallback
6 years ago
9 years ago
8 years ago
8 years ago
9 years ago
8 years ago
9 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
9 years ago
8 years ago
8 years ago
9 years ago
8 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
8 years ago
9 years ago
9 years ago
  1. package mempool
  2. import (
  3. "bytes"
  4. "container/list"
  5. "crypto/sha256"
  6. "fmt"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "github.com/pkg/errors"
  11. abci "github.com/tendermint/tendermint/abci/types"
  12. cfg "github.com/tendermint/tendermint/config"
  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. "github.com/tendermint/tendermint/proxy"
  18. "github.com/tendermint/tendermint/types"
  19. )
  20. // PreCheckFunc is an optional filter executed before CheckTx and rejects
  21. // transaction if false is returned. An example would be to ensure that a
  22. // transaction doesn't exceeded the block size.
  23. type PreCheckFunc func(types.Tx) error
  24. // PostCheckFunc is an optional filter executed after CheckTx and rejects
  25. // transaction if false is returned. An example would be to ensure a
  26. // transaction doesn't require more gas than available for the block.
  27. type PostCheckFunc func(types.Tx, *abci.ResponseCheckTx) error
  28. /*
  29. The mempool pushes new txs onto the proxyAppConn.
  30. It gets a stream of (req, res) tuples from the proxy.
  31. The mempool stores good txs in a concurrent linked-list.
  32. Multiple concurrent go-routines can traverse this linked-list
  33. safely by calling .NextWait() on each element.
  34. So we have several go-routines:
  35. 1. Consensus calling Update() and Reap() synchronously
  36. 2. Many mempool reactor's peer routines calling CheckTx()
  37. 3. Many mempool reactor's peer routines traversing the txs linked list
  38. 4. Another goroutine calling GarbageCollectTxs() periodically
  39. To manage these goroutines, there are three methods of locking.
  40. 1. Mutations to the linked-list is protected by an internal mtx (CList is goroutine-safe)
  41. 2. Mutations to the linked-list elements are atomic
  42. 3. CheckTx() calls can be paused upon Update() and Reap(), protected by .proxyMtx
  43. Garbage collection of old elements from mempool.txs is handlde via
  44. the DetachPrev() call, which makes old elements not reachable by
  45. peer broadcastTxRoutine() automatically garbage collected.
  46. TODO: Better handle abci client errors. (make it automatically handle connection errors)
  47. */
  48. var (
  49. // ErrTxInCache is returned to the client if we saw tx earlier
  50. ErrTxInCache = errors.New("Tx already exists in cache")
  51. // ErrMempoolIsFull means Tendermint & an application can't handle that much load
  52. ErrMempoolIsFull = errors.New("Mempool is full")
  53. )
  54. // ErrPreCheck is returned when tx is too big
  55. type ErrPreCheck struct {
  56. Reason error
  57. }
  58. func (e ErrPreCheck) Error() string {
  59. return e.Reason.Error()
  60. }
  61. // IsPreCheckError returns true if err is due to pre check failure.
  62. func IsPreCheckError(err error) bool {
  63. _, ok := err.(ErrPreCheck)
  64. return ok
  65. }
  66. // PreCheckAminoMaxBytes checks that the size of the transaction plus the amino
  67. // overhead is smaller or equal to the expected maxBytes.
  68. func PreCheckAminoMaxBytes(maxBytes int64) PreCheckFunc {
  69. return func(tx types.Tx) error {
  70. // We have to account for the amino overhead in the tx size as well
  71. // NOTE: fieldNum = 1 as types.Block.Data contains Txs []Tx as first field.
  72. // If this field order ever changes this needs to updated here accordingly.
  73. // NOTE: if some []Tx are encoded without a parenting struct, the
  74. // fieldNum is also equal to 1.
  75. aminoOverhead := types.ComputeAminoOverhead(tx, 1)
  76. txSize := int64(len(tx)) + aminoOverhead
  77. if txSize > maxBytes {
  78. return fmt.Errorf("Tx size (including amino overhead) is too big: %d, max: %d",
  79. txSize, maxBytes)
  80. }
  81. return nil
  82. }
  83. }
  84. // PostCheckMaxGas checks that the wanted gas is smaller or equal to the passed
  85. // maxGas. Returns nil if maxGas is -1.
  86. func PostCheckMaxGas(maxGas int64) PostCheckFunc {
  87. return func(tx types.Tx, res *abci.ResponseCheckTx) error {
  88. if maxGas == -1 {
  89. return nil
  90. }
  91. if res.GasWanted > maxGas {
  92. return fmt.Errorf("gas wanted %d is greater than max gas %d",
  93. res.GasWanted, maxGas)
  94. }
  95. return nil
  96. }
  97. }
  98. // TxID is the hex encoded hash of the bytes as a types.Tx.
  99. func TxID(tx []byte) string {
  100. return fmt.Sprintf("%X", types.Tx(tx).Hash())
  101. }
  102. // Mempool is an ordered in-memory pool for transactions before they are proposed in a consensus
  103. // round. Transaction validity is checked using the CheckTx abci message before the transaction is
  104. // added to the pool. The Mempool uses a concurrent list structure for storing transactions that
  105. // can be efficiently accessed by multiple concurrent readers.
  106. type Mempool struct {
  107. config *cfg.MempoolConfig
  108. proxyMtx sync.Mutex
  109. proxyAppConn proxy.AppConnMempool
  110. txs *clist.CList // concurrent linked-list of good txs
  111. height int64 // the last block Update()'d to
  112. rechecking int32 // for re-checking filtered txs on Update()
  113. recheckCursor *clist.CElement // next expected response
  114. recheckEnd *clist.CElement // re-checking stops here
  115. notifiedTxsAvailable bool
  116. txsAvailable chan struct{} // fires once for each height, when the mempool is not empty
  117. preCheck PreCheckFunc
  118. postCheck PostCheckFunc
  119. // Keep a cache of already-seen txs.
  120. // This reduces the pressure on the proxyApp.
  121. cache txCache
  122. // A log of mempool txs
  123. wal *auto.AutoFile
  124. logger log.Logger
  125. metrics *Metrics
  126. }
  127. // MempoolOption sets an optional parameter on the Mempool.
  128. type MempoolOption func(*Mempool)
  129. // NewMempool returns a new Mempool with the given configuration and connection to an application.
  130. func NewMempool(
  131. config *cfg.MempoolConfig,
  132. proxyAppConn proxy.AppConnMempool,
  133. height int64,
  134. options ...MempoolOption,
  135. ) *Mempool {
  136. mempool := &Mempool{
  137. config: config,
  138. proxyAppConn: proxyAppConn,
  139. txs: clist.New(),
  140. height: height,
  141. rechecking: 0,
  142. recheckCursor: nil,
  143. recheckEnd: nil,
  144. logger: log.NewNopLogger(),
  145. metrics: NopMetrics(),
  146. }
  147. if config.CacheSize > 0 {
  148. mempool.cache = newMapTxCache(config.CacheSize)
  149. } else {
  150. mempool.cache = nopTxCache{}
  151. }
  152. proxyAppConn.SetResponseCallback(mempool.resCb)
  153. for _, option := range options {
  154. option(mempool)
  155. }
  156. return mempool
  157. }
  158. // EnableTxsAvailable initializes the TxsAvailable channel,
  159. // ensuring it will trigger once every height when transactions are available.
  160. // NOTE: not thread safe - should only be called once, on startup
  161. func (mem *Mempool) EnableTxsAvailable() {
  162. mem.txsAvailable = make(chan struct{}, 1)
  163. }
  164. // SetLogger sets the Logger.
  165. func (mem *Mempool) SetLogger(l log.Logger) {
  166. mem.logger = l
  167. }
  168. // WithPreCheck sets a filter for the mempool to reject a tx if f(tx) returns
  169. // false. This is ran before CheckTx.
  170. func WithPreCheck(f PreCheckFunc) MempoolOption {
  171. return func(mem *Mempool) { mem.preCheck = f }
  172. }
  173. // WithPostCheck sets a filter for the mempool to reject a tx if f(tx) returns
  174. // false. This is ran after CheckTx.
  175. func WithPostCheck(f PostCheckFunc) MempoolOption {
  176. return func(mem *Mempool) { mem.postCheck = f }
  177. }
  178. // WithMetrics sets the metrics.
  179. func WithMetrics(metrics *Metrics) MempoolOption {
  180. return func(mem *Mempool) { mem.metrics = metrics }
  181. }
  182. // InitWAL creates a directory for the WAL file and opens a file itself.
  183. //
  184. // *panics* if can't create directory or open file.
  185. // *not thread safe*
  186. func (mem *Mempool) InitWAL() {
  187. walDir := mem.config.WalDir()
  188. err := cmn.EnsureDir(walDir, 0700)
  189. if err != nil {
  190. panic(errors.Wrap(err, "Error ensuring Mempool WAL dir"))
  191. }
  192. af, err := auto.OpenAutoFile(walDir + "/wal")
  193. if err != nil {
  194. panic(errors.Wrap(err, "Error opening Mempool WAL file"))
  195. }
  196. mem.wal = af
  197. }
  198. // CloseWAL closes and discards the underlying WAL file.
  199. // Any further writes will not be relayed to disk.
  200. func (mem *Mempool) CloseWAL() {
  201. mem.proxyMtx.Lock()
  202. defer mem.proxyMtx.Unlock()
  203. if err := mem.wal.Close(); err != nil {
  204. mem.logger.Error("Error closing WAL", "err", err)
  205. }
  206. mem.wal = nil
  207. }
  208. // Lock locks the mempool. The consensus must be able to hold lock to safely update.
  209. func (mem *Mempool) Lock() {
  210. mem.proxyMtx.Lock()
  211. }
  212. // Unlock unlocks the mempool.
  213. func (mem *Mempool) Unlock() {
  214. mem.proxyMtx.Unlock()
  215. }
  216. // Size returns the number of transactions in the mempool.
  217. func (mem *Mempool) Size() int {
  218. return mem.txs.Len()
  219. }
  220. // Flushes the mempool connection to ensure async resCb calls are done e.g.
  221. // from CheckTx.
  222. func (mem *Mempool) FlushAppConn() error {
  223. return mem.proxyAppConn.FlushSync()
  224. }
  225. // Flush removes all transactions from the mempool and cache
  226. func (mem *Mempool) Flush() {
  227. mem.proxyMtx.Lock()
  228. defer mem.proxyMtx.Unlock()
  229. mem.cache.Reset()
  230. for e := mem.txs.Front(); e != nil; e = e.Next() {
  231. mem.txs.Remove(e)
  232. e.DetachPrev()
  233. }
  234. }
  235. // TxsFront returns the first transaction in the ordered list for peer
  236. // goroutines to call .NextWait() on.
  237. func (mem *Mempool) TxsFront() *clist.CElement {
  238. return mem.txs.Front()
  239. }
  240. // TxsWaitChan returns a channel to wait on transactions. It will be closed
  241. // once the mempool is not empty (ie. the internal `mem.txs` has at least one
  242. // element)
  243. func (mem *Mempool) TxsWaitChan() <-chan struct{} {
  244. return mem.txs.WaitChan()
  245. }
  246. // CheckTx executes a new transaction against the application to determine its validity
  247. // and whether it should be added to the mempool.
  248. // It blocks if we're waiting on Update() or Reap().
  249. // cb: A callback from the CheckTx command.
  250. // It gets called from another goroutine.
  251. // CONTRACT: Either cb will get called, or err returned.
  252. func (mem *Mempool) CheckTx(tx types.Tx, cb func(*abci.Response)) (err error) {
  253. mem.proxyMtx.Lock()
  254. // use defer to unlock mutex because application (*local client*) might panic
  255. defer mem.proxyMtx.Unlock()
  256. if mem.Size() >= mem.config.Size {
  257. return ErrMempoolIsFull
  258. }
  259. if mem.preCheck != nil {
  260. if err := mem.preCheck(tx); err != nil {
  261. return ErrPreCheck{err}
  262. }
  263. }
  264. // CACHE
  265. if !mem.cache.Push(tx) {
  266. return ErrTxInCache
  267. }
  268. // END CACHE
  269. // WAL
  270. if mem.wal != nil {
  271. // TODO: Notify administrators when WAL fails
  272. _, err := mem.wal.Write([]byte(tx))
  273. if err != nil {
  274. mem.logger.Error("Error writing to WAL", "err", err)
  275. }
  276. _, err = mem.wal.Write([]byte("\n"))
  277. if err != nil {
  278. mem.logger.Error("Error writing to WAL", "err", err)
  279. }
  280. }
  281. // END WAL
  282. // NOTE: proxyAppConn may error if tx buffer is full
  283. if err = mem.proxyAppConn.Error(); err != nil {
  284. return err
  285. }
  286. reqRes := mem.proxyAppConn.CheckTxAsync(tx)
  287. if cb != nil {
  288. reqRes.SetCallback(cb)
  289. }
  290. return nil
  291. }
  292. // ABCI callback function
  293. func (mem *Mempool) resCb(req *abci.Request, res *abci.Response) {
  294. if mem.recheckCursor == nil {
  295. mem.resCbNormal(req, res)
  296. } else {
  297. mem.metrics.RecheckTimes.Add(1)
  298. mem.resCbRecheck(req, res)
  299. }
  300. mem.metrics.Size.Set(float64(mem.Size()))
  301. }
  302. func (mem *Mempool) resCbNormal(req *abci.Request, res *abci.Response) {
  303. switch r := res.Value.(type) {
  304. case *abci.Response_CheckTx:
  305. tx := req.GetCheckTx().Tx
  306. var postCheckErr error
  307. if mem.postCheck != nil {
  308. postCheckErr = mem.postCheck(tx, r.CheckTx)
  309. }
  310. if (r.CheckTx.Code == abci.CodeTypeOK) && postCheckErr == nil {
  311. memTx := &mempoolTx{
  312. height: mem.height,
  313. gasWanted: r.CheckTx.GasWanted,
  314. tx: tx,
  315. }
  316. mem.txs.PushBack(memTx)
  317. mem.logger.Info("Added good transaction",
  318. "tx", TxID(tx),
  319. "res", r,
  320. "height", memTx.height,
  321. "total", mem.Size(),
  322. )
  323. mem.metrics.TxSizeBytes.Observe(float64(len(tx)))
  324. mem.notifyTxsAvailable()
  325. } else {
  326. // ignore bad transaction
  327. mem.logger.Info("Rejected bad transaction", "tx", TxID(tx), "res", r, "err", postCheckErr)
  328. mem.metrics.FailedTxs.Add(1)
  329. // remove from cache (it might be good later)
  330. mem.cache.Remove(tx)
  331. }
  332. default:
  333. // ignore other messages
  334. }
  335. }
  336. func (mem *Mempool) resCbRecheck(req *abci.Request, res *abci.Response) {
  337. switch r := res.Value.(type) {
  338. case *abci.Response_CheckTx:
  339. tx := req.GetCheckTx().Tx
  340. memTx := mem.recheckCursor.Value.(*mempoolTx)
  341. if !bytes.Equal(req.GetCheckTx().Tx, memTx.tx) {
  342. cmn.PanicSanity(
  343. fmt.Sprintf(
  344. "Unexpected tx response from proxy during recheck\nExpected %X, got %X",
  345. r.CheckTx.Data,
  346. memTx.tx,
  347. ),
  348. )
  349. }
  350. var postCheckErr error
  351. if mem.postCheck != nil {
  352. postCheckErr = mem.postCheck(tx, r.CheckTx)
  353. }
  354. if (r.CheckTx.Code == abci.CodeTypeOK) && postCheckErr == nil {
  355. // Good, nothing to do.
  356. } else {
  357. // Tx became invalidated due to newly committed block.
  358. mem.logger.Info("Tx is no longer valid", "tx", TxID(tx), "res", r, "err", postCheckErr)
  359. mem.txs.Remove(mem.recheckCursor)
  360. mem.recheckCursor.DetachPrev()
  361. // remove from cache (it might be good later)
  362. mem.cache.Remove(tx)
  363. }
  364. if mem.recheckCursor == mem.recheckEnd {
  365. mem.recheckCursor = nil
  366. } else {
  367. mem.recheckCursor = mem.recheckCursor.Next()
  368. }
  369. if mem.recheckCursor == nil {
  370. // Done!
  371. atomic.StoreInt32(&mem.rechecking, 0)
  372. mem.logger.Info("Done rechecking txs")
  373. // incase the recheck removed all txs
  374. if mem.Size() > 0 {
  375. mem.notifyTxsAvailable()
  376. }
  377. }
  378. default:
  379. // ignore other messages
  380. }
  381. }
  382. // TxsAvailable returns a channel which fires once for every height,
  383. // and only when transactions are available in the mempool.
  384. // NOTE: the returned channel may be nil if EnableTxsAvailable was not called.
  385. func (mem *Mempool) TxsAvailable() <-chan struct{} {
  386. return mem.txsAvailable
  387. }
  388. func (mem *Mempool) notifyTxsAvailable() {
  389. if mem.Size() == 0 {
  390. panic("notified txs available but mempool is empty!")
  391. }
  392. if mem.txsAvailable != nil && !mem.notifiedTxsAvailable {
  393. // channel cap is 1, so this will send once
  394. mem.notifiedTxsAvailable = true
  395. select {
  396. case mem.txsAvailable <- struct{}{}:
  397. default:
  398. }
  399. }
  400. }
  401. // ReapMaxBytesMaxGas reaps transactions from the mempool up to maxBytes bytes total
  402. // with the condition that the total gasWanted must be less than maxGas.
  403. // If both maxes are negative, there is no cap on the size of all returned
  404. // transactions (~ all available transactions).
  405. func (mem *Mempool) ReapMaxBytesMaxGas(maxBytes, maxGas int64) types.Txs {
  406. mem.proxyMtx.Lock()
  407. defer mem.proxyMtx.Unlock()
  408. for atomic.LoadInt32(&mem.rechecking) > 0 {
  409. // TODO: Something better?
  410. time.Sleep(time.Millisecond * 10)
  411. }
  412. var totalBytes int64
  413. var totalGas int64
  414. // TODO: we will get a performance boost if we have a good estimate of avg
  415. // size per tx, and set the initial capacity based off of that.
  416. // txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), max/mem.avgTxSize))
  417. txs := make([]types.Tx, 0, mem.txs.Len())
  418. for e := mem.txs.Front(); e != nil; e = e.Next() {
  419. memTx := e.Value.(*mempoolTx)
  420. // Check total size requirement
  421. aminoOverhead := types.ComputeAminoOverhead(memTx.tx, 1)
  422. if maxBytes > -1 && totalBytes+int64(len(memTx.tx))+aminoOverhead > maxBytes {
  423. return txs
  424. }
  425. totalBytes += int64(len(memTx.tx)) + aminoOverhead
  426. // Check total gas requirement
  427. if maxGas > -1 && totalGas+memTx.gasWanted > maxGas {
  428. return txs
  429. }
  430. totalGas += memTx.gasWanted
  431. txs = append(txs, memTx.tx)
  432. }
  433. return txs
  434. }
  435. // ReapMaxTxs reaps up to max transactions from the mempool.
  436. // If max is negative, there is no cap on the size of all returned
  437. // transactions (~ all available transactions).
  438. func (mem *Mempool) ReapMaxTxs(max int) types.Txs {
  439. mem.proxyMtx.Lock()
  440. defer mem.proxyMtx.Unlock()
  441. if max < 0 {
  442. max = mem.txs.Len()
  443. }
  444. for atomic.LoadInt32(&mem.rechecking) > 0 {
  445. // TODO: Something better?
  446. time.Sleep(time.Millisecond * 10)
  447. }
  448. txs := make([]types.Tx, 0, cmn.MinInt(mem.txs.Len(), max))
  449. for e := mem.txs.Front(); e != nil && len(txs) <= max; e = e.Next() {
  450. memTx := e.Value.(*mempoolTx)
  451. txs = append(txs, memTx.tx)
  452. }
  453. return txs
  454. }
  455. // Update informs the mempool that the given txs were committed and can be discarded.
  456. // NOTE: this should be called *after* block is committed by consensus.
  457. // NOTE: unsafe; Lock/Unlock must be managed by caller
  458. func (mem *Mempool) Update(
  459. height int64,
  460. txs types.Txs,
  461. preCheck PreCheckFunc,
  462. postCheck PostCheckFunc,
  463. ) error {
  464. // Set height
  465. mem.height = height
  466. mem.notifiedTxsAvailable = false
  467. if preCheck != nil {
  468. mem.preCheck = preCheck
  469. }
  470. if postCheck != nil {
  471. mem.postCheck = postCheck
  472. }
  473. // Add committed transactions to cache (if missing).
  474. for _, tx := range txs {
  475. _ = mem.cache.Push(tx)
  476. }
  477. // Remove committed transactions.
  478. txsLeft := mem.removeTxs(txs)
  479. // Recheck mempool txs if any txs were committed in the block
  480. if mem.config.Recheck && len(txsLeft) > 0 {
  481. mem.logger.Info("Recheck txs", "numtxs", len(txsLeft), "height", height)
  482. mem.recheckTxs(txsLeft)
  483. // At this point, mem.txs are being rechecked.
  484. // mem.recheckCursor re-scans mem.txs and possibly removes some txs.
  485. // Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
  486. }
  487. // Update metrics
  488. mem.metrics.Size.Set(float64(mem.Size()))
  489. return nil
  490. }
  491. func (mem *Mempool) removeTxs(txs types.Txs) []types.Tx {
  492. // Build a map for faster lookups.
  493. txsMap := make(map[string]struct{}, len(txs))
  494. for _, tx := range txs {
  495. txsMap[string(tx)] = struct{}{}
  496. }
  497. txsLeft := make([]types.Tx, 0, mem.txs.Len())
  498. for e := mem.txs.Front(); e != nil; e = e.Next() {
  499. memTx := e.Value.(*mempoolTx)
  500. // Remove the tx if it's already in a block.
  501. if _, ok := txsMap[string(memTx.tx)]; ok {
  502. // remove from clist
  503. mem.txs.Remove(e)
  504. e.DetachPrev()
  505. // NOTE: we don't remove committed txs from the cache.
  506. continue
  507. }
  508. txsLeft = append(txsLeft, memTx.tx)
  509. }
  510. return txsLeft
  511. }
  512. // NOTE: pass in txs because mem.txs can mutate concurrently.
  513. func (mem *Mempool) recheckTxs(txs []types.Tx) {
  514. if len(txs) == 0 {
  515. return
  516. }
  517. atomic.StoreInt32(&mem.rechecking, 1)
  518. mem.recheckCursor = mem.txs.Front()
  519. mem.recheckEnd = mem.txs.Back()
  520. // Push txs to proxyAppConn
  521. // NOTE: resCb() may be called concurrently.
  522. for _, tx := range txs {
  523. mem.proxyAppConn.CheckTxAsync(tx)
  524. }
  525. mem.proxyAppConn.FlushAsync()
  526. }
  527. //--------------------------------------------------------------------------------
  528. // mempoolTx is a transaction that successfully ran
  529. type mempoolTx struct {
  530. height int64 // height that this tx had been validated in
  531. gasWanted int64 // amount of gas this tx states it will require
  532. tx types.Tx //
  533. }
  534. // Height returns the height for this transaction
  535. func (memTx *mempoolTx) Height() int64 {
  536. return atomic.LoadInt64(&memTx.height)
  537. }
  538. //--------------------------------------------------------------------------------
  539. type txCache interface {
  540. Reset()
  541. Push(tx types.Tx) bool
  542. Remove(tx types.Tx)
  543. }
  544. // mapTxCache maintains a cache of transactions. This only stores
  545. // the hash of the tx, due to memory concerns.
  546. type mapTxCache struct {
  547. mtx sync.Mutex
  548. size int
  549. map_ map[[sha256.Size]byte]*list.Element
  550. list *list.List // to remove oldest tx when cache gets too big
  551. }
  552. var _ txCache = (*mapTxCache)(nil)
  553. // newMapTxCache returns a new mapTxCache.
  554. func newMapTxCache(cacheSize int) *mapTxCache {
  555. return &mapTxCache{
  556. size: cacheSize,
  557. map_: make(map[[sha256.Size]byte]*list.Element, cacheSize),
  558. list: list.New(),
  559. }
  560. }
  561. // Reset resets the cache to an empty state.
  562. func (cache *mapTxCache) Reset() {
  563. cache.mtx.Lock()
  564. cache.map_ = make(map[[sha256.Size]byte]*list.Element, cache.size)
  565. cache.list.Init()
  566. cache.mtx.Unlock()
  567. }
  568. // Push adds the given tx to the cache and returns true. It returns false if tx
  569. // is already in the cache.
  570. func (cache *mapTxCache) Push(tx types.Tx) bool {
  571. cache.mtx.Lock()
  572. defer cache.mtx.Unlock()
  573. // Use the tx hash in the cache
  574. txHash := sha256.Sum256(tx)
  575. if moved, exists := cache.map_[txHash]; exists {
  576. cache.list.MoveToFront(moved)
  577. return false
  578. }
  579. if cache.list.Len() >= cache.size {
  580. popped := cache.list.Front()
  581. poppedTxHash := popped.Value.([sha256.Size]byte)
  582. delete(cache.map_, poppedTxHash)
  583. if popped != nil {
  584. cache.list.Remove(popped)
  585. }
  586. }
  587. cache.list.PushBack(txHash)
  588. cache.map_[txHash] = cache.list.Back()
  589. return true
  590. }
  591. // Remove removes the given tx from the cache.
  592. func (cache *mapTxCache) Remove(tx types.Tx) {
  593. cache.mtx.Lock()
  594. txHash := sha256.Sum256(tx)
  595. popped := cache.map_[txHash]
  596. delete(cache.map_, txHash)
  597. if popped != nil {
  598. cache.list.Remove(popped)
  599. }
  600. cache.mtx.Unlock()
  601. }
  602. type nopTxCache struct{}
  603. var _ txCache = (*nopTxCache)(nil)
  604. func (nopTxCache) Reset() {}
  605. func (nopTxCache) Push(types.Tx) bool { return true }
  606. func (nopTxCache) Remove(types.Tx) {}