2018-06-12 23:49:16 -07:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
2018-07-18 06:53:53 -07:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
|
|
"github.com/tendermint/tendermint/types/proto3"
|
2018-06-12 23:49:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestProto3Compatibility(t *testing.T) {
|
|
|
|
tm, err := time.Parse("Mon Jan 2 15:04:05 -0700 MST 2006", "Mon Jan 2 15:04:05 -0700 MST 2006")
|
|
|
|
assert.NoError(t, err)
|
2018-07-18 10:06:38 -07:00
|
|
|
// add some nanos, otherwise protobuf will skip over this while amino (still) won't!
|
|
|
|
tm = tm.Add(50000 * time.Nanosecond)
|
2018-06-13 00:43:06 -07:00
|
|
|
seconds := tm.Unix()
|
|
|
|
nanos := int32(tm.Nanosecond())
|
2018-07-18 10:06:38 -07:00
|
|
|
t.Log("seconds", seconds)
|
|
|
|
t.Log("nanos", nanos)
|
|
|
|
|
2018-06-12 23:49:16 -07:00
|
|
|
pbHeader := proto3.Header{
|
|
|
|
ChainID: "cosmos",
|
2018-07-18 06:53:53 -07:00
|
|
|
Height: 150,
|
|
|
|
Time: &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
|
|
|
|
NumTxs: 7,
|
2018-06-12 23:49:16 -07:00
|
|
|
LastBlockID: &proto3.BlockID{
|
|
|
|
Hash: []byte("some serious hashing"),
|
2018-07-18 06:53:53 -07:00
|
|
|
PartsHeader: &proto3.PartSetHeader{
|
2018-06-12 23:49:16 -07:00
|
|
|
Total: 8,
|
2018-07-18 06:53:53 -07:00
|
|
|
Hash: []byte("some more serious hashing"),
|
2018-06-12 23:49:16 -07:00
|
|
|
},
|
|
|
|
},
|
2018-07-18 06:53:53 -07:00
|
|
|
TotalTxs: 100,
|
2018-06-12 23:49:16 -07:00
|
|
|
LastCommitHash: []byte("commit hash"),
|
2018-07-18 06:53:53 -07:00
|
|
|
DataHash: []byte("data hash"),
|
|
|
|
ValidatorsHash: []byte("validators hash"),
|
2018-06-12 23:49:16 -07:00
|
|
|
}
|
|
|
|
aminoHeader := Header{
|
|
|
|
ChainID: "cosmos",
|
2018-07-18 06:53:53 -07:00
|
|
|
Height: 150,
|
|
|
|
Time: tm,
|
|
|
|
NumTxs: 7,
|
2018-06-12 23:49:16 -07:00
|
|
|
LastBlockID: BlockID{
|
|
|
|
Hash: []byte("some serious hashing"),
|
|
|
|
PartsHeader: PartSetHeader{
|
|
|
|
Total: 8,
|
2018-07-18 06:53:53 -07:00
|
|
|
Hash: []byte("some more serious hashing"),
|
2018-06-12 23:49:16 -07:00
|
|
|
},
|
|
|
|
},
|
2018-07-18 06:53:53 -07:00
|
|
|
TotalTxs: 100,
|
2018-06-12 23:49:16 -07:00
|
|
|
LastCommitHash: []byte("commit hash"),
|
2018-07-18 06:53:53 -07:00
|
|
|
DataHash: []byte("data hash"),
|
|
|
|
ValidatorsHash: []byte("validators hash"),
|
2018-06-12 23:49:16 -07:00
|
|
|
}
|
|
|
|
ab, err := cdc.MarshalBinaryBare(aminoHeader)
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
|
|
|
|
pb, err := proto.Marshal(&pbHeader)
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
// This works:
|
|
|
|
assert.Equal(t, ab, pb, "encoding doesn't match")
|
2018-06-13 01:11:21 -07:00
|
|
|
|
|
|
|
emptyLastBlockPb := proto3.Header{
|
|
|
|
ChainID: "cosmos",
|
2018-07-18 06:53:53 -07:00
|
|
|
Height: 150,
|
|
|
|
Time: &proto3.Timestamp{Seconds: seconds, Nanos: nanos},
|
|
|
|
NumTxs: 7,
|
2018-07-18 10:06:38 -07:00
|
|
|
// This is not fully skipped in amino (yet) although it is empty:
|
|
|
|
LastBlockID: &proto3.BlockID{
|
|
|
|
PartsHeader: &proto3.PartSetHeader{
|
|
|
|
},
|
|
|
|
},
|
2018-07-18 06:53:53 -07:00
|
|
|
TotalTxs: 100,
|
2018-06-13 01:11:21 -07:00
|
|
|
LastCommitHash: []byte("commit hash"),
|
2018-07-18 06:53:53 -07:00
|
|
|
DataHash: []byte("data hash"),
|
|
|
|
ValidatorsHash: []byte("validators hash"),
|
2018-06-13 01:11:21 -07:00
|
|
|
}
|
|
|
|
emptyLastBlockAm := Header{
|
2018-07-18 06:53:53 -07:00
|
|
|
ChainID: "cosmos",
|
|
|
|
Height: 150,
|
|
|
|
Time: tm,
|
|
|
|
NumTxs: 7,
|
|
|
|
TotalTxs: 100,
|
2018-06-13 01:11:21 -07:00
|
|
|
LastCommitHash: []byte("commit hash"),
|
2018-07-18 06:53:53 -07:00
|
|
|
DataHash: []byte("data hash"),
|
|
|
|
ValidatorsHash: []byte("validators hash"),
|
2018-06-13 01:11:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ab, err = cdc.MarshalBinaryBare(emptyLastBlockAm)
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
|
|
|
|
pb, err = proto.Marshal(&emptyLastBlockPb)
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
// This works:
|
|
|
|
assert.Equal(t, ab, pb, "encoding doesn't match")
|
2018-06-20 15:15:05 -07:00
|
|
|
|
|
|
|
pb, err = proto.Marshal(&proto3.Header{})
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
t.Log(pb)
|
2018-07-18 10:06:38 -07:00
|
|
|
|
|
|
|
// While in protobuf Header{} encodes to an empty byte slice it does not in amino:
|
2018-07-18 07:37:15 -07:00
|
|
|
ab, err = cdc.MarshalBinaryBare(Header{})
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
t.Log(ab)
|
2018-06-20 15:15:05 -07:00
|
|
|
|
|
|
|
pb, err = proto.Marshal(&proto3.Timestamp{})
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
t.Log(pb)
|
|
|
|
|
2018-07-18 10:06:38 -07:00
|
|
|
ab, err = cdc.MarshalBinaryBare(time.Time{})
|
|
|
|
assert.NoError(t, err, "unexpected error")
|
|
|
|
t.Log(ab)
|
2018-06-12 23:49:16 -07:00
|
|
|
}
|