github.com/decred/dcrd/blockchain@v1.2.1/mediantime_test.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2015-2018 The Decred developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package blockchain
     7  
     8  import (
     9  	"strconv"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  // TestMedianTime tests the medianTime implementation.
    15  func TestMedianTime(t *testing.T) {
    16  	tests := []struct {
    17  		in         []int64
    18  		wantOffset int64
    19  		useDupID   bool
    20  	}{
    21  		// Not enough samples must result in an offset of 0.
    22  		{in: []int64{1}, wantOffset: 0},
    23  		{in: []int64{1, 2}, wantOffset: 0},
    24  		{in: []int64{1, 2, 3}, wantOffset: 0},
    25  		{in: []int64{1, 2, 3, 4}, wantOffset: 0},
    26  
    27  		// Various number of entries.  The expected offset is only
    28  		// updated on odd number of elements.
    29  		{in: []int64{-13, 57, -4, -23, -12}, wantOffset: -12},
    30  		{in: []int64{55, -13, 61, -52, 39, 55}, wantOffset: 39},
    31  		{in: []int64{-62, -58, -30, -62, 51, -30, 15}, wantOffset: -30},
    32  		{in: []int64{29, -47, 39, 54, 42, 41, 8, -33}, wantOffset: 39},
    33  		{in: []int64{37, 54, 9, -21, -56, -36, 5, -11, -39}, wantOffset: -11},
    34  		{in: []int64{57, -28, 25, -39, 9, 63, -16, 19, -60, 25}, wantOffset: 9},
    35  		{in: []int64{-5, -4, -3, -2, -1}, wantOffset: -3, useDupID: true},
    36  
    37  		// The offset stops being updated once the max number of entries
    38  		// has been reached.  This is actually a bug from Bitcoin Core,
    39  		// but since the time is ultimately used as a part of the
    40  		// consensus rules, it must be mirrored.
    41  		{in: []int64{-67, 67, -50, 24, 63, 17, 58, -14, 5, -32, -52}, wantOffset: 17},
    42  		{in: []int64{-67, 67, -50, 24, 63, 17, 58, -14, 5, -32, -52, 45}, wantOffset: 17},
    43  		{in: []int64{-67, 67, -50, 24, 63, 17, 58, -14, 5, -32, -52, 45, 4}, wantOffset: 17},
    44  
    45  		// Offsets that are too far away from the local time should
    46  		// be ignored.
    47  		{in: []int64{-4201, 4202, -4203, 4204, -4205}, wantOffset: 0},
    48  
    49  		// Exercise the condition where the median offset is greater
    50  		// than the max allowed adjustment, but there is at least one
    51  		// sample that is close enough to the current time to avoid
    52  		// triggering a warning about an invalid local clock.
    53  		{in: []int64{4201, 4202, 4203, 4204, -299}, wantOffset: 0},
    54  	}
    55  
    56  	// Modify the max number of allowed median time entries for these tests.
    57  	maxMedianTimeEntries = 10
    58  	defer func() { maxMedianTimeEntries = 200 }()
    59  
    60  	for i, test := range tests {
    61  		filter := NewMedianTime()
    62  		for j, offset := range test.in {
    63  			id := strconv.Itoa(j)
    64  			now := time.Unix(time.Now().Unix(), 0)
    65  			tOffset := now.Add(time.Duration(offset) * time.Second)
    66  			filter.AddTimeSample(id, tOffset)
    67  
    68  			// Ensure the duplicate IDs are ignored.
    69  			if test.useDupID {
    70  				// Modify the offsets to ensure the final median
    71  				// would be different if the duplicate is added.
    72  				tOffset = tOffset.Add(time.Duration(offset) *
    73  					time.Second)
    74  				filter.AddTimeSample(id, tOffset)
    75  			}
    76  		}
    77  
    78  		// Since it is possible that the time.Now call in AddTimeSample
    79  		// and the time.Now calls here in the tests will be off by one
    80  		// second, allow a fudge factor to compensate.
    81  		gotOffset := filter.Offset()
    82  		wantOffset := time.Duration(test.wantOffset) * time.Second
    83  		wantOffset2 := time.Duration(test.wantOffset-1) * time.Second
    84  		if gotOffset != wantOffset && gotOffset != wantOffset2 {
    85  			t.Errorf("Offset #%d: unexpected offset -- got %v, "+
    86  				"want %v or %v", i, gotOffset, wantOffset,
    87  				wantOffset2)
    88  			continue
    89  		}
    90  
    91  		// Since it is possible that the time.Now call in AdjustedTime
    92  		// and the time.Now call here in the tests will be off by one
    93  		// second, allow a fudge factor to compensate.
    94  		adjustedTime := filter.AdjustedTime()
    95  		now := time.Unix(time.Now().Unix(), 0)
    96  		wantTime := now.Add(filter.Offset())
    97  		wantTime2 := now.Add(filter.Offset() - time.Second)
    98  		if !adjustedTime.Equal(wantTime) && !adjustedTime.Equal(wantTime2) {
    99  			t.Errorf("AdjustedTime #%d: unexpected result -- got %v, "+
   100  				"want %v or %v", i, adjustedTime, wantTime,
   101  				wantTime2)
   102  			continue
   103  		}
   104  	}
   105  }