Merge pull request #234 from svost/bitcoin-syncscore
authorCryptoManiac <CryptoManiac@users.noreply.github.com>
Sun, 20 Sep 2015 13:08:26 +0000 (06:08 -0700)
committerCryptoManiac <CryptoManiac@users.noreply.github.com>
Sun, 20 Sep 2015 13:08:26 +0000 (06:08 -0700)
Use pnode->nLastRecv as sync score directly

src/init.cpp
src/miner.cpp
src/ntp.cpp
src/ntp.h

index 1eb28a5..b93f2bd 100644 (file)
@@ -8,6 +8,7 @@
 #include "net.h"
 #include "init.h"
 #include "util.h"
+#include "ntp.h"
 #include "ui_interface.h"
 #include "checkpoints.h"
 #include <boost/format.hpp>
@@ -995,6 +996,23 @@ bool AppInit2()
     if (fServer)
         NewThread(ThreadRPCServer, NULL);
 
+    // ********************************************************* Step 12: add time data from four random NTP servers.
+    uiInterface.InitMessage(_("Synchronizing time through NTP..."));
+    printf("Synchronizing time through NTP...\n");
+    int i = 0;
+    while(i < 4) {
+        CNetAddr ip;
+        int64_t nTime = NtpGetTime(ip);
+
+        if (nTime > 0 && nTime != 2085978496) { // Skip the deliberately wrong timestamps
+            AddTimeData(ip, nTime);
+            printf("AddTimeData(%s, %" PRId64 ")\n", ip.ToString().c_str(), nTime);
+        }
+
+        i++;
+    }
+    uiInterface.InitMessage(_("Done"));
+    printf("Done\n");
     // ********************************************************* Step 12: finished
 
     uiInterface.InitMessage(_("Done loading"));
index 0d63e3c..f12376a 100644 (file)
@@ -289,9 +289,6 @@ CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake)
             if (tx.nTime > GetAdjustedTime() || (fProofOfStake && tx.nTime > txCoinStake->nTime))
                 continue;
 
-            // Simplify transaction fee - allow free = false
-            int64_t nMinFee = tx.GetMinFee(nBlockSize, true, GMF_BLOCK, nTxSize);
-
             // Skip free transactions if we're past the minimum block size:
             if (fSortedByFee && (dFeePerKb < nMinTxFee) && (nBlockSize + nTxSize >= nBlockMinSize))
                 continue;
@@ -314,10 +311,13 @@ CBlock* CreateNewBlock(CWallet* pwallet, CTransaction *txCoinStake)
             if (!tx.FetchInputs(txdb, mapTestPoolTmp, false, true, mapInputs, fInvalid))
                 continue;
 
+            // Transaction fee
             int64_t nTxFees = tx.GetValueIn(mapInputs)-tx.GetValueOut();
+            int64_t nMinFee = tx.GetMinFee(nBlockSize, true, GMF_BLOCK, nTxSize);
             if (nTxFees < nMinFee)
                 continue;
 
+            // Sigops accumulation
             nTxSigOps += tx.GetP2SHSigOpCount(mapInputs);
             if (nBlockSigOps + nTxSigOps >= MAX_BLOCK_SIGOPS)
                 continue;
index 58d224c..9c1b6e5 100644 (file)
@@ -81,9 +81,9 @@ struct pkt {
   uint8_t  mac[5 * sizeof(uint32_t)]; /* mac */
 };
 
-int nServersCount = 103;
+int nServersCount = 107;
 
-std::string NtpServers[103] = {
+std::string NtpServers[107] = {
     // Microsoft
     "time.windows.com",
 
@@ -123,6 +123,10 @@ std::string NtpServers[103] = {
     "3.ru.pool.ntp.org",
 
     // United States
+    "ntp-01.caltech.edu",
+    "ntp-02.caltech.edu",
+    "ntp-03.caltech.edu",
+    "ntp-04.caltech.edu",
     "nist1-pa.ustiming.org",
     "time-a.nist.gov ",
     "time-b.nist.gov ",
@@ -344,7 +348,7 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr)
     }
 
     fd_set fdset;
-    struct timeval timeout = {5, 0};
+    struct timeval timeout = {10, 0};
     FD_ZERO(&fdset);
     FD_SET(sockfd, &fdset);
 
@@ -389,6 +393,24 @@ int64_t NtpGetTime()
     return nTime;
 }
 
+int64_t NtpGetTime(CNetAddr& ip)
+{
+    struct sockaddr cliaddr;
+
+    SOCKET sockfd;
+    socklen_t servlen;
+
+    if (!InitWithRandom(sockfd, servlen, &cliaddr))
+        return -1;
+
+    ip = CNetAddr(((sockaddr_in *)&cliaddr)->sin_addr);
+    int64_t nTime = DoReq(sockfd, servlen, cliaddr);
+
+    closesocket(sockfd);
+
+    return nTime;
+}
+
 int64_t NtpGetTime(std::string &strHostName)
 {
     struct sockaddr cliaddr;
index 4963124..d6c5863 100644 (file)
--- a/src/ntp.h
+++ b/src/ntp.h
@@ -1,3 +1,8 @@
+// Get time from random server.
 int64_t NtpGetTime();
-int64_t NtpGetTime(std::string &strHostName);
 
+// Get time from random server and return server address.
+int64_t NtpGetTime(CNetAddr& ip);
+
+// Get time from provided server.
+int64_t NtpGetTime(std::string &strHostName);