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
8.5 KiB

mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
mempool: move interface into mempool package (#3524) ## Description Refs #2659 Breaking changes in the mempool package: [mempool] #2659 Mempool now an interface old Mempool renamed to CListMempool NewMempool renamed to NewCListMempool Option renamed to CListOption MempoolReactor renamed to Reactor NewMempoolReactor renamed to NewReactor unexpose TxID method TxInfo.PeerID renamed to SenderID unexpose MempoolReactor.Mempool Breaking changes in the state package: [state] #2659 Mempool interface moved to mempool package MockMempool moved to top-level mock package and renamed to Mempool Non Breaking changes in the node package: [node] #2659 Add Mempool method, which allows you to access mempool ## Commits * move Mempool interface into mempool package Refs #2659 Breaking changes in the mempool package: - Mempool now an interface - old Mempool renamed to CListMempool Breaking changes to state package: - MockMempool moved to mempool/mock package and renamed to Mempool - Mempool interface moved to mempool package * assert CListMempool impl Mempool * gofmt code * rename MempoolReactor to Reactor - combine everything into one interface - rename TxInfo.PeerID to TxInfo.SenderID - unexpose MempoolReactor.Mempool * move mempool mock into top-level mock package * add a fixme TxsFront should not be a part of the Mempool interface because it leaks implementation details. Instead, we need to come up with general interface for querying the mempool so the MempoolReactor can fetch and broadcast txs to peers. * change node#Mempool to return interface * save commit = new reactor arch * Revert "save commit = new reactor arch" This reverts commit 1bfceacd9d65a720574683a7f22771e69af9af4d. * require CListMempool in mempool.Reactor * add two changelog entries * fixes after my own review * quote interfaces, structs and functions * fixes after Ismail's review * make node's mempool an interface * make InitWAL/CloseWAL methods a part of Mempool interface * fix merge conflicts * make node's mempool an interface
6 years ago
cs/replay: execCommitBlock should not read from state.lastValidators (#3067) * execCommitBlock should not read from state.lastValidators * fix height 1 * fix blockchain/reactor_test * fix consensus/mempool_test * fix consensus/reactor_test * fix consensus/replay_test * add CHANGELOG * fix consensus/reactor_test * fix consensus/replay_test * add a test for replay validators change * fix mem_pool test * fix byzantine test * remove a redundant code * reduce validator change blocks to 6 * fix * return peer0 config * seperate testName * seperate testName 1 * seperate testName 2 * seperate app db path * seperate app db path 1 * add a lock before startNet * move the lock to reactor_test * simulate just once * try to find problem * handshake only saveState when app version changed * update gometalinter to 3.0.0 (#3233) in the attempt to fix https://circleci.com/gh/tendermint/tendermint/43165 also code is simplified by running gofmt -s . remove unused vars enable linters we're currently passing remove deprecated linters (cherry picked from commit d47094550315c094512a242445e0dde24b5a03f5) * gofmt code * goimport code * change the bool name to testValidatorsChange * adjust receive kvstore.ProtocolVersion * adjust receive kvstore.ProtocolVersion 1 * adjust receive kvstore.ProtocolVersion 3 * fix merge execution.go * fix merge develop * fix merge develop 1 * fix run cleanupFunc * adjust code according to reviewers' opinion * modify the func name match the convention * simplify simulate a chain containing some validator change txs 1 * test CI error * Merge remote-tracking branch 'upstream/develop' into fixReplay 1 * fix pubsub_test * subscribeUnbuffered vote channel
6 years ago
  1. package blockchain
  2. import (
  3. "os"
  4. "sort"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. abci "github.com/tendermint/tendermint/abci/types"
  9. cfg "github.com/tendermint/tendermint/config"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. dbm "github.com/tendermint/tendermint/libs/db"
  12. "github.com/tendermint/tendermint/libs/log"
  13. "github.com/tendermint/tendermint/mock"
  14. "github.com/tendermint/tendermint/p2p"
  15. "github.com/tendermint/tendermint/proxy"
  16. sm "github.com/tendermint/tendermint/state"
  17. "github.com/tendermint/tendermint/types"
  18. tmtime "github.com/tendermint/tendermint/types/time"
  19. )
  20. var config *cfg.Config
  21. func randGenesisDoc(numValidators int, randPower bool, minPower int64) (*types.GenesisDoc, []types.PrivValidator) {
  22. validators := make([]types.GenesisValidator, numValidators)
  23. privValidators := make([]types.PrivValidator, numValidators)
  24. for i := 0; i < numValidators; i++ {
  25. val, privVal := types.RandValidator(randPower, minPower)
  26. validators[i] = types.GenesisValidator{
  27. PubKey: val.PubKey,
  28. Power: val.VotingPower,
  29. }
  30. privValidators[i] = privVal
  31. }
  32. sort.Sort(types.PrivValidatorsByAddress(privValidators))
  33. return &types.GenesisDoc{
  34. GenesisTime: tmtime.Now(),
  35. ChainID: config.ChainID(),
  36. Validators: validators,
  37. }, privValidators
  38. }
  39. func makeVote(header *types.Header, blockID types.BlockID, valset *types.ValidatorSet, privVal types.PrivValidator) *types.Vote {
  40. addr := privVal.GetPubKey().Address()
  41. idx, _ := valset.GetByAddress(addr)
  42. vote := &types.Vote{
  43. ValidatorAddress: addr,
  44. ValidatorIndex: idx,
  45. Height: header.Height,
  46. Round: 1,
  47. Timestamp: tmtime.Now(),
  48. Type: types.PrecommitType,
  49. BlockID: blockID,
  50. }
  51. privVal.SignVote(header.ChainID, vote)
  52. return vote
  53. }
  54. type BlockchainReactorPair struct {
  55. reactor *BlockchainReactor
  56. app proxy.AppConns
  57. }
  58. func newBlockchainReactor(logger log.Logger, genDoc *types.GenesisDoc, privVals []types.PrivValidator, maxBlockHeight int64) BlockchainReactorPair {
  59. if len(privVals) != 1 {
  60. panic("only support one validator")
  61. }
  62. app := &testApp{}
  63. cc := proxy.NewLocalClientCreator(app)
  64. proxyApp := proxy.NewAppConns(cc)
  65. err := proxyApp.Start()
  66. if err != nil {
  67. panic(cmn.ErrorWrap(err, "error start app"))
  68. }
  69. blockDB := dbm.NewMemDB()
  70. stateDB := dbm.NewMemDB()
  71. blockStore := NewBlockStore(blockDB)
  72. state, err := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
  73. if err != nil {
  74. panic(cmn.ErrorWrap(err, "error constructing state from genesis file"))
  75. }
  76. // Make the BlockchainReactor itself.
  77. // NOTE we have to create and commit the blocks first because
  78. // pool.height is determined from the store.
  79. fastSync := true
  80. db := dbm.NewMemDB()
  81. blockExec := sm.NewBlockExecutor(db, log.TestingLogger(), proxyApp.Consensus(),
  82. mock.Mempool{}, sm.MockEvidencePool{})
  83. sm.SaveState(db, state)
  84. // let's add some blocks in
  85. for blockHeight := int64(1); blockHeight <= maxBlockHeight; blockHeight++ {
  86. lastCommit := types.NewCommit(types.BlockID{}, nil)
  87. if blockHeight > 1 {
  88. lastBlockMeta := blockStore.LoadBlockMeta(blockHeight - 1)
  89. lastBlock := blockStore.LoadBlock(blockHeight - 1)
  90. vote := makeVote(&lastBlock.Header, lastBlockMeta.BlockID, state.Validators, privVals[0]).CommitSig()
  91. lastCommit = types.NewCommit(lastBlockMeta.BlockID, []*types.CommitSig{vote})
  92. }
  93. thisBlock := makeBlock(blockHeight, state, lastCommit)
  94. thisParts := thisBlock.MakePartSet(types.BlockPartSizeBytes)
  95. blockID := types.BlockID{thisBlock.Hash(), thisParts.Header()}
  96. state, err = blockExec.ApplyBlock(state, blockID, thisBlock)
  97. if err != nil {
  98. panic(cmn.ErrorWrap(err, "error apply block"))
  99. }
  100. blockStore.SaveBlock(thisBlock, thisParts, lastCommit)
  101. }
  102. bcReactor := NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
  103. bcReactor.SetLogger(logger.With("module", "blockchain"))
  104. return BlockchainReactorPair{bcReactor, proxyApp}
  105. }
  106. func TestNoBlockResponse(t *testing.T) {
  107. config = cfg.ResetTestRoot("blockchain_reactor_test")
  108. defer os.RemoveAll(config.RootDir)
  109. genDoc, privVals := randGenesisDoc(1, false, 30)
  110. maxBlockHeight := int64(65)
  111. reactorPairs := make([]BlockchainReactorPair, 2)
  112. reactorPairs[0] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
  113. reactorPairs[1] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  114. p2p.MakeConnectedSwitches(config.P2P, 2, func(i int, s *p2p.Switch) *p2p.Switch {
  115. s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
  116. return s
  117. }, p2p.Connect2Switches)
  118. defer func() {
  119. for _, r := range reactorPairs {
  120. r.reactor.Stop()
  121. r.app.Stop()
  122. }
  123. }()
  124. tests := []struct {
  125. height int64
  126. existent bool
  127. }{
  128. {maxBlockHeight + 2, false},
  129. {10, true},
  130. {1, true},
  131. {100, false},
  132. }
  133. for {
  134. if reactorPairs[1].reactor.pool.IsCaughtUp() {
  135. break
  136. }
  137. time.Sleep(10 * time.Millisecond)
  138. }
  139. assert.Equal(t, maxBlockHeight, reactorPairs[0].reactor.store.Height())
  140. for _, tt := range tests {
  141. block := reactorPairs[1].reactor.store.LoadBlock(tt.height)
  142. if tt.existent {
  143. assert.True(t, block != nil)
  144. } else {
  145. assert.True(t, block == nil)
  146. }
  147. }
  148. }
  149. // NOTE: This is too hard to test without
  150. // an easy way to add test peer to switch
  151. // or without significant refactoring of the module.
  152. // Alternatively we could actually dial a TCP conn but
  153. // that seems extreme.
  154. func TestBadBlockStopsPeer(t *testing.T) {
  155. config = cfg.ResetTestRoot("blockchain_reactor_test")
  156. defer os.RemoveAll(config.RootDir)
  157. genDoc, privVals := randGenesisDoc(1, false, 30)
  158. maxBlockHeight := int64(148)
  159. otherChain := newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
  160. defer func() {
  161. otherChain.reactor.Stop()
  162. otherChain.app.Stop()
  163. }()
  164. reactorPairs := make([]BlockchainReactorPair, 4)
  165. reactorPairs[0] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, maxBlockHeight)
  166. reactorPairs[1] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  167. reactorPairs[2] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  168. reactorPairs[3] = newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  169. switches := p2p.MakeConnectedSwitches(config.P2P, 4, func(i int, s *p2p.Switch) *p2p.Switch {
  170. s.AddReactor("BLOCKCHAIN", reactorPairs[i].reactor)
  171. return s
  172. }, p2p.Connect2Switches)
  173. defer func() {
  174. for _, r := range reactorPairs {
  175. r.reactor.Stop()
  176. r.app.Stop()
  177. }
  178. }()
  179. for {
  180. if reactorPairs[3].reactor.pool.IsCaughtUp() {
  181. break
  182. }
  183. time.Sleep(1 * time.Second)
  184. }
  185. //at this time, reactors[0-3] is the newest
  186. assert.Equal(t, 3, reactorPairs[1].reactor.Switch.Peers().Size())
  187. //mark reactorPairs[3] is an invalid peer
  188. reactorPairs[3].reactor.store = otherChain.reactor.store
  189. lastReactorPair := newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 0)
  190. reactorPairs = append(reactorPairs, lastReactorPair)
  191. switches = append(switches, p2p.MakeConnectedSwitches(config.P2P, 1, func(i int, s *p2p.Switch) *p2p.Switch {
  192. s.AddReactor("BLOCKCHAIN", reactorPairs[len(reactorPairs)-1].reactor)
  193. return s
  194. }, p2p.Connect2Switches)...)
  195. for i := 0; i < len(reactorPairs)-1; i++ {
  196. p2p.Connect2Switches(switches, i, len(reactorPairs)-1)
  197. }
  198. for {
  199. if lastReactorPair.reactor.pool.IsCaughtUp() || lastReactorPair.reactor.Switch.Peers().Size() == 0 {
  200. break
  201. }
  202. time.Sleep(1 * time.Second)
  203. }
  204. assert.True(t, lastReactorPair.reactor.Switch.Peers().Size() < len(reactorPairs)-1)
  205. }
  206. //----------------------------------------------
  207. // utility funcs
  208. func makeTxs(height int64) (txs []types.Tx) {
  209. for i := 0; i < 10; i++ {
  210. txs = append(txs, types.Tx([]byte{byte(height), byte(i)}))
  211. }
  212. return txs
  213. }
  214. func makeBlock(height int64, state sm.State, lastCommit *types.Commit) *types.Block {
  215. block, _ := state.MakeBlock(height, makeTxs(height), lastCommit, nil, state.Validators.GetProposer().Address)
  216. return block
  217. }
  218. type testApp struct {
  219. abci.BaseApplication
  220. }
  221. var _ abci.Application = (*testApp)(nil)
  222. func (app *testApp) Info(req abci.RequestInfo) (resInfo abci.ResponseInfo) {
  223. return abci.ResponseInfo{}
  224. }
  225. func (app *testApp) BeginBlock(req abci.RequestBeginBlock) abci.ResponseBeginBlock {
  226. return abci.ResponseBeginBlock{}
  227. }
  228. func (app *testApp) EndBlock(req abci.RequestEndBlock) abci.ResponseEndBlock {
  229. return abci.ResponseEndBlock{}
  230. }
  231. func (app *testApp) DeliverTx(tx []byte) abci.ResponseDeliverTx {
  232. return abci.ResponseDeliverTx{Tags: []cmn.KVPair{}}
  233. }
  234. func (app *testApp) CheckTx(tx []byte) abci.ResponseCheckTx {
  235. return abci.ResponseCheckTx{}
  236. }
  237. func (app *testApp) Commit() abci.ResponseCommit {
  238. return abci.ResponseCommit{}
  239. }
  240. func (app *testApp) Query(reqQuery abci.RequestQuery) (resQuery abci.ResponseQuery) {
  241. return
  242. }