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.

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