-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lettura nuovo file blockchain.json e aggiornamento funzioni
- Loading branch information
Showing
8 changed files
with
241 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
|
||
blockchain.json | ||
qldb.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"json_results": { | ||
"01-5-2023__31-5-2023": "a66d8b9b2a6959daf5516f0f777a5eeed3e32eabb810eb4e8494764310f1f8e7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
[ | ||
{ | ||
"merkleRoot": "36d12cb8e28699290b0cef1ea5a1fadfd2c6bc4afad02633330e86b197b61884", | ||
"txHash": "0x4de4beaf81e16c53537f1369180ab77a803b4251d8a29a6ee937b5f72af8a0c9", | ||
"timestamp": 1683380719 | ||
{ | ||
"json_results": { | ||
"01-5-2023__31-5-2023": "a66d8b9b2a6959daf5516f0f777a5eeed3e32eabb810eb4e8494764310f1f8e7" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
/** | ||
* Funzione generiche | ||
* | ||
* Author: Sergio Casizzone | ||
* Date: 18.06.2023 | ||
*/ | ||
|
||
|
||
/** | ||
* Questa funzione carica un file e ne restituisce il contenuto in formato json | ||
* | ||
* @param string $filename | ||
* @return json | ||
*/ | ||
function loadFile($filename) | ||
{ | ||
// verifico l'esistenza del file | ||
if (file_exists($filename)) { | ||
$json = json_decode(file_get_contents($filename)); | ||
} else { | ||
echo "Il file {$filename} non esiste.\n"; | ||
die(); | ||
} | ||
|
||
// verifico se il file è un json | ||
if ((json_last_error() === JSON_ERROR_NONE) == false) { | ||
echo "Il file {$filename} contiene un errore e non viene riconosciuto.\n"; | ||
die(); | ||
} | ||
return $json; | ||
} | ||
|
||
/** | ||
* Questa funzione estrae il mese e l'anno dal periodo dal file blockchain.json | ||
* | ||
* @param string $period | ||
* @return date in format yyyy-mm | ||
*/ | ||
function dateFromPeriod($period){ | ||
$dates = explode('__', $period); | ||
// echo '<pre>' . print_r($dates, true) . '</pre>'; | ||
// exit; | ||
|
||
$startDate = \DateTime::createFromFormat('d-m-Y H:i:s', $dates[0] . ' 00:00:00'); | ||
$finishDate = \DateTime::createFromFormat('d-m-Y H:i:s', $dates[1] . ' 23:59:59'); | ||
// echo '<pre>' . print_r($startDate, true) . '</pre>'; | ||
// echo '<pre>' . print_r($finishDate, true) . '</pre>'; | ||
// exit; | ||
|
||
$result = new \stdClass; | ||
$result->startDate = $startDate; | ||
$result->finishDate = $finishDate; | ||
|
||
return $result; | ||
} | ||
|
||
/** | ||
* Questa funzione verifica entrambi i merkle root e restituisce gli array da stampare a video | ||
* | ||
* @param string $qldb_json, $blockchain_json | ||
* @return array | ||
*/ | ||
function verificaMerkleRoot($qldb_json, $blockchain_json) | ||
{ | ||
$blockchainResults = (array) $blockchain_json->json_results; | ||
// echo '<pre>' . print_r($blockchainResults, true) . '</pre>'; | ||
// exit; | ||
|
||
// genero l'array con i merkle root dalla blockchain suddivisi per anno/mese | ||
foreach ($blockchainResults as $period => $merkleRootByPeriod) { | ||
// funzione che estrae date dal period salvato su blockchain | ||
$dateFromPeriod = dateFromPeriod($period); | ||
|
||
// estraggo dal file qldb solo gli hash del mese/anno elaborato | ||
foreach ($qldb_json as $qldb) { | ||
// data ricavata dal timestamp | ||
$stringDate = date('Y-m-d H:i:s', $qldb->activity_timestamp); | ||
// data convertita in formato datetime | ||
$matchDate = new \DateTime($stringDate); | ||
|
||
// confronto la data del qldb con il periodo | ||
if ($matchDate >= $dateFromPeriod->startDate && $matchDate <= $dateFromPeriod->finishDate) { | ||
// se appartiene al periodo la salvo in array | ||
$qldb_hashes[$matchDate->format('Y-m')][] = $qldb->document_hash; | ||
|
||
// salvo anche le informazioni del merkle root della blockchain relative | ||
$blockchain_data[$matchDate->format('Y-m')] = $merkleRootByPeriod; | ||
} | ||
$all_qldb_hashes[$matchDate->format('Y-m')][] = $qldb->document_hash; | ||
|
||
} | ||
} | ||
|
||
return [ | ||
'qldb_hashes' => $qldb_hashes, | ||
'blockchain_data' => $blockchain_data, | ||
'all_qldb_hashes' => $all_qldb_hashes | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,66 @@ | ||
<?php | ||
|
||
require __DIR__ . '/src/MerkleRootCalculator.php'; | ||
require __DIR__ . '/func/functions.php'; | ||
|
||
/** | ||
* Questa funzione carica un file e ne restituisce il contenuto in formato json | ||
* | ||
* @param string $filename | ||
* @return json | ||
*/ | ||
function loadFile($filename){ | ||
// verifico l'esistenza del file | ||
if (file_exists($filename)) { | ||
$json = json_decode(file_get_contents($filename)); | ||
} else { | ||
echo "Il file {$filename} non esiste.\n"; | ||
die(); | ||
} | ||
|
||
// verifico se il file è un json | ||
if ((json_last_error() === JSON_ERROR_NONE) == false) { | ||
echo "Il file {$filename} contiene un errore e non viene riconosciuto.\n"; | ||
die(); | ||
} | ||
return $json; | ||
} | ||
|
||
/** | ||
* Inizio della verifica | ||
*/ | ||
echo "Merkle Root Calculator\n\n"; | ||
|
||
echo "Fai la tua scelta:\n\n"; | ||
echo " 1. Verifica Merkle Root\n"; | ||
echo " 2. Estrai i Merkle Root dal file QLDB\n"; | ||
echo " 3. Estrai i Merkle Root dal file Blockchain\n"; | ||
$scelta = readline(); | ||
|
||
if ($scelta != '1' && $scelta != '2' && $scelta != '3'){ | ||
echo "\nScelta non consentita!\n\n"; | ||
die(); | ||
} | ||
|
||
// carico il contenuto dei file json | ||
$qldb_data = loadFile('qldb.json'); | ||
$blockchain_data = loadFile('blockchain.json'); | ||
$qldb_json = loadFile('qldb.json'); | ||
$blockchain_json = loadFile('blockchain.json'); | ||
|
||
// Estraggo le informazioni dai file | ||
$result = verificaMerkleRoot($qldb_json, $blockchain_json); | ||
|
||
// genero l'array di hash di qldb | ||
foreach ($qldb_data as $data) $qldb_hashes[] = $data->document_hash; | ||
if ($scelta == '1'){ | ||
foreach ($result['blockchain_data'] as $period => $merkleRoot) { | ||
// Inizializzo la classe e calcolo il Merkle root con gli hash ricavati dal qldb | ||
$merkle = new MerkleRootCalculator(); | ||
$qldb_root = $merkle->root($result['qldb_hashes'][$period]); | ||
|
||
// prendo solo l'elemento 0 del json della blockchain | ||
$blockchain_data = $blockchain_data[0]; | ||
// stampo il risultato per singolo periodo | ||
echo "Periodo: $period\n"; | ||
echo "Merkle root da Blockchain: $merkleRoot\n"; | ||
echo "Merkle root da QLDB: $qldb_root\n"; | ||
echo "Verifica Merkle root: " . ($merkle->verify($result['qldb_hashes'][$period], $merkleRoot) ? 'SUCCESSO' : 'FALLITO') . "\n"; | ||
} | ||
|
||
die(); | ||
} | ||
|
||
if ($scelta == '2'){ | ||
foreach ($result['all_qldb_hashes'] as $period => $hashes) { | ||
// Inizializzo la classe e calcolo il Merkle root con gli hash ricavati dal qldb | ||
$merkle = new MerkleRootCalculator(); | ||
$qldb_root = $merkle->root($hashes); | ||
|
||
// inizializzo la classe e calcolo il Merkle root con gli hash ricavati dal qldb | ||
$merkle = new MerkleRootCalculator(); | ||
$qldb_root = $merkle->root($qldb_hashes); | ||
// stampo il risultato per singolo periodo | ||
echo "Periodo: $period\n"; | ||
echo "Merkle root da QLDB: $qldb_root\n"; | ||
} | ||
|
||
die(); | ||
} | ||
|
||
// stampo il risultato | ||
echo "Merkle root da Blockchain: $blockchain_data->merkleRoot\n"; | ||
echo "Merkle root da QLDB: $qldb_root\n"; | ||
echo "Verifica Merkle root: " . ($merkle->verify($qldb_hashes, $blockchain_data->merkleRoot) ? 'SUCCESSO' : 'FALLITO') . "\n"; | ||
if ($scelta == '3') { | ||
foreach ($result['blockchain_data'] as $period => $merkleRoot) { | ||
|
||
// stampo il risultato per singolo periodo | ||
echo "Periodo: $period\n"; | ||
echo "Merkle root da Blockchain: $merkleRoot\n"; | ||
} | ||
|
||
die(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[ | ||
{ | ||
"id": 5, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683380719 | ||
}, | ||
{ | ||
"id": 2, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1680779370 | ||
}, | ||
{ | ||
"id": 4, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683379607 | ||
}, | ||
{ | ||
"id": 3, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683357746 | ||
}, | ||
{ | ||
"id": 6, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683464522 | ||
}, | ||
{ | ||
"id": 1, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1680779370 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,42 @@ | ||
[ | ||
{ | ||
"id": 5, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683380719 | ||
}, | ||
{ | ||
"id": 2, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1680779370 | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1684770201 | ||
}, | ||
{ | ||
"id": 4, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683379607 | ||
"id": 9, | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1686564728 | ||
}, | ||
{ | ||
"id": 3, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683357746 | ||
"id": 10, | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1686564965 | ||
}, | ||
{ | ||
"id": 6, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1683464522 | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1684935291 | ||
}, | ||
{ | ||
"id": 5, | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1684933749 | ||
}, | ||
{ | ||
"id": 3, | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1684924135 | ||
}, | ||
{ | ||
"id": 1, | ||
"document_hash": "5f2fb9087b45d6a030b1827a21dcd1816d24fec6833afff8183bafbee95e72a0", | ||
"activity_timestamp": 1680779370 | ||
"activity_timestamp": 1683563084 | ||
}, | ||
{ | ||
"id": 14, | ||
"document_hash": "2b44e6ec8a82f17f87f17c62b7c10b98afb0e84d8247417c7e276a438ae81d1b", | ||
"activity_timestamp": 1686763270 | ||
} | ||
] |