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

8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package types
  2. import (
  3. "fmt"
  4. "reflect"
  5. "time"
  6. abci "github.com/tendermint/abci/types"
  7. crypto "github.com/tendermint/go-crypto"
  8. )
  9. //-------------------------------------------------------
  10. // Use strings to distinguish types in ABCI messages
  11. const (
  12. ABCIEvidenceTypeDuplicateVote = "duplicate/vote"
  13. ABCIEvidenceTypeMockGood = "mock/good"
  14. )
  15. const (
  16. ABCIPubKeyTypeEd25519 = "ed25519"
  17. ABCIPubKeyTypeSecp256k1 = "secp256k1"
  18. )
  19. //-------------------------------------------------------
  20. // TM2PB is used for converting Tendermint ABCI to protobuf ABCI.
  21. // UNSTABLE
  22. var TM2PB = tm2pb{}
  23. type tm2pb struct{}
  24. func (tm2pb) Header(header *Header) abci.Header {
  25. return abci.Header{
  26. ChainID: header.ChainID,
  27. Height: header.Height,
  28. Time: header.Time.Unix(),
  29. NumTxs: int32(header.NumTxs), // XXX: overflow
  30. LastBlockHash: header.LastBlockID.Hash,
  31. ValidatorsHash: header.ValidatorsHash,
  32. AppHash: header.AppHash,
  33. }
  34. }
  35. func (tm2pb) Validator(val *Validator) abci.Validator {
  36. return abci.Validator{
  37. PubKey: TM2PB.PubKey(val.PubKey),
  38. Power: val.VotingPower,
  39. }
  40. }
  41. func (tm2pb) PubKey(pubKey crypto.PubKey) abci.PubKey {
  42. switch pk := pubKey.(type) {
  43. case crypto.PubKeyEd25519:
  44. return abci.PubKey{
  45. Type: ABCIPubKeyTypeEd25519,
  46. Data: pk[:],
  47. }
  48. case crypto.PubKeySecp256k1:
  49. return abci.PubKey{
  50. Type: ABCIPubKeyTypeSecp256k1,
  51. Data: pk[:],
  52. }
  53. default:
  54. panic(fmt.Sprintf("unknown pubkey type: %v %v", pubKey, reflect.TypeOf(pubKey)))
  55. }
  56. }
  57. func (tm2pb) Validators(vals *ValidatorSet) []abci.Validator {
  58. validators := make([]abci.Validator, len(vals.Validators))
  59. for i, val := range vals.Validators {
  60. validators[i] = TM2PB.Validator(val)
  61. }
  62. return validators
  63. }
  64. func (tm2pb) ConsensusParams(params *ConsensusParams) *abci.ConsensusParams {
  65. return &abci.ConsensusParams{
  66. BlockSize: &abci.BlockSize{
  67. MaxBytes: int32(params.BlockSize.MaxBytes),
  68. MaxTxs: int32(params.BlockSize.MaxTxs),
  69. MaxGas: params.BlockSize.MaxGas,
  70. },
  71. TxSize: &abci.TxSize{
  72. MaxBytes: int32(params.TxSize.MaxBytes),
  73. MaxGas: params.TxSize.MaxGas,
  74. },
  75. BlockGossip: &abci.BlockGossip{
  76. BlockPartSizeBytes: int32(params.BlockGossip.BlockPartSizeBytes),
  77. },
  78. }
  79. }
  80. // ABCI Evidence includes information from the past that's not included in the evidence itself
  81. // so Evidence types stays compact.
  82. func (tm2pb) Evidence(ev Evidence, valSet *ValidatorSet, evTime time.Time) abci.Evidence {
  83. _, val := valSet.GetByAddress(ev.Address())
  84. if val == nil {
  85. // should already have checked this
  86. panic(val)
  87. }
  88. abciEvidence := abci.Evidence{
  89. Validator: abci.Validator{
  90. Address: ev.Address(),
  91. PubKey: TM2PB.PubKey(val.PubKey),
  92. Power: val.VotingPower,
  93. },
  94. Height: ev.Height(),
  95. Time: evTime.Unix(),
  96. TotalVotingPower: valSet.TotalVotingPower(),
  97. }
  98. // set type
  99. switch ev.(type) {
  100. case *DuplicateVoteEvidence:
  101. abciEvidence.Type = ABCIEvidenceTypeDuplicateVote
  102. case *MockGoodEvidence, MockGoodEvidence:
  103. abciEvidence.Type = ABCIEvidenceTypeMockGood
  104. default:
  105. panic(fmt.Sprintf("Unknown evidence type: %v %v", ev, reflect.TypeOf(ev)))
  106. }
  107. return abciEvidence
  108. }
  109. func (tm2pb) ValidatorFromPubKeyAndPower(pubkey crypto.PubKey, power int64) abci.Validator {
  110. pubkeyABCI := TM2PB.PubKey(pubkey)
  111. return abci.Validator{
  112. Address: pubkey.Address(),
  113. PubKey: pubkeyABCI,
  114. Power: power,
  115. }
  116. }
  117. //----------------------------------------------------------------------------
  118. // PB2TM is used for converting protobuf ABCI to Tendermint ABCI.
  119. // UNSTABLE
  120. var PB2TM = pb2tm{}
  121. type pb2tm struct{}
  122. func (pb2tm) PubKey(pubKey abci.PubKey) (crypto.PubKey, error) {
  123. // TODO: define these in go-crypto and use them
  124. sizeEd := 32
  125. sizeSecp := 33
  126. switch pubKey.Type {
  127. case ABCIPubKeyTypeEd25519:
  128. if len(pubKey.Data) != sizeEd {
  129. return nil, fmt.Errorf("Invalid size for PubKeyEd25519. Got %d, expected %d", len(pubKey.Data), sizeEd)
  130. }
  131. var pk crypto.PubKeyEd25519
  132. copy(pk[:], pubKey.Data)
  133. return pk, nil
  134. case ABCIPubKeyTypeSecp256k1:
  135. if len(pubKey.Data) != sizeSecp {
  136. return nil, fmt.Errorf("Invalid size for PubKeyEd25519. Got %d, expected %d", len(pubKey.Data), sizeSecp)
  137. }
  138. var pk crypto.PubKeySecp256k1
  139. copy(pk[:], pubKey.Data)
  140. return pk, nil
  141. default:
  142. return nil, fmt.Errorf("Unknown pubkey type %v", pubKey.Type)
  143. }
  144. }