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.

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