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.

223 lines
5.8 KiB

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