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.

159 lines
4.5 KiB

  1. package lite
  2. import (
  3. "time"
  4. crypto "github.com/tendermint/tendermint/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()
  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()
  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()
  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 < len(v); 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 := vote.SignBytes(header.ChainID)
  91. // TODO Consider reworking makeVote API to return an error
  92. sig, err := key.Sign(signBytes)
  93. if err != nil {
  94. panic(err)
  95. }
  96. vote.Signature = sig
  97. return vote
  98. }
  99. // Silences warning that vals can also be merkle.Hashable
  100. // nolint: interfacer
  101. func genHeader(chainID string, height int64, txs types.Txs,
  102. vals *types.ValidatorSet, appHash, consHash, resHash []byte) *types.Header {
  103. return &types.Header{
  104. ChainID: chainID,
  105. Height: height,
  106. Time: time.Now(),
  107. NumTxs: int64(len(txs)),
  108. TotalTxs: int64(len(txs)),
  109. // LastBlockID
  110. // LastCommitHash
  111. ValidatorsHash: vals.Hash(),
  112. DataHash: txs.Hash(),
  113. AppHash: appHash,
  114. ConsensusHash: consHash,
  115. LastResultsHash: resHash,
  116. }
  117. }
  118. // GenCommit calls genHeader and signHeader and combines them into a Commit.
  119. func (v ValKeys) GenCommit(chainID string, height int64, txs types.Txs,
  120. vals *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) Commit {
  121. header := genHeader(chainID, height, txs, vals, appHash, consHash, resHash)
  122. check := Commit{
  123. Header: header,
  124. Commit: v.signHeader(header, first, last),
  125. }
  126. return check
  127. }
  128. // GenFullCommit calls genHeader and signHeader and combines them into a Commit.
  129. func (v ValKeys) GenFullCommit(chainID string, height int64, txs types.Txs,
  130. vals *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) FullCommit {
  131. header := genHeader(chainID, height, txs, vals, appHash, consHash, resHash)
  132. commit := Commit{
  133. Header: header,
  134. Commit: v.signHeader(header, first, last),
  135. }
  136. return NewFullCommit(commit, vals)
  137. }