forked from phpcoinn/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeerRequest.php
More file actions
836 lines (731 loc) · 26.3 KB
/
PeerRequest.php
File metadata and controls
836 lines (731 loc) · 26.3 KB
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
<?php
class PeerRequest
{
public static $ip;
public static $data;
public static $requestId;
public static $info;
public static $peer;
static function processRequest() {
global $_config;
_logp("PeerRequest: received peer request");
if(isset($_config) && $_config['offline']==true) {
_logf("Peer is set to offline");
api_err("Peer is set to offline");
}
if (!empty($_POST['data'])) {
$data = json_decode(trim($_POST['data']), true);
}
global $_config;
if (!isset($_POST['coin']) || $_POST['coin'] != COIN) {
_logf("Invalid coin request=".json_encode($_REQUEST)." server=".json_encode($_SERVER));
api_err("Invalid coin ".json_encode($_REQUEST), 3);
}
if(isset($_POST['network'])) {
if($_POST['network'] != NETWORK) {
_logf("Invalid network");
api_err("Invalid network ".$_POST['network']);
}
}
if(isset($_POST['chain_id']) && strlen($_POST['chain_id'])>0) {
if($_POST['chain_id'] != CHAIN_ID) {
_logf("Invalid chain ID");
api_err("Invalid chain ID ".$_POST['chain_id']);
}
}
$ip = Nodeutil::getRemoteAddr();
if(!isset($_POST['version']) || version_compare($_POST['version'], MIN_VERSION) < 0) {
$peer = Peer::findByIp($ip);
if($peer) {
Peer::blacklist($peer['id'], "Invalid version ".$_POST['version']);
}
_logp("request=".json_encode($_REQUEST));
_logf("Invalid version ".$_POST['version']);
api_err("Invalid version ".$_POST['version']);
}
$requestId = $_POST['requestId'] ?? null;
$q = $_GET['q'] ?? null;
$chain_id = $_POST['chain_id'] ?? null;
_log("Peer request from IP = $ip requestId=$requestId q=".$q." chainId=".$chain_id ,4);
_logp("q=".$q);
$info = $_POST['info'] ?? null;
$ip = Peer::validateIp($ip);
_log("Filtered IP = $ip",4);
if(($ip === false || strlen($ip)==0)) {
_logf("Invalid peer IP address");
api_err("Invalid peer IP address");
}
$ip = $ip . (empty(COIN_PORT) ? "" : ":" . COIN_PORT);
_logp("ip=$ip");
if(!Blacklist::checkIp($ip)) {
_logf("blocked-ip");
api_err("blocked-ip");
}
$peer = Peer::getByIp($ip);
if($peer) {
if($peer['blacklisted'] > time() && strpos($peer['blacklist_reason'],"Invalid hostname") === 0) {
$hostname=$info['hostname'];
_log("Peer request from blacklisted peer ip=$ip hostname=$hostname reason=".$peer['blacklist_reason']." info=".json_encode($info). "REMOTE_ADDR=".$_SERVER['REMOTE_ADDR'].
" HTTP_X_FORWARDED_FOR=".@$_SERVER['HTTP_X_FORWARDED_FOR']);
_logf("blacklisted-peer");
api_err("blacklisted-peer SERVER=".json_encode($_SERVER). " peer=".json_encode($peer));
}
} else {
_log("Peer with $ip not in list", 2);
}
if(!empty($info)) {
$hostname=$info['hostname'];
if(!empty($hostname)) {
if(!empty($peer['hostname']) && $peer['hostname'] != $hostname) {
Peer::blacklist($peer['id'], "Invalid hostname $hostname");
_logf("blocked-invalid-hostname");
api_err("blocked-invalid-hostname");
}
_log("PRC: ip=$ip hostname=$hostname mn=".@$info['masternode']." found_peer=". ($peer ? $peer['hostname'] : ""),5);
}
_logp("update peer info");
Peer::updatePeerInfo($ip, $info);
if(!empty($peer['id'])) {
$peer['height'] = $info['height'];
if ($peer['blacklisted'] < time() && $peer['fails'] > 0) {
_logp("clear blacklist");
Peer::clearFails($peer['id']);
Peer::clearStuck($peer['id']);
}
_log("check peer height hostname=$hostname height=" . $peer['height'], 5);
$current_height = Block::getHeight();
if (isset($peer['height']) && ($current_height - $peer['height'] > 100)) {
Peer::blacklist($peer['id'], "100 blocks behind");
}
if ($peer['blacklisted'] > time() && $peer['blacklist_reason'] == "100 blocks behind") {
if ($current_height - $peer['height'] < 10) {
_log("PBH: Check peer if is still blocks behind current_height = $current_height peer_height=" . $peer['height'] . " blacklisted=" . ($peer['blacklisted'] > time()) .
" reason=" . $peer['blacklist_reason'] . " - remove form blacklist", 5);
Peer::clearBlacklist($peer['id']);
}
}
}
}
_logf("finish process");
self::$ip=$ip;
self::$data=$data;
self::$requestId=$requestId;
self::$info = $info;
self::$peer = $peer;
}
static function peer() {
$data =self::$data;
$ip =self::$ip;
global $_config;
_log("Received peer requst: ". json_encode($data),3);
if(!Peer::validate($data['hostname'])) {
api_err("invalid-hostname");
}
// sanitize the hostname
$hostname = filter_var($data['hostname'], FILTER_SANITIZE_URL);
$hostname = san_host($hostname);
_log("Received peer request from $hostname",3);
if($hostname === $_config['hostname']) {
api_err("self-peer");
}
// if it's already peered, only repeer on request
$res = Peer::getSingle($hostname, $ip);
if ($res == 1) {
_log("$hostname is already in peer db",3);
if (isset($data['repeer']) && $data['repeer'] == 1) {
$res = peer_post($hostname."/peer.php?q=peer", ["hostname" => $_config['hostname']], 30, $err);
if ($res !== false) {
api_echo("re-peer-ok");
} else {
api_err("re-peer failed - $err");
}
}
api_echo("peer-ok-already");
} else {
_log("$hostname is new peer",2);
}
_log("Inserting $hostname in peer db",3);
$res = Peer::insert($ip, $hostname);
_log("Inserted $hostname = $res",3);
if(isset($_REQUEST['info'])) {
Peer::updatePeerInfo($ip, $_REQUEST['info']);
}
// re-peer to make sure the peer is valid
if (isset($data['repeer']) && $data['repeer'] == 1) {
_log("Repeer to $hostname",3);
$res = peer_post($hostname . "/peer.php?q=peer", ["hostname" => $_config['hostname']], 30, $err);
_log("peer response " . print_r($res,1),4);
if ($res !== false) {
_log("Repeer OK",3);
api_echo("re-peer-ok");
} else {
_log("Repeer FAILED - DELETING",2);
if($ip) {
Peer::deleteByIp($ip);
api_err("re-peer failed - $err");
} else {
api_err("invalid peer ip");
}
}
} else {
api_echo("peer-ok");
}
}
static function ping() {
// confirm peer is active
api_echo("pong");
}
static function submitTransaction() {
$data = self::$data;
global $db, $_config;
_log("receive a new transaction from a peer from ".self::$ip,2);
_log("data: ".json_encode($data),3);
$tx = Transaction::getFromArray($data);
$tx->mempool = true;
// receive a new transaction from a peer
// $current = $block->current();
if(Config::getVal("blockchain_invalid") == 1) {
api_err("invalid-peer");
}
// no transactions accepted if the sync is running
if (Config::isSync()) {
api_err("sync");
}
$hash = $tx->id;
// make sure it's not already in mempool
$res = Mempool::existsTx($hash);
if ($res != 0) {
api_err("The transaction is already in mempool");
}
$res = Mempool::checkMempoolBalance($tx, $error);
if(!$res) {
api_err("Error processing new transaction in mempool: $error");
}
// validate transaction data
if (!$tx->check(null, false, $txerr)) {
api_err("Invalid transaction: $txerr");
}
// make sure the peer is not flooding us with transactions
$res = Mempool::getSourceTxCount($tx->src);
if ($res > 25) {
api_err("Too many transactions from this address in mempool. Please rebroadcast later.");
}
$res = Mempool::getPeerTxCount(self::$ip);
if ($res > $_config['peer_max_mempool']) {
api_err("Too many transactions broadcasted from this peer");
}
// make sure the transaction is not already on the blockchain
$res = $db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $hash]);
if ($res != 0) {
api_err("The transaction is already in a block");
}
// make sure the sender has enough balance
$balance = $db->single("SELECT balance FROM accounts WHERE id=:id", [":id" => $tx->src]);
if ($balance < $tx->val + $tx->fee) {
api_err("Not enough funds");
}
// make sure the sender has enough pending balance
$memspent = Mempool::getSourceMempoolBalance($tx->src);
if ($balance - $memspent < $tx->val + $tx->fee) {
api_err("Not enough funds (mempool)");
}
$max_txs = Block::max_transactions();
$mempool_size = Transaction::getMempoolCount();
if($mempool_size + 1 > $max_txs) {
$error = "Mempool full";
_log("Not added transaction to mempool because is full: max_txs=$max_txs mempool_size=$mempool_size");
api_err($error);
}
if(FEATURE_SMART_CONTRACTS) {
if ($tx->type == TX_TYPE_SC_EXEC || $tx->type == TX_TYPE_SC_SEND) {
$schash = SmartContract::processSmartContractTx($tx, Block::getHeight() + 1, $error);
if ($schash === false) {
throw new Exception("Error processing smart contract transaction: Invalid transaction schash: " . $error);
}
}
}
// add to mempool
$tx->add_mempool(self::$ip);
Propagate::transactionToAll($tx->id);
api_echo("transaction-ok");
}
static function submitBlock() {
self::submitBlockNew();
return;
$ip = self::$ip;
$data = self::$data;
$current = Block::current();
$microsync = isset($data['microsync']);
_log("submitBlock: ".($microsync ? "[microsync] " : "")."Receive new block from a peer $ip : id=".$data['id']." height=".$data['height']." current=".$current['height'], 5);
$data['id'] = san($data['id']);
if ($current['id'] == $data['id'] && $current['height']==$data['height']) {
_log("submitBlock: We have this block - OK",3);
api_echo("block-ok");
}
if ($data['date'] > time() + 30) {
_log("submitBlock: block in the future");
api_err("block in the future");
}
//_log("submitBlock: current_height=".$current['height']." data_height=".$data['height']." current_id=".$current['id']." data_id=".$data['id']);
if ($current['height'] == $data['height'] && $current['id'] != $data['id']) {
$accept_new = false;
_log("submitBlock:: DIFFERENT FORKS SAME HEIGHT", 3);
_log("submitBlock: id= ".json_encode($data),3);
$ourblock = Block::export("", $current['height']);
_log("submitBlock:our ".json_encode($ourblock),3);
//compare two blocks
_log("submitBlock: compare blocks data -> our: id=".$data['id']." -> ".$ourblock['id']." elapsed=".$data['elapsed']." -> ".$ourblock['elapsed']." date=".$data['date']." -> ".$ourblock['date'], 5);
if($data['elapsed']==$ourblock['elapsed']) {
if($data['date']==$ourblock['date']) {
$accept_new = strcmp($data['id'], $ourblock['id']);
} else {
$accept_new = $data['date'] < $ourblock['date'];
}
} else {
$accept_new = $data['elapsed'] < $ourblock['elapsed'];
}
_log("submitBlock: accept_new=$accept_new");
// //wins block with lowest elapsed time - highest difficulty
// $difficulty1 = $current['difficulty'];
// $difficulty2 = $data['difficulty'];
// //_log("DFSH: compare difficulty: difficulty1=$difficulty1 difficulty2=$difficulty2");
// if($difficulty1 > $difficulty2) {
// $accept_new = true;
// } else if ($difficulty1 == $difficulty2) {
// $date1 = $current['date'];
// $date2 = $data['date'];
// //_log("DFSH: compare date: date1=$date1 date2=$date2");
// if($date1 > $date2) {
// $accept_new = true;
// } else if ($date1 == $date2) {
// $miner1=$current['miner'];
// $miner2=$data['miner'];
// $generator1=$current['generator'];
// $generator2=$data['generator'];
// //_log("DFSH: compare miners: miner1=$miner1 miner2=$miner2");
// //_log("DFSH: compare generator: generator1=$generator1 generator2=$generator2");
// $id1=$current['id'];
// $id2=$data['id'];
// //_log("DFSH: compare ids: id1=$id1 id2=$id2");
// if(strcmp($id1, $id2)) {
// $accept_new = true;
// }
// }
// }
//_log("DFSH: Accept new = ".$accept_new);
if ($accept_new) {
// if the new block is accepted, run a microsync to sync it
_log('submitBlock: ['.$ip."] Starting microsync - $data[height]",1);
$ip=escapeshellarg($ip);
$dir = ROOT."/cli";
system( "php $dir/microsync.php '$ip' > /dev/null 2>&1 &");
api_echo("microsync");
} else {
_log('submitBlock: ['.$ip."] suggesting reverse-microsync - $data[height]",1);
api_echo("reverse-microsync"); // if it's not, suggest to the peer to get the block from us
}
}
// if it's not the next block
if ($current['height'] != $data['height'] - 1) {
_log("Not next block - exit", 5);
api_err("not-next-block");
// if($microsync) {
// _logf("already in microsync", 5);
// api_err("already-microsync");
// }
// _log("if it's not the next block",1);
// if the height of the block submitted is lower than our current height, send them our current block
if ($data['height'] < $current['height']) {
//_log("DFSH: Our height is higher");
$pr = Peer::getByIp($ip);
if (!$pr) {
//_log("DFSH: No peer by IP $ip");
api_err("block-too-old");
}
Propagate::blockToPeer($pr['hostname'], $pr['ip'], "current");
_log('submitBlock: ['.$ip."] block too old, sending our current block - $current[height]",3);
//_log("DFSH: Send our block to peer ".$pr['hostname']);
api_err("block-too-old");
}
// if the block difference is bigger than 150, nothing should be done. They should sync
if ($data['height'] - $current['height'] > 150) {
_log('submitBlock: ['.$ip."] block-out-of-sync - $data[height]",2);
//_log("DFSH: block difference higher than 150");
api_err("block-out-of-sync");
}
// request them to send us a microsync with the latest blocks
$pr = Peer::getByIp($ip);
$hostname = $pr['hostname'];
_log('submitBlock:: ['.$hostname."] requesting microsync - $current[height] - $data[height]",2);
api_echo(["request" => "microsync", "height" => $current['height'], "block" => $current['id']]);
}
// check block data
$block = Block::getFromArray($data);
if (!$block->check()) {
_log('submitBlock: ['.$ip."] invalid block - $data[height]",1);
api_err("invalid-block");
}
$block->prevBlockId = $current['id'];
$current = Block::current();
//_log("DFSH: check add BLOCK ".$block->height. " current=".$current['height']);
if($block->height == $current['height']) {
_log("submitBlock: block checked ok", 5);
api_echo("block-ok");
}
$lock_file = ROOT . "/tmp/lock-block-".$block->height;
_log("Check lock file $lock_file", 5);
if (!mkdir($lock_file, 0700)) {
_log("submitBlock: Lock file exists $lock_file", 3);
api_echo("sync");
}
//_log("DFSH: ADD BLOCK ".$block->height);
$res = $block->add($error);
_log("Remove lock file $lock_file", 5);
@rmdir($lock_file);
if (Config::isSync()) {
api_err("submitBlock: sync");
}
if (!$res) {
_log('submitBlock: ['.$ip."] invalid block data - $data[height] Error:$error",1);
api_err("invalid-block-data $error");
}
$last_block = Block::export("", $data['height']);
$bl = Block::getFromArray($last_block);
$res = $bl->verifyBlock($err);
if (!$res) {
_log("Can not verify added block err=$err",1);
api_err("invalid-block-data");
}
_log('submitBlock: ['.$ip."] block ok, repropagating - $data[height]",1);
Propagate::blockToAll($data['id']);
api_echo("block-ok");
}
static function currentBlock() {
$current = Block::current();
api_echo(["block"=>$current, "info"=>Peer::getInfo()]);
}
static function getBlock() {
$data = self::$data;
$height = intval($data['height']);
$export = Block::export("", $height);
if (!$export) {
api_err("invalid-block");
}
api_echo($export);
}
static function getBlocks() {
$data = self::$data;
global $db;
// returns X block starting at height, used in syncing
$height = intval($data['height']);
$r = $db->run(
"SELECT id,height FROM blocks WHERE height>=:height ORDER by height ASC LIMIT 100",
[":height" => $height]
);
foreach ($r as $x) {
$blocks[$x['height']] = Block::export($x['id']);
}
api_echo($blocks);
}
static function getPeerBlocks() {
$data = self::$data;
global $db;
// returns X block starting at height, used in syncing
$height = intval($data['height']);
$count = intval($data['count']);
if(empty($count)) {
$count = 100;
}
if(empty($height)) {
$r = $db->run(
"SELECT id,height FROM blocks ORDER by height DESC LIMIT $count"
);
} else {
$r = $db->run(
"SELECT id,height FROM blocks WHERE height < :height ORDER by height DESC LIMIT $count",
[":height" => $height]
);
}
foreach ($r as $x) {
$blocks[$x['height']] = Block::export($x['id']);
}
$current = Block::current();
api_echo(["block"=>$current,"blocks"=>$blocks, "info"=>Peer::getInfo()]);
}
static function getPeers() {
// _log("Executing getPeers");
$peers = Peer::getPeers();
// _log("Response".print_r($peers,1));
api_echo($peers);
}
static function getAppsHash() {
$appsHashFile = Nodeutil::getAppsHashFile();
$appsHash = file_get_contents($appsHashFile);
api_echo($appsHash);
}
static function updateDapps() {
Dapps::updateDapps(self::$data, self::$ip);
}
static function checkDapps() {
Dapps::checkDapps(self::$data['dapps_id'], self::$ip);
}
static function updateMasternode() {
if(Config::isSync()) {
api_err("sync");
}
$masternode = self::$data;
$ip = self::$ip;
Masternode::updateMasternode($masternode, $ip, $error);
if($error) {
api_err($error);
} else {
api_echo("OK");
}
}
static function getMasternode() {
$public_key = self::$data;
_log("Masternode: getMasternode $public_key");
$masternode = Masternode::get($public_key);
api_echo($masternode);
}
static function propagateMsg7()
{
global $db, $_config;
_log("PM: received propagateMsg7 from ".self::$peer['hostname']);
$envelope = self::$data;
$envelope = json_decode(base64_decode($envelope),true);
$payload = $envelope['payload'];
$payload = json_decode(base64_decode($payload),true);
$public_key = $payload['public_key'];
if($public_key == $_config['masternode_public_key']) {
api_echo("PM: Origin node received message",0);
}
$id = $payload['id'];
$cfg="msg-".$id;
$sql="select * from config c where cfg = ?";
$row = $db->run($sql, [$cfg], false);
if($row) {
api_echo("PM: Message already processed",0);
}
$signature = $envelope['signature'];
$res = ec_verify($envelope['payload'], $signature, $public_key);
if(!$res) {
api_err("PM: Signature failed", 0);
}
$sql="insert into config (cfg, val) values (?, ?)";
$res = $db->run($sql, [$cfg, time()], false);
if($res === false) {
api_err("PM: Error saving message", 0);
}
$message = $payload['message'];
_log("PM: PROCESS MESSAGE $message");
$msg=json_decode($message,true);
$sender=$msg['sender'];
$response = ["msg"=>$msg, "received"=>time()];
$res = peer_post($sender.'/peer.php?q=processedMessage',$response);
_log("PM: send event to master res=".$res);
Propagate::message($envelope);
api_echo(true);
}
static function processedMessage() {
$data = self::$data;
$received = $data['received'];
$sent = $data['msg']['time'];
$elapsed = $received - $sent;
$returned = time() - $received;
_log("PM: processedMessage on peer ".self::$peer['hostname']." data: ".json_encode($data)." received: "
.$received." sent: ".$sent." elapsed: ".$elapsed." returned: ".$returned);
api_echo(true);
}
static function logPropagate() {
$data = self::$data;
$data = base64_decode($data);
$data = json_decode($data, true);
self::emitToScoket("logPropagate", $data);
}
static function peerTest() {
$t1 = self::$data;
_log("PP: received peerTest data=".json_encode(self::$data));
$url = self::$peer['hostname'] . "/peer.php?q=peerTest2";
$data['t1']=$t1;
$data['t2']=time();
$data['t2-t1']=$data['t2'] - $data['t1'];
sleep(5);
$res = peer_post($url, $data);
_log("PP: call back ".self::$peer['hostname']." res=".$res);
api_echo($res);
}
static function peerTest2() {
_log("PP: received peerTest2 request data=".json_encode(self::$data));
$data = self::$data;
$data['t3']=time();
$data['t3-t2']=$data['t3']-$data['t2'];
$data['t3-t1']=$data['t3']-$data['t1'];
sleep(5);
_log("PP: send response");
api_echo($data);
}
static function logSubmitBlock() {
$data = self::$data;
$data = base64_decode($data);
$data = json_decode($data, true);
self::emitToScoket("logSubmitBlock", $data);
}
static function emitToScoket($event, $data) {
global $_config;
$log = ["event"=>$event, "data"=>$data, "hostname" => $_config['hostname']];
$res = peer_post("http://node1.phpcoin.net:3001/emit", $log);
api_echo("OK");
}
static function submitBlockNew() {
$ip = self::$ip;
$data = self::$data;
global $_config;
if(Config::getVal("blockchain_invalid") == 1) {
api_err("invalid-peer");
}
$current = Block::current();
$diff = $current['height']-$data['height'];
$hostname="";
if(self::$peer) {
$hostname = @self::$peer['hostname'];
}
// receive a new block from a peer
_logp("submitBlock: Receive new block from a peer $ip hostname=$hostname : id=".$data['id']." height=".$data['height']." current=".$current['height']. " diff=".$diff, 5);
if($diff < 0) {
if($diff == -1) {
// api_echo("block-ok");
$peer_block = Block::getFromArray($data);
$res = $peer_block->check($err);
if(!$res) {
_logf("block check failed: $err", 5);
api_err("invalid-block");
}
_logp("received block is checked ok", 5);
$peer_block->prevBlockId = $current['id'];
$res = $peer_block->add($err);
if(!$res) {
_logf("Error adding block: $err", 5);
api_err("invalid-block");
}
_logp("added block - ok", 5);
Propagate::blockToAll($data['id']);
api_echo("block-ok");
} else {
_logf("we are on lower block", 5);
// if(self::$peer) {
// $dir = ROOT."/cli";
// $cmd = "php $dir/peersync.php ".self::$peer['hostname'];
// $check_cmd = "php $dir/peersync.php";
// _log("submitBlock: run peer sync with ".self::$peer['hostname']);
// Nodeutil::runSingleProcess($cmd, $check_cmd);
// }
api_echo("peer-sync");
}
} else if ($diff > 0) {
$block=Block::export("",$data['height']);
if(!$block) {
_logf("we do not have block at height ".$data['height']);
api_err("peer-error");
}
_logp("compare blocks our=".$block['id']." remote=".$data['id']);
if($block['id'] == $data['id']) {
_logf("blocks are same");
api_echo("block-ok");
}
_logp("submitBlock: BLOCKS ARE NOT SAME elapsed=".$block['elapsed'].",".$data['elapsed'], 5);
$res = NodeSync::compareBlocks($block, $data);
if($res>0) {
_logf("my block is winner");
if(self::$peer) {
Propagate::blockToPeer(self::$peer['hostname'], self::$peer['ip'], $block['id']);
}
api_err("block-not-ok");
} else if ($res<0) {
_logf("other block is winner");
api_err("block-ok");
} else {
_logf("blocks are actually same");
api_err("block-ok");
}
} else {
_logp("heights are equal", 5);
if($current['id']==$data['id']) {
_logf("blocks are same on same height");
api_echo("block-ok");
}
_logp("BLOCKS ARE NOT SAME ON SAME HEIGHT", 5);
$current = Block::export($current['id']);
$res = NodeSync::compareBlocks($current, $data);
if($res>0) {
_logf("my block is winner", 5);
if(self::$peer) {
Propagate::blockToPeer(self::$peer['hostname'], self::$peer['ip'], $current['id']);
}
api_err("block-not-ok");
} else if ($res<0) {
_logf("other block is winner", 5);
if(self::$peer) {
$dir = ROOT."/cli";
$cmd = "php $dir/deepcheck.php ".self::$peer['hostname']. " ".$data['height'];
$check_cmd = "php $dir/deepcheck.php";
_log("submitBlock: run deep check with ".self::$peer['hostname'], 5);
Nodeutil::runSingleProcess($cmd, $check_cmd);
}
api_err("block-not-ok");
} else {
_logf("blocks are actually same", 5);
api_err("block-ok");
}
}
}
static function deepCheck() {
$hostname = self::$peer['hostname'];
_log("Start run deepCheck",4);
$dir = ROOT."/cli";
$cmd = "php $dir/deepcheck.php $hostname";
$check_cmd = "php $dir/deepcheck.php";
Nodeutil::runSingleProcess($cmd, $check_cmd);
api_echo(["status"=>"started","hostname"=>$hostname]);
}
static function checkMyPeer() {
$peer = Peer::getByIp(self::$ip);
api_echo($peer);
}
static function getDbBlocks() {
global $db;
$data = self::$data;
$height = $data['height'];
$limit = $data['limit'];
$max_height = $height + $limit;
$maxheight = Block::getHeight();
$sql="select * from blocks b where b.height > ? and b.height <= ? order by b.height";
$blocks = $db->run($sql,[$height,$max_height], false);
$sql="select * from transactions t where t.height > ? and t.height <= ? order by t.height";
$txs = $db->run($sql,[$height,$max_height], false);
$sql="select * from smart_contracts where height > ? and height <= ? order by height";
$smart_contracts = $db->run($sql,[$height,$max_height], false);
$sql="select * from smart_contract_state where height > ? and height <= ? order by height";
$smart_contract_state = $db->run($sql,[$height,$max_height], false);
$accounts = [];
$masternodes = [];
if($max_height >= Block::getHeight()) {
$sql = "select * from accounts order by id";
$accounts = $db->run($sql, [], false);
$sql = "select * from masternode";
$masternodes = $db->run($sql, [], false);
}
$block = Block::get($height);
$res=[
"block"=>$block,
"maxheight"=>$maxheight,
"blocks"=>$blocks,
"transactions"=>$txs,
"accounts"=>$accounts,
"masternodes"=>$masternodes,
"smart_contracts"=>$smart_contracts,
"smart_contract_state"=>$smart_contract_state,
];
api_echo($res);
}
}