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.

97 lines
2.5 KiB

  1. package statesync
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gogo/protobuf/proto"
  6. ssproto "github.com/tendermint/tendermint/proto/tendermint/statesync"
  7. )
  8. const (
  9. // snapshotMsgSize is the maximum size of a snapshotResponseMessage
  10. snapshotMsgSize = int(4e6)
  11. // chunkMsgSize is the maximum size of a chunkResponseMessage
  12. chunkMsgSize = int(16e6)
  13. )
  14. // mustEncodeMsg encodes a Protobuf message, panicing on error.
  15. func mustEncodeMsg(pb proto.Message) []byte {
  16. msg := ssproto.Message{}
  17. switch pb := pb.(type) {
  18. case *ssproto.ChunkRequest:
  19. msg.Sum = &ssproto.Message_ChunkRequest{ChunkRequest: pb}
  20. case *ssproto.ChunkResponse:
  21. msg.Sum = &ssproto.Message_ChunkResponse{ChunkResponse: pb}
  22. case *ssproto.SnapshotsRequest:
  23. msg.Sum = &ssproto.Message_SnapshotsRequest{SnapshotsRequest: pb}
  24. case *ssproto.SnapshotsResponse:
  25. msg.Sum = &ssproto.Message_SnapshotsResponse{SnapshotsResponse: pb}
  26. default:
  27. panic(fmt.Errorf("unknown message type %T", pb))
  28. }
  29. bz, err := proto.Marshal(&msg)
  30. if err != nil {
  31. panic(fmt.Errorf("unable to marshal %T: %w", pb, err))
  32. }
  33. return bz
  34. }
  35. // decodeMsg decodes a Protobuf message.
  36. func decodeMsg(bz []byte) (proto.Message, error) {
  37. pb := &ssproto.Message{}
  38. err := proto.Unmarshal(bz, pb)
  39. if err != nil {
  40. return nil, err
  41. }
  42. switch msg := pb.Sum.(type) {
  43. case *ssproto.Message_ChunkRequest:
  44. return msg.ChunkRequest, nil
  45. case *ssproto.Message_ChunkResponse:
  46. return msg.ChunkResponse, nil
  47. case *ssproto.Message_SnapshotsRequest:
  48. return msg.SnapshotsRequest, nil
  49. case *ssproto.Message_SnapshotsResponse:
  50. return msg.SnapshotsResponse, nil
  51. default:
  52. return nil, fmt.Errorf("unknown message type %T", msg)
  53. }
  54. }
  55. // validateMsg validates a message.
  56. func validateMsg(pb proto.Message) error {
  57. if pb == nil {
  58. return errors.New("message cannot be nil")
  59. }
  60. switch msg := pb.(type) {
  61. case *ssproto.ChunkRequest:
  62. if msg.Height == 0 {
  63. return errors.New("height cannot be 0")
  64. }
  65. case *ssproto.ChunkResponse:
  66. if msg.Height == 0 {
  67. return errors.New("height cannot be 0")
  68. }
  69. if msg.Missing && len(msg.Chunk) > 0 {
  70. return errors.New("missing chunk cannot have contents")
  71. }
  72. if !msg.Missing && msg.Chunk == nil {
  73. return errors.New("chunk cannot be nil")
  74. }
  75. case *ssproto.SnapshotsRequest:
  76. case *ssproto.SnapshotsResponse:
  77. if msg.Height == 0 {
  78. return errors.New("height cannot be 0")
  79. }
  80. if len(msg.Hash) == 0 {
  81. return errors.New("snapshot has no hash")
  82. }
  83. if msg.Chunks == 0 {
  84. return errors.New("snapshot has no chunks")
  85. }
  86. default:
  87. return fmt.Errorf("unknown message type %T", msg)
  88. }
  89. return nil
  90. }