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.

176 lines
5.3 KiB

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. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. crypto "github.com/tendermint/go-crypto"
  11. "github.com/tendermint/go-wire/data"
  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 := 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. os.Remove(tempFilePath)
  30. privVal := LoadOrGenPrivValidatorFS(tempFilePath)
  31. addr := privVal.GetAddress()
  32. privVal = LoadOrGenPrivValidatorFS(tempFilePath)
  33. assert.Equal(addr, privVal.GetAddress(), "expected privval addr to be the same")
  34. }
  35. func TestUnmarshalValidator(t *testing.T) {
  36. assert, require := assert.New(t), require.New(t)
  37. // create some fixed values
  38. addrStr := "D028C9981F7A87F3093672BF0D5B0E2A1B3ED456"
  39. pubStr := "3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  40. privStr := "27F82582AEFAE7AB151CFB01C48BB6C1A0DA78F9BDDA979A9F70A84D074EB07D3B3069C422E19688B45CBFAE7BB009FC0FA1B1EA86593519318B7214853803C8"
  41. addrBytes, _ := hex.DecodeString(addrStr)
  42. pubBytes, _ := hex.DecodeString(pubStr)
  43. privBytes, _ := hex.DecodeString(privStr)
  44. // prepend type byte
  45. pubKey, err := crypto.PubKeyFromBytes(append([]byte{1}, pubBytes...))
  46. require.Nil(err, "%+v", err)
  47. privKey, err := crypto.PrivKeyFromBytes(append([]byte{1}, privBytes...))
  48. require.Nil(err, "%+v", err)
  49. serialized := fmt.Sprintf(`{
  50. "address": "%s",
  51. "pub_key": {
  52. "type": "ed25519",
  53. "data": "%s"
  54. },
  55. "last_height": 0,
  56. "last_round": 0,
  57. "last_step": 0,
  58. "last_signature": null,
  59. "priv_key": {
  60. "type": "ed25519",
  61. "data": "%s"
  62. }
  63. }`, addrStr, pubStr, privStr)
  64. val := PrivValidatorFS{}
  65. err = json.Unmarshal([]byte(serialized), &val)
  66. require.Nil(err, "%+v", err)
  67. // make sure the values match
  68. assert.EqualValues(addrBytes, val.GetAddress())
  69. assert.EqualValues(pubKey, val.GetPubKey())
  70. assert.EqualValues(privKey, val.PrivKey)
  71. // export it and make sure it is the same
  72. out, err := json.Marshal(val)
  73. require.Nil(err, "%+v", err)
  74. assert.JSONEq(serialized, string(out))
  75. }
  76. func TestSignVote(t *testing.T) {
  77. assert := assert.New(t)
  78. _, tempFilePath := cmn.Tempfile("priv_validator_")
  79. privVal := GenPrivValidatorFS(tempFilePath)
  80. block1 := BlockID{[]byte{1, 2, 3}, PartSetHeader{}}
  81. block2 := BlockID{[]byte{3, 2, 1}, PartSetHeader{}}
  82. height, round := 10, 1
  83. voteType := VoteTypePrevote
  84. // sign a vote for first time
  85. vote := newVote(privVal.Address, 0, height, round, voteType, block1)
  86. err := privVal.SignVote("mychainid", vote)
  87. assert.NoError(err, "expected no error signing vote")
  88. // try to sign the same vote again; should be fine
  89. err = privVal.SignVote("mychainid", vote)
  90. assert.NoError(err, "expected no error on signing same vote")
  91. // now try some bad votes
  92. cases := []*Vote{
  93. newVote(privVal.Address, 0, height, round-1, voteType, block1), // round regression
  94. newVote(privVal.Address, 0, height-1, round, voteType, block1), // height regression
  95. newVote(privVal.Address, 0, height-2, round+4, voteType, block1), // height regression and different round
  96. newVote(privVal.Address, 0, height, round, voteType, block2), // different block
  97. }
  98. for _, c := range cases {
  99. err = privVal.SignVote("mychainid", c)
  100. assert.Error(err, "expected error on signing conflicting vote")
  101. }
  102. }
  103. func TestSignProposal(t *testing.T) {
  104. assert := assert.New(t)
  105. _, tempFilePath := cmn.Tempfile("priv_validator_")
  106. privVal := GenPrivValidatorFS(tempFilePath)
  107. block1 := PartSetHeader{5, []byte{1, 2, 3}}
  108. block2 := PartSetHeader{10, []byte{3, 2, 1}}
  109. height, round := 10, 1
  110. // sign a proposal for first time
  111. proposal := newProposal(height, round, block1)
  112. err := privVal.SignProposal("mychainid", proposal)
  113. assert.NoError(err, "expected no error signing proposal")
  114. // try to sign the same proposal again; should be fine
  115. err = privVal.SignProposal("mychainid", proposal)
  116. assert.NoError(err, "expected no error on signing same proposal")
  117. // now try some bad Proposals
  118. cases := []*Proposal{
  119. newProposal(height, round-1, block1), // round regression
  120. newProposal(height-1, round, block1), // height regression
  121. newProposal(height-2, round+4, block1), // height regression and different round
  122. newProposal(height, round, block2), // different block
  123. }
  124. for _, c := range cases {
  125. err = privVal.SignProposal("mychainid", c)
  126. assert.Error(err, "expected error on signing conflicting proposal")
  127. }
  128. }
  129. func newVote(addr data.Bytes, idx, height, round int, typ byte, blockID BlockID) *Vote {
  130. return &Vote{
  131. ValidatorAddress: addr,
  132. ValidatorIndex: idx,
  133. Height: height,
  134. Round: round,
  135. Type: typ,
  136. BlockID: blockID,
  137. }
  138. }
  139. func newProposal(height, round int, partsHeader PartSetHeader) *Proposal {
  140. return &Proposal{
  141. Height: height,
  142. Round: round,
  143. BlockPartsHeader: partsHeader,
  144. }
  145. }