github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/cmd/derod/timecheck.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package main
    18  
    19  /*
    20  import "io"
    21  import "os"
    22  
    23  import "fmt"
    24  import "bytes"
    25  import "bufio"
    26  import "strings"
    27  import "strconv"
    28  import "runtime"
    29  import "crypto/sha1"
    30  import "encoding/hex"
    31  import "encoding/json"
    32  import "path/filepath"
    33  
    34  import "github.com/romana/rlog"
    35  import "github.com/chzyer/readline"
    36  import "github.com/docopt/docopt-go"
    37  import log "github.com/sirupsen/logrus"
    38  
    39  import "github.com/deroproject/derosuite/address"
    40  import "github.com/deroproject/derosuite/p2pv2"
    41  
    42  
    43  import "github.com/deroproject/derosuite/config"
    44  
    45  import "github.com/deroproject/derosuite/transaction"
    46  
    47  //import "github.com/deroproject/derosuite/checkpoints"
    48  import "github.com/deroproject/derosuite/crypto"
    49  import "github.com/deroproject/derosuite/crypto/ringct"
    50  import "github.com/deroproject/derosuite/blockchain/rpcserver"
    51  */
    52  
    53  //import "fmt"
    54  import "time"
    55  import "math/rand"
    56  
    57  import "github.com/beevik/ntp"
    58  import "github.com/romana/rlog"
    59  
    60  import "github.com/deroproject/derosuite/globals"
    61  
    62  // these servers automatically rotate every hour as per documentation
    63  // we also rotate them randomly
    64  // TODO support ipv6
    65  var timeservers = []string{
    66  	"0.pool.ntp.org",
    67  	"1.pool.ntp.org",
    68  	"2.pool.ntp.org",
    69  	"3.pool.ntp.org",
    70  }
    71  
    72  // continusosly checks time for deviation if possible
    73  func time_check_routine() {
    74  
    75  	// initial initial warning should NOT get hidden in messages
    76  
    77  	random := rand.New(globals.NewCryptoRandSource())
    78  	timeinsync := false
    79  	for {
    80  
    81  		if !timeinsync {
    82  			time.Sleep(5 * time.Second)
    83  		} else {
    84  			time.Sleep(2 * 60 * time.Second) // check every 2 minutes
    85  		}
    86  
    87  		server := timeservers[random.Int()%len(timeservers)]
    88  		response, err := ntp.Query(server)
    89  		if err != nil {
    90  			rlog.Warnf("error while querying time  server %s err %s", server, err)
    91  		} else {
    92  			//globals.Logger.Infof("Local UTC time %+v server UTC time %+v", time.Now().UTC(), response.Time.UTC())
    93  			if response.ClockOffset.Seconds() > -1.1 && response.ClockOffset.Seconds() < 1.1 {
    94  				timeinsync = true
    95  			} else {
    96  				globals.Logger.Warnf("\nYour system time deviation is more than 1 secs (%s)."+
    97  					"\nYou may experience chain sync issues and/or other side-effects."+
    98  					"\nIf you are mining, your blocks may get rejected."+
    99  					"\nPlease sync your system using NTP software (default availble in all OS)."+
   100  					"\n eg. ntpdate pool.ntp.org  (for linux/unix)", response.ClockOffset)
   101  			}
   102  		}
   103  	}
   104  }