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.

221 lines
5.7 KiB

  1. package types
  2. import (
  3. "fmt"
  4. "reflect"
  5. "time"
  6. abci "github.com/tendermint/tendermint/abci/types"
  7. "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. "github.com/tendermint/tendermint/crypto/secp256k1"
  10. )
  11. //-------------------------------------------------------
  12. // Use strings to distinguish types in ABCI messages
  13. const (
  14. ABCIEvidenceTypeDuplicateVote = "duplicate/vote"
  15. ABCIEvidenceTypeMockGood = "mock/good"
  16. )
  17. const (
  18. ABCIPubKeyTypeEd25519 = "ed25519"
  19. ABCIPubKeyTypeSecp256k1 = "secp256k1"
  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) abci.Header {
  27. return abci.Header{
  28. ChainID: header.ChainID,
  29. Height: header.Height,
  30. Time: header.Time,
  31. NumTxs: header.NumTxs,
  32. TotalTxs: header.TotalTxs,
  33. LastBlockId: TM2PB.BlockID(header.LastBlockID),
  34. LastCommitHash: header.LastCommitHash,
  35. DataHash: header.DataHash,
  36. ValidatorsHash: header.ValidatorsHash,
  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) abci.BlockID {
  51. return abci.BlockID{
  52. Hash: blockID.Hash,
  53. PartsHeader: TM2PB.PartSetHeader(blockID.PartsHeader),
  54. }
  55. }
  56. func (tm2pb) PartSetHeader(header PartSetHeader) abci.PartSetHeader {
  57. return abci.PartSetHeader{
  58. Total: int32(header.Total),
  59. Hash: header.Hash,
  60. }
  61. }
  62. // XXX: panics on unknown pubkey type
  63. func (tm2pb) ValidatorUpdate(val *Validator) abci.ValidatorUpdate {
  64. return abci.ValidatorUpdate{
  65. PubKey: TM2PB.PubKey(val.PubKey),
  66. Power: val.VotingPower,
  67. }
  68. }
  69. // XXX: panics on nil or unknown pubkey type
  70. // TODO: add cases when new pubkey types are added to crypto
  71. func (tm2pb) PubKey(pubKey crypto.PubKey) abci.PubKey {
  72. switch pk := pubKey.(type) {
  73. case ed25519.PubKeyEd25519:
  74. return abci.PubKey{
  75. Type: ABCIPubKeyTypeEd25519,
  76. Data: pk[:],
  77. }
  78. case secp256k1.PubKeySecp256k1:
  79. return abci.PubKey{
  80. Type: ABCIPubKeyTypeSecp256k1,
  81. Data: pk[:],
  82. }
  83. default:
  84. panic(fmt.Sprintf("unknown pubkey type: %v %v", pubKey, reflect.TypeOf(pubKey)))
  85. }
  86. }
  87. // XXX: panics on nil or unknown pubkey type
  88. func (tm2pb) ValidatorUpdates(vals *ValidatorSet) []abci.ValidatorUpdate {
  89. validators := make([]abci.ValidatorUpdate, vals.Size())
  90. for i, val := range vals.Validators {
  91. validators[i] = TM2PB.ValidatorUpdate(val)
  92. }
  93. return validators
  94. }
  95. func (tm2pb) ConsensusParams(params *ConsensusParams) *abci.ConsensusParams {
  96. return &abci.ConsensusParams{
  97. BlockSize: &abci.BlockSize{
  98. MaxBytes: int32(params.BlockSize.MaxBytes),
  99. MaxGas: params.BlockSize.MaxGas,
  100. },
  101. EvidenceParams: &abci.EvidenceParams{
  102. MaxAge: params.EvidenceParams.MaxAge,
  103. },
  104. }
  105. }
  106. // ABCI Evidence includes information from the past that's not included in the evidence itself
  107. // so Evidence types stays compact.
  108. // XXX: panics on nil or unknown pubkey type
  109. func (tm2pb) Evidence(ev Evidence, valSet *ValidatorSet, evTime time.Time) abci.Evidence {
  110. _, val := valSet.GetByAddress(ev.Address())
  111. if val == nil {
  112. // should already have checked this
  113. panic(val)
  114. }
  115. // set type
  116. var evType string
  117. switch ev.(type) {
  118. case *DuplicateVoteEvidence:
  119. evType = ABCIEvidenceTypeDuplicateVote
  120. case MockGoodEvidence:
  121. // XXX: not great to have test types in production paths ...
  122. evType = ABCIEvidenceTypeMockGood
  123. default:
  124. panic(fmt.Sprintf("Unknown evidence type: %v %v", ev, reflect.TypeOf(ev)))
  125. }
  126. return abci.Evidence{
  127. Type: evType,
  128. Validator: TM2PB.Validator(val),
  129. Height: ev.Height(),
  130. Time: evTime,
  131. TotalVotingPower: valSet.TotalVotingPower(),
  132. }
  133. }
  134. // XXX: panics on nil or unknown pubkey type
  135. func (tm2pb) NewValidatorUpdate(pubkey crypto.PubKey, power int64) abci.ValidatorUpdate {
  136. pubkeyABCI := TM2PB.PubKey(pubkey)
  137. return abci.ValidatorUpdate{
  138. PubKey: pubkeyABCI,
  139. Power: power,
  140. }
  141. }
  142. //----------------------------------------------------------------------------
  143. // PB2TM is used for converting protobuf ABCI to Tendermint ABCI.
  144. // UNSTABLE
  145. var PB2TM = pb2tm{}
  146. type pb2tm struct{}
  147. func (pb2tm) PubKey(pubKey abci.PubKey) (crypto.PubKey, error) {
  148. // TODO: define these in crypto and use them
  149. sizeEd := 32
  150. sizeSecp := 33
  151. switch pubKey.Type {
  152. case ABCIPubKeyTypeEd25519:
  153. if len(pubKey.Data) != sizeEd {
  154. return nil, fmt.Errorf("Invalid size for PubKeyEd25519. Got %d, expected %d", len(pubKey.Data), sizeEd)
  155. }
  156. var pk ed25519.PubKeyEd25519
  157. copy(pk[:], pubKey.Data)
  158. return pk, nil
  159. case ABCIPubKeyTypeSecp256k1:
  160. if len(pubKey.Data) != sizeSecp {
  161. return nil, fmt.Errorf("Invalid size for PubKeyEd25519. Got %d, expected %d", len(pubKey.Data), sizeSecp)
  162. }
  163. var pk secp256k1.PubKeySecp256k1
  164. copy(pk[:], pubKey.Data)
  165. return pk, nil
  166. default:
  167. return nil, fmt.Errorf("Unknown pubkey type %v", pubKey.Type)
  168. }
  169. }
  170. func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) {
  171. tmVals := make([]*Validator, len(vals))
  172. for i, v := range vals {
  173. pub, err := PB2TM.PubKey(v.PubKey)
  174. if err != nil {
  175. return nil, err
  176. }
  177. tmVals[i] = NewValidator(pub, v.Power)
  178. }
  179. return tmVals, nil
  180. }
  181. func (pb2tm) ConsensusParams(csp *abci.ConsensusParams) ConsensusParams {
  182. return ConsensusParams{
  183. BlockSize: BlockSize{
  184. MaxBytes: int(csp.BlockSize.MaxBytes), // XXX
  185. MaxGas: csp.BlockSize.MaxGas,
  186. },
  187. EvidenceParams: EvidenceParams{
  188. MaxAge: csp.EvidenceParams.MaxAge, // XXX
  189. },
  190. }
  191. }