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.

139 lines
3.2 KiB

8 years ago
7 years ago
7 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
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
  1. package types
  2. import (
  3. "fmt"
  4. "reflect"
  5. abci "github.com/tendermint/abci/types"
  6. crypto "github.com/tendermint/go-crypto"
  7. )
  8. // TM2PB is used for converting Tendermint abci to protobuf abci.
  9. // UNSTABLE
  10. var TM2PB = tm2pb{}
  11. type tm2pb struct{}
  12. func (tm2pb) Header(header *Header) abci.Header {
  13. return abci.Header{
  14. ChainID: header.ChainID,
  15. Height: header.Height,
  16. Time: header.Time.Unix(),
  17. NumTxs: int32(header.NumTxs), // XXX: overflow
  18. LastBlockHash: header.LastBlockID.Hash,
  19. AppHash: header.AppHash,
  20. }
  21. }
  22. func (tm2pb) Validator(val *Validator) abci.Validator {
  23. return abci.Validator{
  24. PubKey: TM2PB.PubKey(val.PubKey),
  25. Power: val.VotingPower,
  26. }
  27. }
  28. func (tm2pb) PubKey(pubKey crypto.PubKey) abci.PubKey {
  29. switch pk := pubKey.(type) {
  30. case crypto.PubKeyEd25519:
  31. return abci.PubKey{
  32. Type: "ed25519",
  33. Data: pk[:],
  34. }
  35. case crypto.PubKeySecp256k1:
  36. return abci.PubKey{
  37. Type: "secp256k1",
  38. Data: pk[:],
  39. }
  40. default:
  41. panic(fmt.Sprintf("unknown pubkey type: %v %v", pubKey, reflect.TypeOf(pubKey)))
  42. }
  43. }
  44. func (tm2pb) Validators(vals *ValidatorSet) []abci.Validator {
  45. validators := make([]abci.Validator, len(vals.Validators))
  46. for i, val := range vals.Validators {
  47. validators[i] = TM2PB.Validator(val)
  48. }
  49. return validators
  50. }
  51. func (tm2pb) ConsensusParams(params *ConsensusParams) *abci.ConsensusParams {
  52. return &abci.ConsensusParams{
  53. BlockSize: &abci.BlockSize{
  54. MaxBytes: int32(params.BlockSize.MaxBytes),
  55. MaxTxs: int32(params.BlockSize.MaxTxs),
  56. MaxGas: params.BlockSize.MaxGas,
  57. },
  58. TxSize: &abci.TxSize{
  59. MaxBytes: int32(params.TxSize.MaxBytes),
  60. MaxGas: params.TxSize.MaxGas,
  61. },
  62. BlockGossip: &abci.BlockGossip{
  63. BlockPartSizeBytes: int32(params.BlockGossip.BlockPartSizeBytes),
  64. },
  65. }
  66. }
  67. func (tm2pb) Evidence(ev_ Evidence) abci.Evidence {
  68. switch ev := ev_.(type) {
  69. case *DuplicateVoteEvidence:
  70. return abci.Evidence{
  71. Type: "duplicate/vote",
  72. Validator: abci.Validator{
  73. Address: ev.Address(),
  74. // TODO
  75. },
  76. Height: ev.Height(),
  77. // Time: ev.Time(),
  78. // TotalVotingPower: 10,
  79. }
  80. case *MockGoodEvidence, MockGoodEvidence:
  81. return abci.Evidence{
  82. Type: "mock/good",
  83. Validator: abci.Validator{
  84. Address: ev.Address(),
  85. // TODO
  86. },
  87. Height: ev.Height(),
  88. // Time: ev.Time(),
  89. // TotalVotingPower: 10,
  90. }
  91. default:
  92. panic(fmt.Sprintf("Unknown evidence type: %v %v", ev_, reflect.TypeOf(ev_)))
  93. }
  94. }
  95. func (tm2pb) ValidatorFromPubKeyAndPower(pubkey crypto.PubKey, power int64) abci.Validator {
  96. pubkeyABCI := TM2PB.PubKey(pubkey)
  97. return abci.Validator{
  98. Address: pubkey.Address(),
  99. PubKey: pubkeyABCI,
  100. Power: power,
  101. }
  102. }
  103. //----------------------------------------------------------------------------
  104. // PB2TM is used for converting protobuf abci to Tendermint abci.
  105. // UNSTABLE
  106. var PB2TM = pb2tm{}
  107. type pb2tm struct{}
  108. // TODO: validate key lengths ...
  109. func (pb2tm) PubKey(pubKey abci.PubKey) (crypto.PubKey, error) {
  110. switch pubKey.Type {
  111. case "ed25519":
  112. var pk crypto.PubKeyEd25519
  113. copy(pk[:], pubKey.Data)
  114. return pk, nil
  115. case "secp256k1":
  116. var pk crypto.PubKeySecp256k1
  117. copy(pk[:], pubKey.Data)
  118. return pk, nil
  119. default:
  120. return nil, fmt.Errorf("Unknown pubkey type %v", pubKey.Type)
  121. }
  122. }