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.

141 lines
3.6 KiB

  1. package types
  2. import (
  3. abci "github.com/tendermint/tendermint/abci/types"
  4. "github.com/tendermint/tendermint/crypto"
  5. "github.com/tendermint/tendermint/crypto/ed25519"
  6. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  7. "github.com/tendermint/tendermint/crypto/secp256k1"
  8. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  9. )
  10. //-------------------------------------------------------
  11. // Use strings to distinguish types in ABCI messages
  12. const (
  13. ABCIPubKeyTypeEd25519 = ed25519.KeyType
  14. ABCIPubKeyTypeSecp256k1 = secp256k1.KeyType
  15. )
  16. // TODO: Make non-global by allowing for registration of more pubkey types
  17. var ABCIPubKeyTypesToNames = map[string]string{
  18. ABCIPubKeyTypeEd25519: ed25519.PubKeyName,
  19. ABCIPubKeyTypeSecp256k1: secp256k1.PubKeyName,
  20. }
  21. //-------------------------------------------------------
  22. // TM2PB is used for converting Tendermint ABCI to protobuf ABCI.
  23. // UNSTABLE
  24. var TM2PB = tm2pb{}
  25. type tm2pb struct{}
  26. func (tm2pb) Header(header *Header) tmproto.Header {
  27. return tmproto.Header{
  28. Version: header.Version,
  29. ChainID: header.ChainID,
  30. Height: header.Height,
  31. Time: header.Time,
  32. LastBlockId: header.LastBlockID.ToProto(),
  33. LastCommitHash: header.LastCommitHash,
  34. DataHash: header.DataHash,
  35. ValidatorsHash: header.ValidatorsHash,
  36. NextValidatorsHash: header.NextValidatorsHash,
  37. ConsensusHash: header.ConsensusHash,
  38. AppHash: header.AppHash,
  39. LastResultsHash: header.LastResultsHash,
  40. EvidenceHash: header.EvidenceHash,
  41. ProposerAddress: header.ProposerAddress,
  42. }
  43. }
  44. func (tm2pb) Validator(val *Validator) abci.Validator {
  45. return abci.Validator{
  46. Address: val.PubKey.Address(),
  47. Power: val.VotingPower,
  48. }
  49. }
  50. func (tm2pb) BlockID(blockID BlockID) tmproto.BlockID {
  51. return tmproto.BlockID{
  52. Hash: blockID.Hash,
  53. PartSetHeader: TM2PB.PartSetHeader(blockID.PartSetHeader),
  54. }
  55. }
  56. func (tm2pb) PartSetHeader(header PartSetHeader) tmproto.PartSetHeader {
  57. return tmproto.PartSetHeader{
  58. Total: header.Total,
  59. Hash: header.Hash,
  60. }
  61. }
  62. // XXX: panics on unknown pubkey type
  63. func (tm2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate {
  64. pk, err := cryptoenc.PubKeyToProto(val.PubKey)
  65. if err != nil {
  66. panic(err)
  67. }
  68. return abci.ValidatorUpdate{
  69. PubKey: pk,
  70. Power: val.VotingPower,
  71. }
  72. }
  73. // XXX: panics on nil or unknown pubkey type
  74. func (tm2pb) ValidatorUpdates(vals *ValidatorSet) []abci.ValidatorUpdate {
  75. validators := make([]abci.ValidatorUpdate, vals.Size())
  76. for i, val := range vals.Validators {
  77. validators[i] = TM2PB.ValidatorUpdate(val)
  78. }
  79. return validators
  80. }
  81. func (tm2pb) ConsensusParams(params *tmproto.ConsensusParams) *abci.ConsensusParams {
  82. return &abci.ConsensusParams{
  83. Block: &abci.BlockParams{
  84. MaxBytes: params.Block.MaxBytes,
  85. MaxGas: params.Block.MaxGas,
  86. },
  87. Evidence: &params.Evidence,
  88. Validator: &params.Validator,
  89. }
  90. }
  91. // XXX: panics on nil or unknown pubkey type
  92. func (tm2pb) NewValidatorUpdate(pubkey crypto.PubKey, power int64) abci.ValidatorUpdate {
  93. pubkeyABCI, err := cryptoenc.PubKeyToProto(pubkey)
  94. if err != nil {
  95. panic(err)
  96. }
  97. return abci.ValidatorUpdate{
  98. PubKey: pubkeyABCI,
  99. Power: power,
  100. }
  101. }
  102. //----------------------------------------------------------------------------
  103. // PB2TM is used for converting protobuf ABCI to Tendermint ABCI.
  104. // UNSTABLE
  105. var PB2TM = pb2tm{}
  106. type pb2tm struct{}
  107. func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) {
  108. tmVals := make([]*Validator, len(vals))
  109. for i, v := range vals {
  110. pub, err := cryptoenc.PubKeyFromProto(v.PubKey)
  111. if err != nil {
  112. return nil, err
  113. }
  114. tmVals[i] = NewValidator(pub, v.Power)
  115. }
  116. return tmVals, nil
  117. }