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.

298 lines
9.5 KiB

new pubsub package comment out failing consensus tests for now rewrite rpc httpclient to use new pubsub package import pubsub as tmpubsub, query as tmquery make event IDs constants EventKey -> EventTypeKey rename EventsPubsub to PubSub mempool does not use pubsub rename eventsSub to pubsub new subscribe API fix channel size issues and consensus tests bugs refactor rpc client add missing discardFromChan method add mutex rename pubsub to eventBus remove IsRunning from WSRPCConnection interface (not needed) add a comment in broadcastNewRoundStepsAndVotes rename registerEventCallbacks to broadcastNewRoundStepsAndVotes See https://dave.cheney.net/2014/03/19/channel-axioms stop eventBuses after reactor tests remove unnecessary Unsubscribe return subscribe helper function move discardFromChan to where it is used subscribe now returns an err this gives us ability to refuse to subscribe if pubsub is at its max capacity. use context for control overflow cache queries handle err when subscribing in replay_test rename testClientID to testSubscriber extract var set channel buffer capacity to 1 in replay_file fix byzantine_test unsubscribe from single event, not all events refactor httpclient to return events to appropriate channels return failing testReplayCrashBeforeWriteVote test fix TestValidatorSetChanges refactor code a bit fix testReplayCrashBeforeWriteVote add comment fix TestValidatorSetChanges fixes from Bucky's review update comment [ci skip] test TxEventBuffer update changelog fix TestValidatorSetChanges (2nd attempt) only do wg.Done when no errors benchmark event bus create pubsub server inside NewEventBus only expose config params (later if needed) set buffer capacity to 0 so we are not testing cache new tx event format: key = "Tx" plus a tag {"tx.hash": XYZ} This should allow to subscribe to all transactions! or a specific one using a query: "tm.events.type = Tx and tx.hash = '013ABF99434...'" use TimeoutCommit instead of afterPublishEventNewBlockTimeout TimeoutCommit is the time a node waits after committing a block, before it goes into the next height. So it will finish everything from the last block, but then wait a bit. The idea is this gives it time to hear more votes from other validators, to strengthen the commit it includes in the next block. But it also gives it time to hear about new transactions. waitForBlockWithUpdatedVals rewrite WAL crash tests Task: test that we can recover from any WAL crash. Solution: the old tests were relying on event hub being run in the same thread (we were injecting the private validator's last signature). when considering a rewrite, we considered two possible solutions: write a "fuzzy" testing system where WAL is crashing upon receiving a new message, or inject failures and trigger them in tests using something like https://github.com/coreos/gofail. remove sleep no cs.Lock around wal.Save test different cases (empty block, non-empty block, ...) comments add comments test 4 cases: empty block, non-empty block, non-empty block with smaller part size, many blocks fixes as per Bucky's last review reset subscriptions on UnsubscribeAll use a simple counter to track message for which we panicked also, set a smaller part size for all test cases
7 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
  1. package consensus
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path"
  7. "sync"
  8. "testing"
  9. "time"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/stretchr/testify/require"
  12. dbm "github.com/tendermint/tm-db"
  13. abciclient "github.com/tendermint/tendermint/abci/client"
  14. "github.com/tendermint/tendermint/abci/example/kvstore"
  15. abci "github.com/tendermint/tendermint/abci/types"
  16. "github.com/tendermint/tendermint/internal/eventbus"
  17. "github.com/tendermint/tendermint/internal/evidence"
  18. "github.com/tendermint/tendermint/internal/mempool"
  19. "github.com/tendermint/tendermint/internal/p2p"
  20. sm "github.com/tendermint/tendermint/internal/state"
  21. "github.com/tendermint/tendermint/internal/store"
  22. "github.com/tendermint/tendermint/internal/test/factory"
  23. "github.com/tendermint/tendermint/libs/log"
  24. tmtime "github.com/tendermint/tendermint/libs/time"
  25. tmcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
  26. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  27. "github.com/tendermint/tendermint/types"
  28. )
  29. // Byzantine node sends two different prevotes (nil and blockID) to the same
  30. // validator.
  31. func TestByzantinePrevoteEquivocation(t *testing.T) {
  32. // empirically, this test either passes in <1s or hits some
  33. // kind of deadlock and hit the larger timeout. This timeout
  34. // can be extended a bunch if needed, but it's good to avoid
  35. // falling back to a much coarser timeout
  36. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  37. defer cancel()
  38. config := configSetup(t)
  39. nValidators := 4
  40. prevoteHeight := int64(2)
  41. testName := "consensus_byzantine_test"
  42. tickerFunc := newMockTickerFunc(true)
  43. valSet, privVals := factory.ValidatorSet(ctx, t, nValidators, 30)
  44. genDoc := factory.GenesisDoc(config, time.Now(), valSet.Validators, nil)
  45. states := make([]*State, nValidators)
  46. for i := 0; i < nValidators; i++ {
  47. func() {
  48. logger := consensusLogger().With("test", "byzantine", "validator", i)
  49. stateDB := dbm.NewMemDB() // each state needs its own db
  50. stateStore := sm.NewStore(stateDB)
  51. state, err := sm.MakeGenesisState(genDoc)
  52. require.NoError(t, err)
  53. require.NoError(t, stateStore.Save(state))
  54. thisConfig, err := ResetConfig(t.TempDir(), fmt.Sprintf("%s_%d", testName, i))
  55. require.NoError(t, err)
  56. defer os.RemoveAll(thisConfig.RootDir)
  57. ensureDir(t, path.Dir(thisConfig.Consensus.WalFile()), 0700) // dir for wal
  58. app := kvstore.NewApplication()
  59. vals := types.TM2PB.ValidatorUpdates(state.Validators)
  60. app.InitChain(abci.RequestInitChain{Validators: vals})
  61. blockDB := dbm.NewMemDB()
  62. blockStore := store.NewBlockStore(blockDB)
  63. // one for mempool, one for consensus
  64. proxyAppConnMem := abciclient.NewLocalClient(logger, app)
  65. proxyAppConnCon := abciclient.NewLocalClient(logger, app)
  66. // Make Mempool
  67. mempool := mempool.NewTxMempool(
  68. log.NewNopLogger().With("module", "mempool"),
  69. thisConfig.Mempool,
  70. proxyAppConnMem,
  71. )
  72. if thisConfig.Consensus.WaitForTxs() {
  73. mempool.EnableTxsAvailable()
  74. }
  75. eventBus := eventbus.NewDefault(log.NewNopLogger().With("module", "events"))
  76. require.NoError(t, eventBus.Start(ctx))
  77. // Make a full instance of the evidence pool
  78. evidenceDB := dbm.NewMemDB()
  79. evpool := evidence.NewPool(logger.With("module", "evidence"), evidenceDB, stateStore, blockStore, evidence.NopMetrics(), eventBus)
  80. // Make State
  81. blockExec := sm.NewBlockExecutor(stateStore, log.NewNopLogger(), proxyAppConnCon, mempool, evpool, blockStore, eventBus)
  82. cs, err := NewState(ctx, logger, thisConfig.Consensus, stateStore, blockExec, blockStore, mempool, evpool, eventBus)
  83. require.NoError(t, err)
  84. // set private validator
  85. pv := privVals[i]
  86. cs.SetPrivValidator(ctx, pv)
  87. cs.SetTimeoutTicker(tickerFunc())
  88. states[i] = cs
  89. }()
  90. }
  91. rts := setup(ctx, t, nValidators, states, 512) // buffer must be large enough to not deadlock
  92. var bzNodeID types.NodeID
  93. // Set the first state's reactor as the dedicated byzantine reactor and grab
  94. // the NodeID that corresponds to the state so we can reference the reactor.
  95. bzNodeState := states[0]
  96. for nID, s := range rts.states {
  97. if s == bzNodeState {
  98. bzNodeID = nID
  99. break
  100. }
  101. }
  102. bzReactor := rts.reactors[bzNodeID]
  103. // alter prevote so that the byzantine node double votes when height is 2
  104. bzNodeState.doPrevote = func(ctx context.Context, height int64, round int32) {
  105. // allow first height to happen normally so that byzantine validator is no longer proposer
  106. if height == prevoteHeight {
  107. prevote1, err := bzNodeState.signVote(ctx,
  108. tmproto.PrevoteType,
  109. bzNodeState.ProposalBlock.Hash(),
  110. bzNodeState.ProposalBlockParts.Header(),
  111. )
  112. require.NoError(t, err)
  113. prevote2, err := bzNodeState.signVote(ctx, tmproto.PrevoteType, nil, types.PartSetHeader{})
  114. require.NoError(t, err)
  115. // send two votes to all peers (1st to one half, 2nd to another half)
  116. i := 0
  117. for _, ps := range bzReactor.peers {
  118. if i < len(bzReactor.peers)/2 {
  119. require.NoError(t, bzReactor.voteCh.Send(ctx,
  120. p2p.Envelope{
  121. To: ps.peerID,
  122. Message: &tmcons.Vote{
  123. Vote: prevote1.ToProto(),
  124. },
  125. }))
  126. } else {
  127. require.NoError(t, bzReactor.voteCh.Send(ctx,
  128. p2p.Envelope{
  129. To: ps.peerID,
  130. Message: &tmcons.Vote{
  131. Vote: prevote2.ToProto(),
  132. },
  133. }))
  134. }
  135. i++
  136. }
  137. } else {
  138. bzNodeState.defaultDoPrevote(ctx, height, round)
  139. }
  140. }
  141. // Introducing a lazy proposer means that the time of the block committed is
  142. // different to the timestamp that the other nodes have. This tests to ensure
  143. // that the evidence that finally gets proposed will have a valid timestamp.
  144. // lazyProposer := states[1]
  145. lazyNodeState := states[1]
  146. lazyNodeState.decideProposal = func(ctx context.Context, height int64, round int32) {
  147. require.NotNil(t, lazyNodeState.privValidator)
  148. var commit *types.Commit
  149. switch {
  150. case lazyNodeState.Height == lazyNodeState.state.InitialHeight:
  151. // We're creating a proposal for the first block.
  152. // The commit is empty, but not nil.
  153. commit = types.NewCommit(0, 0, types.BlockID{}, nil)
  154. case lazyNodeState.LastCommit.HasTwoThirdsMajority():
  155. // Make the commit from LastCommit
  156. commit = lazyNodeState.LastCommit.MakeCommit()
  157. default: // This shouldn't happen.
  158. lazyNodeState.logger.Error("enterPropose: Cannot propose anything: No commit for the previous block")
  159. return
  160. }
  161. // omit the last signature in the commit
  162. commit.Signatures[len(commit.Signatures)-1] = types.NewCommitSigAbsent()
  163. if lazyNodeState.privValidatorPubKey == nil {
  164. // If this node is a validator & proposer in the current round, it will
  165. // miss the opportunity to create a block.
  166. lazyNodeState.logger.Error("enterPropose", "err", errPubKeyIsNotSet)
  167. return
  168. }
  169. proposerAddr := lazyNodeState.privValidatorPubKey.Address()
  170. block, err := lazyNodeState.blockExec.CreateProposalBlock(
  171. ctx, lazyNodeState.Height, lazyNodeState.state, commit, proposerAddr, nil)
  172. require.NoError(t, err)
  173. blockParts, err := block.MakePartSet(types.BlockPartSizeBytes)
  174. require.NoError(t, err)
  175. // Flush the WAL. Otherwise, we may not recompute the same proposal to sign,
  176. // and the privValidator will refuse to sign anything.
  177. if err := lazyNodeState.wal.FlushAndSync(); err != nil {
  178. lazyNodeState.logger.Error("error flushing to disk")
  179. }
  180. // Make proposal
  181. propBlockID := types.BlockID{Hash: block.Hash(), PartSetHeader: blockParts.Header()}
  182. proposal := types.NewProposal(height, round, lazyNodeState.ValidRound, propBlockID, block.Header.Time)
  183. p := proposal.ToProto()
  184. if err := lazyNodeState.privValidator.SignProposal(ctx, lazyNodeState.state.ChainID, p); err == nil {
  185. proposal.Signature = p.Signature
  186. // send proposal and block parts on internal msg queue
  187. lazyNodeState.sendInternalMessage(ctx, msgInfo{&ProposalMessage{proposal}, "", tmtime.Now()})
  188. for i := 0; i < int(blockParts.Total()); i++ {
  189. part := blockParts.GetPart(i)
  190. lazyNodeState.sendInternalMessage(ctx, msgInfo{&BlockPartMessage{
  191. lazyNodeState.Height, lazyNodeState.Round, part,
  192. }, "", tmtime.Now()})
  193. }
  194. } else if !lazyNodeState.replayMode {
  195. lazyNodeState.logger.Error("enterPropose: Error signing proposal", "height", height, "round", round, "err", err)
  196. }
  197. }
  198. for _, reactor := range rts.reactors {
  199. reactor.SwitchToConsensus(ctx, reactor.state.GetState(), false)
  200. }
  201. // Evidence should be submitted and committed at the third height but
  202. // we will check the first six just in case
  203. evidenceFromEachValidator := make([]types.Evidence, nValidators)
  204. var wg sync.WaitGroup
  205. i := 0
  206. subctx, subcancel := context.WithCancel(ctx)
  207. defer subcancel()
  208. for _, sub := range rts.subs {
  209. wg.Add(1)
  210. go func(j int, s eventbus.Subscription) {
  211. defer wg.Done()
  212. for {
  213. if subctx.Err() != nil {
  214. return
  215. }
  216. msg, err := s.Next(subctx)
  217. if subctx.Err() != nil {
  218. return
  219. }
  220. if err != nil {
  221. t.Errorf("waiting for subscription: %v", err)
  222. subcancel()
  223. return
  224. }
  225. require.NotNil(t, msg)
  226. block := msg.Data().(types.EventDataNewBlock).Block
  227. if len(block.Evidence) != 0 {
  228. evidenceFromEachValidator[j] = block.Evidence[0]
  229. return
  230. }
  231. }
  232. }(i, sub)
  233. i++
  234. }
  235. wg.Wait()
  236. // don't run more assertions if we've encountered a timeout
  237. select {
  238. case <-subctx.Done():
  239. t.Fatal("encountered timeout")
  240. default:
  241. }
  242. pubkey, err := bzNodeState.privValidator.GetPubKey(ctx)
  243. require.NoError(t, err)
  244. for idx, ev := range evidenceFromEachValidator {
  245. require.NotNil(t, ev, idx)
  246. ev, ok := ev.(*types.DuplicateVoteEvidence)
  247. require.True(t, ok)
  248. assert.Equal(t, pubkey.Address(), ev.VoteA.ValidatorAddress)
  249. assert.Equal(t, prevoteHeight, ev.Height())
  250. }
  251. }