Skip to content

Commit

Permalink
Merge pull request #2 from m-primo/x
Browse files Browse the repository at this point in the history
v0.5_1
  • Loading branch information
m-primo authored Apr 24, 2020
2 parents 249574d + 4a92ad0 commit 7b3d0dc
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/app.cli.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
if(!Chain::isValid()) die('Chain is not valid.'.eol);
function stdin() {
return trim(fgets(STDIN));
}
Expand Down
19 changes: 14 additions & 5 deletions app/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ <h5 class="modal-title" id="statusModalCenterTitle">...</h5>
data: $('#transaction_form').serialize(),
success: function(response){
if(response[0] == 200) updateModal('Transaction', '<span class="badge badge-success">Done</span><br>Account: '+response[1]+'<br>Amount: '+response[2]+'<br>Balance: '+response[3]);
else if(response[0] == 100) updateModal('Transaction', '<span class="badge badge-danger">Error</span><br>'+response[1]);
$('#add_btn').html('Add');
$('#transaction_form')[0].reset();
},
error: function(response){
updateModal('Transaction', '<span class="badge badge-danger">Error</span><br>'+response);
console.log(response);
$('#add_btn').html('Add');
}
});
Expand All @@ -114,16 +116,23 @@ <h5 class="modal-title" id="statusModalCenterTitle">...</h5>
data: {get_transactionchain: "1"},
success: function(response){
var s = "";
var keys = ['prev', 'date', 'amount', 'account', 'hash'];
for(var r in response) {
s += '<div class="tx_outer alert alert-secondary" id="tx_outer_index_'+r+'">';
for(var k in keys) s += '<div class="tx_inner">'+keys[k]+': '+response[r][keys[k]]+'</div>';
s += '</div>';
if(response[0] === 100) {
updateModal('TransactionChain', '<span class="badge badge-danger">Error</span><br>'+response[1]);
s = response[1];
}
else {
var keys = ['prev', 'date', 'amount', 'account', 'hash'];
for(var r in response) {
s += '<div class="tx_outer alert alert-secondary" id="tx_outer_index_'+r+'">';
for(var k in keys) s += '<div class="tx_inner">'+keys[k]+': '+response[r][keys[k]]+'</div>';
s += '</div>';
}
}
$('#transactionchain_section').html(s);
},
error: function(response){
updateModal('TransactionChain', '<span class="badge badge-danger">Error</span><br>'+response);
console.log(response);
$('#transactionchain_section').html('<span class="badge badge-danger">Error</span>');
}
});
Expand Down
2 changes: 2 additions & 0 deletions app/web/source.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion db/chain.db.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
"account": "webAcc",
"hash": "22af9a6b2f8f6a7417c35405a807763dca4d00d838c6c045194578d82ef21122"
}
]
]
2 changes: 1 addition & 1 deletion main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
require_once 'inc.php';
// ---------------------
require_once DIR_APP.'app.cli.php';
?>
?>
13 changes: 5 additions & 8 deletions src/Chain.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
?>
?>
5 changes: 1 addition & 4 deletions src/Transaction.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions test/1.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
define('fds', '../');
require_once fds.'inc.php';
// ---------------------
var_dump(Chain::isValid()); // current chain validate
var_dump(Chain::save()); // try to save the chain, current
$transactions[1]['amount'] = 500; // change a transaction
var_dump(Chain::save()); // try to save the chain, after changing a transaction
var_dump(Chain::isValid()); // chain validate after changing a transaction
?>

0 comments on commit 7b3d0dc

Please sign in to comment.