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.

48 lines
1.2 KiB

  1. package factory
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. func MakeCommit(blockID types.BlockID, height int64, round int32,
  10. voteSet *types.VoteSet, validators []types.PrivValidator, now time.Time) (*types.Commit, error) {
  11. // all sign
  12. for i := 0; i < len(validators); i++ {
  13. pubKey, err := validators[i].GetPubKey(context.Background())
  14. if err != nil {
  15. return nil, fmt.Errorf("can't get pubkey: %w", err)
  16. }
  17. vote := &types.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 types.PrivValidator, vote *types.Vote, voteSet *types.VoteSet) (signed bool, err error) {
  34. v := vote.ToProto()
  35. err = privVal.SignVote(context.Background(), voteSet.ChainID(), v)
  36. if err != nil {
  37. return false, err
  38. }
  39. vote.Signature = v.Signature
  40. return voteSet.AddVote(vote)
  41. }