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.

194 lines
5.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "encoding/hex"
  4. "encoding/json"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. crypto "github.com/tendermint/go-crypto"
  12. "github.com/tendermint/go-wire/data"
  13. cmn "github.com/tendermint/tmlibs/common"
  14. )
  15. func TestGenLoadValidator(t *testing.T) {
  16. assert := assert.New(t)
  17. _, tempFilePath := cmn.Tempfile("priv_validator_")
  18. privVal := GenPrivValidatorFS(tempFilePath)
  19. height := int64(100)
  20. privVal.LastHeight = height
  21. privVal.Save()
  22. addr := privVal.GetAddress()
  23. privVal = LoadPrivValidatorFS(tempFilePath)
  24. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  25. assert.Equal(height, privVal.LastHeight, "expected privval.LastHeight to have been saved")
  26. }
  27. func TestLoadOrGenValidator(t *testing.T) {
  28. assert := assert.New(t)
  29. _, tempFilePath := cmn.Tempfile("priv_validator_")
  30. if err := os.Remove(tempFilePath); err != nil {
  31. t.Error(err)
  32. }
  33. privVal := LoadOrGenPrivValidatorFS(tempFilePath)
  34. addr := privVal.GetAddress()
  35. privVal = LoadOrGenPrivValidatorFS(tempFilePath)
  36. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  37. }
  38. func TestUnmarshalValidator(t *testing.T) {
  39. assert, require := assert.New(t), require.New(t)
  40. // create some fixed values
  41. addrStr := "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456"
  42. pubStr := "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  43. privStr := "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  44. addrBytes, _ := hex.DecodeString(addrStr)
  45. pubBytes, _ := hex.DecodeString(pubStr)
  46. privBytes, _ := hex.DecodeString(privStr)
  47. // prepend type byte
  48. pubKey, err := crypto.PubKeyFromBytes(append([]byte{1}, pubBytes...))
  49. require.Nil(err, "%+v", err)
  50. privKey, err := crypto.PrivKeyFromBytes(append([]byte{1}, privBytes...))
  51. require.Nil(err, "%+v", err)
  52. serialized := fmt.Sprintf(`{
  53. "address": "%s",
  54. "pub_key": {
  55. "type": "ed25519",
  56. "data": "%s"
  57. },
  58. "last_height": 0,
  59. "last_round": 0,
  60. "last_step": 0,
  61. "last_signature": null,
  62. "priv_key": {
  63. "type": "ed25519",
  64. "data": "%s"
  65. }
  66. }`, addrStr, pubStr, privStr)
  67. val := PrivValidatorFS{}
  68. err = json.Unmarshal([]byte(serialized), &val)
  69. require.Nil(err, "%+v", err)
  70. // make sure the values match
  71. assert.EqualValues(addrBytes, val.GetAddress())
  72. assert.EqualValues(pubKey, val.GetPubKey())
  73. assert.EqualValues(privKey, val.PrivKey)
  74. // export it and make sure it is the same
  75. out, err := json.Marshal(val)
  76. require.Nil(err, "%+v", err)
  77. assert.JSONEq(serialized, string(out))
  78. }
  79. func TestSignVote(t *testing.T) {
  80. assert := assert.New(t)
  81. _, tempFilePath := cmn.Tempfile("priv_validator_")
  82. privVal := GenPrivValidatorFS(tempFilePath)
  83. block1 := BlockID{[]byte{1, 2, 3}, PartSetHeader{}}
  84. block2 := BlockID{[]byte{3, 2, 1}, PartSetHeader{}}
  85. height, round := int64(10), 1
  86. voteType := VoteTypePrevote
  87. // sign a vote for first time
  88. vote := newVote(privVal.Address, 0, height, round, voteType, block1)
  89. err := privVal.SignVote("mychainid", vote)
  90. assert.NoError(err, "expected no error signing vote")
  91. // try to sign the same vote again; should be fine
  92. err = privVal.SignVote("mychainid", vote)
  93. assert.NoError(err, "expected no error on signing same vote")
  94. // now try some bad votes
  95. cases := []*Vote{
  96. newVote(privVal.Address, 0, height, round-1, voteType, block1), // round regression
  97. newVote(privVal.Address, 0, height-1, round, voteType, block1), // height regression
  98. newVote(privVal.Address, 0, height-2, round+4, voteType, block1), // height regression and different round
  99. newVote(privVal.Address, 0, height, round, voteType, block2), // different block
  100. }
  101. for _, c := range cases {
  102. err = privVal.SignVote("mychainid", c)
  103. assert.Error(err, "expected error on signing conflicting vote")
  104. }
  105. // try signing a vote with a different time stamp
  106. sig := vote.Signature
  107. vote.Timestamp = vote.Timestamp.Add(time.Duration(1000))
  108. err = privVal.SignVote("mychainid", vote)
  109. assert.NoError(err)
  110. assert.Equal(sig, vote.Signature)
  111. }
  112. func TestSignProposal(t *testing.T) {
  113. assert := assert.New(t)
  114. _, tempFilePath := cmn.Tempfile("priv_validator_")
  115. privVal := GenPrivValidatorFS(tempFilePath)
  116. block1 := PartSetHeader{5, []byte{1, 2, 3}}
  117. block2 := PartSetHeader{10, []byte{3, 2, 1}}
  118. height, round := int64(10), 1
  119. // sign a proposal for first time
  120. proposal := newProposal(height, round, block1)
  121. err := privVal.SignProposal("mychainid", proposal)
  122. assert.NoError(err, "expected no error signing proposal")
  123. // try to sign the same proposal again; should be fine
  124. err = privVal.SignProposal("mychainid", proposal)
  125. assert.NoError(err, "expected no error on signing same proposal")
  126. // now try some bad Proposals
  127. cases := []*Proposal{
  128. newProposal(height, round-1, block1), // round regression
  129. newProposal(height-1, round, block1), // height regression
  130. newProposal(height-2, round+4, block1), // height regression and different round
  131. newProposal(height, round, block2), // different block
  132. }
  133. for _, c := range cases {
  134. err = privVal.SignProposal("mychainid", c)
  135. assert.Error(err, "expected error on signing conflicting proposal")
  136. }
  137. // try signing a proposal with a different time stamp
  138. sig := proposal.Signature
  139. proposal.Timestamp = proposal.Timestamp.Add(time.Duration(1000))
  140. err = privVal.SignProposal("mychainid", proposal)
  141. assert.NoError(err)
  142. assert.Equal(sig, proposal.Signature)
  143. }
  144. func newVote(addr data.Bytes, idx int, height int64, round int, typ byte, blockID BlockID) *Vote {
  145. return &Vote{
  146. ValidatorAddress: addr,
  147. ValidatorIndex: idx,
  148. Height: height,
  149. Round: round,
  150. Type: typ,
  151. Timestamp: time.Now().UTC(),
  152. BlockID: blockID,
  153. }
  154. }
  155. func newProposal(height int64, round int, partsHeader PartSetHeader) *Proposal {
  156. return &Proposal{
  157. Height: height,
  158. Round: round,
  159. BlockPartsHeader: partsHeader,
  160. }
  161. }