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.

244 lines
7.4 KiB

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