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.

157 lines
4.7 KiB

  1. package lite
  2. import (
  3. "github.com/tendermint/tendermint/crypto"
  4. "github.com/tendermint/tendermint/crypto/ed25519"
  5. "github.com/tendermint/tendermint/crypto/secp256k1"
  6. "github.com/tendermint/tendermint/types"
  7. tmtime "github.com/tendermint/tendermint/types/time"
  8. )
  9. // privKeys is a helper type for testing.
  10. //
  11. // It lets us simulate signing with many keys. The main use case is to create
  12. // a set, and call GenSignedHeader to get properly signed header for testing.
  13. //
  14. // You can set different weights of validators each time you call ToValidators,
  15. // and can optionally extend the validator set later with Extend.
  16. type privKeys []crypto.PrivKey
  17. // genPrivKeys produces an array of private keys to generate commits.
  18. func genPrivKeys(n int) privKeys {
  19. res := make(privKeys, n)
  20. for i := range res {
  21. res[i] = ed25519.GenPrivKey()
  22. }
  23. return res
  24. }
  25. // Change replaces the key at index i.
  26. func (pkz privKeys) Change(i int) privKeys {
  27. res := make(privKeys, len(pkz))
  28. copy(res, pkz)
  29. res[i] = ed25519.GenPrivKey()
  30. return res
  31. }
  32. // Extend adds n more keys (to remove, just take a slice).
  33. func (pkz privKeys) Extend(n int) privKeys {
  34. extra := genPrivKeys(n)
  35. return append(pkz, extra...)
  36. }
  37. // GenSecpPrivKeys produces an array of secp256k1 private keys to generate commits.
  38. func GenSecpPrivKeys(n int) privKeys {
  39. res := make(privKeys, n)
  40. for i := range res {
  41. res[i] = secp256k1.GenPrivKey()
  42. }
  43. return res
  44. }
  45. // ExtendSecp adds n more secp256k1 keys (to remove, just take a slice).
  46. func (pkz privKeys) ExtendSecp(n int) privKeys {
  47. extra := GenSecpPrivKeys(n)
  48. return append(pkz, extra...)
  49. }
  50. // ToValidators produces a valset 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 (pkz privKeys) ToValidators(init, inc int64) *types.ValidatorSet {
  55. res := make([]*types.Validator, len(pkz))
  56. for i, k := range pkz {
  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 (pkz privKeys) signHeader(header *types.Header, first, last int) *types.Commit {
  63. votes := make([]*types.Vote, len(pkz))
  64. // We need this list to keep the ordering.
  65. vset := pkz.ToValidators(1, 0)
  66. // Fill in the votes we want.
  67. for i := first; i < last && i < len(pkz); i++ {
  68. vote := makeVote(header, vset, pkz[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, valset *types.ValidatorSet, key crypto.PrivKey) *types.Vote {
  78. addr := key.PubKey().Address()
  79. idx, _ := valset.GetByAddress(addr)
  80. vote := &types.Vote{
  81. ValidatorAddress: addr,
  82. ValidatorIndex: idx,
  83. Height: header.Height,
  84. Round: 1,
  85. Timestamp: tmtime.Now(),
  86. Type: types.PrecommitType,
  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. func genHeader(chainID string, height int64, txs types.Txs,
  100. valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte) *types.Header {
  101. return &types.Header{
  102. ChainID: chainID,
  103. Height: height,
  104. Time: tmtime.Now(),
  105. NumTxs: int64(len(txs)),
  106. TotalTxs: int64(len(txs)),
  107. // LastBlockID
  108. // LastCommitHash
  109. ValidatorsHash: valset.Hash(),
  110. NextValidatorsHash: nextValset.Hash(),
  111. DataHash: txs.Hash(),
  112. AppHash: appHash,
  113. ConsensusHash: consHash,
  114. LastResultsHash: resHash,
  115. }
  116. }
  117. // GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader.
  118. func (pkz privKeys) GenSignedHeader(chainID string, height int64, txs types.Txs,
  119. valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) types.SignedHeader {
  120. header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash)
  121. check := types.SignedHeader{
  122. Header: header,
  123. Commit: pkz.signHeader(header, first, last),
  124. }
  125. return check
  126. }
  127. // GenFullCommit calls genHeader and signHeader and combines them into a FullCommit.
  128. func (pkz privKeys) GenFullCommit(chainID string, height int64, txs types.Txs,
  129. valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) FullCommit {
  130. header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash)
  131. commit := types.SignedHeader{
  132. Header: header,
  133. Commit: pkz.signHeader(header, first, last),
  134. }
  135. return NewFullCommit(commit, valset, nextValset)
  136. }