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.

110 lines
3.0 KiB

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. "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. NumTxs: 7,
  23. LastBlockID: &proto3.BlockID{
  24. Hash: []byte("some serious hashing"),
  25. PartsHeader: &proto3.PartSetHeader{
  26. Total: 8,
  27. Hash: []byte("some more serious hashing"),
  28. },
  29. },
  30. TotalTxs: 100,
  31. LastCommitHash: []byte("commit hash"),
  32. DataHash: []byte("data hash"),
  33. ValidatorsHash: []byte("validators hash"),
  34. }
  35. aminoHeader := Header{
  36. ChainID: "cosmos",
  37. Height: 150,
  38. Time: tm,
  39. NumTxs: 7,
  40. LastBlockID: BlockID{
  41. Hash: []byte("some serious hashing"),
  42. PartsHeader: PartSetHeader{
  43. Total: 8,
  44. Hash: []byte("some more serious hashing"),
  45. },
  46. },
  47. TotalTxs: 100,
  48. LastCommitHash: []byte("commit hash"),
  49. DataHash: []byte("data hash"),
  50. ValidatorsHash: []byte("validators hash"),
  51. }
  52. ab, err := cdc.MarshalBinaryBare(aminoHeader)
  53. assert.NoError(t, err, "unexpected error")
  54. pb, err := proto.Marshal(&pbHeader)
  55. assert.NoError(t, err, "unexpected error")
  56. // This works:
  57. assert.Equal(t, ab, pb, "encoding doesn't match")
  58. emptyLastBlockPb := proto3.Header{
  59. ChainID: "cosmos",
  60. Height: 150,
  61. Time: &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
  62. NumTxs: 7,
  63. TotalTxs: 100,
  64. LastCommitHash: []byte("commit hash"),
  65. DataHash: []byte("data hash"),
  66. ValidatorsHash: []byte("validators hash"),
  67. }
  68. emptyLastBlockAm := Header{
  69. ChainID: "cosmos",
  70. Height: 150,
  71. Time: tm,
  72. NumTxs: 7,
  73. TotalTxs: 100,
  74. LastCommitHash: []byte("commit hash"),
  75. DataHash: []byte("data hash"),
  76. ValidatorsHash: []byte("validators hash"),
  77. }
  78. ab, err = cdc.MarshalBinaryBare(emptyLastBlockAm)
  79. assert.NoError(t, err, "unexpected error")
  80. pb, err = proto.Marshal(&emptyLastBlockPb)
  81. assert.NoError(t, err, "unexpected error")
  82. // This works:
  83. assert.Equal(t, ab, pb, "encoding doesn't match")
  84. pb, err = proto.Marshal(&proto3.Header{})
  85. assert.NoError(t, err, "unexpected error")
  86. t.Log(pb)
  87. // While in protobuf Header{} encodes to an empty byte slice it does not in amino:
  88. ab, err = cdc.MarshalBinaryBare(Header{})
  89. assert.NoError(t, err, "unexpected error")
  90. t.Log(ab)
  91. pb, err = proto.Marshal(&proto3.Timestamp{})
  92. assert.NoError(t, err, "unexpected error")
  93. t.Log(pb)
  94. ab, err = cdc.MarshalBinaryBare(time.Time{})
  95. assert.NoError(t, err, "unexpected error")
  96. t.Log(ab)
  97. }