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.

62 lines
1.5 KiB

  1. package types
  2. import (
  3. abci "github.com/tendermint/tendermint/abci/types"
  4. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  5. )
  6. //-------------------------------------------------------
  7. // TM2PB is used for converting Tendermint ABCI to protobuf ABCI.
  8. // UNSTABLE
  9. var TM2PB = tm2pb{}
  10. type tm2pb struct{}
  11. func (tm2pb) Validator(val *Validator) abci.Validator {
  12. return abci.Validator{
  13. Address: val.PubKey.Address(),
  14. Power: val.VotingPower,
  15. }
  16. }
  17. // XXX: panics on unknown pubkey type
  18. func (tm2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate {
  19. pk, err := cryptoenc.PubKeyToProto(val.PubKey)
  20. if err != nil {
  21. panic(err)
  22. }
  23. return abci.ValidatorUpdate{
  24. PubKey: pk,
  25. Power: val.VotingPower,
  26. }
  27. }
  28. // XXX: panics on nil or unknown pubkey type
  29. func (tm2pb) ValidatorUpdates(vals *ValidatorSet) []abci.ValidatorUpdate {
  30. validators := make([]abci.ValidatorUpdate, vals.Size())
  31. for i, val := range vals.Validators {
  32. validators[i] = TM2PB.ValidatorUpdate(val)
  33. }
  34. return validators
  35. }
  36. //----------------------------------------------------------------------------
  37. // PB2TM is used for converting protobuf ABCI to Tendermint ABCI.
  38. // UNSTABLE
  39. var PB2TM = pb2tm{}
  40. type pb2tm struct{}
  41. func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) {
  42. tmVals := make([]*Validator, len(vals))
  43. for i, v := range vals {
  44. pub, err := cryptoenc.PubKeyFromProto(v.PubKey)
  45. if err != nil {
  46. return nil, err
  47. }
  48. tmVals[i] = NewValidator(pub, v.Power)
  49. }
  50. return tmVals, nil
  51. }