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.

102 lines
2.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package types
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/golang/protobuf/proto"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/tendermint/tendermint/types/proto3"
  8. )
  9. func TestProto3Compatibility(t *testing.T) {
  10. tm, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
  11. assert.NoError(t, err)
  12. // add some nanos, otherwise protobuf will skip over this while amino (still) won't!
  13. tm = tm.Add(50000 * time.Nanosecond)
  14. seconds := tm.Unix()
  15. nanos := int32(tm.Nanosecond())
  16. t.Log("seconds", seconds)
  17. t.Log("nanos", nanos)
  18. pbHeader := proto3.Header{
  19. ChainID: "cosmos",
  20. Height: 150,
  21. Time: &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
  22. LastBlockID: &proto3.BlockID{
  23. Hash: []byte("some serious hashing"),
  24. PartsHeader: &proto3.PartSetHeader{
  25. Total: 8,
  26. Hash: []byte("some more serious hashing"),
  27. },
  28. },
  29. LastCommitHash: []byte("commit hash"),
  30. DataHash: []byte("data hash"),
  31. ValidatorsHash: []byte("validators hash"),
  32. }
  33. aminoHeader := Header{
  34. ChainID: "cosmos",
  35. Height: 150,
  36. Time: tm,
  37. LastBlockID: BlockID{
  38. Hash: []byte("some serious hashing"),
  39. PartsHeader: PartSetHeader{
  40. Total: 8,
  41. Hash: []byte("some more serious hashing"),
  42. },
  43. },
  44. LastCommitHash: []byte("commit hash"),
  45. DataHash: []byte("data hash"),
  46. ValidatorsHash: []byte("validators hash"),
  47. }
  48. ab, err := cdc.MarshalBinaryBare(aminoHeader)
  49. assert.NoError(t, err, "unexpected error")
  50. pb, err := proto.Marshal(&pbHeader)
  51. assert.NoError(t, err, "unexpected error")
  52. // This works:
  53. assert.Equal(t, ab, pb, "encoding doesn't match")
  54. emptyLastBlockPb := proto3.Header{
  55. ChainID: "cosmos",
  56. Height: 150,
  57. Time: &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
  58. LastCommitHash: []byte("commit hash"),
  59. DataHash: []byte("data hash"),
  60. ValidatorsHash: []byte("validators hash"),
  61. }
  62. emptyLastBlockAm := Header{
  63. ChainID: "cosmos",
  64. Height: 150,
  65. Time: tm,
  66. LastCommitHash: []byte("commit hash"),
  67. DataHash: []byte("data hash"),
  68. ValidatorsHash: []byte("validators hash"),
  69. }
  70. ab, err = cdc.MarshalBinaryBare(emptyLastBlockAm)
  71. assert.NoError(t, err, "unexpected error")
  72. pb, err = proto.Marshal(&emptyLastBlockPb)
  73. assert.NoError(t, err, "unexpected error")
  74. // This works:
  75. assert.Equal(t, ab, pb, "encoding doesn't match")
  76. pb, err = proto.Marshal(&proto3.Header{})
  77. assert.NoError(t, err, "unexpected error")
  78. t.Log(pb)
  79. // While in protobuf Header{} encodes to an empty byte slice it does not in amino:
  80. ab, err = cdc.MarshalBinaryBare(Header{})
  81. assert.NoError(t, err, "unexpected error")
  82. t.Log(ab)
  83. pb, err = proto.Marshal(&proto3.Timestamp{})
  84. assert.NoError(t, err, "unexpected error")
  85. t.Log(pb)
  86. ab, err = cdc.MarshalBinaryBare(time.Time{})
  87. assert.NoError(t, err, "unexpected error")
  88. t.Log(ab)
  89. }