forked from samweru-zz/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
187 lines (120 loc) · 4.35 KB
/
index.php
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
use Blockly\{Block, Chain, Trx, Data, PoW};
use Strukt\Http\Request;
use Zend\Http\Client;
use Strukt\Env;
$loader = require "vendor/autoload.php";
$loader->add('Blockly', "src/");
Env::set("root_dir", getcwd());
Env::set("rel_static_dir", "/public/static");
Env::set("is_dev", true);
$difficulty = 3; //three preceding zeros on hash
$cache = new \Doctrine\Common\Cache\ApcuCache();
$chain = new Chain();
if($cache->contains("chain")){
$chainArr = $cache->fetch("chain");
$chain = new Chain($chainArr);
}
$app = new Strukt\Router\QuickStart();
$app->get("/", function(){
return "Blockly chain.";
});
$app->get("/register/nodes", function(Request $req) use ($cache){
$nodesTmp = [];
$body = json_decode(str_replace("'", '"', trim((string)$req->getContent())), 1);
if(is_array($body))
if(!empty($body))
$nodesTmp = $body["nodes"];
if(empty($nodesTmp))
return "Found no nodes in request!";
$nodes = [];
if($cache->contains("nodes"))
$nodes = $cache->fetch("nodes");
if(!empty($nodes))
$nodes = array_diff($nodes, $nodesTmp);
$nodes = array_merge($nodesTmp, $nodes);
$cache->save("nodes", $nodes, 21600);
return "Nodes successfully saved";
});
$app->get("/nodes", function() use ($cache){
if(!$cache->contains("nodes"))
return "There are no nodes!";
return json_encode($cache->fetch("nodes"));
});
$app->post("/add/trx", function(Request $req) use ($cache, $chain, $difficulty){
$sender = $req->get("sender");
$recipient = $req->get("recipient");
$amount = $req->get("amount");
$data = new Data();
$data->addTrx(new Trx(sha1($sender), sha1($recipient), $amount));
$block = new Block($data, $chain->getLastBlock(), $difficulty);
$chain->addBlock($block);
$cache->save("chain", $chain->getArr(), 21600);
return "Transaction saved successfully!";
});
$app->get("/mine", function() use ($cache, $chain){
$chain->mineBlocks();
$cache->save("chain", $chain->getArr(), 21600);
return "Mining successful.";
});
$app->get("/consensus", function(Request $req) use ($cache, $chain){
$http_post = $req->server->get('HTTP_HOST');
$message = "Our chain rules them all!";
$nodes = $cache->fetch("nodes");
if(empty($nodes))
return "There no nodes available!";
$ourBlocks = $chain->getBlocks();
foreach($nodes as $node){
if($node == $http_post)
continue;
$client = new Client();
$client->setUri(sprintf('http://%s/chain', $node));
$client->setMethod('GET');
$client->setOptions(array(
'maxredirects' => 0,
'timeout' => 1200
));
$response = $client->send();
$body = $response->getBody();
$notOurBlocksArr = json_decode($body, 1);
$notOurChain = new Chain($notOurBlocksArr);
$notOurBlocks = $notOurChain->getBlocks();
$ourBlocks = $chain->getBlocks();
$isProofOfWorkValid = true;
$isOtherChainGreater = false;
//check length of other chain compare to ours
if(count($notOurBlocks) > count($ourBlocks)){
$isOtherChainGreater = true;
$prevBlock = array_shift($notOurBlocks);
foreach($notOurBlocks as $block){
if(!PoW::validate($block, $prevBlock->getNonce())){
$isProofOfWorkValid = false;
}
}
}
$isOtherChainTrxValid = true;
// validate other chains transactions
foreach($notOurBlocks as $block){
$merkleTree = Chain::newMerkleTree();
$data = $block->getData();
$arrData = $data->getArr();
foreach($arrData as $trx)
$tree = $merkleTree->add(new Merkle\Leaf($trx));
$mTree = $data->getMerkleTree();
if(!is_null($mTree))
if(key($mTree)!=key($tree)){
$isOtherChainTrxValid = false;
break;
}
}
if($isOtherChainTrxValid && $isProofOfWorkValid && $isOtherChainGreater){
$cache->save("chain", $notOurChain->getArr());
$message = "Our chain has been replaced!";
}
}
return $message;
});
$app->get("/chain", function() use ($chain){
return (string)$chain;
});
$app->run();