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.

308 lines
9.8 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
8 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
8 years ago
  1. package consensus
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. dbm "github.com/tendermint/tm-db"
  12. "github.com/tendermint/tendermint/abci/example/code"
  13. abci "github.com/tendermint/tendermint/abci/types"
  14. "github.com/tendermint/tendermint/internal/mempool"
  15. sm "github.com/tendermint/tendermint/internal/state"
  16. "github.com/tendermint/tendermint/internal/store"
  17. "github.com/tendermint/tendermint/libs/log"
  18. "github.com/tendermint/tendermint/types"
  19. )
  20. // for testing
  21. func assertMempool(t *testing.T, txn txNotifier) mempool.Mempool {
  22. t.Helper()
  23. mp, ok := txn.(mempool.Mempool)
  24. require.True(t, ok)
  25. return mp
  26. }
  27. func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
  28. ctx, cancel := context.WithCancel(context.Background())
  29. defer cancel()
  30. baseConfig := configSetup(t)
  31. config, err := ResetConfig("consensus_mempool_txs_available_test")
  32. require.NoError(t, err)
  33. t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
  34. config.Consensus.CreateEmptyBlocks = false
  35. state, privVals := makeGenesisState(ctx, t, baseConfig, genesisStateArgs{
  36. Validators: 1,
  37. Power: 10})
  38. cs := newStateWithConfig(ctx, t, log.TestingLogger(), config, state, privVals[0], NewCounterApplication())
  39. assertMempool(t, cs.txNotifier).EnableTxsAvailable()
  40. height, round := cs.Height, cs.Round
  41. newBlockCh := subscribe(ctx, t, cs.eventBus, types.EventQueryNewBlock)
  42. startTestRound(ctx, cs, height, round)
  43. ensureNewEventOnChannel(t, newBlockCh) // first block gets committed
  44. ensureNoNewEventOnChannel(t, newBlockCh)
  45. deliverTxsRange(ctx, t, cs, 0, 1)
  46. ensureNewEventOnChannel(t, newBlockCh) // commit txs
  47. ensureNewEventOnChannel(t, newBlockCh) // commit updated app hash
  48. ensureNoNewEventOnChannel(t, newBlockCh)
  49. }
  50. func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
  51. baseConfig := configSetup(t)
  52. ctx, cancel := context.WithCancel(context.Background())
  53. defer cancel()
  54. config, err := ResetConfig("consensus_mempool_txs_available_test")
  55. require.NoError(t, err)
  56. t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
  57. config.Consensus.CreateEmptyBlocksInterval = ensureTimeout
  58. state, privVals := makeGenesisState(ctx, t, baseConfig, genesisStateArgs{
  59. Validators: 1,
  60. Power: 10})
  61. cs := newStateWithConfig(ctx, t, log.TestingLogger(), config, state, privVals[0], NewCounterApplication())
  62. assertMempool(t, cs.txNotifier).EnableTxsAvailable()
  63. newBlockCh := subscribe(ctx, t, cs.eventBus, types.EventQueryNewBlock)
  64. startTestRound(ctx, cs, cs.Height, cs.Round)
  65. ensureNewEventOnChannel(t, newBlockCh) // first block gets committed
  66. ensureNoNewEventOnChannel(t, newBlockCh) // then we dont make a block ...
  67. ensureNewEventOnChannel(t, newBlockCh) // until the CreateEmptyBlocksInterval has passed
  68. }
  69. func TestMempoolProgressInHigherRound(t *testing.T) {
  70. baseConfig := configSetup(t)
  71. ctx, cancel := context.WithCancel(context.Background())
  72. defer cancel()
  73. config, err := ResetConfig("consensus_mempool_txs_available_test")
  74. require.NoError(t, err)
  75. t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
  76. config.Consensus.CreateEmptyBlocks = false
  77. state, privVals := makeGenesisState(ctx, t, baseConfig, genesisStateArgs{
  78. Validators: 1,
  79. Power: 10})
  80. cs := newStateWithConfig(ctx, t, log.TestingLogger(), config, state, privVals[0], NewCounterApplication())
  81. assertMempool(t, cs.txNotifier).EnableTxsAvailable()
  82. height, round := cs.Height, cs.Round
  83. newBlockCh := subscribe(ctx, t, cs.eventBus, types.EventQueryNewBlock)
  84. newRoundCh := subscribe(ctx, t, cs.eventBus, types.EventQueryNewRound)
  85. timeoutCh := subscribe(ctx, t, cs.eventBus, types.EventQueryTimeoutPropose)
  86. cs.setProposal = func(proposal *types.Proposal, recvTime time.Time) error {
  87. if cs.Height == 2 && cs.Round == 0 {
  88. // dont set the proposal in round 0 so we timeout and
  89. // go to next round
  90. return nil
  91. }
  92. return cs.defaultSetProposal(proposal, recvTime)
  93. }
  94. startTestRound(ctx, cs, height, round)
  95. ensureNewRound(t, newRoundCh, height, round) // first round at first height
  96. ensureNewEventOnChannel(t, newBlockCh) // first block gets committed
  97. height++ // moving to the next height
  98. round = 0
  99. ensureNewRound(t, newRoundCh, height, round) // first round at next height
  100. deliverTxsRange(ctx, t, cs, 0, 1) // we deliver txs, but dont set a proposal so we get the next round
  101. ensureNewTimeout(t, timeoutCh, height, round, cs.config.TimeoutPropose.Nanoseconds())
  102. round++ // moving to the next round
  103. ensureNewRound(t, newRoundCh, height, round) // wait for the next round
  104. ensureNewEventOnChannel(t, newBlockCh) // now we can commit the block
  105. }
  106. func deliverTxsRange(ctx context.Context, t *testing.T, cs *State, start, end int) {
  107. t.Helper()
  108. // Deliver some txs.
  109. for i := start; i < end; i++ {
  110. txBytes := make([]byte, 8)
  111. binary.BigEndian.PutUint64(txBytes, uint64(i))
  112. err := assertMempool(t, cs.txNotifier).CheckTx(ctx, txBytes, nil, mempool.TxInfo{})
  113. require.NoError(t, err, "error after checkTx")
  114. }
  115. }
  116. func TestMempoolTxConcurrentWithCommit(t *testing.T) {
  117. ctx, cancel := context.WithCancel(context.Background())
  118. defer cancel()
  119. config := configSetup(t)
  120. logger := log.TestingLogger()
  121. state, privVals := makeGenesisState(ctx, t, config, genesisStateArgs{
  122. Validators: 1,
  123. Power: 10})
  124. stateStore := sm.NewStore(dbm.NewMemDB())
  125. blockStore := store.NewBlockStore(dbm.NewMemDB())
  126. cs := newStateWithConfigAndBlockStore(
  127. ctx,
  128. t,
  129. logger, config, state, privVals[0], NewCounterApplication(), blockStore)
  130. err := stateStore.Save(state)
  131. require.NoError(t, err)
  132. newBlockHeaderCh := subscribe(ctx, t, cs.eventBus, types.EventQueryNewBlockHeader)
  133. const numTxs int64 = 3000
  134. go deliverTxsRange(ctx, t, cs, 0, int(numTxs))
  135. startTestRound(ctx, cs, cs.Height, cs.Round)
  136. for n := int64(0); n < numTxs; {
  137. select {
  138. case msg := <-newBlockHeaderCh:
  139. headerEvent := msg.Data().(types.EventDataNewBlockHeader)
  140. n += headerEvent.NumTxs
  141. case <-time.After(30 * time.Second):
  142. t.Fatal("Timed out waiting 30s to commit blocks with transactions")
  143. }
  144. }
  145. }
  146. func TestMempoolRmBadTx(t *testing.T) {
  147. config := configSetup(t)
  148. ctx, cancel := context.WithCancel(context.Background())
  149. defer cancel()
  150. state, privVals := makeGenesisState(ctx, t, config, genesisStateArgs{
  151. Validators: 1,
  152. Power: 10})
  153. app := NewCounterApplication()
  154. stateStore := sm.NewStore(dbm.NewMemDB())
  155. blockStore := store.NewBlockStore(dbm.NewMemDB())
  156. cs := newStateWithConfigAndBlockStore(ctx, t, log.TestingLogger(), config, state, privVals[0], app, blockStore)
  157. err := stateStore.Save(state)
  158. require.NoError(t, err)
  159. // increment the counter by 1
  160. txBytes := make([]byte, 8)
  161. binary.BigEndian.PutUint64(txBytes, uint64(0))
  162. resDeliver := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
  163. assert.False(t, resDeliver.IsErr(), fmt.Sprintf("expected no error. got %v", resDeliver))
  164. resCommit := app.Commit()
  165. assert.True(t, len(resCommit.Data) > 0)
  166. emptyMempoolCh := make(chan struct{})
  167. checkTxRespCh := make(chan struct{})
  168. go func() {
  169. // Try to send the tx through the mempool.
  170. // CheckTx should not err, but the app should return a bad abci code
  171. // and the tx should get removed from the pool
  172. err := assertMempool(t, cs.txNotifier).CheckTx(ctx, txBytes, func(r *abci.Response) {
  173. if r.GetCheckTx().Code != code.CodeTypeBadNonce {
  174. t.Errorf("expected checktx to return bad nonce, got %v", r)
  175. return
  176. }
  177. checkTxRespCh <- struct{}{}
  178. }, mempool.TxInfo{})
  179. if err != nil {
  180. t.Errorf("error after CheckTx: %w", err)
  181. return
  182. }
  183. // check for the tx
  184. for {
  185. txs := assertMempool(t, cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), -1)
  186. if len(txs) == 0 {
  187. emptyMempoolCh <- struct{}{}
  188. return
  189. }
  190. time.Sleep(10 * time.Millisecond)
  191. }
  192. }()
  193. // Wait until the tx returns
  194. ticker := time.After(time.Second * 5)
  195. select {
  196. case <-checkTxRespCh:
  197. // success
  198. case <-ticker:
  199. t.Errorf("timed out waiting for tx to return")
  200. return
  201. }
  202. // Wait until the tx is removed
  203. ticker = time.After(time.Second * 5)
  204. select {
  205. case <-emptyMempoolCh:
  206. // success
  207. case <-ticker:
  208. t.Errorf("timed out waiting for tx to be removed")
  209. return
  210. }
  211. }
  212. // CounterApplication that maintains a mempool state and resets it upon commit
  213. type CounterApplication struct {
  214. abci.BaseApplication
  215. txCount int
  216. mempoolTxCount int
  217. }
  218. func NewCounterApplication() *CounterApplication {
  219. return &CounterApplication{}
  220. }
  221. func (app *CounterApplication) Info(req abci.RequestInfo) abci.ResponseInfo {
  222. return abci.ResponseInfo{Data: fmt.Sprintf("txs:%v", app.txCount)}
  223. }
  224. func (app *CounterApplication) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
  225. txValue := txAsUint64(req.Tx)
  226. if txValue != uint64(app.txCount) {
  227. return abci.ResponseDeliverTx{
  228. Code: code.CodeTypeBadNonce,
  229. Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
  230. }
  231. app.txCount++
  232. return abci.ResponseDeliverTx{Code: code.CodeTypeOK}
  233. }
  234. func (app *CounterApplication) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
  235. txValue := txAsUint64(req.Tx)
  236. if txValue != uint64(app.mempoolTxCount) {
  237. return abci.ResponseCheckTx{
  238. Code: code.CodeTypeBadNonce,
  239. Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.mempoolTxCount, txValue)}
  240. }
  241. app.mempoolTxCount++
  242. return abci.ResponseCheckTx{Code: code.CodeTypeOK}
  243. }
  244. func txAsUint64(tx []byte) uint64 {
  245. tx8 := make([]byte, 8)
  246. copy(tx8[len(tx8)-len(tx):], tx)
  247. return binary.BigEndian.Uint64(tx8)
  248. }
  249. func (app *CounterApplication) Commit() abci.ResponseCommit {
  250. app.mempoolTxCount = app.txCount
  251. if app.txCount == 0 {
  252. return abci.ResponseCommit{}
  253. }
  254. hash := make([]byte, 8)
  255. binary.BigEndian.PutUint64(hash, uint64(app.txCount))
  256. return abci.ResponseCommit{Data: hash}
  257. }
  258. func (app *CounterApplication) PrepareProposal(
  259. req abci.RequestPrepareProposal) abci.ResponsePrepareProposal {
  260. return abci.ResponsePrepareProposal{BlockData: req.BlockData}
  261. }