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.

91 lines
2.1 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. "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. err = privVal.SignVote(voteSet.ChainID(), vote)
  33. if err != nil {
  34. return false, err
  35. }
  36. return voteSet.AddVote(vote)
  37. }
  38. func MakeVote(
  39. height int64,
  40. blockID BlockID,
  41. valSet *ValidatorSet,
  42. privVal PrivValidator,
  43. chainID string,
  44. now time.Time,
  45. ) (*Vote, error) {
  46. pubKey, err := privVal.GetPubKey()
  47. if err != nil {
  48. return nil, fmt.Errorf("can't get pubkey: %w", err)
  49. }
  50. addr := pubKey.Address()
  51. idx, _ := valSet.GetByAddress(addr)
  52. vote := &Vote{
  53. ValidatorAddress: addr,
  54. ValidatorIndex: idx,
  55. Height: height,
  56. Round: 0,
  57. Timestamp: now,
  58. Type: tmproto.PrecommitType,
  59. BlockID: blockID,
  60. }
  61. if err := privVal.SignVote(chainID, vote); err != nil {
  62. return nil, err
  63. }
  64. return vote, nil
  65. }
  66. // MakeBlock returns a new block with an empty header, except what can be
  67. // computed from itself.
  68. // It populates the same set of fields validated by ValidateBasic.
  69. func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
  70. block := &Block{
  71. Header: Header{
  72. Height: height,
  73. },
  74. Data: Data{
  75. Txs: txs,
  76. },
  77. Evidence: EvidenceData{Evidence: evidence},
  78. LastCommit: lastCommit,
  79. }
  80. block.fillHeader()
  81. return block
  82. }