-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgetblocks.php
176 lines (140 loc) · 6.63 KB
/
getblocks.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
<?php
/**
* File: getblocks.php
* Name: lacksfish
* Date: 09/06/13
* Project: Primecoinchains
* Desc: This script will extract the index, hash, primechain type, prime origin, length and difficulty
* of every block within the networks blockchain.
* It has to be run, just like getinfo.php, via a cronjob every minute.
*/
require_once('lib/conf.php');
//Connect to MySQL database
$con = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PW, MYSQL_DB);
//If connection fails
if(!$con){
die("Connection to Database not established");
}
if($con){
//If primecoindPath points to primecoind.exe
if(file_exists($primecoindPath)){
//Check if table numinos_prime_data does not yet exists
if(mysqli_query($con, "SHOW TABLES LIKE 'numinos_prime_blockhashes';")->num_rows == NULL){
//Create it
$sql = "
CREATE TABLE IF NOT EXISTS numinos_prime_blockhashes
(
`index` INT,
`hash` TEXT,
`primechain` TEXT,
`primeorigin` TEXT,
`length` INT,
`difficulty` DOUBLE
);
";
$res = mysqli_query($con, $sql)
or die("Tabelle wurde nicht geschrieben: " . mysqli_error($con));
}
$sql="
SELECT `value` FROM `numinos_prime_data`
WHERE `info`='blocks';
";
//Retrieve block number of most current block
$primeBlockCount = mysqli_fetch_array(mysqli_query($con, $sql));
$formatedBlockArray = array();
//Get latest block processed (highest id)
$sql = "
SELECT `index` FROM `numinos_prime_blockhashes`
ORDER BY `index` DESC LIMIT 0, 1;
";
$maxDbBlockID = mysqli_fetch_array(mysqli_query($con, $sql));
//If there are no entries in the database
if(is_null($maxDbBlockID["index"])){
//Set $maxDbBlockID to zero
$maxDbBlockID["index"] = 0;
}else{//If there are already entries in the database
//Get next block data
$lastBlockhash = shell_exec($primecoindPath . " -datadir=" . $primecoindDataDir . " getblockhash " . $maxDbBlockID["index"]);
$lastBlock = shell_exec($primecoindPath . " -datadir=" . $primecoindDataDir . " getblock " . $lastBlockhash);
//Format block
$formatedBlockArray = getFormatedBlock($lastBlock);
}
//Process all them blocks
for($i = $maxDbBlockID["index"]; $i <= $primeBlockCount["value"]; $i++){
//If it is not the genesis block
if($i != 0){
//Get next block based on nextblockhash
$Block = shell_exec($primecoindPath . " -datadir=" . $primecoindDataDir . " getblock " . $formatedBlockArray["nextblockhash"]);
//Format block
$formatedBlockArray = getFormatedBlock($Block);
$sql = "
INSERT INTO `numinos_prime_blockhashes`
(
`index` , `hash` , `primechain` , `primeorigin` , `length` , `difficulty`
)
VALUES
(
'" . $formatedBlockArray["height"] . "' , '" . $formatedBlockArray["hash"] . "' , '" . $formatedBlockArray["primechain"] . "' , '" . $formatedBlockArray["primeorigin"] . "' , '" . getLength($formatedBlockArray["primechain"]) . "' , '" . $formatedBlockArray["difficulty"] . "'
);
";
}else{//If it is the genesis block
//Get genesis block data
$genesisBlockhash = shell_exec($primecoindPath . " -datadir=" . $primecoindDataDir . " getblockhash 0");
$genesisBlock = shell_exec($primecoindPath . " -datadir=" . $primecoindDataDir . " getblock " . $genesisBlockhash);
//Format genesis block data
$formatedBlockArray = getFormatedBlock($genesisBlock);
$sql = "
INSERT INTO `numinos_prime_blockhashes`
(
`index` , `hash` , `primechain` , `primeorigin` , `length` , `difficulty`
)
VALUES
(
'" . $formatedBlockArray["height"] . "' , '" . $formatedBlockArray["hash"] . "' , '" . $formatedBlockArray["primechain"] . "' , '" . $formatedBlockArray["primeorigin"] . "' , '" . getLength($formatedBlockArray["primechain"]) . "' , '" . $formatedBlockArray["difficulty"] . "'
);
";
}
//Execute query
$res = mysqli_query($con, $sql)
or die("Datensatz wurde nicht geschrieben: " . mysqli_error($con));
}
//Remove broken entrys
$sql="
DELETE FROM `numinos_prime_blockhashes`
WHERE `difficulty`=0;
";
//Execute query
$res = mysqli_query($con, $sql)
or die("Datensatz wurde nicht geschrieben: " . mysqli_error($con));
}//if
//Close MySQL Connection
mysqli_close($con);
}//if($con)
function getFormatedBlock($Block){
//I have to admit, i didn't know what JSON was by the time I programmed this.. Wish I did.
$formatedBlockArray = array();
//tx contains a ",", so the following explode would mess it up
//replacing tx's "," with a ";"
$Block = str_replace("\",\n \"", "\";\n \"", $Block);
//Split into separate information strings
$BlockArray = explode(",", $Block);
foreach($BlockArray as $BlockString){
//Take appart the "xxxxxxx : xxxxxxx" String at the colon
$BlockStringArray = explode(" : ", $BlockString);
//Remove '{', ' ', '"' and '}'
$needles = array("{", "}", "\"", " ", "[", "]");
$BlockStringArray = str_replace($needles, "", $BlockStringArray);
//Remove all tabs, newlines and carriage returns
$BlockStringArray = preg_replace("/\r|\n|\t/", "", $BlockStringArray);
//Add block data to $formatedBlockArray
$formatedBlockArray[$BlockStringArray[0]] = $BlockStringArray[1];
}
return $formatedBlockArray;
}
function getLength($primechaintype){
//Strip length in hex format from $primechaintype (1CC07 would be 7, TWN0a would be 10)
$chainlengthhex = substr($primechaintype, 3, 2);
//Convert hex to dec and return
return base_convert($chainlengthhex, 16, 10);
}
?>