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.

75 lines
1.9 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(height int64, blockID BlockID, valSet *ValidatorSet, privVal PrivValidator, chainID string) (*Vote, error) {
  34. addr := privVal.GetPubKey().Address()
  35. idx, _ := valSet.GetByAddress(addr)
  36. vote := &Vote{
  37. ValidatorAddress: addr,
  38. ValidatorIndex: idx,
  39. Height: height,
  40. Round: 0,
  41. Timestamp: tmtime.Now(),
  42. Type: PrecommitType,
  43. BlockID: blockID,
  44. }
  45. if err := privVal.SignVote(chainID, vote); err != nil {
  46. return nil, err
  47. }
  48. return vote, nil
  49. }
  50. // MakeBlock returns a new block with an empty header, except what can be
  51. // computed from itself.
  52. // It populates the same set of fields validated by ValidateBasic.
  53. func MakeBlock(height int64, txs []Tx, lastCommit *Commit, evidence []Evidence) *Block {
  54. block := &Block{
  55. Header: Header{
  56. Height: height,
  57. NumTxs: int64(len(txs)),
  58. },
  59. Data: Data{
  60. Txs: txs,
  61. },
  62. Evidence: EvidenceData{Evidence: evidence},
  63. LastCommit: lastCommit,
  64. }
  65. block.fillHeader()
  66. return block
  67. }