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.

253 lines
7.8 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
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. cmn "github.com/tendermint/tmlibs/common"
  13. )
  14. func TestGenLoadValidator(t *testing.T) {
  15. assert := assert.New(t)
  16. _, tempFilePath := cmn.Tempfile("priv_validator_")
  17. privVal := GenPrivValidatorFS(tempFilePath)
  18. height := int64(100)
  19. privVal.LastHeight = height
  20. privVal.Save()
  21. addr := privVal.GetAddress()
  22. privVal = LoadPrivValidatorFS(tempFilePath)
  23. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  24. assert.Equal(height, privVal.LastHeight, "expected privval.LastHeight to have been saved")
  25. }
  26. func TestLoadOrGenValidator(t *testing.T) {
  27. assert := assert.New(t)
  28. _, tempFilePath := cmn.Tempfile("priv_validator_")
  29. if err := os.Remove(tempFilePath); err != nil {
  30. t.Error(err)
  31. }
  32. privVal := LoadOrGenPrivValidatorFS(tempFilePath)
  33. addr := privVal.GetAddress()
  34. privVal = LoadOrGenPrivValidatorFS(tempFilePath)
  35. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  36. }
  37. func TestUnmarshalValidator(t *testing.T) {
  38. assert, require := assert.New(t), require.New(t)
  39. // create some fixed values
  40. addrStr := "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456"
  41. pubStr := "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  42. privStr := "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  43. addrBytes, _ := hex.DecodeString(addrStr)
  44. pubBytes, _ := hex.DecodeString(pubStr)
  45. privBytes, _ := hex.DecodeString(privStr)
  46. // prepend type byte
  47. pubKey, err := crypto.PubKeyFromBytes(append([]byte{1}, pubBytes...))
  48. require.Nil(err, "%+v", err)
  49. privKey, err := crypto.PrivKeyFromBytes(append([]byte{1}, privBytes...))
  50. require.Nil(err, "%+v", err)
  51. serialized := fmt.Sprintf(`{
  52. "address": "%s",
  53. "pub_key": {
  54. "type": "ed25519",
  55. "data": "%s"
  56. },
  57. "last_height": 0,
  58. "last_round": 0,
  59. "last_step": 0,
  60. "last_signature": null,
  61. "priv_key": {
  62. "type": "ed25519",
  63. "data": "%s"
  64. }
  65. }`, addrStr, pubStr, privStr)
  66. val := PrivValidatorFS{}
  67. err = json.Unmarshal([]byte(serialized), &val)
  68. require.Nil(err, "%+v", err)
  69. // make sure the values match
  70. assert.EqualValues(addrBytes, val.GetAddress())
  71. assert.EqualValues(pubKey, val.GetPubKey())
  72. assert.EqualValues(privKey, val.PrivKey)
  73. // export it and make sure it is the same
  74. out, err := json.Marshal(val)
  75. require.Nil(err, "%+v", err)
  76. assert.JSONEq(serialized, string(out))
  77. }
  78. func TestSignVote(t *testing.T) {
  79. assert := assert.New(t)
  80. _, tempFilePath := cmn.Tempfile("priv_validator_")
  81. privVal := GenPrivValidatorFS(tempFilePath)
  82. block1 := BlockID{[]byte{1, 2, 3}, PartSetHeader{}}
  83. block2 := BlockID{[]byte{3, 2, 1}, PartSetHeader{}}
  84. height, round := int64(10), 1
  85. voteType := VoteTypePrevote
  86. // sign a vote for first time
  87. vote := newVote(privVal.Address, 0, height, round, voteType, block1)
  88. err := privVal.SignVote("mychainid", vote)
  89. assert.NoError(err, "expected no error signing vote")
  90. // try to sign the same vote again; should be fine
  91. err = privVal.SignVote("mychainid", vote)
  92. assert.NoError(err, "expected no error on signing same vote")
  93. // now try some bad votes
  94. cases := []*Vote{
  95. newVote(privVal.Address, 0, height, round-1, voteType, block1), // round regression
  96. newVote(privVal.Address, 0, height-1, round, voteType, block1), // height regression
  97. newVote(privVal.Address, 0, height-2, round+4, voteType, block1), // height regression and different round
  98. newVote(privVal.Address, 0, height, round, voteType, block2), // different block
  99. }
  100. for _, c := range cases {
  101. err = privVal.SignVote("mychainid", c)
  102. assert.Error(err, "expected error on signing conflicting vote")
  103. }
  104. // try signing a vote with a different time stamp
  105. sig := vote.Signature
  106. vote.Timestamp = vote.Timestamp.Add(time.Duration(1000))
  107. err = privVal.SignVote("mychainid", vote)
  108. assert.NoError(err)
  109. assert.Equal(sig, vote.Signature)
  110. }
  111. func TestSignProposal(t *testing.T) {
  112. assert := assert.New(t)
  113. _, tempFilePath := cmn.Tempfile("priv_validator_")
  114. privVal := GenPrivValidatorFS(tempFilePath)
  115. block1 := PartSetHeader{5, []byte{1, 2, 3}}
  116. block2 := PartSetHeader{10, []byte{3, 2, 1}}
  117. height, round := int64(10), 1
  118. // sign a proposal for first time
  119. proposal := newProposal(height, round, block1)
  120. err := privVal.SignProposal("mychainid", proposal)
  121. assert.NoError(err, "expected no error signing proposal")
  122. // try to sign the same proposal again; should be fine
  123. err = privVal.SignProposal("mychainid", proposal)
  124. assert.NoError(err, "expected no error on signing same proposal")
  125. // now try some bad Proposals
  126. cases := []*Proposal{
  127. newProposal(height, round-1, block1), // round regression
  128. newProposal(height-1, round, block1), // height regression
  129. newProposal(height-2, round+4, block1), // height regression and different round
  130. newProposal(height, round, block2), // different block
  131. }
  132. for _, c := range cases {
  133. err = privVal.SignProposal("mychainid", c)
  134. assert.Error(err, "expected error on signing conflicting proposal")
  135. }
  136. // try signing a proposal with a different time stamp
  137. sig := proposal.Signature
  138. proposal.Timestamp = proposal.Timestamp.Add(time.Duration(1000))
  139. err = privVal.SignProposal("mychainid", proposal)
  140. assert.NoError(err)
  141. assert.Equal(sig, proposal.Signature)
  142. }
  143. func TestDifferByTimestamp(t *testing.T) {
  144. _, tempFilePath := cmn.Tempfile("priv_validator_")
  145. privVal := GenPrivValidatorFS(tempFilePath)
  146. block1 := PartSetHeader{5, []byte{1, 2, 3}}
  147. height, round := int64(10), 1
  148. chainID := "mychainid"
  149. // test proposal
  150. {
  151. proposal := newProposal(height, round, block1)
  152. err := privVal.SignProposal(chainID, proposal)
  153. assert.NoError(t, err, "expected no error signing proposal")
  154. signBytes := SignBytes(chainID, proposal)
  155. sig := proposal.Signature
  156. timeStamp := clipToMS(proposal.Timestamp)
  157. // manipulate the timestamp. should get changed back
  158. proposal.Timestamp = proposal.Timestamp.Add(time.Millisecond)
  159. proposal.Signature = crypto.Signature{}
  160. err = privVal.SignProposal("mychainid", proposal)
  161. assert.NoError(t, err, "expected no error on signing same proposal")
  162. assert.Equal(t, timeStamp, proposal.Timestamp)
  163. assert.Equal(t, signBytes, SignBytes(chainID, proposal))
  164. assert.Equal(t, sig, proposal.Signature)
  165. }
  166. // test vote
  167. {
  168. voteType := VoteTypePrevote
  169. blockID := BlockID{[]byte{1, 2, 3}, PartSetHeader{}}
  170. vote := newVote(privVal.Address, 0, height, round, voteType, blockID)
  171. err := privVal.SignVote("mychainid", vote)
  172. assert.NoError(t, err, "expected no error signing vote")
  173. signBytes := SignBytes(chainID, vote)
  174. sig := vote.Signature
  175. timeStamp := clipToMS(vote.Timestamp)
  176. // manipulate the timestamp. should get changed back
  177. vote.Timestamp = vote.Timestamp.Add(time.Millisecond)
  178. vote.Signature = crypto.Signature{}
  179. err = privVal.SignVote("mychainid", vote)
  180. assert.NoError(t, err, "expected no error on signing same vote")
  181. assert.Equal(t, timeStamp, vote.Timestamp)
  182. assert.Equal(t, signBytes, SignBytes(chainID, vote))
  183. assert.Equal(t, sig, vote.Signature)
  184. }
  185. }
  186. func newVote(addr Address, idx int, height int64, round int, typ byte, blockID BlockID) *Vote {
  187. return &Vote{
  188. ValidatorAddress: addr,
  189. ValidatorIndex: idx,
  190. Height: height,
  191. Round: round,
  192. Type: typ,
  193. Timestamp: time.Now().UTC(),
  194. BlockID: blockID,
  195. }
  196. }
  197. func newProposal(height int64, round int, partsHeader PartSetHeader) *Proposal {
  198. return &Proposal{
  199. Height: height,
  200. Round: round,
  201. BlockPartsHeader: partsHeader,
  202. Timestamp: time.Now().UTC(),
  203. }
  204. }
  205. func clipToMS(t time.Time) time.Time {
  206. nano := t.UnixNano()
  207. million := int64(1000000)
  208. nano = (nano / million) * million
  209. return time.Unix(0, nano).UTC()
  210. }