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.

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