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.

232 lines
6.1 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. TxSize: &abci.TxSize{
  102. MaxBytes: int32(params.TxSize.MaxBytes),
  103. MaxGas: params.TxSize.MaxGas,
  104. },
  105. BlockGossip: &abci.BlockGossip{
  106. BlockPartSizeBytes: int32(params.BlockGossip.BlockPartSizeBytes),
  107. },
  108. }
  109. }
  110. // ABCI Evidence includes information from the past that's not included in the evidence itself
  111. // so Evidence types stays compact.
  112. // XXX: panics on nil or unknown pubkey type
  113. func (tm2pb) Evidence(ev Evidence, valSet *ValidatorSet, evTime time.Time) abci.Evidence {
  114. _, val := valSet.GetByAddress(ev.Address())
  115. if val == nil {
  116. // should already have checked this
  117. panic(val)
  118. }
  119. // set type
  120. var evType string
  121. switch ev.(type) {
  122. case *DuplicateVoteEvidence:
  123. evType = ABCIEvidenceTypeDuplicateVote
  124. case MockGoodEvidence:
  125. // XXX: not great to have test types in production paths ...
  126. evType = ABCIEvidenceTypeMockGood
  127. default:
  128. panic(fmt.Sprintf("Unknown evidence type: %v %v", ev, reflect.TypeOf(ev)))
  129. }
  130. return abci.Evidence{
  131. Type: evType,
  132. Validator: TM2PB.Validator(val),
  133. Height: ev.Height(),
  134. Time: evTime,
  135. TotalVotingPower: valSet.TotalVotingPower(),
  136. }
  137. }
  138. // XXX: panics on nil or unknown pubkey type
  139. func (tm2pb) NewValidatorUpdate(pubkey crypto.PubKey, power int64) abci.ValidatorUpdate {
  140. pubkeyABCI := TM2PB.PubKey(pubkey)
  141. return abci.ValidatorUpdate{
  142. PubKey: pubkeyABCI,
  143. Power: power,
  144. }
  145. }
  146. //----------------------------------------------------------------------------
  147. // PB2TM is used for converting protobuf ABCI to Tendermint ABCI.
  148. // UNSTABLE
  149. var PB2TM = pb2tm{}
  150. type pb2tm struct{}
  151. func (pb2tm) PubKey(pubKey abci.PubKey) (crypto.PubKey, error) {
  152. // TODO: define these in crypto and use them
  153. sizeEd := 32
  154. sizeSecp := 33
  155. switch pubKey.Type {
  156. case ABCIPubKeyTypeEd25519:
  157. if len(pubKey.Data) != sizeEd {
  158. return nil, fmt.Errorf("Invalid size for PubKeyEd25519. Got %d, expected %d", len(pubKey.Data), sizeEd)
  159. }
  160. var pk ed25519.PubKeyEd25519
  161. copy(pk[:], pubKey.Data)
  162. return pk, nil
  163. case ABCIPubKeyTypeSecp256k1:
  164. if len(pubKey.Data) != sizeSecp {
  165. return nil, fmt.Errorf("Invalid size for PubKeyEd25519. Got %d, expected %d", len(pubKey.Data), sizeSecp)
  166. }
  167. var pk secp256k1.PubKeySecp256k1
  168. copy(pk[:], pubKey.Data)
  169. return pk, nil
  170. default:
  171. return nil, fmt.Errorf("Unknown pubkey type %v", pubKey.Type)
  172. }
  173. }
  174. func (pb2tm) ValidatorUpdates(vals []abci.ValidatorUpdate) ([]*Validator, error) {
  175. tmVals := make([]*Validator, len(vals))
  176. for i, v := range vals {
  177. pub, err := PB2TM.PubKey(v.PubKey)
  178. if err != nil {
  179. return nil, err
  180. }
  181. tmVals[i] = NewValidator(pub, v.Power)
  182. }
  183. return tmVals, nil
  184. }
  185. func (pb2tm) ConsensusParams(csp *abci.ConsensusParams) ConsensusParams {
  186. return ConsensusParams{
  187. BlockSize: BlockSize{
  188. MaxBytes: int(csp.BlockSize.MaxBytes), // XXX
  189. MaxGas: csp.BlockSize.MaxGas,
  190. },
  191. TxSize: TxSize{
  192. MaxBytes: int(csp.TxSize.MaxBytes), // XXX
  193. MaxGas: csp.TxSize.MaxGas,
  194. },
  195. BlockGossip: BlockGossip{
  196. BlockPartSizeBytes: int(csp.BlockGossip.BlockPartSizeBytes), // XXX
  197. },
  198. // TODO: EvidenceParams: EvidenceParams{
  199. // MaxAge: int(csp.Evidence.MaxAge), // XXX
  200. // },
  201. }
  202. }