From eabb1ece8ecedc42fb74d5bc83b42a76cd42fa38 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Tue, 4 Sep 2018 04:20:58 -0400 Subject: [PATCH] tmtime: Canonical, some comments (#2312) --- types/canonical_json.go | 3 ++- types/genesis.go | 3 ++- types/protobuf_test.go | 3 ++- types/time/time.go | 11 +++++++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/types/canonical_json.go b/types/canonical_json.go index 4f512af8b..d8399ff19 100644 --- a/types/canonical_json.go +++ b/types/canonical_json.go @@ -4,6 +4,7 @@ import ( "time" cmn "github.com/tendermint/tendermint/libs/common" + tmtime "github.com/tendermint/tendermint/types/time" ) // Canonical json is amino's json for structs with fields in alphabetical order @@ -110,5 +111,5 @@ func CanonicalTime(t time.Time) string { // Note that sending time over amino resets it to // local time, we need to force UTC here, so the // signatures match - return t.Round(0).UTC().Format(TimeFormat) + return tmtime.Canonical(t).Format(TimeFormat) } diff --git a/types/genesis.go b/types/genesis.go index 0594c0e3e..4cf3b7309 100644 --- a/types/genesis.go +++ b/types/genesis.go @@ -8,6 +8,7 @@ import ( "github.com/tendermint/tendermint/crypto" cmn "github.com/tendermint/tendermint/libs/common" + tmtime "github.com/tendermint/tendermint/types/time" ) const ( @@ -79,7 +80,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error { } if genDoc.GenesisTime.IsZero() { - genDoc.GenesisTime = time.Now() + genDoc.GenesisTime = tmtime.Now() } return nil diff --git a/types/protobuf_test.go b/types/protobuf_test.go index 2a511225b..f8682abf8 100644 --- a/types/protobuf_test.go +++ b/types/protobuf_test.go @@ -9,6 +9,7 @@ import ( "github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/secp256k1" + tmtime "github.com/tendermint/tendermint/types/time" ) func TestABCIPubKey(t *testing.T) { @@ -77,7 +78,7 @@ func TestABCIConsensusParams(t *testing.T) { func TestABCIHeader(t *testing.T) { header := &Header{ Height: int64(3), - Time: time.Now(), + Time: tmtime.Now(), NumTxs: int64(10), ProposerAddress: []byte("cloak"), } diff --git a/types/time/time.go b/types/time/time.go index 2f6b7899f..62aca9ec3 100644 --- a/types/time/time.go +++ b/types/time/time.go @@ -5,16 +5,23 @@ import ( "time" ) -// Now returns UTC time rounded since the zero time. +// Now returns the current time in UTC with no monotonic component. func Now() time.Time { - return time.Now().Round(0).UTC() + return Canonical(time.Now()) } +// Canonical returns UTC time with no monotonic component. +func Canonical(t time.Time) time.Time { + return t.Round(0).UTC() +} + +// WeightedTime for computing a median. type WeightedTime struct { Time time.Time Weight int64 } +// NewWeightedTime with time and weight. func NewWeightedTime(time time.Time, weight int64) *WeightedTime { return &WeightedTime{ Time: time,