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.

152 lines
4.3 KiB

  1. package lite
  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 properly 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. Timestamp: time.Now().UTC(),
  86. Type: types.VoteTypePrecommit,
  87. BlockID: types.BlockID{Hash: header.Hash()},
  88. }
  89. // Sign it
  90. signBytes := types.SignBytes(header.ChainID, vote)
  91. vote.Signature = key.Sign(signBytes)
  92. return vote
  93. }
  94. // Silences warning that vals can also be merkle.Hashable
  95. // nolint: interfacer
  96. func genHeader(chainID string, height int64, txs types.Txs,
  97. vals *types.ValidatorSet, appHash, consHash []byte) *types.Header {
  98. return &types.Header{
  99. ChainID: chainID,
  100. Height: height,
  101. Time: time.Now(),
  102. NumTxs: int64(len(txs)),
  103. TotalTxs: int64(len(txs)),
  104. // LastBlockID
  105. // LastCommitHash
  106. ValidatorsHash: vals.Hash(),
  107. DataHash: txs.Hash(),
  108. AppHash: appHash,
  109. ConsensusHash: consHash,
  110. }
  111. }
  112. // GenCommit calls genHeader and signHeader and combines them into a Commit.
  113. func (v ValKeys) GenCommit(chainID string, height int64, txs types.Txs,
  114. vals *types.ValidatorSet, appHash, consHash []byte, first, last int) Commit {
  115. header := genHeader(chainID, height, txs, vals, appHash, consHash)
  116. check := Commit{
  117. Header: header,
  118. Commit: v.signHeader(header, first, last),
  119. }
  120. return check
  121. }
  122. // GenFullCommit calls genHeader and signHeader and combines them into a Commit.
  123. func (v ValKeys) GenFullCommit(chainID string, height int64, txs types.Txs,
  124. vals *types.ValidatorSet, appHash, consHash []byte, first, last int) FullCommit {
  125. header := genHeader(chainID, height, txs, vals, appHash, consHash)
  126. commit := Commit{
  127. Header: header,
  128. Commit: v.signHeader(header, first, last),
  129. }
  130. return NewFullCommit(commit, vals)
  131. }