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.

282 lines
8.4 KiB

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