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.

105 lines
4.3 KiB

9 years ago
9 years ago
  1. package consensus
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. /*
  10. The easiest way to generate this data is to copy ~/.tendermint_test/somedir/* to ~/.tendermint
  11. and to run a local node.
  12. Be sure to set the db to "leveldb" to create a cswal file in ~/.tendermint/data/cswal.
  13. If you need to change the signatures, you can use a script as follows:
  14. The privBytes comes from config/tendermint_test/...
  15. ```
  16. package main
  17. import (
  18. "encoding/hex"
  19. "fmt"
  20. "github.com/tendermint/go-crypto"
  21. )
  22. func main() {
  23. signBytes, err := hex.DecodeString("7B22636861696E5F6964223A2274656E6465726D696E745F74657374222C22766F7465223A7B22626C6F636B5F68617368223A2242453544373939433846353044354645383533364334333932464443384537423342313830373638222C22626C6F636B5F70617274735F686561646572223A506172745365747B543A31204236323237323535464632307D2C22686569676874223A312C22726F756E64223A302C2274797065223A327D7D")
  24. if err != nil {
  25. panic(err)
  26. }
  27. privBytes, err := hex.DecodeString("27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8")
  28. if err != nil {
  29. panic(err)
  30. }
  31. privKey := crypto.PrivKeyEd25519{}
  32. copy(privKey[:], privBytes)
  33. signature := privKey.Sign(signBytes)
  34. signatureEd25519 := signature.(crypto.SignatureEd25519)
  35. fmt.Printf("Signature Bytes: %X\n", signatureEd25519[:])
  36. }
  37. ```
  38. */
  39. var testLog = `{"time":"2016-04-03T11:23:54.387Z","msg":[3,{"duration":972835254,"height":1,"round":0,"step":1}]}
  40. {"time":"2016-04-03T11:23:54.388Z","msg":[1,{"height":1,"round":0,"step":"RoundStepPropose"}]}
  41. {"time":"2016-04-03T11:23:54.388Z","msg":[2,{"msg":[17,{"Proposal":{"height":1,"round":0,"block_parts_header":{"total":1,"hash":"3BA1E90CB868DA6B4FD7F3589826EC461E9EB4EF"},"pol_round":-1,"signature":"3A2ECD5023B21EC144EC16CFF1B992A4321317B83EEDD8969FDFEA6EB7BF4389F38DDA3E7BB109D63A07491C16277A197B241CF1F05F5E485C59882ECACD9E07"}}],"peer_key":""}]}
  42. {"time":"2016-04-03T11:23:54.389Z","msg":[2,{"msg":[19,{"Height":1,"Round":0,"Part":{"index":0,"bytes":"0101010F74656E6465726D696E745F7465737401011441D59F4B718AC00000000000000114C4B01D3810579550997AC5641E759E20D99B51C10001000100","proof":{"aunts":[]}}}],"peer_key":""}]}
  43. {"time":"2016-04-03T11:23:54.390Z","msg":[1,{"height":1,"round":0,"step":"RoundStepPrevote"}]}
  44. {"time":"2016-04-03T11:23:54.390Z","msg":[2,{"msg":[20,{"ValidatorIndex":0,"Vote":{"height":1,"round":0,"type":1,"block_hash":"4291966B8A9DFBA00AEC7C700F2718E61DF4331D","block_parts_header":{"total":1,"hash":"3BA1E90CB868DA6B4FD7F3589826EC461E9EB4EF"},"signature":"47D2A75A4E2F15DB1F0D1B656AC0637AF9AADDFEB6A156874F6553C73895E5D5DC948DBAEF15E61276C5342D0E638DFCB77C971CD282096EA8735A564A90F008"}}],"peer_key":""}]}
  45. {"time":"2016-04-03T11:23:54.392Z","msg":[1,{"height":1,"round":0,"step":"RoundStepPrecommit"}]}
  46. {"time":"2016-04-03T11:23:54.392Z","msg":[2,{"msg":[20,{"ValidatorIndex":0,"Vote":{"height":1,"round":0,"type":2,"block_hash":"4291966B8A9DFBA00AEC7C700F2718E61DF4331D","block_parts_header":{"total":1,"hash":"3BA1E90CB868DA6B4FD7F3589826EC461E9EB4EF"},"signature":"39147DA595F08B73CF8C899967C8403B5872FD9042FFA4E239159E0B6C5D9665C9CA81D766EACA2AE658872F94C2FCD1E34BF51859CD5B274DA8512BACE4B50D"}}],"peer_key":""}]}
  47. `
  48. func TestReplayCatchup(t *testing.T) {
  49. // write the needed wal to file
  50. f, err := ioutil.TempFile(os.TempDir(), "replay_test_")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. name := f.Name()
  55. _, err = f.WriteString(testLog)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. f.Close()
  60. cs := fixedConsensusState()
  61. // we've already precommitted on the first block
  62. // without replay catchup we would be halted here forever
  63. cs.privValidator.LastHeight = 1 // first block
  64. cs.privValidator.LastStep = 3 // precommit
  65. newBlockCh := subscribeToEvent(cs.evsw, "tester", types.EventStringNewBlock(), 0)
  66. // start timeout and receive routines
  67. cs.startRoutines(0)
  68. // open wal and run catchup messages
  69. openWAL(t, cs, name)
  70. if err := cs.catchupReplay(cs.Height); err != nil {
  71. t.Fatalf("Error on catchup replay %v", err)
  72. }
  73. after := time.After(time.Second * 15)
  74. select {
  75. case <-newBlockCh:
  76. case <-after:
  77. t.Fatal("Timed out waiting for new block")
  78. }
  79. }
  80. func openWAL(t *testing.T, cs *ConsensusState, file string) {
  81. // open the wal
  82. wal, err := NewWAL(file, config.GetBool("cswal_light"))
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. wal.exists = true
  87. cs.wal = wal
  88. }