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.

81 lines
2.2 KiB

  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/require"
  6. wire "github.com/tendermint/go-wire"
  7. )
  8. func exampleVote() *Vote {
  9. var stamp, err = time.Parse(timeFormat, "2017-12-25T03:00:01.234Z")
  10. if err != nil {
  11. panic(err)
  12. }
  13. return &Vote{
  14. ValidatorAddress: []byte("addr"),
  15. ValidatorIndex: 56789,
  16. Height: 12345,
  17. Round: 2,
  18. Timestamp: stamp,
  19. Type: byte(2),
  20. BlockID: BlockID{
  21. Hash: []byte("hash"),
  22. PartsHeader: PartSetHeader{
  23. Total: 1000000,
  24. Hash: []byte("parts_hash"),
  25. },
  26. },
  27. }
  28. }
  29. func TestVoteSignable(t *testing.T) {
  30. vote := exampleVote()
  31. signBytes := SignBytes("test_chain_id", vote)
  32. signStr := string(signBytes)
  33. expected := `{"chain_id":"test_chain_id","vote":{"block_id":{"hash":"68617368","parts":{"hash":"70617274735F68617368","total":1000000}},"height":12345,"round":2,"timestamp":"2017-12-25T03:00:01.234Z","type":2}}`
  34. if signStr != expected {
  35. // NOTE: when this fails, you probably want to fix up consensus/replay_test too
  36. t.Errorf("Got unexpected sign string for Vote. Expected:\n%v\nGot:\n%v", expected, signStr)
  37. }
  38. }
  39. func TestVoteString(t *testing.T) {
  40. str := exampleVote().String()
  41. expected := `Vote{56789:616464720000 12345/02/2(Precommit) 686173680000 {<nil>} @ 2017-12-25T03:00:01.234Z}`
  42. if str != expected {
  43. t.Errorf("Got unexpected string for Proposal. Expected:\n%v\nGot:\n%v", expected, str)
  44. }
  45. }
  46. func TestVoteVerifySignature(t *testing.T) {
  47. privVal := GenPrivValidatorFS("")
  48. pubKey := privVal.GetPubKey()
  49. vote := exampleVote()
  50. signBytes := SignBytes("test_chain_id", vote)
  51. // sign it
  52. signature, err := privVal.Signer.Sign(signBytes)
  53. require.NoError(t, err)
  54. // verify the same vote
  55. valid := pubKey.VerifyBytes(SignBytes("test_chain_id", vote), signature)
  56. require.True(t, valid)
  57. // serialize, deserialize and verify again....
  58. precommit := new(Vote)
  59. bs := wire.BinaryBytes(vote)
  60. err = wire.ReadBinaryBytes(bs, &precommit)
  61. require.NoError(t, err)
  62. // verify the transmitted vote
  63. newSignBytes := SignBytes("test_chain_id", precommit)
  64. require.Equal(t, string(signBytes), string(newSignBytes))
  65. valid = pubKey.VerifyBytes(newSignBytes, signature)
  66. require.True(t, valid)
  67. }