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.

274 lines
8.7 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
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
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 years ago
lint: Enable Golint (#4212) * Fix many golint errors * Fix golint errors in the 'lite' package * Don't export Pool.store * Fix typo * Revert unwanted changes * Fix errors in counter package * Fix linter errors in kvstore package * Fix linter error in example package * Fix error in tests package * Fix linter errors in v2 package * Fix linter errors in consensus package * Fix linter errors in evidence package * Fix linter error in fail package * Fix linter errors in query package * Fix linter errors in core package * Fix linter errors in node package * Fix linter errors in mempool package * Fix linter error in conn package * Fix linter errors in pex package * Rename PEXReactor export to Reactor * Fix linter errors in trust package * Fix linter errors in upnp package * Fix linter errors in p2p package * Fix linter errors in proxy package * Fix linter errors in mock_test package * Fix linter error in client_test package * Fix linter errors in coretypes package * Fix linter errors in coregrpc package * Fix linter errors in rpcserver package * Fix linter errors in rpctypes package * Fix linter errors in rpctest package * Fix linter error in json2wal script * Fix linter error in wal2json script * Fix linter errors in kv package * Fix linter error in state package * Fix linter error in grpc_client * Fix linter errors in types package * Fix linter error in version package * Fix remaining errors * Address review comments * Fix broken tests * Reconcile package coregrpc * Fix golangci bot error * Fix new golint errors * Fix broken reference * Enable golint linter * minor changes to bring golint into line * fix failing test * fix pex reactor naming * address PR comments
5 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
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/types"
  18. )
  19. // for testing
  20. func assertMempool(txn txNotifier) mempool.Mempool {
  21. return txn.(mempool.Mempool)
  22. }
  23. func TestMempoolNoProgressUntilTxsAvailable(t *testing.T) {
  24. baseConfig := configSetup(t)
  25. config, err := ResetConfig("consensus_mempool_txs_available_test")
  26. require.NoError(t, err)
  27. t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
  28. config.Consensus.CreateEmptyBlocks = false
  29. state, privVals := randGenesisState(baseConfig, 1, false, 10)
  30. cs := newStateWithConfig(config, state, privVals[0], NewCounterApplication())
  31. assertMempool(cs.txNotifier).EnableTxsAvailable()
  32. height, round := cs.Height, cs.Round
  33. newBlockCh := subscribe(t, cs.eventBus, types.EventQueryNewBlock)
  34. startTestRound(cs, height, round)
  35. ensureNewEventOnChannel(newBlockCh) // first block gets committed
  36. ensureNoNewEventOnChannel(newBlockCh)
  37. deliverTxsRange(cs, 0, 1)
  38. ensureNewEventOnChannel(newBlockCh) // commit txs
  39. ensureNewEventOnChannel(newBlockCh) // commit updated app hash
  40. ensureNoNewEventOnChannel(newBlockCh)
  41. }
  42. func TestMempoolProgressAfterCreateEmptyBlocksInterval(t *testing.T) {
  43. baseConfig := configSetup(t)
  44. config, err := ResetConfig("consensus_mempool_txs_available_test")
  45. require.NoError(t, err)
  46. t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
  47. config.Consensus.CreateEmptyBlocksInterval = ensureTimeout
  48. state, privVals := randGenesisState(baseConfig, 1, false, 10)
  49. cs := newStateWithConfig(config, state, privVals[0], NewCounterApplication())
  50. assertMempool(cs.txNotifier).EnableTxsAvailable()
  51. newBlockCh := subscribe(t, cs.eventBus, types.EventQueryNewBlock)
  52. startTestRound(cs, cs.Height, cs.Round)
  53. ensureNewEventOnChannel(newBlockCh) // first block gets committed
  54. ensureNoNewEventOnChannel(newBlockCh) // then we dont make a block ...
  55. ensureNewEventOnChannel(newBlockCh) // until the CreateEmptyBlocksInterval has passed
  56. }
  57. func TestMempoolProgressInHigherRound(t *testing.T) {
  58. baseConfig := configSetup(t)
  59. config, err := ResetConfig("consensus_mempool_txs_available_test")
  60. require.NoError(t, err)
  61. t.Cleanup(func() { _ = os.RemoveAll(config.RootDir) })
  62. config.Consensus.CreateEmptyBlocks = false
  63. state, privVals := randGenesisState(baseConfig, 1, false, 10)
  64. cs := newStateWithConfig(config, state, privVals[0], NewCounterApplication())
  65. assertMempool(cs.txNotifier).EnableTxsAvailable()
  66. height, round := cs.Height, cs.Round
  67. newBlockCh := subscribe(t, cs.eventBus, types.EventQueryNewBlock)
  68. newRoundCh := subscribe(t, cs.eventBus, types.EventQueryNewRound)
  69. timeoutCh := subscribe(t, cs.eventBus, types.EventQueryTimeoutPropose)
  70. cs.setProposal = func(proposal *types.Proposal) error {
  71. if cs.Height == 2 && cs.Round == 0 {
  72. // dont set the proposal in round 0 so we timeout and
  73. // go to next round
  74. cs.Logger.Info("Ignoring set proposal at height 2, round 0")
  75. return nil
  76. }
  77. return cs.defaultSetProposal(proposal)
  78. }
  79. startTestRound(cs, height, round)
  80. ensureNewRound(newRoundCh, height, round) // first round at first height
  81. ensureNewEventOnChannel(newBlockCh) // first block gets committed
  82. height++ // moving to the next height
  83. round = 0
  84. ensureNewRound(newRoundCh, height, round) // first round at next height
  85. deliverTxsRange(cs, 0, 1) // we deliver txs, but dont set a proposal so we get the next round
  86. ensureNewTimeout(timeoutCh, height, round, cs.config.TimeoutPropose.Nanoseconds())
  87. round++ // moving to the next round
  88. ensureNewRound(newRoundCh, height, round) // wait for the next round
  89. ensureNewEventOnChannel(newBlockCh) // now we can commit the block
  90. }
  91. func deliverTxsRange(cs *State, start, end int) {
  92. // Deliver some txs.
  93. for i := start; i < end; i++ {
  94. txBytes := make([]byte, 8)
  95. binary.BigEndian.PutUint64(txBytes, uint64(i))
  96. err := assertMempool(cs.txNotifier).CheckTx(context.Background(), txBytes, nil, mempool.TxInfo{})
  97. if err != nil {
  98. panic(fmt.Sprintf("Error after CheckTx: %v", err))
  99. }
  100. }
  101. }
  102. func TestMempoolTxConcurrentWithCommit(t *testing.T) {
  103. config := configSetup(t)
  104. state, privVals := randGenesisState(config, 1, false, 10)
  105. stateStore := sm.NewStore(dbm.NewMemDB())
  106. blockStore := store.NewBlockStore(dbm.NewMemDB())
  107. cs := newStateWithConfigAndBlockStore(config, state, privVals[0], NewCounterApplication(), blockStore)
  108. err := stateStore.Save(state)
  109. require.NoError(t, err)
  110. newBlockHeaderCh := subscribe(t, cs.eventBus, types.EventQueryNewBlockHeader)
  111. const numTxs int64 = 3000
  112. go deliverTxsRange(cs, 0, int(numTxs))
  113. startTestRound(cs, cs.Height, cs.Round)
  114. for n := int64(0); n < numTxs; {
  115. select {
  116. case msg := <-newBlockHeaderCh:
  117. headerEvent := msg.Data().(types.EventDataNewBlockHeader)
  118. n += headerEvent.NumTxs
  119. case <-time.After(30 * time.Second):
  120. t.Fatal("Timed out waiting 30s to commit blocks with transactions")
  121. }
  122. }
  123. }
  124. func TestMempoolRmBadTx(t *testing.T) {
  125. config := configSetup(t)
  126. state, privVals := randGenesisState(config, 1, false, 10)
  127. app := NewCounterApplication()
  128. stateStore := sm.NewStore(dbm.NewMemDB())
  129. blockStore := store.NewBlockStore(dbm.NewMemDB())
  130. cs := newStateWithConfigAndBlockStore(config, state, privVals[0], app, blockStore)
  131. err := stateStore.Save(state)
  132. require.NoError(t, err)
  133. // increment the counter by 1
  134. txBytes := make([]byte, 8)
  135. binary.BigEndian.PutUint64(txBytes, uint64(0))
  136. resDeliver := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
  137. assert.False(t, resDeliver.IsErr(), fmt.Sprintf("expected no error. got %v", resDeliver))
  138. resCommit := app.Commit()
  139. assert.True(t, len(resCommit.Data) > 0)
  140. emptyMempoolCh := make(chan struct{})
  141. checkTxRespCh := make(chan struct{})
  142. go func() {
  143. // Try to send the tx through the mempool.
  144. // CheckTx should not err, but the app should return a bad abci code
  145. // and the tx should get removed from the pool
  146. err := assertMempool(cs.txNotifier).CheckTx(context.Background(), txBytes, func(r *abci.Response) {
  147. if r.GetCheckTx().Code != code.CodeTypeBadNonce {
  148. t.Errorf("expected checktx to return bad nonce, got %v", r)
  149. return
  150. }
  151. checkTxRespCh <- struct{}{}
  152. }, mempool.TxInfo{})
  153. if err != nil {
  154. t.Errorf("error after CheckTx: %v", err)
  155. return
  156. }
  157. // check for the tx
  158. for {
  159. txs := assertMempool(cs.txNotifier).ReapMaxBytesMaxGas(int64(len(txBytes)), -1)
  160. if len(txs) == 0 {
  161. emptyMempoolCh <- struct{}{}
  162. return
  163. }
  164. time.Sleep(10 * time.Millisecond)
  165. }
  166. }()
  167. // Wait until the tx returns
  168. ticker := time.After(time.Second * 5)
  169. select {
  170. case <-checkTxRespCh:
  171. // success
  172. case <-ticker:
  173. t.Errorf("timed out waiting for tx to return")
  174. return
  175. }
  176. // Wait until the tx is removed
  177. ticker = time.After(time.Second * 5)
  178. select {
  179. case <-emptyMempoolCh:
  180. // success
  181. case <-ticker:
  182. t.Errorf("timed out waiting for tx to be removed")
  183. return
  184. }
  185. }
  186. // CounterApplication that maintains a mempool state and resets it upon commit
  187. type CounterApplication struct {
  188. abci.BaseApplication
  189. txCount int
  190. mempoolTxCount int
  191. }
  192. func NewCounterApplication() *CounterApplication {
  193. return &CounterApplication{}
  194. }
  195. func (app *CounterApplication) Info(req abci.RequestInfo) abci.ResponseInfo {
  196. return abci.ResponseInfo{Data: fmt.Sprintf("txs:%v", app.txCount)}
  197. }
  198. func (app *CounterApplication) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
  199. txValue := txAsUint64(req.Tx)
  200. if txValue != uint64(app.txCount) {
  201. return abci.ResponseDeliverTx{
  202. Code: code.CodeTypeBadNonce,
  203. Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
  204. }
  205. app.txCount++
  206. return abci.ResponseDeliverTx{Code: code.CodeTypeOK}
  207. }
  208. func (app *CounterApplication) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
  209. txValue := txAsUint64(req.Tx)
  210. if txValue != uint64(app.mempoolTxCount) {
  211. return abci.ResponseCheckTx{
  212. Code: code.CodeTypeBadNonce,
  213. Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.mempoolTxCount, txValue)}
  214. }
  215. app.mempoolTxCount++
  216. return abci.ResponseCheckTx{Code: code.CodeTypeOK}
  217. }
  218. func txAsUint64(tx []byte) uint64 {
  219. tx8 := make([]byte, 8)
  220. copy(tx8[len(tx8)-len(tx):], tx)
  221. return binary.BigEndian.Uint64(tx8)
  222. }
  223. func (app *CounterApplication) Commit() abci.ResponseCommit {
  224. app.mempoolTxCount = app.txCount
  225. if app.txCount == 0 {
  226. return abci.ResponseCommit{}
  227. }
  228. hash := make([]byte, 8)
  229. binary.BigEndian.PutUint64(hash, uint64(app.txCount))
  230. return abci.ResponseCommit{Data: hash}
  231. }