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.

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