Browse Source

tmtime: Canonical, some comments (#2312)

pull/2316/merge
Ethan Buchman 6 years ago
committed by Anton Kaliaev
parent
commit
eabb1ece8e
4 changed files with 15 additions and 5 deletions
  1. +2
    -1
      types/canonical_json.go
  2. +2
    -1
      types/genesis.go
  3. +2
    -1
      types/protobuf_test.go
  4. +9
    -2
      types/time/time.go

+ 2
- 1
types/canonical_json.go View File

@ -4,6 +4,7 @@ import (
"time" "time"
cmn "github.com/tendermint/tendermint/libs/common" 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 // 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 // Note that sending time over amino resets it to
// local time, we need to force UTC here, so the // local time, we need to force UTC here, so the
// signatures match // signatures match
return t.Round(0).UTC().Format(TimeFormat)
return tmtime.Canonical(t).Format(TimeFormat)
} }

+ 2
- 1
types/genesis.go View File

@ -8,6 +8,7 @@ import (
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common" cmn "github.com/tendermint/tendermint/libs/common"
tmtime "github.com/tendermint/tendermint/types/time"
) )
const ( const (
@ -79,7 +80,7 @@ func (genDoc *GenesisDoc) ValidateAndComplete() error {
} }
if genDoc.GenesisTime.IsZero() { if genDoc.GenesisTime.IsZero() {
genDoc.GenesisTime = time.Now()
genDoc.GenesisTime = tmtime.Now()
} }
return nil return nil


+ 2
- 1
types/protobuf_test.go View File

@ -9,6 +9,7 @@ import (
"github.com/tendermint/tendermint/crypto" "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/secp256k1" "github.com/tendermint/tendermint/crypto/secp256k1"
tmtime "github.com/tendermint/tendermint/types/time"
) )
func TestABCIPubKey(t *testing.T) { func TestABCIPubKey(t *testing.T) {
@ -77,7 +78,7 @@ func TestABCIConsensusParams(t *testing.T) {
func TestABCIHeader(t *testing.T) { func TestABCIHeader(t *testing.T) {
header := &Header{ header := &Header{
Height: int64(3), Height: int64(3),
Time: time.Now(),
Time: tmtime.Now(),
NumTxs: int64(10), NumTxs: int64(10),
ProposerAddress: []byte("cloak"), ProposerAddress: []byte("cloak"),
} }


+ 9
- 2
types/time/time.go View File

@ -5,16 +5,23 @@ import (
"time" "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 { 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 { type WeightedTime struct {
Time time.Time Time time.Time
Weight int64 Weight int64
} }
// NewWeightedTime with time and weight.
func NewWeightedTime(time time.Time, weight int64) *WeightedTime { func NewWeightedTime(time time.Time, weight int64) *WeightedTime {
return &WeightedTime{ return &WeightedTime{
Time: time, Time: time,


Loading…
Cancel
Save