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.

147 lines
4.1 KiB

  1. package certifiers
  2. import (
  3. "time"
  4. crypto "github.com/tendermint/go-crypto"
  5. "github.com/tendermint/tendermint/types"
  6. )
  7. // ValKeys is a helper for testing.
  8. //
  9. // It lets us simulate signing with many keys, either ed25519 or secp256k1.
  10. // The main use case is to create a set, and call GenCommit
  11. // to get propely signed header for testing.
  12. //
  13. // You can set different weights of validators each time you call
  14. // ToValidators, and can optionally extend the validator set later
  15. // with Extend or ExtendSecp
  16. type ValKeys []crypto.PrivKey
  17. // GenValKeys produces an array of private keys to generate commits
  18. func GenValKeys(n int) ValKeys {
  19. res := make(ValKeys, n)
  20. for i := range res {
  21. res[i] = crypto.GenPrivKeyEd25519().Wrap()
  22. }
  23. return res
  24. }
  25. // Change replaces the key at index i
  26. func (v ValKeys) Change(i int) ValKeys {
  27. res := make(ValKeys, len(v))
  28. copy(res, v)
  29. res[i] = crypto.GenPrivKeyEd25519().Wrap()
  30. return res
  31. }
  32. // Extend adds n more keys (to remove, just take a slice)
  33. func (v ValKeys) Extend(n int) ValKeys {
  34. extra := GenValKeys(n)
  35. return append(v, extra...)
  36. }
  37. // GenSecpValKeys produces an array of secp256k1 private keys to generate commits
  38. func GenSecpValKeys(n int) ValKeys {
  39. res := make(ValKeys, n)
  40. for i := range res {
  41. res[i] = crypto.GenPrivKeySecp256k1().Wrap()
  42. }
  43. return res
  44. }
  45. // ExtendSecp adds n more secp256k1 keys (to remove, just take a slice)
  46. func (v ValKeys) ExtendSecp(n int) ValKeys {
  47. extra := GenSecpValKeys(n)
  48. return append(v, extra...)
  49. }
  50. // ToValidators produces a list of validators from the set of keys
  51. // The first key has weight `init` and it increases by `inc` every step
  52. // so we can have all the same weight, or a simple linear distribution
  53. // (should be enough for testing)
  54. func (v ValKeys) ToValidators(init, inc int64) *types.ValidatorSet {
  55. res := make([]*types.Validator, len(v))
  56. for i, k := range v {
  57. res[i] = types.NewValidator(k.PubKey(), init+int64(i)*inc)
  58. }
  59. return types.NewValidatorSet(res)
  60. }
  61. // signHeader properly signs the header with all keys from first to last exclusive
  62. func (v ValKeys) signHeader(header *types.Header, first, last int) *types.Commit {
  63. votes := make([]*types.Vote, len(v))
  64. // we need this list to keep the ordering...
  65. vset := v.ToValidators(1, 0)
  66. // fill in the votes we want
  67. for i := first; i < last; i++ {
  68. vote := makeVote(header, vset, v[i])
  69. votes[vote.ValidatorIndex] = vote
  70. }
  71. res := &types.Commit{
  72. BlockID: types.BlockID{Hash: header.Hash()},
  73. Precommits: votes,
  74. }
  75. return res
  76. }
  77. func makeVote(header *types.Header, vals *types.ValidatorSet, key crypto.PrivKey) *types.Vote {
  78. addr := key.PubKey().Address()
  79. idx, _ := vals.GetByAddress(addr)
  80. vote := &types.Vote{
  81. ValidatorAddress: addr,
  82. ValidatorIndex: idx,
  83. Height: header.Height,
  84. Round: 1,
  85. Type: types.VoteTypePrecommit,
  86. BlockID: types.BlockID{Hash: header.Hash()},
  87. }
  88. // Sign it
  89. signBytes := types.SignBytes(header.ChainID, vote)
  90. vote.Signature = key.Sign(signBytes)
  91. return vote
  92. }
  93. func genHeader(chainID string, height int, txs types.Txs,
  94. vals *types.ValidatorSet, appHash []byte) *types.Header {
  95. return &types.Header{
  96. ChainID: chainID,
  97. Height: height,
  98. Time: time.Now(),
  99. NumTxs: len(txs),
  100. // LastBlockID
  101. // LastCommitHash
  102. ValidatorsHash: vals.Hash(),
  103. DataHash: txs.Hash(),
  104. AppHash: appHash,
  105. }
  106. }
  107. // GenCommit calls genHeader and signHeader and combines them into a Commit
  108. func (v ValKeys) GenCommit(chainID string, height int, txs types.Txs,
  109. vals *types.ValidatorSet, appHash []byte, first, last int) Commit {
  110. header := genHeader(chainID, height, txs, vals, appHash)
  111. check := Commit{
  112. Header: header,
  113. Commit: v.signHeader(header, first, last),
  114. }
  115. return check
  116. }
  117. // GenFullCommit calls genHeader and signHeader and combines them into a Commit
  118. func (v ValKeys) GenFullCommit(chainID string, height int, txs types.Txs,
  119. vals *types.ValidatorSet, appHash []byte, first, last int) FullCommit {
  120. header := genHeader(chainID, height, txs, vals, appHash)
  121. commit := Commit{
  122. Header: header,
  123. Commit: v.signHeader(header, first, last),
  124. }
  125. return NewFullCommit(commit, vals)
  126. }