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.

90 lines
2.0 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. )
  6. func MakeCommit(blockID BlockID, height int64, round int,
  7. voteSet *VoteSet, validators []PrivValidator, now time.Time) (*Commit, error) {
  8. // all sign
  9. for i := 0; i < len(validators); i++ {
  10. pubKey, err := validators[i].GetPubKey()
  11. if err != nil {
  12. return nil, errors.Wrap(err, "can't get pubkey")
  13. }
  14. vote := &Vote{
  15. ValidatorAddress: pubKey.Address(),
  16. ValidatorIndex: i,
  17. Height: height,
  18. Round: round,
  19. Type: PrecommitType,
  20. BlockID: blockID,
  21. Timestamp: now,
  22. }
  23. _, err = signAddVote(validators[i], vote, voteSet)
  24. if err != nil {
  25. return nil, err
  26. }
  27. }
  28. return voteSet.MakeCommit(), nil
  29. }
  30. func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bool, err error) {
  31. err = privVal.SignVote(voteSet.ChainID(), vote)
  32. if err != nil {
  33. return false, err
  34. }
  35. return voteSet.AddVote(vote)
  36. }
  37. func MakeVote(
  38. height int64,
  39. blockID BlockID,
  40. valSet *ValidatorSet,
  41. privVal PrivValidator,
  42. chainID string,
  43. now time.Time,
  44. ) (*Vote, error) {
  45. pubKey, err := privVal.GetPubKey()
  46. if err != nil {
  47. return nil, errors.Wrap(err, "can't get pubkey")
  48. }
  49. addr := pubKey.Address()
  50. idx, _ := valSet.GetByAddress(addr)
  51. vote := &Vote{
  52. ValidatorAddress: addr,
  53. ValidatorIndex: idx,
  54. Height: height,
  55. Round: 0,
  56. Timestamp: now,
  57. Type: PrecommitType,
  58. BlockID: blockID,
  59. }
  60. if err := privVal.SignVote(chainID, vote); err != nil {
  61. return nil, err
  62. }
  63. return vote, nil
  64. }
  65. // MakeBlock returns a new block with an empty header, except what can be
  66. // computed from itself.
  67. // It populates the same set of fields validated by ValidateBasic.
  68. func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
  69. block := &Block{
  70. Header: Header{
  71. Height: height,
  72. },
  73. Data: Data{
  74. Txs: txs,
  75. },
  76. Evidence: EvidenceData{Evidence: evidence},
  77. LastCommit: lastCommit,
  78. }
  79. block.fillHeader()
  80. return block
  81. }