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.

92 lines
2.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package types
  2. import (
  3. "testing"
  4. "github.com/tendermint/tendermint/types/proto3"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/golang/protobuf/proto"
  7. "time"
  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. seconds := tm.Unix()
  13. nanos := int32(tm.Nanosecond())
  14. pbHeader := proto3.Header{
  15. ChainID: "cosmos",
  16. Height:150,
  17. Time: &proto3.Timestamp{Seconds:seconds, Nanos:nanos},
  18. NumTxs: 7,
  19. LastBlockID: &proto3.BlockID{
  20. Hash: []byte("some serious hashing"),
  21. PartsHeader:&proto3.PartSetHeader{
  22. Total: 8,
  23. Hash: []byte("some more serious hashing"),
  24. },
  25. },
  26. TotalTxs: 100,
  27. LastCommitHash: []byte("commit hash"),
  28. DataHash: []byte("data hash"),
  29. ValidatorsHash:[]byte("validators hash"),
  30. }
  31. aminoHeader := Header{
  32. ChainID: "cosmos",
  33. Height:150,
  34. Time: tm,
  35. NumTxs: 7,
  36. LastBlockID: BlockID{
  37. Hash: []byte("some serious hashing"),
  38. PartsHeader: PartSetHeader{
  39. Total: 8,
  40. Hash: []byte("some more serious hashing"),
  41. },
  42. },
  43. TotalTxs: 100,
  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. NumTxs: 7,
  59. // TODO(ismail): as Jae suggested, we'll add a flag to make this obsolete:
  60. LastBlockID: &proto3.BlockID{
  61. PartsHeader: &proto3.PartSetHeader{},
  62. },
  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. }