timekoin /v1.9/generation.php

Language PHP Lines 188
MD5 Hash 7ec1f27768402b608251b9e23e3ffe9a Estimated Cost $2,865 (why?)
Repository https://bitbucket.org/rev22/timekoin.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?PHP
include 'configuration.php';
include 'function.php';
set_time_limit(60);
//***********************************************************************************
//***********************************************************************************
if(GENERATION_DISABLED == TRUE || TIMEKOIN_DISABLED == TRUE)
{
	// This has been disabled
	exit;
}
//***********************************************************************************
//***********************************************************************************
mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
mysql_select_db(MYSQL_DATABASE);

$loop_active = mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'generation_heartbeat_active' LIMIT 1"),0,"field_data");

// Check if loop is already running
if($loop_active == 0)
{
	// Set the working status of 1
	$sql = "UPDATE `main_loop_status` SET `field_data` = '1' WHERE `main_loop_status`.`field_name` = 'generation_heartbeat_active' LIMIT 1";
	mysql_query($sql);
}
else
{
	// Loop called while still working
	exit;
}
//***********************************************************************************
//***********************************************************************************
// Is generation turned on for our server key?
$next_generation_cycle = transaction_cycle(1);
$current_generation_cycle = transaction_cycle(0);

// Can we work on the transactions in the database?
// Not allowed 60 seconds before and 35 seconds after generation cycle.
if(($next_generation_cycle - time()) > 60 && (time() - $current_generation_cycle) > 35)
{
	// Generation Peer Check	
	$sql = "SELECT * FROM `generating_peer_list`";
	
	$sql_result = mysql_query($sql);
	$sql_num_results = mysql_num_rows($sql_result);

	if($sql_num_results > 0 )
	{
		for ($i = 0; $i < $sql_num_results; $i++)
		{
			$sql_row = mysql_fetch_array($sql_result);
			$public_key = $sql_row["public_key"];
			$last_generation = $sql_row["last_generation"];

			if(time() - $last_generation > 7200) // 2 Hours without generation gets the peer removed
			{
				$sql = "DELETE FROM `generating_peer_list` WHERE `generating_peer_list`.`public_key` = '$public_key'";
				mysql_query($sql);
				$peer_purge = TRUE;				
			}
		}

		if($peer_purge == TRUE)
		{
			// Update peer list hash to avoid a race condition
			$sql = "SELECT * FROM `generating_peer_list` ORDER BY `join_peer_list`";

			$sql_result = mysql_query($sql);
			$sql_num_results = mysql_num_rows($sql_result);

			$generating_hash = 0;

			if($sql_num_results > 0)
			{
				for ($i = 0; $i < $sql_num_results; $i++)
				{
					$sql_row = mysql_fetch_array($sql_result);
					$generating_hash .= $sql_row["public_key"] . $sql_row["join_peer_list"];
				}

				$generating_hash = hash('md5', $generating_hash);
			}

			$sql = "UPDATE `options` SET `field_data` = '$generating_hash' WHERE `options`.`field_name` = 'generating_peers_hash' LIMIT 1";
			mysql_query($sql);

		} // End peer purge check

	} // End results check
//***********************************************************************************	
	// Generation Check
	$generation_option = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'generate_currency' LIMIT 1"),0,"field_data");

	if($generation_option == "0")
	{
		// Generation is turned off
	}
	else
	{
		// Check to see if we are in the allowed generation peer list
		$my_public_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_public_key' LIMIT 1"),0,"field_data");
		$found_public_key = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `public_key` = '$my_public_key' LIMIT 1"),0,"join_peer_list");

		if(empty($found_public_key) == TRUE)
		{
			// Check to see if this request is already in my transaction queue
			$found_public_trans_queue = mysql_result(mysql_query("SELECT * FROM `my_transaction_queue` WHERE `attribute` = 'R' LIMIT 1"),0,"timestamp");				

			if(empty($found_public_trans_queue) == TRUE)
			{
				$my_private_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_private_key' LIMIT 1"),0,"field_data");

				// Generate a network request to be added to the generation peer list
				$generation_request = ARBITRARY_KEY . rand(1, 999999);

				openssl_private_encrypt($generation_request, $encryptedData1, $my_private_key);
				$encryptedData64_1 = base64_encode($encryptedData1);				
				$duplicate_hash_check = hash('sha256', $generation_request . $generation_request . $generation_request);

				$sql = "INSERT INTO `my_transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)
				VALUES ('" . time() . "', '$my_public_key', '$encryptedData64_1', '$generation_request' , '$generation_request', '$duplicate_hash_check' , 'R')";

				mysql_query($sql);
			}
		}
		else
		{
			// Look into the future with the magic of math to see if the next generation will even be allowed
			$str = strval(transaction_cycle(1));
			$last3_gen = $str[strlen($str)-3];

			TKRandom::seed(transaction_cycle(1, TRUE));
			$tk_random_number = TKRandom::num(0, 9);

			// Random generation time that can be duplicated across all servers
			if($last3_gen + $tk_random_number < 10)
			{
				$my_private_key = mysql_result(mysql_query("SELECT * FROM `my_keys` WHERE `field_name` = 'server_private_key' LIMIT 1"),0,"field_data");

				// Server public key is listed as a qualified generation server.
				// Has the server submitted it's currency generation to the transaction queue?
				$found_public_key_my_queue = mysql_result(mysql_query("SELECT * FROM `my_transaction_queue` WHERE `attribute` = 'G' LIMIT 1"),0,"timestamp");
				$found_public_key_trans_queue = mysql_result(mysql_query("SELECT * FROM `transaction_queue` WHERE `public_key` = '$my_public_key' AND `attribute` = 'G' LIMIT 1"),0,"timestamp");				
				$join_peer_list = mysql_result(mysql_query("SELECT * FROM `generating_peer_list` WHERE `public_key` = '$my_public_key' LIMIT 1"),0,"join_peer_list");

				if(empty($found_public_key_my_queue) == TRUE && empty($found_public_key_trans_queue) == TRUE && (time() - $join_peer_list) >= 3600)
				{
					// How much can be generated at one time?
					$allowed_amount = peer_gen_amount($my_public_key);

					//Not found, add it to transaction queue
					$arr1 = str_split($my_public_key, 181);

					openssl_private_encrypt($arr1[0], $encryptedData1, $my_private_key);
					$encryptedData64_1 = base64_encode($encryptedData1);
					openssl_private_encrypt($arr1[1], $encryptedData2, $my_private_key);
					$encryptedData64_2 = base64_encode($encryptedData2);
					$transaction_data = "AMOUNT=$allowed_amount---TIME=" . time() . "---HASH=" . hash('sha256', $encryptedData64_1 . $encryptedData64_2);
					openssl_private_encrypt($transaction_data, $encryptedData3, $my_private_key);
					$encryptedData64_3 = base64_encode($encryptedData3);
					$duplicate_hash_check = hash('sha256', $encryptedData64_1 . $encryptedData64_2 . $encryptedData64_3);

					$sql = "INSERT INTO `my_transaction_queue` (`timestamp`,`public_key`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)
					VALUES ('" . time() . "', '$my_public_key', '$encryptedData64_1', '$encryptedData64_2' , '$encryptedData64_3', '$duplicate_hash_check' , 'G')";
					
					mysql_query($sql);
				}

			} // Future generation allowed check

		} // Public Key Check

	} // Generation enabled check
//***********************************************************************************

} // End Time allowed check
//***********************************************************************************
//***********************************************************************************
// Script finished, set status to 0
$sql = "UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'generation_heartbeat_active' LIMIT 1";
mysql_query($sql);

// Record when this script finished
$sql = "UPDATE `main_loop_status` SET `field_data` = '" . time() . "' WHERE `main_loop_status`.`field_name` = 'generation_last_heartbeat' LIMIT 1";
mysql_query($sql);

?>
Back to Top