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.

81 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
7 years ago
7 years ago
  1. package types
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  7. )
  8. func MakeCommit(blockID BlockID, height int64, round int32,
  9. voteSet *VoteSet, validators []PrivValidator, now time.Time) (*Commit, error) {
  10. // all sign
  11. for i := 0; i < len(validators); i++ {
  12. pubKey, err := validators[i].GetPubKey(context.Background())
  13. if err != nil {
  14. return nil, fmt.Errorf("can't get pubkey: %w", err)
  15. }
  16. vote := &Vote{
  17. ValidatorAddress: pubKey.Address(),
  18. ValidatorIndex: int32(i),
  19. Height: height,
  20. Round: round,
  21. Type: tmproto.PrecommitType,
  22. BlockID: blockID,
  23. Timestamp: now,
  24. }
  25. _, err = signAddVote(validators[i], vote, voteSet)
  26. if err != nil {
  27. return nil, err
  28. }
  29. }
  30. return voteSet.MakeCommit(), nil
  31. }
  32. func signAddVote(privVal PrivValidator, vote *Vote, voteSet *VoteSet) (signed bool, err error) {
  33. v := vote.ToProto()
  34. err = privVal.SignVote(context.Background(), voteSet.ChainID(), v)
  35. if err != nil {
  36. return false, err
  37. }
  38. vote.Signature = v.Signature
  39. return voteSet.AddVote(vote)
  40. }
  41. func MakeVote(
  42. height int64,
  43. blockID BlockID,
  44. valSet *ValidatorSet,
  45. privVal PrivValidator,
  46. chainID string,
  47. now time.Time,
  48. ) (*Vote, error) {
  49. pubKey, err := privVal.GetPubKey(context.Background())
  50. if err != nil {
  51. return nil, fmt.Errorf("can't get pubkey: %w", err)
  52. }
  53. addr := pubKey.Address()
  54. idx, _ := valSet.GetByAddress(addr)
  55. vote := &Vote{
  56. ValidatorAddress: addr,
  57. ValidatorIndex: idx,
  58. Height: height,
  59. Round: 0,
  60. Timestamp: now,
  61. Type: tmproto.PrecommitType,
  62. BlockID: blockID,
  63. }
  64. v := vote.ToProto()
  65. if err := privVal.SignVote(context.Background(), chainID, v); err != nil {
  66. return nil, err
  67. }
  68. vote.Signature = v.Signature
  69. return vote, nil
  70. }