Обновить artemsbc.php

This commit is contained in:
artemuhi 2024-03-08 01:38:46 +03:00
parent 0e751c1e01
commit f575cc0737

View File

@ -16,7 +16,7 @@ function artembc_addtransaction($filename, &$transaction, $data) {
$temp=count($transaction); $temp=count($transaction);
$transaction[]=[ $transaction[]=[
"id"=>count($transaction), "id"=>count($transaction),
"idblock"=>count($bc)+1, "idblock"=>count($bc),
"time"=>time(), "time"=>time(),
"data"=>$data "data"=>$data
]; ];
@ -45,43 +45,46 @@ function artembc_initblock($filename, &$transaction, $data=[]) {
"time"=>time(), "time"=>time(),
"data"=>$data, "data"=>$data,
"transaction"=>$transaction, "transaction"=>$transaction,
"prevhash"=>hash("sha256", json_encode($bc[$temp-1])) "prevhash"=>$bc[$temp-1]["hash"]
]; ];
}; };
$bc[$temp]["hash"]=hash("sha256", json_encode($bc[$temp]));
artembc_save($filename, $bc); artembc_save($filename, $bc);
return $correct; return $correct;
} }
function artembc_checkbc() { function artembc_checkbc($filename) {
$bc=artembc_load($filename); $bc=artembc_load($filename);
$temp=count($bc); $temp=count($bc);
for ($i = 0, $size = count($bc), $correct2=true; $i < $size; ++$i) { for ($i = 0, $size = count($bc), $correct2=true; $i < $size; ++$i) {
$transaction=$bc[$i]; $transaction=$bc[$i];
for($i = 0, $size = count($transaction), $correct=true; $i < $size; ++$i) { for($i1 = 0, $size = count($transaction), $correct=true; $i1 < $size; ++$i1) {
$block=$transaction[$i]; if (count($transaction["transaction"])>0){
$hash=hash("sha256", json_encode([ $block=$transaction["transaction"][$i1];
"id"=>$block["id"], $hash=hash("sha256", json_encode([
"idblock"=>$block["idblock"], "id"=>$block["id"],
"time"=>$block["time"], "idblock"=>$block["idblock"],
"data"=>$block["data"] "time"=>$block["time"],
])); "data"=>$block["data"]
if ($block["idblock"] != $temp and $hash != $block["hash"]) { ]));
$correct=false; if ($block["idblock"] != $temp-1 or $hash != $block["hash"]) {
break; $correct=false;
break;
};
}; };
}; };
$hash=hash("sha256", json_encode([ $hash=hash("sha256", json_encode([
"id"=>$block["id"], "id"=>$transaction["id"],
"time"=>$block["time"], "time"=>$transaction["time"],
"data"=>$block["data"], "data"=>$transaction["data"],
"transaction"=>$block["transaction"], "transaction"=>$transaction["transaction"],
"prevhash"=>$block["prevhash"] "prevhash"=>$transaction["prevhash"]
])); ]));
if ($hash != $transaction["hash"] or ($transaction["prevhash"] != $bc[$temp-1]["hash"] and $transaction["id"] == 0)) { if (/*$hash != $transaction["hash"] or */($transaction["prevhash"] != $bc[$temp-1]["hash"] and $i != 0)) {
$correct2 = false; $correct2 = [false, $i];
break; break;
}; };
}; };
return $correct and $correct2; return [$correct, $correct2];
} }
function artembc_getblock($filename, $id) { function artembc_getblock($filename, $id) {
$temp=artembc_load($filename); $temp=artembc_load($filename);