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.

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