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.

167 lines
4.3 KiB

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