diff --git a/app/app.cli.php b/app/app.cli.php
index 06edf72..6ae3227 100644
--- a/app/app.cli.php
+++ b/app/app.cli.php
@@ -1,4 +1,5 @@
...
data: $('#transaction_form').serialize(),
success: function(response){
if(response[0] == 200) updateModal('Transaction', 'Done
Account: '+response[1]+'
Amount: '+response[2]+'
Balance: '+response[3]);
+ else if(response[0] == 100) updateModal('Transaction', 'Error
'+response[1]);
$('#add_btn').html('Add');
$('#transaction_form')[0].reset();
},
error: function(response){
updateModal('Transaction', 'Error
'+response);
+ console.log(response);
$('#add_btn').html('Add');
}
});
@@ -114,16 +116,23 @@
...
data: {get_transactionchain: "1"},
success: function(response){
var s = "";
- var keys = ['prev', 'date', 'amount', 'account', 'hash'];
- for(var r in response) {
- s += '';
- for(var k in keys) s += '
'+keys[k]+': '+response[r][keys[k]]+'
';
- s += '
';
+ if(response[0] === 100) {
+ updateModal('TransactionChain', 'Error
'+response[1]);
+ s = response[1];
+ }
+ else {
+ var keys = ['prev', 'date', 'amount', 'account', 'hash'];
+ for(var r in response) {
+ s += '';
+ for(var k in keys) s += '
'+keys[k]+': '+response[r][keys[k]]+'
';
+ s += '
';
+ }
}
$('#transactionchain_section').html(s);
},
error: function(response){
updateModal('TransactionChain', 'Error
'+response);
+ console.log(response);
$('#transactionchain_section').html('Error');
}
});
diff --git a/app/web/source.php b/app/web/source.php
index 4f7a797..554fff0 100644
--- a/app/web/source.php
+++ b/app/web/source.php
@@ -16,6 +16,8 @@ function getChain() {
Chain::show(true);
}
// ---------------------
+if(!Chain::isValid()) die(json_encode([100, 'Chain is not valid.']));
+// ---------------------
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action']) && isset($_POST['account']) && isset($_POST['amount'])) {
$account = $_POST['account'];
$amount = round($_POST['amount'], ROUND_PREC);
diff --git a/db/chain.db.json b/db/chain.db.json
index eea332a..fa62afb 100644
--- a/db/chain.db.json
+++ b/db/chain.db.json
@@ -20,4 +20,4 @@
"account": "webAcc",
"hash": "22af9a6b2f8f6a7417c35405a807763dca4d00d838c6c045194578d82ef21122"
}
-]
+]
\ No newline at end of file
diff --git a/main.php b/main.php
index 4f9b0a0..60ed6ba 100644
--- a/main.php
+++ b/main.php
@@ -3,4 +3,4 @@
require_once 'inc.php';
// ---------------------
require_once DIR_APP.'app.cli.php';
-?>
+?>
\ No newline at end of file
diff --git a/src/Chain.class.php b/src/Chain.class.php
index a7296aa..e223471 100644
--- a/src/Chain.class.php
+++ b/src/Chain.class.php
@@ -8,11 +8,13 @@ public static function balanceOf(string $account):float {
}
return round($balance, ROUND_PREC);
}
- public static function save():void {
+ public static function save():bool {
if(self::isValid()) {
global $transactions;
file_put_contents(TRANSACTIONS_DB_FILE, json_encode($transactions, JSON_PRETTY_PRINT));
+ return true;
}
+ return false;
}
public static function show(?bool $current = false):void {
global $transactions;
@@ -21,15 +23,10 @@ public static function show(?bool $current = false):void {
}
public static function isValid():bool {
global $transactions;
- $txo = [];
- $i = 0;
foreach($transactions as $tx) {
- $txo[] = new Transaction($txo[count($txo)-1], $tx['amount'], $tx['account'], $tx['date'], true);
- if(!$txo[$i]->isValid()) return false;
- if(!PoW::verifyHash($txo[$i]->prev.':'.$txo[$i]->amount.':'.$txo[$i]->date.':'.$txo[$i]->account, $txo[$i]->hash)) return false;
- $i++;
+ if(!PoW::verifyHash($tx['prev'].':'.$tx['amount'].':'.$tx['date'].':'.$tx['account'], $tx['hash'])) return false;
}
return true;
}
}
-?>
+?>
\ No newline at end of file
diff --git a/src/Transaction.class.php b/src/Transaction.class.php
index a171de0..d1986a5 100644
--- a/src/Transaction.class.php
+++ b/src/Transaction.class.php
@@ -14,10 +14,7 @@ public function __construct($prev, ?float $amount, ?string $account = null, $dat
$this->account = $account ? $account : null;
$this->toHash = $this->prev.':'.$this->amount.':'.$this->date.':'.$this->account;
$this->hash = PoW::hash($this->toHash);
- if(!$isFake) if($this->isValid()) $this->save();
- }
- public function isValid():bool {
- return PoW::verifyHash($this->toHash, $this->hash);
+ if(!$isFake) $this->save();
}
public function save():void {
global $transactions;
diff --git a/test/1.test.php b/test/1.test.php
new file mode 100644
index 0000000..b805e2f
--- /dev/null
+++ b/test/1.test.php
@@ -0,0 +1,10 @@
+
\ No newline at end of file