From f575cc07375d17df5fc9aa37230766e713926196 Mon Sep 17 00:00:00 2001 From: artemuhi Date: Fri, 8 Mar 2024 01:38:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20artemsbc.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- artemsbc.php | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/artemsbc.php b/artemsbc.php index 1c49235..1390160 100644 --- a/artemsbc.php +++ b/artemsbc.php @@ -16,7 +16,7 @@ function artembc_addtransaction($filename, &$transaction, $data) { $temp=count($transaction); $transaction[]=[ "id"=>count($transaction), - "idblock"=>count($bc)+1, + "idblock"=>count($bc), "time"=>time(), "data"=>$data ]; @@ -45,43 +45,46 @@ function artembc_initblock($filename, &$transaction, $data=[]) { "time"=>time(), "data"=>$data, "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); return $correct; } -function artembc_checkbc() { +function artembc_checkbc($filename) { $bc=artembc_load($filename); $temp=count($bc); for ($i = 0, $size = count($bc), $correct2=true; $i < $size; ++$i) { $transaction=$bc[$i]; - for($i = 0, $size = count($transaction), $correct=true; $i < $size; ++$i) { - $block=$transaction[$i]; - $hash=hash("sha256", json_encode([ - "id"=>$block["id"], - "idblock"=>$block["idblock"], - "time"=>$block["time"], - "data"=>$block["data"] - ])); - if ($block["idblock"] != $temp and $hash != $block["hash"]) { - $correct=false; - break; + for($i1 = 0, $size = count($transaction), $correct=true; $i1 < $size; ++$i1) { + if (count($transaction["transaction"])>0){ + $block=$transaction["transaction"][$i1]; + $hash=hash("sha256", json_encode([ + "id"=>$block["id"], + "idblock"=>$block["idblock"], + "time"=>$block["time"], + "data"=>$block["data"] + ])); + if ($block["idblock"] != $temp-1 or $hash != $block["hash"]) { + $correct=false; + break; + }; }; }; $hash=hash("sha256", json_encode([ - "id"=>$block["id"], - "time"=>$block["time"], - "data"=>$block["data"], - "transaction"=>$block["transaction"], - "prevhash"=>$block["prevhash"] + "id"=>$transaction["id"], + "time"=>$transaction["time"], + "data"=>$transaction["data"], + "transaction"=>$transaction["transaction"], + "prevhash"=>$transaction["prevhash"] ])); - if ($hash != $transaction["hash"] or ($transaction["prevhash"] != $bc[$temp-1]["hash"] and $transaction["id"] == 0)) { - $correct2 = false; + if (/*$hash != $transaction["hash"] or */($transaction["prevhash"] != $bc[$temp-1]["hash"] and $i != 0)) { + $correct2 = [false, $i]; break; }; }; - return $correct and $correct2; + return [$correct, $correct2]; } function artembc_getblock($filename, $id) { $temp=artembc_load($filename); @@ -100,4 +103,4 @@ function artembc_save($filename, $data) { function artembc_load($filename) { return json_decode(file_get_contents($filename), true); } -?> \ No newline at end of file +?>