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.

115 lines
2.9 KiB

  1. package consensus
  2. import (
  3. "errors"
  4. "fmt"
  5. tmcon "github.com/tendermint/tendermint/consensus"
  6. cstypes "github.com/tendermint/tendermint/consensus/types"
  7. tmmath "github.com/tendermint/tendermint/libs/math"
  8. "github.com/tendermint/tendermint/p2p"
  9. tmcons "github.com/tendermint/tendermint/proto/tendermint/consensus"
  10. tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func WALToProto(msg tmcon.WALMessage) (*tmcons.WALMessage, error) {
  14. var pb tmcons.WALMessage
  15. switch msg := msg.(type) {
  16. case types.EventDataRoundState:
  17. pb = tmcons.WALMessage{
  18. Sum: &tmcons.WALMessage_EventDataRoundState{
  19. EventDataRoundState: &tmproto.EventDataRoundState{
  20. Height: msg.Height,
  21. Round: msg.Round,
  22. Step: msg.Step,
  23. },
  24. },
  25. }
  26. case msgInfo:
  27. consMsg, err := tmcon.MsgToProto(msg.Msg)
  28. if err != nil {
  29. return nil, err
  30. }
  31. pb = tmcons.WALMessage{
  32. Sum: &tmcons.WALMessage_MsgInfo{
  33. MsgInfo: &tmcons.MsgInfo{
  34. Msg: *consMsg,
  35. PeerID: string(msg.PeerID),
  36. },
  37. },
  38. }
  39. case timeoutInfo:
  40. pb = tmcons.WALMessage{
  41. Sum: &tmcons.WALMessage_TimeoutInfo{
  42. TimeoutInfo: &tmcons.TimeoutInfo{
  43. Duration: msg.Duration,
  44. Height: msg.Height,
  45. Round: msg.Round,
  46. Step: uint32(msg.Step),
  47. },
  48. },
  49. }
  50. case tmcon.EndHeightMessage:
  51. pb = tmcons.WALMessage{
  52. Sum: &tmcons.WALMessage_EndHeight{
  53. EndHeight: &tmcons.EndHeight{
  54. Height: msg.Height,
  55. },
  56. },
  57. }
  58. default:
  59. return nil, fmt.Errorf("to proto: wal message not recognized: %T", msg)
  60. }
  61. return &pb, nil
  62. }
  63. // WALFromProto takes a proto wal message and return a consensus walMessage and error
  64. func WALFromProto(msg *tmcons.WALMessage) (tmcon.WALMessage, error) {
  65. if msg == nil {
  66. return nil, errors.New("nil WAL message")
  67. }
  68. var pb tmcon.WALMessage
  69. switch msg := msg.Sum.(type) {
  70. case *tmcons.WALMessage_EventDataRoundState:
  71. pb = types.EventDataRoundState{
  72. Height: msg.EventDataRoundState.Height,
  73. Round: msg.EventDataRoundState.Round,
  74. Step: msg.EventDataRoundState.Step,
  75. }
  76. case *tmcons.WALMessage_MsgInfo:
  77. walMsg, err := tmcon.MsgFromProto(&msg.MsgInfo.Msg)
  78. if err != nil {
  79. return nil, fmt.Errorf("msgInfo from proto error: %w", err)
  80. }
  81. pb = msgInfo{
  82. Msg: walMsg,
  83. PeerID: p2p.ID(msg.MsgInfo.PeerID),
  84. }
  85. case *tmcons.WALMessage_TimeoutInfo:
  86. tis, err := tmmath.SafeConvertUint8(int64(msg.TimeoutInfo.Step))
  87. // deny message based on possible overflow
  88. if err != nil {
  89. return nil, fmt.Errorf("denying message due to possible overflow: %w", err)
  90. }
  91. pb = timeoutInfo{
  92. Duration: msg.TimeoutInfo.Duration,
  93. Height: msg.TimeoutInfo.Height,
  94. Round: msg.TimeoutInfo.Round,
  95. Step: cstypes.RoundStepType(tis),
  96. }
  97. return pb, nil
  98. case *tmcons.WALMessage_EndHeight:
  99. pb := tmcon.EndHeightMessage{
  100. Height: msg.EndHeight.Height,
  101. }
  102. return pb, nil
  103. default:
  104. return nil, fmt.Errorf("from proto: wal message not recognized: %T", msg)
  105. }
  106. return pb, nil
  107. }