コード例 #1
0
ファイル: task.php プロジェクト: LoveLeAnon/timekoin
function transaction_queue()
{
    $next_transaction_cycle = transaction_cycle(1);
    $current_transaction_cycle = transaction_cycle(0);
    $results;
    // Wipe transaction queue of all old transaction from current to previous cycle
    if (rand(1, 2) == 2) {
        mysql_query("DELETE QUICK FROM `transaction_queue` WHERE `transaction_queue`.`timestamp` < {$current_transaction_cycle}");
    }
    // Create a hash of my own transaction queue
    $transaction_queue_hash = queue_hash();
    // How does my transaction queue compare to others?
    // Ask all of my active peers
    ini_set('user_agent', 'Timekoin Client (Queueclerk) v' . TIMEKOIN_VERSION);
    ini_set('default_socket_timeout', 2);
    // Timeout for request in seconds
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    $hash_different = array();
    $sql = "SELECT * FROM `active_peer_list` ORDER BY RAND()";
    $sql_result = mysql_query($sql);
    $sql_num_results = mysql_num_rows($sql_result);
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    if ($sql_num_results > 0) {
        $hash_different = array();
        for ($i = 0; $i < $sql_num_results; $i++) {
            $sql_row = mysql_fetch_array($sql_result);
            $ip_address = $sql_row["IP_Address"];
            $domain = $sql_row["domain"];
            $subfolder = $sql_row["subfolder"];
            $port_number = $sql_row["port_number"];
            $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 40, "queueclerk.php?action=trans_hash&client=api");
            if ($transaction_queue_hash === $poll_peer) {
                $transaction_queue_hash_match++;
            } else {
                if (empty($poll_peer) == FALSE) {
                    $transaction_queue_hash_different++;
                    $hash_different["ip_address{$transaction_queue_hash_different}"] = $ip_address;
                    $hash_different["domain{$transaction_queue_hash_different}"] = $domain;
                    $hash_different["subfolder{$transaction_queue_hash_different}"] = $subfolder;
                    $hash_different["port_number{$transaction_queue_hash_different}"] = $port_number;
                }
            }
        }
        // End for Loop
    }
    // End number of results check
    // Compare tallies
    if ($transaction_queue_hash_different > 0) {
        // Transaction Queue still not in sync with all peers
        $hash_array = array();
        $transaction_counter = 0;
        for ($i = 1; $i < $transaction_queue_hash_different + 1; $i++) {
            $ip_address = $hash_different["ip_address{$i}"];
            $domain = $hash_different["domain{$i}"];
            $subfolder = $hash_different["subfolder{$i}"];
            $port_number = $hash_different["port_number{$i}"];
            $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 8200, "queueclerk.php?action=queue&client=api");
            // Bring up first match (if any) to compare agaist our database
            $match_number = 1;
            $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            $transaction_counter = 0;
            $peer_transaction_limit = 100;
            $mismatch_error_count = 0;
            $mismatch_error_limit = 10;
            while (empty($current_hash) == FALSE) {
                // Count transactions coming from this peer
                $transaction_counter++;
                if ($transaction_counter > $peer_transaction_limit) {
                    break;
                }
                if ($mismatch_error_count > $mismatch_error_limit) {
                    break;
                }
                //Check if this transaction is already in our queue
                $hash_match = mysql_result(mysql_query("SELECT hash FROM `transaction_queue` WHERE `hash` = '{$current_hash}' LIMIT 1"), 0, 0);
                if (empty($hash_match) == TRUE) {
                    // This peer has a different transaction, ask for the full details of it
                    $poll_hash = poll_peer($ip_address, $domain, $subfolder, $port_number, 1500, "queueclerk.php?action=transaction&number={$current_hash}&client=api");
                    $transaction_timestamp = filter_sql(find_string("-----timestamp=", "-----public_key", $poll_hash));
                    $transaction_public_key = find_string("-----public_key=", "-----crypt1", $poll_hash);
                    $transaction_crypt1 = filter_sql(find_string("-----crypt1=", "-----crypt2", $poll_hash));
                    $transaction_crypt2 = filter_sql(find_string("-----crypt2=", "-----crypt3", $poll_hash));
                    $transaction_crypt3 = filter_sql(find_string("-----crypt3=", "-----hash", $poll_hash));
                    $transaction_hash = filter_sql(find_string("-----hash=", "-----attribute", $poll_hash));
                    $transaction_attribute = find_string("-----attribute=", "-----end", $poll_hash);
                    $transaction_qhash = find_string("---qhash=", "---endqhash", $poll_hash);
                    // If a qhash is included, use this to verify the data
                    if (empty($transaction_qhash) == FALSE) {
                        $qhash = $transaction_timestamp . $transaction_public_key . $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3 . $transaction_hash . $transaction_attribute;
                        $qhash = hash('md5', $qhash);
                        // Compare hashes to make sure data is intact
                        if ($transaction_qhash != $qhash) {
                            $transaction_attribute = "mismatch";
                            $mismatch_error_count++;
                        }
                    } else {
                        // Qhash is required to match hash now
                        $transaction_attribute = "mismatch";
                        $mismatch_error_count++;
                    }
                    $transaction_public_key = filter_sql(base64_decode($transaction_public_key));
                    if ($transaction_attribute == "T" || $transaction_attribute == "G") {
                        // Decrypt transaction information for regular transaction data
                        // and check to make sure the public key that is being sent to
                        // has not been tampered with.
                        $transaction_info = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt3));
                        $inside_transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
                        // Check if a message is encoded in this data as well
                        if (strlen($inside_transaction_hash) != 64) {
                            // A message is also encoded
                            $inside_transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
                        }
                        // Check Hash against 3 crypt fields
                        $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
                    }
                    $final_hash_compare = hash('sha256', $transaction_crypt1 . $transaction_crypt2);
                    // Check to make sure this transaction is even valid (hash check, length check, & timestamp)
                    if ($transaction_hash == $crypt_hash_check && $inside_transaction_hash == $final_hash_compare && strlen($transaction_public_key) > 300 && $transaction_timestamp >= $current_transaction_cycle && $transaction_timestamp < $next_transaction_cycle) {
                        // Check for 100 public key limit in the transaction queue
                        $sql = "SELECT timestamp FROM `transaction_queue` WHERE `public_key` = '{$transaction_public_key}'";
                        $sql_result = mysql_query($sql);
                        $sql_num_results = mysql_num_rows($sql_result);
                        if ($sql_num_results < 100) {
                            // Transaction hash and real hash match
                            $sql = "INSERT INTO `transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)\n\t\t\t\t\t\t\tVALUES ('{$transaction_timestamp}', '{$transaction_public_key}', '{$transaction_crypt1}', '{$transaction_crypt2}' , '{$transaction_crypt3}', '{$transaction_hash}' , '{$transaction_attribute}')";
                            mysql_query($sql);
                        }
                    }
                }
                // End Empty Hash Check
                $match_number++;
                $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            }
            // End While Loop
        }
        // End For Loop
    }
    // End Compare Tallies
    return;
}
コード例 #2
0
ファイル: queueclerk.php プロジェクト: LoveLeAnon/timekoin
                             // Transaction hash and real hash match.
                             mysql_query("INSERT INTO `transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)\n\t\t\t\t\t\t\tVALUES ('{$transaction_timestamp}', '{$transaction_public_key}', '{$transaction_crypt1}', '{$transaction_crypt2}' , '{$transaction_crypt3}', '{$transaction_hash}' , '{$transaction_attribute}')");
                         } else {
                             write_log("More Than 100 Transactions Trying to Queue for Key: " . base64_encode($transaction_public_key), "QC");
                         }
                     }
                 }
                 // End Empty Hash Check
                 $match_number++;
                 $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
             }
             // End While Loop
         }
         // End For Loop
         // Build queue hash after completion
         $transaction_queue_hash = queue_hash();
         // Store in database for quick reference from database
         mysql_query("UPDATE `options` SET `field_data` = '{$transaction_queue_hash}' WHERE `options`.`field_name` = 'transaction_queue_hash' LIMIT 1");
     }
     // End Compare Tallies
 }
 // If/then Check for valid times
 //***********************************************************************************
 //***********************************************************************************
 $loop_active = mysql_result(mysql_query("SELECT field_data FROM `main_loop_status` WHERE `field_name` = 'queueclerk_heartbeat_active' LIMIT 1"), 0, 0);
 // Check script status
 if ($loop_active == 3) {
     // Time to exit
     mysql_query("DELETE FROM `main_loop_status` WHERE `main_loop_status`.`field_name` = 'queueclerk_heartbeat_active'");
     exit;
 }
コード例 #3
0
ファイル: task.php プロジェクト: LoveLeAnon/timekoin
function transaction_queue()
{
    $next_transaction_cycle = transaction_cycle(1);
    $current_transaction_cycle = transaction_cycle(0);
    $results;
    // Wipe transaction queue of all old transaction from current to previous cycle
    if (rand(1, 2) == 2) {
        mysql_query("DELETE QUICK FROM `transaction_queue` WHERE `transaction_queue`.`timestamp` < {$current_transaction_cycle}");
    }
    // Create a hash of my own transaction queue
    $transaction_queue_hash = queue_hash();
    // How does my transaction queue compare to others?
    // Ask all of my active peers
    ini_set('user_agent', 'Timekoin Client (Queueclerk) v' . TIMEKOIN_VERSION);
    ini_set('default_socket_timeout', 2);
    // Timeout for request in seconds
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    $hash_different = array();
    $sql = "SELECT * FROM `active_peer_list` ORDER BY RAND()";
    $sql_result = mysql_query($sql);
    $sql_num_results = mysql_num_rows($sql_result);
    $transaction_queue_hash_match = 0;
    $transaction_queue_hash_different = 0;
    if ($sql_num_results > 0) {
        $hash_different = array();
        for ($i = 0; $i < $sql_num_results; $i++) {
            $sql_row = mysql_fetch_array($sql_result);
            $ip_address = $sql_row["IP_Address"];
            $domain = $sql_row["domain"];
            $subfolder = $sql_row["subfolder"];
            $port_number = $sql_row["port_number"];
            $poll_peer = poll_peer($ip_address, $domain, $subfolder, $port_number, 32, "queueclerk.php?action=trans_hash&client=api");
            if ($transaction_queue_hash === $poll_peer) {
                $transaction_queue_hash_match++;
            } else {
                if (empty($poll_peer) == FALSE) {
                    $transaction_queue_hash_different++;
                    $hash_different["ip_address{$transaction_queue_hash_different}"] = $ip_address;
                    $hash_different["domain{$transaction_queue_hash_different}"] = $domain;
                    $hash_different["subfolder{$transaction_queue_hash_different}"] = $subfolder;
                    $hash_different["port_number{$transaction_queue_hash_different}"] = $port_number;
                }
            }
        }
        // End for Loop
    }
    // End number of results check
    // Compare tallies
    if ($transaction_queue_hash_different > 0) {
        // Transaction Queue still not in sync with all peers
        $hash_array = array();
        $transaction_counter = 0;
        for ($i = 1; $i < $transaction_queue_hash_different + 1; $i++) {
            $ip_address = $hash_different["ip_address{$i}"];
            $domain = $hash_different["domain{$i}"];
            $subfolder = $hash_different["subfolder{$i}"];
            $port_number = $hash_different["port_number{$i}"];
            $poll_peer = filter_sql(poll_peer($ip_address, $domain, $subfolder, $port_number, 83000, "queueclerk.php?action=queue&client=api"));
            // Bring up first match (if any) to compare agaist our database
            $match_number = 1;
            $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            $transaction_counter = 0;
            $peer_transaction_limit = 1000;
            $mismatch_error_count = 0;
            $mismatch_error_limit = 10;
            while (empty($current_hash) == FALSE) {
                // Count transactions coming from this peer
                $transaction_counter++;
                if ($transaction_counter > $peer_transaction_limit) {
                    break;
                }
                if ($mismatch_error_count > $mismatch_error_limit) {
                    break;
                }
                if (strlen($current_hash) >= 64) {
                    // Old Queue System Check
                    //Check if this transaction is already in our queue
                    $hash_match = mysql_result(mysql_query("SELECT timestamp FROM `transaction_queue` WHERE `hash` = '{$current_hash}' LIMIT 1"), 0, 0);
                } else {
                    // New Queue System Check
                    $sql2 = "SELECT * FROM `transaction_queue`";
                    $sql_result2 = mysql_query($sql2);
                    $sql_num_results2 = mysql_num_rows($sql_result2);
                    $queue_hash_test = NULL;
                    $hash_match = NULL;
                    if ($sql_num_results2 > 0) {
                        for ($i2 = 0; $i2 < $sql_num_results2; $i2++) {
                            $sql_row2 = mysql_fetch_array($sql_result2);
                            $queue_hash_test .= $sql_row2["timestamp"] . $sql_row2["public_key"] . $sql_row2["crypt_data1"] . $sql_row2["crypt_data2"] . $sql_row2["crypt_data3"] . $sql_row2["hash"] . $sql_row2["attribute"];
                            if (hash('md5', $queue_hash_test) == $current_hash) {
                                // This Transaction Already Exist in the Queue
                                $hash_match = TRUE;
                                break;
                            } else {
                                // No match, continue searching
                                $hash_match = NULL;
                            }
                            // No match, move on to next record
                            $queue_hash_test = NULL;
                        }
                    }
                }
                if (empty($hash_match) == TRUE) {
                    // This peer has a different transaction, ask for the full details of it
                    $poll_hash = poll_peer($ip_address, $domain, $subfolder, $port_number, 1500, "queueclerk.php?action=transaction&number={$current_hash}&client=api");
                    $transaction_timestamp = find_string("-----timestamp=", "-----public_key", $poll_hash);
                    $transaction_public_key = find_string("-----public_key=", "-----crypt1", $poll_hash);
                    $transaction_crypt1 = find_string("-----crypt1=", "-----crypt2", $poll_hash);
                    $transaction_crypt2 = find_string("-----crypt2=", "-----crypt3", $poll_hash);
                    $transaction_crypt3 = find_string("-----crypt3=", "-----hash", $poll_hash);
                    $transaction_hash = find_string("-----hash=", "-----attribute", $poll_hash);
                    $transaction_attribute = find_string("-----attribute=", "-----end", $poll_hash);
                    $transaction_qhash = find_string("---qhash=", "---endqhash", $poll_hash);
                    // If a qhash is included, use this to verify the data
                    if (empty($transaction_qhash) == FALSE) {
                        $qhash = $transaction_timestamp . $transaction_public_key . $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3 . $transaction_hash . $transaction_attribute;
                        $qhash = hash('md5', $qhash);
                        // Compare hashes to make sure data is intact
                        if ($transaction_qhash != $qhash) {
                            $transaction_attribute = "mismatch";
                            $mismatch_error_count++;
                        } else {
                            // Make sure hash is actually valid and not made up to stop other transactions
                            $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
                            if ($crypt_hash_check != $transaction_hash) {
                                // Ok, something is wrong here...
                                $transaction_attribute = "mismatch";
                                $mismatch_error_count++;
                            }
                        }
                    } else {
                        // Qhash is required to match hash
                        $transaction_attribute = "mismatch";
                        $mismatch_error_count++;
                    }
                    $transaction_public_key = filter_sql(base64_decode($transaction_public_key));
                    if ($transaction_attribute == "R") {
                        // Check to make sure this public key isn't forged or made up to win the list
                        $inside_transaction_hash = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt1));
                        $final_hash_compare = $transaction_crypt2;
                        $crypt_hash_check = $transaction_hash;
                        $valid_amount = TRUE;
                        // No amount, but needs this to pass amount test
                        $public_key_to = $transaction_public_key;
                        // None is used, but needs this to pass the key length test
                    } else {
                        if ($transaction_attribute == "T" || $transaction_attribute == "G") {
                            // Decrypt transaction information for regular transaction data
                            // and check to make sure the public key that is being sent to
                            // has not been tampered with.
                            $transaction_info = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt3));
                            // Find destination public key
                            $public_key_to_1 = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt1));
                            $public_key_to_2 = tk_decrypt($transaction_public_key, base64_decode($transaction_crypt2));
                            $public_key_to = $public_key_to_1 . $public_key_to_2;
                            $transaction_amount_sent = find_string("AMOUNT=", "---TIME", $transaction_info);
                            $transaction_amount_sent_test = intval($transaction_amount_sent);
                            if ($transaction_amount_sent_test == $transaction_amount_sent) {
                                // Is a valid integer, amount greater than zero?
                                if ($transaction_amount_sent > 0) {
                                    $valid_amount = TRUE;
                                } else {
                                    $valid_amount = FALSE;
                                }
                            } else {
                                // Is NOT a valid integer, fail check
                                $valid_amount = FALSE;
                            }
                            if ($transaction_attribute == "G") {
                                if ($transaction_amount_sent_test > 10) {
                                    // Filter silly generation amounts :p
                                    $valid_amount = FALSE;
                                }
                            }
                            $inside_transaction_hash = find_string("HASH=", "", $transaction_info, TRUE);
                            // Check if a message is encoded in this data as well
                            if (strlen($inside_transaction_hash) != 64) {
                                // A message is also encoded
                                $inside_transaction_hash = find_string("HASH=", "---MSG", $transaction_info);
                            }
                            // Check Hash against 3 crypt fields
                            $crypt_hash_check = hash('sha256', $transaction_crypt1 . $transaction_crypt2 . $transaction_crypt3);
                        }
                        $final_hash_compare = hash('sha256', $transaction_crypt1 . $transaction_crypt2);
                    }
                    // Check to make sure this transaction is even valid (hash check, length check, & timestamp)
                    if ($transaction_hash == $crypt_hash_check && $inside_transaction_hash == $final_hash_compare && strlen($transaction_public_key) > 300 && strlen($public_key_to) > 300 && $transaction_timestamp >= $current_transaction_cycle && $transaction_timestamp < $next_transaction_cycle && $valid_amount == TRUE) {
                        // Check for 100 public key limit in the transaction queue
                        $sql = "SELECT timestamp FROM `transaction_queue` WHERE `public_key` = '{$transaction_public_key}'";
                        $sql_result = mysql_query($sql);
                        $sql_num_results = mysql_num_rows($sql_result);
                        if ($sql_num_results < 100) {
                            // Transaction hash and real hash match.
                            mysql_query("INSERT INTO `transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)\n\t\t\t\t\t\t\tVALUES ('{$transaction_timestamp}', '{$transaction_public_key}', '{$transaction_crypt1}', '{$transaction_crypt2}' , '{$transaction_crypt3}', '{$transaction_hash}' , '{$transaction_attribute}')");
                        }
                    }
                }
                // End Empty Hash Check
                $match_number++;
                $current_hash = find_string("---queue{$match_number}=", "---end{$match_number}", $poll_peer);
            }
            // End While Loop
        }
        // End For Loop
    }
    // End Compare Tallies
    return;
}