Re: Difficulty

http://nullvoid.org/bitcoin/difficultiez.php
Code:

<?
 header("Content-type: text/html");
 require_once 'jsonRPCClient.php';
 $data=new jsonRPCClient('http://127.0.0.1:8332');
 $blockcount = $data->getblockcount();
 $now = date("U");
 $blockfile = "blockdata";
 $data = file($blockfile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); array_pop($data);
?><html>
 <head>
  <meta http-equiv="refresh" content="100000">
 </head>
 <body>
  <pre>
<?
 function humantime($secs) {
 if ($secs<0) return false;
 $m = (int)($secs / 60); $s = $secs % 60; $s = ($s <= 9) ? "0$s" : $s;
 $h = (int)($m / 60); $m = $m % 60; $m = ($m <= 9) ? "0$m" : $m;
 $d = (int)($h / 24); $h = $h % 24; $h = ($h <= 9) ? "0$h" : $h;
 $d = ($d <= 9) ? "0$d" : $d;
 return $d."d $h:$m:$s";
 }

 // Converted from ArtForz's python code http://bitcointalk.org/index.php?topic=464.msg4080#msg4080
 function uint256_from_compact($c) {
 $nbytes = ($c >> 24) & 0xFF;
 return bcmul($c & 0xFFFFFF,bcpow(2,8 * ($nbytes - 3)));
 }
 
 $tblock = 0;
 $nTargetTimespan = 60 * 60 * 24 * 14;
 bcscale(256);
 foreach ($data as $line) {
 $blocks = strtok($line, " ");
 $date = strtok(" ");
 $avghash = strtok(" ");
 $bits = strtok(" ");
 if ($blocks == 0 || $blocks == $tblock) {
 $tblock = $blocks + 2016;
 $blocknum = str_repeat(" ", 6 - strlen($blocks)).$blocks;
 echo "Block $blocknum was generated at $date (".date("r", $date).")";
 if ($blocks != 0) {
 $intervalnum = $date - $lastdate;
 $interval = str_repeat(" ", 9 - strlen($intervalnum)).$intervalnum;
 echo "    $interval seconds interval (".humantime($intervalnum).")";
 echo "   Difficulty: ".bcdiv(bcdiv(bcmul(uint256_from_compact(0x1D00FFFF),1000), uint256_from_compact($bits)), 1000);
 $lastdate = $date;
 }
 echo "<br>";
 }
 }
?>
  </pre>
 </body>
</html>

You were looking at the wrong code.  Here’s the code that applies:

Code:
bool CBlock::CheckBlock() const
{
...
    // Check timestamp
    if (nTime > GetAdjustedTime() + 2 * 60 * 60)
        return error("CheckBlock() : block timestamp too far in the future");
...

bool CBlock::AcceptBlock()
{
   ...
    // Check timestamp against prev
    if (nTime <= pindexPrev->GetMedianTimePast())
        return error("AcceptBlock() : block's timestamp is too early");

The timestamp is limited to up to 2 hours in the future.  It can be earlier than the previous block, but it must be greater than the median of the last 11 blocks.  The reason for doing it that way is so the time can get corrected in the next block if the previous block had the time too far in the future, like what happened.

1,061 total views, 2 views today

https://bitcointalk.org/index.php?topic=587.msg6301#msg6301