diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2018-09-11 13:28:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-11 13:28:44 +0800 |
commit | 582a491aa0bcb784ac7b65ebbfb42139945ea703 (patch) | |
tree | 8589bad986f512455717728012c3d9edf3b68c4f /core/utils.go | |
parent | 2439f49063d8498eadf26d4fa1220c5eac8412a8 (diff) | |
download | dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.gz dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.bz2 dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.lz dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.xz dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.tar.zst dexon-consensus-582a491aa0bcb784ac7b65ebbfb42139945ea703.zip |
core: timestamp (#98)
Diffstat (limited to 'core/utils.go')
-rw-r--r-- | core/utils.go | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/core/utils.go b/core/utils.go index 6b03f93..59aa7a4 100644 --- a/core/utils.go +++ b/core/utils.go @@ -25,7 +25,6 @@ import ( "time" "github.com/dexon-foundation/dexon-consensus-core/common" - "github.com/dexon-foundation/dexon-consensus-core/core/types" ) var ( @@ -72,25 +71,25 @@ func interpoTime(t1 time.Time, t2 time.Time, sep int) []time.Time { } return timestamps } -func getMedianTime(block *types.Block) (t time.Time, err error) { - timestamps := []time.Time{} - for _, timestamp := range block.Timestamps { - timestamps = append(timestamps, timestamp) - } + +func getMedianTime(timestamps []time.Time) (t time.Time, err error) { if len(timestamps) == 0 { err = ErrEmptyTimestamps return } - sort.Sort(common.ByTime(timestamps)) - if len(timestamps)%2 == 0 { - t1 := timestamps[len(timestamps)/2-1] - t2 := timestamps[len(timestamps)/2] + tscopy := make([]time.Time, 0, len(timestamps)) + for _, ts := range timestamps { + tscopy = append(tscopy, ts) + } + sort.Sort(common.ByTime(tscopy)) + if len(tscopy)%2 == 0 { + t1 := tscopy[len(tscopy)/2-1] + t2 := tscopy[len(tscopy)/2] t = interpoTime(t1, t2, 1)[0] } else { - t = timestamps[len(timestamps)/2] + t = tscopy[len(tscopy)/2] } return - } func removeFromSortedIntSlice(xs []int, x int) []int { |