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.

153 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. commitSigs := make([]*types.CommitSig, 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. commitSigs[vote.ValidatorIndex] = vote.CommitSig()
  70. }
  71. blockID := types.BlockID{Hash: header.Hash()}
  72. return types.NewCommit(blockID, commitSigs)
  73. }
  74. func makeVote(header *types.Header, valset *types.ValidatorSet, key crypto.PrivKey) *types.Vote {
  75. addr := key.PubKey().Address()
  76. idx, _ := valset.GetByAddress(addr)
  77. vote := &types.Vote{
  78. ValidatorAddress: addr,
  79. ValidatorIndex: idx,
  80. Height: header.Height,
  81. Round: 1,
  82. Timestamp: tmtime.Now(),
  83. Type: types.PrecommitType,
  84. BlockID: types.BlockID{Hash: header.Hash()},
  85. }
  86. // Sign it
  87. signBytes := vote.SignBytes(header.ChainID)
  88. // TODO Consider reworking makeVote API to return an error
  89. sig, err := key.Sign(signBytes)
  90. if err != nil {
  91. panic(err)
  92. }
  93. vote.Signature = sig
  94. return vote
  95. }
  96. func genHeader(chainID string, height int64, txs types.Txs,
  97. valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte) *types.Header {
  98. return &types.Header{
  99. ChainID: chainID,
  100. Height: height,
  101. Time: tmtime.Now(),
  102. NumTxs: int64(len(txs)),
  103. TotalTxs: int64(len(txs)),
  104. // LastBlockID
  105. // LastCommitHash
  106. ValidatorsHash: valset.Hash(),
  107. NextValidatorsHash: nextValset.Hash(),
  108. DataHash: txs.Hash(),
  109. AppHash: appHash,
  110. ConsensusHash: consHash,
  111. LastResultsHash: resHash,
  112. }
  113. }
  114. // GenSignedHeader calls genHeader and signHeader and combines them into a SignedHeader.
  115. func (pkz privKeys) GenSignedHeader(chainID string, height int64, txs types.Txs,
  116. valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) types.SignedHeader {
  117. header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash)
  118. check := types.SignedHeader{
  119. Header: header,
  120. Commit: pkz.signHeader(header, first, last),
  121. }
  122. return check
  123. }
  124. // GenFullCommit calls genHeader and signHeader and combines them into a FullCommit.
  125. func (pkz privKeys) GenFullCommit(chainID string, height int64, txs types.Txs,
  126. valset, nextValset *types.ValidatorSet, appHash, consHash, resHash []byte, first, last int) FullCommit {
  127. header := genHeader(chainID, height, txs, valset, nextValset, appHash, consHash, resHash)
  128. commit := types.SignedHeader{
  129. Header: header,
  130. Commit: pkz.signHeader(header, first, last),
  131. }
  132. return NewFullCommit(commit, valset, nextValset)
  133. }