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
3.1 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. // This is not fully skipped in amino (yet) although it is empty:
  64. LastBlockID: &proto3.BlockID{
  65. PartsHeader: &proto3.PartSetHeader{
  66. },
  67. },
  68. TotalTxs: 100,
  69. LastCommitHash: []byte("commit hash"),
  70. DataHash: []byte("data hash"),
  71. ValidatorsHash: []byte("validators hash"),
  72. }
  73. emptyLastBlockAm := Header{
  74. ChainID: "cosmos",
  75. Height: 150,
  76. Time: tm,
  77. NumTxs: 7,
  78. TotalTxs: 100,
  79. LastCommitHash: []byte("commit hash"),
  80. DataHash: []byte("data hash"),
  81. ValidatorsHash: []byte("validators hash"),
  82. }
  83. ab, err = cdc.MarshalBinaryBare(emptyLastBlockAm)
  84. assert.NoError(t, err, "unexpected error")
  85. pb, err = proto.Marshal(&emptyLastBlockPb)
  86. assert.NoError(t, err, "unexpected error")
  87. // This works:
  88. assert.Equal(t, ab, pb, "encoding doesn't match")
  89. pb, err = proto.Marshal(&proto3.Header{})
  90. assert.NoError(t, err, "unexpected error")
  91. t.Log(pb)
  92. // While in protobuf Header{} encodes to an empty byte slice it does not in amino:
  93. ab, err = cdc.MarshalBinaryBare(Header{})
  94. assert.NoError(t, err, "unexpected error")
  95. t.Log(ab)
  96. pb, err = proto.Marshal(&proto3.Timestamp{})
  97. assert.NoError(t, err, "unexpected error")
  98. t.Log(pb)
  99. ab, err = cdc.MarshalBinaryBare(time.Time{})
  100. assert.NoError(t, err, "unexpected error")
  101. t.Log(ab)
  102. }