fix time decoding / encoding and add a little test TestVoteEncodingRountrip

Signed-off-by: Ismail Khoffi <Ismail.Khoffi@gmail.com>
This commit is contained in:
Ismail Khoffi
2019-04-19 17:57:53 +02:00
parent 793e1b540f
commit 61f60e86ef
2 changed files with 22 additions and 4 deletions
+3 -4
View File
@@ -166,9 +166,6 @@ func (vote *Vote) ValidateBasic() error {
return nil
}
func (vote Vote) toCustomType() VoteCustomType {
return VoteCustomType{Vote: &vote}
}
func (vote Vote) Equal(other Vote) bool {
proto := vote.toVoteType()
return proto.Equal(other.toVoteType())
@@ -180,7 +177,7 @@ func (vote *Vote) toVoteType() *VoteType {
Height: vote.Height,
Round: int64(vote.Round), // TODO
BlockID: &BlockIDType{Hash: vote.BlockID.Hash, PartSetHeader: &PartSetHeaderType{Total: int64(vote.BlockID.PartsHeader.Total), Hash: vote.BlockID.PartsHeader.Hash.Bytes()}},
TimestampField: &types.Timestamp{Seconds: int64(vote.Timestamp.Second()), Nanos: int32(vote.Timestamp.Nanosecond())},
TimestampField: &types.Timestamp{Seconds: int64(vote.Timestamp.Unix()), Nanos: int32(vote.Timestamp.Nanosecond())},
ValidatorAddress: vote.ValidatorAddress[:],
ValidatorIndex: int64(vote.ValidatorIndex),
Signature: vote.Signature,
@@ -197,6 +194,8 @@ func fromVoteType(voteType *VoteType) *Vote {
vote.Round = int(voteType.Round) // FIXME
vote.BlockID = BlockID{Hash: voteType.BlockID.Hash, PartsHeader: PartSetHeader{int(voteType.BlockID.PartSetHeader.Total), voteType.BlockID.PartSetHeader.Hash}}
ti, err := ptypes.Timestamp(&tspb.Timestamp{Seconds: voteType.TimestampField.Seconds, Nanos: voteType.TimestampField.Nanos})
ti = ti.UTC().Truncate(0)
fmt.Println("ti", ti)
if err != nil {
panic(err)
}
+19
View File
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
amino "github.com/tendermint/go-amino"
@@ -293,3 +294,21 @@ func TestVoteValidateBasic(t *testing.T) {
})
}
}
func TestVoteEncodingRountrip(t *testing.T) {
// none of the below would pass validation:
now := time.Now().Truncate(0).UTC()
bid := BlockID{Hash: []byte("fake hash"), PartsHeader: PartSetHeader{0, []byte("another fake hash")}}
vote := &Vote{Height: 1, Round: 1, Type: PrevoteType, BlockID: bid, ValidatorAddress: []byte("no real addr"), Timestamp: now, Signature: []byte("totaly_fake_sig")}
bz, err := proto.Marshal(&VoteCustomType{vote})
if err != nil {
panic(err)
}
vct := &VoteCustomType{}
err = proto.Unmarshal(bz, vct)
if err != nil {
panic(err)
}
require.Equal(t, vct.Vote, vote)
}