2014-10-10 08:11:39 +02:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package btcchain_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-01-17 01:40:47 +01:00
|
|
|
"github.com/btcsuite/btcchain"
|
2014-10-10 08:11:39 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestMedianTime tests the medianTime implementation.
|
|
|
|
func TestMedianTime(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in []int64
|
|
|
|
wantOffset int64
|
|
|
|
useDupID bool
|
|
|
|
}{
|
|
|
|
// Not enough samples must result in an offset of 0.
|
|
|
|
{in: []int64{1}, wantOffset: 0},
|
|
|
|
{in: []int64{1, 2}, wantOffset: 0},
|
|
|
|
{in: []int64{1, 2, 3}, wantOffset: 0},
|
|
|
|
{in: []int64{1, 2, 3, 4}, wantOffset: 0},
|
|
|
|
|
|
|
|
// Various number of entries. The expected offset is only
|
|
|
|
// updated on odd number of elements.
|
2015-01-08 12:08:11 +01:00
|
|
|
{in: []int64{-13, 57, -4, -23, -12}, wantOffset: -12},
|
|
|
|
{in: []int64{55, -13, 61, -52, 39, 55}, wantOffset: 39},
|
|
|
|
{in: []int64{-62, -58, -30, -62, 51, -30, 15}, wantOffset: -30},
|
|
|
|
{in: []int64{29, -47, 39, 54, 42, 41, 8, -33}, wantOffset: 39},
|
|
|
|
{in: []int64{37, 54, 9, -21, -56, -36, 5, -11, -39}, wantOffset: -11},
|
|
|
|
{in: []int64{57, -28, 25, -39, 9, 63, -16, 19, -60, 25}, wantOffset: 9},
|
|
|
|
{in: []int64{-5, -4, -3, -2, -1}, wantOffset: -3, useDupID: true},
|
2014-10-10 08:11:39 +02:00
|
|
|
|
|
|
|
// The offset stops being updated once the max number of entries
|
|
|
|
// has been reached. This is actually a bug from Bitcoin Core,
|
|
|
|
// but since the time is ultimately used as a part of the
|
|
|
|
// consensus rules, it must be mirrored.
|
2015-01-08 12:08:11 +01:00
|
|
|
{in: []int64{-67, 67, -50, 24, 63, 17, 58, -14, 5, -32, -52}, wantOffset: 17},
|
|
|
|
{in: []int64{-67, 67, -50, 24, 63, 17, 58, -14, 5, -32, -52, 45}, wantOffset: 17},
|
|
|
|
{in: []int64{-67, 67, -50, 24, 63, 17, 58, -14, 5, -32, -52, 45, 4}, wantOffset: 17},
|
2014-10-10 08:11:39 +02:00
|
|
|
|
|
|
|
// Offsets that are too far away from the local time should
|
|
|
|
// be ignored.
|
|
|
|
{in: []int64{-4201, 4202, -4203, 4204, -4205}, wantOffset: 0},
|
|
|
|
|
|
|
|
// Excerise the condition where the median offset is greater
|
|
|
|
// than the max allowed adjustment, but there is at least one
|
|
|
|
// sample that is close enough to the current time to avoid
|
|
|
|
// triggering a warning about an invalid local clock.
|
|
|
|
{in: []int64{4201, 4202, 4203, 4204, -299}, wantOffset: 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Modify the max number of allowed median time entries for these tests.
|
|
|
|
btcchain.TstSetMaxMedianTimeEntries(10)
|
|
|
|
defer btcchain.TstSetMaxMedianTimeEntries(200)
|
|
|
|
|
|
|
|
for i, test := range tests {
|
|
|
|
filter := btcchain.NewMedianTime()
|
|
|
|
for j, offset := range test.in {
|
|
|
|
id := strconv.Itoa(j)
|
2014-10-12 01:03:14 +02:00
|
|
|
now := time.Unix(time.Now().Unix(), 0)
|
|
|
|
tOffset := now.Add(time.Duration(offset) * time.Second)
|
2014-10-10 08:11:39 +02:00
|
|
|
filter.AddTimeSample(id, tOffset)
|
|
|
|
|
|
|
|
// Ensure the duplicate IDs are ignored.
|
|
|
|
if test.useDupID {
|
|
|
|
// Modify the offsets to ensure the final median
|
|
|
|
// would be different if the duplicate is added.
|
|
|
|
tOffset = tOffset.Add(time.Duration(offset) *
|
|
|
|
time.Second)
|
|
|
|
filter.AddTimeSample(id, tOffset)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 01:03:14 +02:00
|
|
|
// Since it is possible that the time.Now call in AddTimeSample
|
|
|
|
// and the time.Now calls here in the tests will be off by one
|
|
|
|
// second, allow a fudge factor to compensate.
|
2014-10-10 08:11:39 +02:00
|
|
|
gotOffset := filter.Offset()
|
|
|
|
wantOffset := time.Duration(test.wantOffset) * time.Second
|
2014-10-12 01:03:14 +02:00
|
|
|
wantOffset2 := time.Duration(test.wantOffset-1) * time.Second
|
|
|
|
if gotOffset != wantOffset && gotOffset != wantOffset2 {
|
2014-10-10 08:11:39 +02:00
|
|
|
t.Errorf("Offset #%d: unexpected offset -- got %v, "+
|
2014-10-12 01:03:14 +02:00
|
|
|
"want %v or %v", i, gotOffset, wantOffset,
|
|
|
|
wantOffset2)
|
2014-10-10 08:11:39 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-10-11 21:49:35 +02:00
|
|
|
// Since it is possible that the time.Now call in AdjustedTime
|
|
|
|
// and the time.Now call here in the tests will be off by one
|
|
|
|
// second, allow a fudge factor to compensate.
|
|
|
|
adjustedTime := filter.AdjustedTime()
|
|
|
|
now := time.Unix(time.Now().Unix(), 0)
|
|
|
|
wantTime := now.Add(filter.Offset())
|
|
|
|
wantTime2 := now.Add(filter.Offset() - time.Second)
|
|
|
|
if !adjustedTime.Equal(wantTime) && !adjustedTime.Equal(wantTime2) {
|
2014-10-10 08:11:39 +02:00
|
|
|
t.Errorf("AdjustedTime #%d: unexpected result -- got %v, "+
|
2014-10-11 21:49:35 +02:00
|
|
|
"want %v or %v", i, adjustedTime, wantTime,
|
|
|
|
wantTime2)
|
2014-10-10 08:11:39 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|