libs/time: move types/time into libs (#6595)

This commit is contained in:
Sam Kleinman
2021-06-17 16:02:31 -04:00
committed by GitHub
parent 09a409df9c
commit a6b30faf35
28 changed files with 27 additions and 27 deletions
+1 -1
View File
@@ -23,9 +23,9 @@ import (
"github.com/tendermint/tendermint/libs/bits"
"github.com/tendermint/tendermint/libs/bytes"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmversion "github.com/tendermint/tendermint/proto/tendermint/version"
tmtime "github.com/tendermint/tendermint/types/time"
"github.com/tendermint/tendermint/version"
)
+1 -1
View File
@@ -3,8 +3,8 @@ package types
import (
"time"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
// Canonical* wraps the structs in types for amino encoding them for use in SignBytes / the Signable interface.
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"github.com/tendermint/tendermint/crypto"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtime "github.com/tendermint/tendermint/types/time"
tmtime "github.com/tendermint/tendermint/libs/time"
)
const (
+1 -1
View File
@@ -10,7 +10,7 @@ import (
"github.com/tendermint/tendermint/crypto/ed25519"
tmjson "github.com/tendermint/tendermint/libs/json"
tmtime "github.com/tendermint/tendermint/types/time"
tmtime "github.com/tendermint/tendermint/libs/time"
)
func TestGenesisBad(t *testing.T) {
+1 -1
View File
@@ -7,8 +7,8 @@ import (
"github.com/tendermint/tendermint/internal/libs/protoio"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
var (
-58
View File
@@ -1,58 +0,0 @@
package time
import (
"sort"
"time"
)
// Now returns the current time in UTC with no monotonic component.
func Now() time.Time {
return Canonical(time.Now())
}
// Canonical returns UTC time with no monotonic component.
// Stripping the monotonic component is for time equality.
// See https://github.com/tendermint/tendermint/pull/2203#discussion_r215064334
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,
Weight: weight,
}
}
// WeightedMedian computes weighted median time for a given array of WeightedTime and the total voting power.
func WeightedMedian(weightedTimes []*WeightedTime, totalVotingPower int64) (res time.Time) {
median := totalVotingPower / 2
sort.Slice(weightedTimes, func(i, j int) bool {
if weightedTimes[i] == nil {
return false
}
if weightedTimes[j] == nil {
return true
}
return weightedTimes[i].Time.UnixNano() < weightedTimes[j].Time.UnixNano()
})
for _, weightedTime := range weightedTimes {
if weightedTime != nil {
if median <= weightedTime.Weight {
res = weightedTime.Time
break
}
median -= weightedTime.Weight
}
}
return
}
-56
View File
@@ -1,56 +0,0 @@
package time
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWeightedMedian(t *testing.T) {
m := make([]*WeightedTime, 3)
t1 := Now()
t2 := t1.Add(5 * time.Second)
t3 := t1.Add(10 * time.Second)
m[2] = NewWeightedTime(t1, 33) // faulty processes
m[0] = NewWeightedTime(t2, 40) // correct processes
m[1] = NewWeightedTime(t3, 27) // correct processes
totalVotingPower := int64(100)
median := WeightedMedian(m, totalVotingPower)
assert.Equal(t, t2, median)
// median always returns value between values of correct processes
assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) &&
(median.Before(t3) || median.Equal(t3)))
m[1] = NewWeightedTime(t1, 40) // correct processes
m[2] = NewWeightedTime(t2, 27) // correct processes
m[0] = NewWeightedTime(t3, 33) // faulty processes
totalVotingPower = int64(100)
median = WeightedMedian(m, totalVotingPower)
assert.Equal(t, t2, median)
// median always returns value between values of correct processes
assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) &&
(median.Before(t2) || median.Equal(t2)))
m = make([]*WeightedTime, 8)
t4 := t1.Add(15 * time.Second)
t5 := t1.Add(60 * time.Second)
m[3] = NewWeightedTime(t1, 10) // correct processes
m[1] = NewWeightedTime(t2, 10) // correct processes
m[5] = NewWeightedTime(t2, 10) // correct processes
m[4] = NewWeightedTime(t3, 23) // faulty processes
m[0] = NewWeightedTime(t4, 20) // correct processes
m[7] = NewWeightedTime(t5, 10) // faulty processes
totalVotingPower = int64(83)
median = WeightedMedian(m, totalVotingPower)
assert.Equal(t, t3, median)
// median always returns value between values of correct processes
assert.Equal(t, true, (median.After(t1) || median.Equal(t1)) &&
(median.Before(t4) || median.Equal(t4)))
}
+1 -1
View File
@@ -11,8 +11,8 @@ import (
"github.com/tendermint/tendermint/crypto"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmtime "github.com/tendermint/tendermint/libs/time"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
tmtime "github.com/tendermint/tendermint/types/time"
)
func TestVoteSet_AddVote_Good(t *testing.T) {