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.

251 lines
7.6 KiB

  1. package privval
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "github.com/tendermint/tendermint/crypto/ed25519"
  12. "github.com/tendermint/tendermint/types"
  13. tmtime "github.com/tendermint/tendermint/types/time"
  14. )
  15. func TestGenLoadValidator(t *testing.T) {
  16. assert := assert.New(t)
  17. tempFile, err := ioutil.TempFile("", "priv_validator_")
  18. require.Nil(t, err)
  19. privVal := GenFilePV(tempFile.Name())
  20. height := int64(100)
  21. privVal.LastHeight = height
  22. privVal.Save()
  23. addr := privVal.GetAddress()
  24. privVal = LoadFilePV(tempFile.Name())
  25. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  26. assert.Equal(height, privVal.LastHeight, "expected privval.LastHeight to have been saved")
  27. }
  28. func TestLoadOrGenValidator(t *testing.T) {
  29. assert := assert.New(t)
  30. tempFile, err := ioutil.TempFile("", "priv_validator_")
  31. require.Nil(t, err)
  32. tempFilePath := tempFile.Name()
  33. if err := os.Remove(tempFilePath); err != nil {
  34. t.Error(err)
  35. }
  36. privVal := LoadOrGenFilePV(tempFilePath)
  37. addr := privVal.GetAddress()
  38. privVal = LoadOrGenFilePV(tempFilePath)
  39. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  40. }
  41. func TestUnmarshalValidator(t *testing.T) {
  42. assert, require := assert.New(t), require.New(t)
  43. // create some fixed values
  44. privKey := ed25519.GenPrivKey()
  45. pubKey := privKey.PubKey()
  46. addr := pubKey.Address()
  47. pubArray := [32]byte(pubKey.(ed25519.PubKeyEd25519))
  48. pubBytes := pubArray[:]
  49. privArray := [64]byte(privKey)
  50. privBytes := privArray[:]
  51. pubB64 := base64.StdEncoding.EncodeToString(pubBytes)
  52. privB64 := base64.StdEncoding.EncodeToString(privBytes)
  53. serialized := fmt.Sprintf(`{
  54. "address": "%s",
  55. "pub_key": {
  56. "type": "tendermint/PubKeyEd25519",
  57. "value": "%s"
  58. },
  59. "last_height": "0",
  60. "last_round": "0",
  61. "last_step": 0,
  62. "priv_key": {
  63. "type": "tendermint/PrivKeyEd25519",
  64. "value": "%s"
  65. }
  66. }`, addr, pubB64, privB64)
  67. val := FilePV{}
  68. err := cdc.UnmarshalJSON([]byte(serialized), &val)
  69. require.Nil(err, "%+v", err)
  70. // make sure the values match
  71. assert.EqualValues(addr, 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 := cdc.MarshalJSON(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. tempFile, err := ioutil.TempFile("", "priv_validator_")
  82. require.Nil(t, err)
  83. privVal := GenFilePV(tempFile.Name())
  84. block1 := types.BlockID{[]byte{1, 2, 3}, types.PartSetHeader{}}
  85. block2 := types.BlockID{[]byte{3, 2, 1}, types.PartSetHeader{}}
  86. height, round := int64(10), 1
  87. voteType := types.VoteTypePrevote
  88. // sign a vote for first time
  89. vote := newVote(privVal.Address, 0, height, round, voteType, block1)
  90. err = privVal.SignVote("mychainid", vote)
  91. assert.NoError(err, "expected no error signing vote")
  92. // try to sign the same vote again; should be fine
  93. err = privVal.SignVote("mychainid", vote)
  94. assert.NoError(err, "expected no error on signing same vote")
  95. // now try some bad votes
  96. cases := []*types.Vote{
  97. newVote(privVal.Address, 0, height, round-1, voteType, block1), // round regression
  98. newVote(privVal.Address, 0, height-1, round, voteType, block1), // height regression
  99. newVote(privVal.Address, 0, height-2, round+4, voteType, block1), // height regression and different round
  100. newVote(privVal.Address, 0, height, round, voteType, block2), // different block
  101. }
  102. for _, c := range cases {
  103. err = privVal.SignVote("mychainid", c)
  104. assert.Error(err, "expected error on signing conflicting vote")
  105. }
  106. // try signing a vote with a different time stamp
  107. sig := vote.Signature
  108. vote.Timestamp = vote.Timestamp.Add(time.Duration(1000))
  109. err = privVal.SignVote("mychainid", vote)
  110. assert.NoError(err)
  111. assert.Equal(sig, vote.Signature)
  112. }
  113. func TestSignProposal(t *testing.T) {
  114. assert := assert.New(t)
  115. tempFile, err := ioutil.TempFile("", "priv_validator_")
  116. require.Nil(t, err)
  117. privVal := GenFilePV(tempFile.Name())
  118. block1 := types.PartSetHeader{5, []byte{1, 2, 3}}
  119. block2 := types.PartSetHeader{10, []byte{3, 2, 1}}
  120. height, round := int64(10), 1
  121. // sign a proposal for first time
  122. proposal := newProposal(height, round, block1)
  123. err = privVal.SignProposal("mychainid", proposal)
  124. assert.NoError(err, "expected no error signing proposal")
  125. // try to sign the same proposal again; should be fine
  126. err = privVal.SignProposal("mychainid", proposal)
  127. assert.NoError(err, "expected no error on signing same proposal")
  128. // now try some bad Proposals
  129. cases := []*types.Proposal{
  130. newProposal(height, round-1, block1), // round regression
  131. newProposal(height-1, round, block1), // height regression
  132. newProposal(height-2, round+4, block1), // height regression and different round
  133. newProposal(height, round, block2), // different block
  134. }
  135. for _, c := range cases {
  136. err = privVal.SignProposal("mychainid", c)
  137. assert.Error(err, "expected error on signing conflicting proposal")
  138. }
  139. // try signing a proposal with a different time stamp
  140. sig := proposal.Signature
  141. proposal.Timestamp = proposal.Timestamp.Add(time.Duration(1000))
  142. err = privVal.SignProposal("mychainid", proposal)
  143. assert.NoError(err)
  144. assert.Equal(sig, proposal.Signature)
  145. }
  146. func TestDifferByTimestamp(t *testing.T) {
  147. tempFile, err := ioutil.TempFile("", "priv_validator_")
  148. require.Nil(t, err)
  149. privVal := GenFilePV(tempFile.Name())
  150. block1 := types.PartSetHeader{5, []byte{1, 2, 3}}
  151. height, round := int64(10), 1
  152. chainID := "mychainid"
  153. // test proposal
  154. {
  155. proposal := newProposal(height, round, block1)
  156. err := privVal.SignProposal(chainID, proposal)
  157. assert.NoError(t, err, "expected no error signing proposal")
  158. signBytes := proposal.SignBytes(chainID)
  159. sig := proposal.Signature
  160. timeStamp := proposal.Timestamp
  161. // manipulate the timestamp. should get changed back
  162. proposal.Timestamp = proposal.Timestamp.Add(time.Millisecond)
  163. var emptySig []byte
  164. proposal.Signature = emptySig
  165. err = privVal.SignProposal("mychainid", proposal)
  166. assert.NoError(t, err, "expected no error on signing same proposal")
  167. assert.Equal(t, timeStamp, proposal.Timestamp)
  168. assert.Equal(t, signBytes, proposal.SignBytes(chainID))
  169. assert.Equal(t, sig, proposal.Signature)
  170. }
  171. // test vote
  172. {
  173. voteType := types.VoteTypePrevote
  174. blockID := types.BlockID{[]byte{1, 2, 3}, types.PartSetHeader{}}
  175. vote := newVote(privVal.Address, 0, height, round, voteType, blockID)
  176. err := privVal.SignVote("mychainid", vote)
  177. assert.NoError(t, err, "expected no error signing vote")
  178. signBytes := vote.SignBytes(chainID)
  179. sig := vote.Signature
  180. timeStamp := vote.Timestamp
  181. // manipulate the timestamp. should get changed back
  182. vote.Timestamp = vote.Timestamp.Add(time.Millisecond)
  183. var emptySig []byte
  184. vote.Signature = emptySig
  185. err = privVal.SignVote("mychainid", vote)
  186. assert.NoError(t, err, "expected no error on signing same vote")
  187. assert.Equal(t, timeStamp, vote.Timestamp)
  188. assert.Equal(t, signBytes, vote.SignBytes(chainID))
  189. assert.Equal(t, sig, vote.Signature)
  190. }
  191. }
  192. func newVote(addr types.Address, idx int, height int64, round int, typ byte, blockID types.BlockID) *types.Vote {
  193. return &types.Vote{
  194. ValidatorAddress: addr,
  195. ValidatorIndex: idx,
  196. Height: height,
  197. Round: round,
  198. Type: typ,
  199. Timestamp: tmtime.Now(),
  200. BlockID: blockID,
  201. }
  202. }
  203. func newProposal(height int64, round int, partsHeader types.PartSetHeader) *types.Proposal {
  204. return &types.Proposal{
  205. Height: height,
  206. Round: round,
  207. BlockPartsHeader: partsHeader,
  208. Timestamp: tmtime.Now(),
  209. }
  210. }