timekoin /v1.9/transclerk.php

Language PHP Lines 862
MD5 Hash d4c07af276778cfc87447029e4cdedbf Estimated Cost $15,387 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
<?PHP
include 'configuration.php';
include 'function.php';
set_time_limit(120);
//***********************************************************************************
//***********************************************************************************
if(TRANSCLERK_DISABLED == TRUE || TIMEKOIN_DISABLED == TRUE)
{
	// This has been disabled
	exit;
}
//***********************************************************************************
//***********************************************************************************
// Open persistent connection to database
mysql_connect(MYSQL_IP,MYSQL_USERNAME,MYSQL_PASSWORD);
mysql_select_db(MYSQL_DATABASE);

// Check for banned IP address
$ip = mysql_result(mysql_query("SELECT * FROM `ip_banlist` WHERE `ip` = '" . $_SERVER['REMOTE_ADDR'] . "' LIMIT 1"),0,0);

if(empty($ip) == FALSE)
{
	// Sorry, your IP address has been banned :(
	exit;
}
//***********************************************************************************
//***********************************************************************************
// Answer transaction history hash poll
if($_GET["action"] == "history_hash")
{
	$current_history_hash = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'transaction_history_hash' LIMIT 1"),0,"field_data");

	echo $current_history_hash;

	// Log inbound IP activity
	mysql_query("INSERT INTO `ip_activity` (`timestamp` ,`ip`, `attribute`)VALUES ('" . time() . "', '" . $_SERVER['REMOTE_ADDR'] . "', 'TR')");
	exit;
}
//***********************************************************************************
//***********************************************************************************
// Answer block hash poll
if($_GET["action"] == "block_hash" && $_GET["block_number"] >= 0)
{
	$block_number = intval($_GET["block_number"]);

	$current_generation_block = transaction_cycle(0, TRUE);
	
	$time1 = transaction_cycle(0 - $current_generation_block + 1 + $block_number);
	$time2 = transaction_cycle(0 - $current_generation_block + 2 + $block_number);	

	$hash = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 AND `attribute` = 'H' LIMIT 1"),0,"hash");

	echo $hash;

	// Log inbound IP activity
	mysql_query("INSERT INTO `ip_activity` (`timestamp` ,`ip`, `attribute`)VALUES ('" . time() . "', '" . $_SERVER['REMOTE_ADDR'] . "', 'TR')");
	exit;
}
//***********************************************************************************
//***********************************************************************************
// Answer transaction data poll
if($_GET["action"] == "transaction_data" && $_GET["block_number"] >= 0)
{
	$block_number = intval($_GET["block_number"]);

	$current_generation_block = transaction_cycle(0, TRUE);
	
	$time1 = transaction_cycle(0 - $current_generation_block + $block_number);
	$time2 = transaction_cycle(0 - $current_generation_block + 1 + $block_number);	

	$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2";

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

	if($sql_num_results > 0)
	{
		for ($i = 0; $i < $sql_num_results; $i++)
		{
			$sql_row = mysql_fetch_array($sql_result);
			
			echo "-----timestamp$c=" , $sql_row["timestamp"] , "-----public_key_from$c=" , base64_encode($sql_row["public_key_from"]) , "-----public_key_to$c=" , base64_encode($sql_row["public_key_to"]);
			echo "-----crypt1data$c=" , $sql_row["crypt_data1"] , "-----crypt2data$c=" , $sql_row["crypt_data2"] , "-----crypt3data$c=" , $sql_row["crypt_data3"] , "-----hash$c=" , $sql_row["hash"];
			echo "-----attribute$c=" , $sql_row["attribute"] , "-----end$c";			

			$c++;
		}		
	}

	// Log inbound IP activity
	mysql_query("INSERT INTO `ip_activity` (`timestamp` ,`ip`, `attribute`)VALUES ('" . time() . "', '" . $_SERVER['REMOTE_ADDR'] . "', 'TR')");
	exit;
}
//***********************************************************************************
//***********************************************************************************
$loop_active = intval(mysql_result(mysql_query("SELECT * FROM `main_loop_status` WHERE `field_name` = 'transclerk_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` = 'transclerk_heartbeat_active' LIMIT 1";
	mysql_query($sql);
}
else
{
	// Loop called while still working
	exit;
}
//***********************************************************************************
//***********************************************************************************
$current_generation_cycle = transaction_cycle(0);
$next_generation_cycle = transaction_cycle(1);
$current_generation_block = transaction_cycle(0, TRUE);

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

// Can we work on the transactions in the database?
// Not allowed 30 seconds before and 30 seconds after generation cycle.
if(($next_generation_cycle - time()) > 30 && (time() - $current_generation_cycle) > 30 && $foundation_active == 0)
{
	// Check if the transaction history is blank or not (either from reset or new setup)
	$sql = "SELECT * FROM `transaction_history` LIMIT 5";

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

	$generation_arbitrary = ARBITRARY_KEY;
//***********************************************************************************
	if($sql_num_results == 0 && $sql_result !== FALSE) //New or blank transaction history
	{
		// Start by inserting the beginning arbitrary transaction from which all others will hash against 

		$sql = "INSERT INTO `transaction_history` (`timestamp` ,`public_key_from` ,`public_key_to` ,`crypt_data1` ,`crypt_data2` ,`crypt_data3` ,`hash` ,`attribute`)
	VALUES ('" . TRANSACTION_EPOCH . "', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', 'B')";
	
		mysql_query($sql);

		// Lock Treasurer script to prevent database chaos during the initial phase
		activate(TREASURER, 0);
	}
//***********************************************************************************
	if($sql_num_results > 0 && $sql_num_results < 4) // Write out some test hashes and compare to verify sha256 accuracy
	{
		// Beginning transaction should be in, check to make sure
		$beginning_transaction = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `public_key_from` = '$generation_arbitrary' AND `hash` = '$generation_arbitrary' LIMIT 1"),0,"timestamp");

		if($beginning_transaction == TRANSACTION_EPOCH)
		{
			// First transaction complete, continue processing
			$current_generation_block = transaction_cycle(0, TRUE);
			$cycles = 0;

			while($cycles < 3)
			{
				$first_generation_cycle = transaction_cycle((0 - $current_generation_block));	
				$second_generation_cycle = transaction_cycle((0 - $current_generation_block + 1));

				// Build Hash
				$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $first_generation_cycle AND `timestamp` < $second_generation_cycle";

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

				for ($i = 0; $i < $sql_num_results; $i++)
				{
					$sql_row = mysql_fetch_array($sql_result);
					$hash .= $sql_row["hash"];
				}

				// Transaction hash
				$hash = hash('sha256', $hash);

					$sql = "INSERT INTO `transaction_history` (`timestamp` ,`public_key_from` ,`public_key_to` ,`crypt_data1` ,`crypt_data2` ,`crypt_data3` ,`hash` ,`attribute`)
				VALUES ('$second_generation_cycle', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$generation_arbitrary', '$hash', 'H')";
		
				mysql_query($sql);

				$current_generation_block--;
				$cycles++;
			}

			$current_generation_block++;
			$second_generation_cycle = transaction_cycle((0 - $current_generation_block + 1));
			$hash_check = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` = '$second_generation_cycle' AND `attribute` = 'H' LIMIT 1"),0,"hash");

			// Now let's check the results to make sure they match what should be expected
			if($hash_check == SHA256TEST)
			{
				// Passed final hash checking.
				// Unlock Treasurer script to allow live processing
				activate(TREASURER, 1);
				
				// Start a block rebuild from this since a new database is going to be far
				// behind the history of the other active peers
				$sql = "UPDATE `options` SET `field_data` = '3' WHERE `options`.`field_name` = 'block_check_start' LIMIT 1";
				mysql_query($sql);
			}
			else
			{
				write_log("Server Failed Initial Encryption Generation and Verification Testing", "TR");
			}
		}

	} // End database preparation and hash testing
//***********************************************************************************

	// Check the transaction hash generation
	if($sql_num_results > 3) // 4 or more transactions on a live database, let the magic begin
	{	
//***********************************************************************************		
// Update transaction history hash
		$current_history_hash = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'transaction_history_hash' LIMIT 1"),0,"field_data");
		$transaction_history_block_check = intval(mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'transaction_history_block_check' LIMIT 1"),0,"field_data"));
		$foundation_block_check = intval(mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'foundation_block_check' LIMIT 1"),0,"field_data"));

		if($transaction_history_block_check != 0 || $foundation_block_check == 1)
		{
			//A random block check came up wrong, do a single error check sweep
			$error_check_active = TRUE;

			// Change hash to mismatch on purpose
			$current_history_hash = "ERROR_CHECK";

			if($transaction_history_block_check > 0 && $foundation_block_check == 0)
			{
				write_log("Starting History Check from Block #$transaction_history_block_check", "TC");
			}
			else
			{
				$foundation_block_check_start = intval(mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'foundation_block_check_start' LIMIT 1"),0,"field_data"));
				write_log("Resuming History Check from Block #$foundation_block_check_start", "TC");
			}

			// Update database with ERROR_CHECK hash
			$sql = "UPDATE `options` SET `field_data` = '$current_history_hash' WHERE `field_name` = 'transaction_history_hash' LIMIT 1";
			mysql_query($sql);
		}
		else
		{
			$total = mysql_query("SELECT COUNT(*) FROM `transaction_history`");
			$total = mysql_fetch_array($total); 
			$hash = $total[0];

			$previous_foundation_block = foundation_cycle(-1, TRUE);
			$current_foundation_cycle = foundation_cycle(0);
			$next_foundation_cycle = foundation_cycle(1);			
			$current_history_foundation = mysql_result(mysql_query("SELECT * FROM `transaction_foundation` WHERE `block` = $previous_foundation_block LIMIT 1"),0,"hash");

			$hash .= $current_history_foundation;

			$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $current_foundation_cycle AND `timestamp` < $next_foundation_cycle AND `attribute` = 'H' ORDER BY `timestamp`";
			$sql_result = mysql_query($sql);
			$sql_num_results = mysql_num_rows($sql_result);

			for ($i = 0; $i < $sql_num_results; $i++)
			{
				$sql_row = mysql_fetch_array($sql_result);
				$hash .= $sql_row["hash"];
			}	

			$history_hash = hash('md5', $hash);

			if($history_hash == $current_history_hash)
			{
				// Already in database, no need to update
			}
			else
			{
				$current_history_hash = $history_hash;
				
				// Update database with new hash
				$sql = "UPDATE `options` SET `field_data` = '$history_hash' WHERE `field_name` = 'transaction_history_hash' LIMIT 1";
				mysql_query($sql);
			}
		}
//***********************************************************************************	
//***********************************************************************************
	// Does my current history hash match all my peers?
	// Ask all of my active peers
	ini_set('default_socket_timeout', 3); // Timeout for request in seconds
	ini_set('user_agent', 'Timekoin Server (Transclerk) v' . TIMEKOIN_VERSION);
	$context = stream_context_create(array('http' => array('header'=>'Connection: close'))); // Force close socket after complete

	$sql = "SELECT * FROM `active_peer_list`";

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

	$trans_list_hash_match = 0;
	$trans_list_hash_different = 0;
	$site_address;

	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"];

			if(empty($domain) == TRUE)
			{
				$site_address = $ip_address;
			}
			else
			{
				$site_address = $domain;
			}

			if($port_number == 443)
			{
				$ssl = "s";
			}
			else
			{
				$ssl = NULL;
			}

			$poll_peer = filter_sql(file_get_contents("http$ssl://$site_address:$port_number/$subfolder/transclerk.php?action=history_hash", FALSE, $context, NULL, 65));
			
			if($current_history_hash === $poll_peer)
			{
				$trans_list_hash_match++;
			}
			else
			{
				if(empty($poll_peer) == FALSE && strlen($poll_peer) > 5 && $poll_peer != "ERROR_CHECK")
				{
					$trans_list_hash_different++;

					$hash_different["ip_address$trans_list_hash_different"] = $ip_address;
					$hash_different["domain$trans_list_hash_different"] = $domain;
					$hash_different["subfolder$trans_list_hash_different"] = $subfolder;
					$hash_different["port_number$trans_list_hash_different"] = $port_number;				
				}
			}
		} // End for Loop

	} // End number of results check
//***********************************************************************************
	// Compare tallies
	if($trans_list_hash_different > $trans_list_hash_match)
	{
		//More than 50% of the active peers have a different transaction history list, start comparing your
		//transaction list with one that is different
		$hash_check_counter = 15; // How many blocks to check at each cycle (default)

		if($error_check_active == FALSE)
		{
			$hash_number = intval(mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'block_check_start' LIMIT 1"),0,"field_data"));

			if($hash_number == 0)
			{
				// A new check, start just 10 blocks from the end to avoid checking the entire history
				// from the beginning
				$hash_number = transaction_cycle(-10, TRUE);
			}
			else
			{
				write_log("Resuming History Check from Block #$hash_number", "TC");
			}
		}
		else
		{
			if($foundation_block_check == 1)
			{
				// Start from the block that the foundation begins
				$foundation_block_check_start = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'foundation_block_check_start' LIMIT 1"),0,"field_data");
				$foundation_block_check_end = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'foundation_block_check_end' LIMIT 1"),0,"field_data");

				if($foundation_block_check_start > $foundation_block_check_end)
				{
					// Check is finished
					$sql = "UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'foundation_block_check' LIMIT 1";
					mysql_query($sql);

					// Push forward any previous block checks that were in progress
					$sql = "UPDATE `options` SET `field_data` = '$foundation_block_check_start' WHERE `options`.`field_name` = 'block_check_start' LIMIT 1";
					mysql_query($sql);

					// Reset block back counter
					$sql = "UPDATE `options` SET `field_data` = '1' WHERE `options`.`field_name` = 'block_check_back' LIMIT 1";
					mysql_query($sql);

					$hash_number = $foundation_block_check_start;
				}
				else
				{
					$hash_check_counter = 20;
					$hash_number = $foundation_block_check_start;
				}
			}
			else
			{
				$hash_number = ($transaction_history_block_check - 1); // Start back 1 block from the error section
			}
		}

		$double_check_counter = 0;
		$sync_block = 0;

		if(($hash_number + $hash_check_counter) >= $current_generation_block)
		{
			// Adjust for when near the end of the transaction history
			$hash_check_counter = $hash_check_counter - (($hash_number + $hash_check_counter) - $current_generation_block);

			if($hash_check_counter == 0)
			{
				// Reached the end, delete the most current block to allow an auto-recorrect
				$sql = "DELETE FROM `transaction_history` WHERE `timestamp` >= $current_generation_cycle AND `timestamp` < $next_generation_cycle";
				mysql_query($sql);
				
				// How many times have this checked reached the end and still not fixed the transaction history?
				$hash_number_back_database = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'block_check_back' LIMIT 1"),0,"field_data");
				
				$hash_number_back = $hash_number_back_database * 10;

				// Every time we reach the end and still the problem exist, go back another 10 blocks until eventually
				// we reach a point in history where the problem can be repaired
				$hash_number = $current_generation_block - $hash_number_back;

				$foundation_to_cycles = foundation_cycle(0, TRUE) * 500;

				if($current_generation_block - $hash_number < $foundation_to_cycles)
				{
					// Restart a check so that it doesn't dig into a valid
					// transaction foundation by accident
					$hash_number = $foundation_to_cycles;
				}

				if($hash_number_back > 30)
				{
					// More than 30 blocks back, start from the beginning of the current
					// transaction foundation as something might be wrong deeper in the history
					$hash_number = $foundation_to_cycles;
					mysql_query("UPDATE `options` SET `field_data` = '1' WHERE `options`.`field_name` = 'block_check_back' LIMIT 1");
				}
				else
				{
					// Increment back counter in case this was not far back enough
					// and it reaches this point again
					$hash_number_back_database++;
					mysql_query("UPDATE `options` SET `field_data` = '$hash_number_back_database' WHERE `options`.`field_name` = 'block_check_back' LIMIT 1");
				}

				$hash_check_counter = 10;  // Reset to check another 10 blocks forward
			}
		}

		$hash_disagree_peers = array();

		for ($h = 0; $h < $hash_check_counter; $h++)
		{
			$hash_agree = 0;
			$hash_disagree = 0;
			$double_check_block = FALSE;

			$time1 = transaction_cycle(0 - $current_generation_block + $hash_number);
			$time2 = transaction_cycle(0 - $current_generation_block + 1 + $hash_number);	

			$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";

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

			for ($i = 0; $i < $sql_num_results; $i++)
			{
				$sql_row = mysql_fetch_array($sql_result);
				$my_hash .= $sql_row["hash"];
			}		

			$my_hash = hash('sha256', $my_hash);

			for ($i = 1; $i < $trans_list_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"];

				if(empty($domain) == TRUE)
				{
					$site_address = $ip_address;
				}
				else
				{
					$site_address = $domain;
				}

				if($port_number == 443)
				{
					$ssl = "s";
				}
				else
				{
					$ssl = NULL;
				}

				// Start with the first hash and work our way up
				$poll_peer = filter_sql(file_get_contents("http$ssl://$site_address:$port_number/$subfolder/transclerk.php?action=block_hash&block_number=$hash_number", FALSE, $context, NULL, 65));

				if($my_hash === $poll_peer)
				{
					$hash_agree++;
				}
				else if($my_hash !== $poll_peer && empty($poll_peer) == FALSE)
				{
					$hash_disagree++;

					$hash_disagree_peers["ip_address$hash_disagree"] = $ip_address;
					$hash_disagree_peers["domain$hash_disagree"] = $domain;
					$hash_disagree_peers["subfolder$hash_disagree"] = $subfolder;
					$hash_disagree_peers["port_number$hash_disagree"] = $port_number;					
				}

			} // End For Loop
//***********************************************************************************
			// Compare peers that agree and disagree
			if($hash_disagree > $hash_agree)
			{
				//More than 50% of the active peers disagree on the hash value
				//so poll the transaction data from one them randomly

				// Clear out transaction block to allow the new one to be
				// inserted in place
				$sql = "DELETE FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2";

				if(mysql_query($sql) == FALSE)
				{
					//Something didn't work
				}
				else
				{
					$peer_number = rand(1,$hash_disagree);// Random peer from array
					$ip_address = $hash_disagree_peers["ip_address$peer_number"];
					$domain = $hash_disagree_peers["domain$peer_number"];
					$subfolder = $hash_disagree_peers["subfolder$peer_number"];
					$port_number = $hash_disagree_peers["port_number$peer_number"];
					$block_number = $hash_number;

					if(empty($domain) == TRUE)
					{
						$site_address = $ip_address;
					}
					else
					{
						$site_address = $domain;
					}

					if($port_number == 443)
					{
						$ssl = "s";
					}
					else
					{
						$ssl = NULL;
					}

					$poll_peer = filter_sql(file_get_contents("http$ssl://$site_address:$port_number/$subfolder/transclerk.php?action=transaction_data&block_number=$block_number", FALSE, $context, NULL, 200000));

					$tc = 1;

					while(empty($poll_peer) == FALSE)
					{
						$transaction_timestamp = find_string("-----timestamp$tc=", "-----public_key_from$tc", $poll_peer);
						$transaction_public_key_from = find_string("-----public_key_from$tc=", "-----public_key_to$tc", $poll_peer);
						$transaction_public_key_to = find_string("-----public_key_to$tc=", "-----crypt1data$tc", $poll_peer);
						$transaction_crypt1 = find_string("-----crypt1data$tc=", "-----crypt2data$tc", $poll_peer);
						$transaction_crypt2 = find_string("-----crypt2data$tc=", "-----crypt3data$tc", $poll_peer);
						$transaction_crypt3 = find_string("-----crypt3data$tc=", "-----hash$tc", $poll_peer);
						$transaction_hash = find_string("-----hash$tc=", "-----attribute$tc", $poll_peer);
						$transaction_attribute = find_string("-----attribute$tc=", "-----end$tc", $poll_peer);

						if(empty($transaction_public_key_from) == TRUE && empty($transaction_public_key_to) == TRUE)
						{
							// No more data, break while loop
							break;
						}

						$transaction_public_key_from = base64_decode($transaction_public_key_from);
						$transaction_public_key_to = base64_decode($transaction_public_key_to);				

						$found_duplicate = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` = '$transaction_timestamp' AND `public_key_from` = '$transaction_public_key_from' AND `hash` = '$transaction_hash' LIMIT 1"),0,0);

						// Check for valid attribute
						if($transaction_attribute == "G" || $transaction_attribute == "T" || $transaction_attribute == "H")
						{
							if(empty($found_duplicate) == TRUE)
							{
								$sql = "INSERT INTO `transaction_history` (`timestamp`,`public_key_from`,`public_key_to`,`crypt_data1`,`crypt_data2`,`crypt_data3`, `hash`, `attribute`)
								VALUES ('$transaction_timestamp', '$transaction_public_key_from', '$transaction_public_key_to', '$transaction_crypt1', '$transaction_crypt2' , '$transaction_crypt3', '$transaction_hash' , '$transaction_attribute')";

								if(mysql_query($sql) == TRUE)
								{
									// Flag for a re-check afterwards
									$double_check_block = TRUE;
								}
							}
						}

						$tc++;

					} // End while loop

				}//End Database clear block check

				// Double check the new hash against the last block transanstion(s) in case of tampering
				$time3 = transaction_cycle(0 - $current_generation_block + 1 + $hash_number);
				$time4 = transaction_cycle(0 - $current_generation_block + 2 + $hash_number);
				$double_check_hash = mysql_result(mysql_query("SELECT * FROM `transaction_history` WHERE `timestamp` >= $time3 AND `timestamp` < $time4 AND `attribute` = 'H' LIMIT 1"),0,"hash");

				// Build Hash from previous transaction block data
				$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";
				$sql_result = mysql_query($sql);
				$sql_num_results = mysql_num_rows($sql_result);
				$build_hash = 0;

				for ($i = 0; $i < $sql_num_results; $i++)
				{
					$sql_row = mysql_fetch_array($sql_result);
					$build_hash .= $sql_row["hash"];
				}

				// Transaction(s) hash
				$build_hash = hash('sha256', $build_hash);

				if($double_check_hash == $build_hash)
				{
					// Hash matches up to previous transaction data
				}
				else
				{
					// Hash is invalid, something is wrong, trigger another double check
					$double_check_block = TRUE;
				}

			} // End Hash agree/disagree
			else
			{
				// Majority peers agree +1 to sync blocks
				$sync_block++;
			}

			if($double_check_block == TRUE && $double_check_counter < 2)
			{
				//Reset to run loop again
				$hash_number--;
				$h--;
				$double_check_counter++;
			}
			else if($double_check_block == TRUE && $double_check_counter >= 2)
			{
				// There is too much conflict between the peers
				$double_check_counter = 0;

				// Wipe this block and hope that a future check
				// will have the peer conflict resolved
				$sql = "DELETE FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2";
				mysql_query($sql);

				write_log("Too Much Peer Conflict for Block #$block_number. This block will remain empty until repaired.", "TC");				
			}
			else
			{
				//Reset failsafe counter
				$double_check_counter = 0;
			}

			$hash_number++;

		} // End for Loop - Hash check cycling
//***********************************************************************************
		if($sync_block == $hash_check_counter)
		{
			if($error_check_active == FALSE)
			{
				write_log("Automatic History Check Complete. No Errors Found with Block #" . ($hash_number - ($hash_check_counter - 1)) . " to Block #" . $hash_number, "TC");
			}
			
			// The number of block checks equals the number in sync
			// so store the last block number in the database so that
			// the server will know where to start from on the next cycle
			if($foundation_block_check == 1)
			{
				$sql = "UPDATE `options` SET `field_data` = '$hash_number' WHERE `options`.`field_name` = 'foundation_block_check_start' LIMIT 1";
				mysql_query($sql);
			}
			else
			{
				$sql = "UPDATE `options` SET `field_data` = '$hash_number' WHERE `options`.`field_name` = 'block_check_start' LIMIT 1";
				mysql_query($sql);
			}
		}

		if($error_check_active == TRUE 
			&& $foundation_block_check != 1 
			&& $sync_block == $hash_check_counter) // Reset transaction history hash after error check completes
		{
			$total = mysql_query("SELECT COUNT(*) FROM `transaction_history`");
			$total = mysql_fetch_array($total); 
			$hash = $total[0];

			$previous_foundation_block = foundation_cycle(-1, TRUE);
			$current_foundation_cycle = foundation_cycle(0);
			$next_foundation_cycle = foundation_cycle(1);			
			$current_history_foundation = mysql_result(mysql_query("SELECT * FROM `transaction_foundation` WHERE `block` = $previous_foundation_block LIMIT 1"),0,"hash");

			$hash = $hash . $current_history_foundation;

			$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $current_foundation_cycle AND `timestamp` < $next_foundation_cycle AND `attribute` = 'H' ORDER BY `timestamp`";
			$sql_result = mysql_query($sql);
			$sql_num_results = mysql_num_rows($sql_result);

			for ($i = 0; $i < $sql_num_results; $i++)
			{
				$sql_row = mysql_fetch_array($sql_result);
				$hash .= $sql_row["hash"];	
			}	

			$history_hash = hash('md5', $hash);

			// Update database with new hash
			$sql = "UPDATE `options` SET `field_data` = '$history_hash' WHERE `field_name` = 'transaction_history_hash' LIMIT 1";
			mysql_query($sql);

			// Reset error block
			$sql = "UPDATE `options` SET `field_data` = '0' WHERE `field_name` = 'transaction_history_block_check' LIMIT 1";
			mysql_query($sql);

			write_log("Manual History Check Complete. No Errors Found with Block #$transaction_history_block_check to Block #" . ($transaction_history_block_check + $hash_check_counter - 1), "TC");
		}

//***********************************************************************************
	} // End Compare Tallies
	else
	{
		// Entire Transaction History in sync, reset block check start to 0
		$block_check_start = mysql_result(mysql_query("SELECT * FROM `options` WHERE `field_name` = 'block_check_start' LIMIT 1"),0,"field_data");		

		if($block_check_start > 0)
		{
			$sql = "UPDATE `options` SET `field_data` = '0' WHERE `options`.`field_name` = 'block_check_start' LIMIT 1";
			mysql_query($sql);

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

		if(rand(1,4) == 2)
		{
			// Poll a random block from a random peer for random accuracy :)
			// Within the range of the current foundation block to now
			$current_foundation_block = foundation_cycle(0, TRUE) * 500;
			$random_block = rand($current_foundation_block, transaction_cycle(-1, TRUE));

			$sql = "SELECT * FROM `active_peer_list` ORDER BY RAND()";

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

			if($sql_num_results > 0)
			{
				$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"];

				if(empty($domain) == TRUE)
				{
					$site_address = $ip_address;
				}
				else
				{
					$site_address = $domain;
				}

				if($port_number == 443)
				{
					$ssl = "s";
				}
				else
				{
					$ssl = NULL;
				}

				// Start with this hash
				$poll_peer = filter_sql(file_get_contents("http$ssl://$site_address:$port_number/$subfolder/transclerk.php?action=block_hash&block_number=$random_block", FALSE, $context, NULL, 65));

				if(empty($poll_peer) == FALSE && strlen($poll_peer) > 32)
				{
					// Do a real hash compare
					$current_generation_block = transaction_cycle(0, TRUE);
					
					$time1 = transaction_cycle(0 - $current_generation_block + $random_block);
					$time2 = transaction_cycle(0 - $current_generation_block + 1 + $random_block);	

					$sql = "SELECT * FROM `transaction_history` WHERE `timestamp` >= $time1 AND `timestamp` < $time2 ORDER BY `timestamp`, `hash`";

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

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

					$random_hash_build = hash('sha256', $random_hash_build);

					if($poll_peer === $random_hash_build)
					{
						// All is well in the transaction history
					}
					else if($poll_peer !== $random_hash_build && empty($poll_peer) == FALSE)
					{
						// Something is wrong, transaction history has an error.
						// Schedule a check in case the peer has an error and not us.
						$sql = "UPDATE `options` SET `field_data` = '$random_block' WHERE `field_name` = 'transaction_history_block_check' LIMIT 1";
						mysql_query($sql);

						write_log("One of my Peers ($site_address) Reports that My Block #$random_block is Wrong. Will Double Check with other Peers before making any corrections.", "TC");
					}
				} // End empty poll check
			} // End if/then record count check
		} // Random chance check

	} // End else

//***********************************************************************************

	} // End if/then check for processing for more than 3 records

//***********************************************************************************	
} // End if/then time check

//***********************************************************************************
//***********************************************************************************
// Script finished, set status to 0
$sql = "UPDATE `main_loop_status` SET `field_data` = '0' WHERE `main_loop_status`.`field_name` = 'transclerk_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` = 'transclerk_last_heartbeat' LIMIT 1";
mysql_query($sql);

?>
Back to Top