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.

72 lines
3.1 KiB

  1. package consensus
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. var testLog = `{"time":"2016-01-18T20:46:00.774Z","msg":[3,{"duration":982632969,"height":1,"round":0,"step":1}]}
  10. {"time":"2016-01-18T20:46:00.776Z","msg":[1,{"height":1,"round":0,"step":"RoundStepPropose"}]}
  11. {"time":"2016-01-18T20:46:00.776Z","msg":[2,{"msg":[17,{"Proposal":{"height":1,"round":0,"block_parts_header":{"total":1,"hash":"B6227255FF20758326B0B7DFF529F20E33E58F45"},"pol_round":-1,"signature":"A1803A1364F6398C154FE45D5649A89129039F18A0FE42B211BADFDF6E81EA53F48F83D3610DDD848C3A5284D3F09BDEB26FA1D856BDF70D48C507BF2453A70E"}}],"peer_key":""}]}
  12. {"time":"2016-01-18T20:46:00.777Z","msg":[2,{"msg":[19,{"Height":1,"Round":0,"Part":{"index":0,"bytes":"0101010F74656E6465726D696E745F746573740101142AA030B15DDFC000000000000000000000000000000114C4B01D3810579550997AC5641E759E20D99B51C10001000100","proof":{"aunts":[]}}}],"peer_key":""}]}
  13. {"time":"2016-01-18T20:46:00.781Z","msg":[1,{"height":1,"round":0,"step":"RoundStepPrevote"}]}
  14. {"time":"2016-01-18T20:46:00.781Z","msg":[2,{"msg":[20,{"ValidatorIndex":0,"Vote":{"height":1,"round":0,"type":1,"block_hash":"E05D1DB8DEC7CDA507A42C8FF208EE4317C663F6","block_parts_header":{"total":1,"hash":"B6227255FF20758326B0B7DFF529F20E33E58F45"},"signature":"88F5708C802BEE54EFBF438967FBC6C6EAAFC41258A85D92B9B055481175BE9FA71007B1AAF2BFBC3BF3CC0542DB48A9812324B7BBA7307446CCDBF029077F07"}}],"peer_key":""}]}
  15. {"time":"2016-01-18T20:46:00.786Z","msg":[1,{"height":1,"round":0,"step":"RoundStepPrecommit"}]}
  16. {"time":"2016-01-18T20:46:00.786Z","msg":[2,{"msg":[20,{"ValidatorIndex":0,"Vote":{"height":1,"round":0,"type":2,"block_hash":"E05D1DB8DEC7CDA507A42C8FF208EE4317C663F6","block_parts_header":{"total":1,"hash":"B6227255FF20758326B0B7DFF529F20E33E58F45"},"signature":"65B0C9D2A8C9919FC9B036F82C3F1818E706E8BC066A78D99D3316E4814AB06594841E387B323AA7773F926D253C1E4D4A0930F7A8C8AE1E838CA15C673B2B02"}}],"peer_key":""}]}
  17. `
  18. func TestReplayCatchup(t *testing.T) {
  19. // write the needed wal to file
  20. f, err := ioutil.TempFile(os.TempDir(), "replay_test_")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. name := f.Name()
  25. _, err = f.WriteString(testLog)
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. f.Close()
  30. cs := fixedConsensusState()
  31. // we've already precommitted on the first block
  32. // without replay catchup we would be halted here forever
  33. cs.privValidator.LastHeight = 1 // first block
  34. cs.privValidator.LastStep = 3 // precommit
  35. newBlockCh := subscribeToEvent(cs.evsw, "tester", types.EventStringNewBlock(), 0)
  36. // start timeout and receive routines
  37. cs.startRoutines(0)
  38. // open wal and run catchup messages
  39. openWAL(t, cs, name)
  40. if err := cs.catchupReplay(cs.Height); err != nil {
  41. t.Fatalf("Error on catchup replay %v", err)
  42. }
  43. cs.enterNewRound(cs.Height, cs.Round)
  44. after := time.After(time.Second * 2)
  45. select {
  46. case <-newBlockCh:
  47. case <-after:
  48. t.Fatal("Timed out waiting for new block")
  49. }
  50. }
  51. func openWAL(t *testing.T, cs *ConsensusState, file string) {
  52. // open the wal
  53. wal, err := NewWAL(file)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. wal.exists = true
  58. cs.wal = wal
  59. }