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.

216 lines
6.8 KiB

9 years ago
  1. package consensus
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path"
  7. "strings"
  8. "testing"
  9. "time"
  10. . "github.com/tendermint/go-common"
  11. "github.com/tendermint/go-wire"
  12. "github.com/tendermint/tendermint/types"
  13. )
  14. // TODO: these tests ensure we can always recover from any state of the wal,
  15. // assuming it comes with a correct related state for the priv_validator.json.
  16. // It would be better to verify explicitly which states we can recover from without the wal
  17. // and which ones we need the wal for - then we'd also be able to only flush the
  18. // wal writer when we need to, instead of with every message.
  19. var data_dir = path.Join(GoPath, "src/github.com/tendermint/tendermint/consensus", "test_data")
  20. // the priv validator changes step at these lines for a block with 1 val and 1 part
  21. var baseStepChanges = []int{3, 6, 8}
  22. // test recovery from each line in each testCase
  23. var testCases = []*testCase{
  24. newTestCase("empty_block", baseStepChanges), // empty block (has 1 block part)
  25. newTestCase("small_block1", baseStepChanges), // small block with txs in 1 block part
  26. newTestCase("small_block2", []int{3, 10, 12}), // small block with txs across 5 smaller block parts
  27. }
  28. type testCase struct {
  29. name string
  30. log string //full cs wal
  31. stepMap map[int]int8 // map lines of log to privval step
  32. proposeLine int
  33. prevoteLine int
  34. precommitLine int
  35. }
  36. func newTestCase(name string, stepChanges []int) *testCase {
  37. if len(stepChanges) != 3 {
  38. panic(Fmt("a full wal has 3 step changes! Got array %v", stepChanges))
  39. }
  40. return &testCase{
  41. name: name,
  42. log: readWAL(path.Join(data_dir, name+".cswal")),
  43. stepMap: newMapFromChanges(stepChanges),
  44. proposeLine: stepChanges[0],
  45. prevoteLine: stepChanges[1],
  46. precommitLine: stepChanges[2],
  47. }
  48. }
  49. func newMapFromChanges(changes []int) map[int]int8 {
  50. changes = append(changes, changes[2]+1) // so we add the last step change to the map
  51. m := make(map[int]int8)
  52. var count int
  53. for changeNum, nextChange := range changes {
  54. for ; count < nextChange; count++ {
  55. m[count] = int8(changeNum)
  56. }
  57. }
  58. return m
  59. }
  60. func readWAL(p string) string {
  61. b, err := ioutil.ReadFile(p)
  62. if err != nil {
  63. panic(err)
  64. }
  65. return string(b)
  66. }
  67. func writeWAL(walMsgs string) string {
  68. tempDir := os.TempDir()
  69. walDir := tempDir + "/wal" + RandStr(12)
  70. // Create WAL directory
  71. err := EnsureDir(walDir, 0700)
  72. if err != nil {
  73. panic(err)
  74. }
  75. // Write the needed WAL to file
  76. err = WriteFile(walDir+"/wal", []byte(walMsgs), 0600)
  77. if err != nil {
  78. panic(err)
  79. }
  80. return walDir
  81. }
  82. func waitForBlock(newBlockCh chan interface{}, thisCase *testCase, i int) {
  83. after := time.After(time.Second * 10)
  84. select {
  85. case <-newBlockCh:
  86. case <-after:
  87. panic(Fmt("Timed out waiting for new block for case '%s' line %d", thisCase.name, i))
  88. }
  89. }
  90. func runReplayTest(t *testing.T, cs *ConsensusState, walDir string, newBlockCh chan interface{},
  91. thisCase *testCase, i int) {
  92. cs.config.Set("cs_wal_dir", walDir)
  93. cs.Start()
  94. // Wait to make a new block.
  95. // This is just a signal that we haven't halted; its not something contained in the WAL itself.
  96. // Assuming the consensus state is running, replay of any WAL, including the empty one,
  97. // should eventually be followed by a new block, or else something is wrong
  98. waitForBlock(newBlockCh, thisCase, i)
  99. cs.Stop()
  100. cs.Wait()
  101. }
  102. func toPV(pv PrivValidator) *types.PrivValidator {
  103. return pv.(*types.PrivValidator)
  104. }
  105. func setupReplayTest(thisCase *testCase, nLines int, crashAfter bool) (*ConsensusState, chan interface{}, string, string) {
  106. fmt.Println("-------------------------------------")
  107. log.Notice(Fmt("Starting replay test %v (of %d lines of WAL). Crash after = %v", thisCase.name, nLines, crashAfter))
  108. lineStep := nLines
  109. if crashAfter {
  110. lineStep -= 1
  111. }
  112. split := strings.Split(thisCase.log, "\n")
  113. lastMsg := split[nLines]
  114. // we write those lines up to (not including) one with the signature
  115. walDir := writeWAL(strings.Join(split[:nLines], "\n") + "\n")
  116. cs := fixedConsensusStateDummy()
  117. // set the last step according to when we crashed vs the wal
  118. toPV(cs.privValidator).LastHeight = 1 // first block
  119. toPV(cs.privValidator).LastStep = thisCase.stepMap[lineStep]
  120. log.Warn("setupReplayTest", "LastStep", toPV(cs.privValidator).LastStep)
  121. newBlockCh := subscribeToEvent(cs.evsw, "tester", types.EventStringNewBlock(), 1)
  122. return cs, newBlockCh, lastMsg, walDir
  123. }
  124. func readTimedWALMessage(t *testing.T, walMsg string) TimedWALMessage {
  125. var err error
  126. var msg TimedWALMessage
  127. wire.ReadJSON(&msg, []byte(walMsg), &err)
  128. if err != nil {
  129. t.Fatalf("Error reading json data: %v", err)
  130. }
  131. return msg
  132. }
  133. //-----------------------------------------------
  134. // Test the log at every iteration, and set the privVal last step
  135. // as if the log was written after signing, before the crash
  136. func TestReplayCrashAfterWrite(t *testing.T) {
  137. for _, thisCase := range testCases {
  138. split := strings.Split(thisCase.log, "\n")
  139. for i := 0; i < len(split)-1; i++ {
  140. cs, newBlockCh, _, walDir := setupReplayTest(thisCase, i+1, true)
  141. runReplayTest(t, cs, walDir, newBlockCh, thisCase, i+1)
  142. }
  143. }
  144. }
  145. //-----------------------------------------------
  146. // Test the log as if we crashed after signing but before writing.
  147. // This relies on privValidator.LastSignature being set
  148. func TestReplayCrashBeforeWritePropose(t *testing.T) {
  149. for _, thisCase := range testCases {
  150. lineNum := thisCase.proposeLine
  151. // setup replay test where last message is a proposal
  152. cs, newBlockCh, proposalMsg, walDir := setupReplayTest(thisCase, lineNum, false)
  153. msg := readTimedWALMessage(t, proposalMsg)
  154. proposal := msg.Msg.(msgInfo).Msg.(*ProposalMessage)
  155. // Set LastSig
  156. toPV(cs.privValidator).LastSignBytes = types.SignBytes(cs.state.ChainID, proposal.Proposal)
  157. toPV(cs.privValidator).LastSignature = proposal.Proposal.Signature
  158. runReplayTest(t, cs, walDir, newBlockCh, thisCase, lineNum)
  159. }
  160. }
  161. func TestReplayCrashBeforeWritePrevote(t *testing.T) {
  162. for _, thisCase := range testCases {
  163. testReplayCrashBeforeWriteVote(t, thisCase, thisCase.prevoteLine, types.EventStringCompleteProposal())
  164. }
  165. }
  166. func TestReplayCrashBeforeWritePrecommit(t *testing.T) {
  167. for _, thisCase := range testCases {
  168. testReplayCrashBeforeWriteVote(t, thisCase, thisCase.precommitLine, types.EventStringPolka())
  169. }
  170. }
  171. func testReplayCrashBeforeWriteVote(t *testing.T, thisCase *testCase, lineNum int, eventString string) {
  172. // setup replay test where last message is a vote
  173. cs, newBlockCh, voteMsg, walDir := setupReplayTest(thisCase, lineNum, false)
  174. types.AddListenerForEvent(cs.evsw, "tester", eventString, func(data types.TMEventData) {
  175. msg := readTimedWALMessage(t, voteMsg)
  176. vote := msg.Msg.(msgInfo).Msg.(*VoteMessage)
  177. // Set LastSig
  178. toPV(cs.privValidator).LastSignBytes = types.SignBytes(cs.state.ChainID, vote.Vote)
  179. toPV(cs.privValidator).LastSignature = vote.Vote.Signature
  180. })
  181. runReplayTest(t, cs, walDir, newBlockCh, thisCase, lineNum)
  182. }