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.

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