From 9e9c12fbbb47e982f1abb94136ad10aa13abdfa8 Mon Sep 17 00:00:00 2001 From: Arionum Date: Sat, 11 Aug 2018 23:00:33 +0300 Subject: [PATCH] accounts resync option --- util.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/util.php b/util.php index 7834f84..8dd1984 100755 --- a/util.php +++ b/util.php @@ -447,6 +447,27 @@ elseif ($cmd == 'get-address') { } elseif ($cmd == 'clean-blacklist') { $db->run("UPDATE peers SET blacklisted=0, fails=0, stuckfail=0"); echo "All the peers have been removed from the blacklist\n"; +}elseif($cmd == 'resync-accounts'){ +// resyncs the balance on all accounts + + // lock table to avoid race conditions on blocks + $db->exec("LOCK TABLES blocks WRITE, accounts WRITE, transactions WRITE, mempool WRITE"); + +$r=$db->run("SELECT * FROM accounts"); +foreach($r as $x){ + $alias=$x['alias']; + if(empty($alias)) $alias="A"; + $rec=$db->single("SELECT SUM(val) FROM transactions WHERE (dst=:id or dst=:alias) AND (height<80000 OR (version!=100 AND version!=103)) and version<111",[":id"=>$x['id'], ":alias"=>$alias]); + $spent=$db->single("SELECT SUM(val+fee) FROM transactions WHERE public_key=:pub AND version>0",[":pub"=>$x['public_key']]); + if($spent==false) $spent=0; + $balance=round(($rec-$spent),8); + if($x['balance']!=$balance){ + echo "rec: $rec, spent: $spent, bal: $x[balance], should be: $balance - $x[id] $x[public_key]\n"; + $db->run("UPDATE accounts SET balance=:bal WHERE id=:id",[":id"=>$x['id'], ":bal"=>$balance]); + } +} +$db->exec("UNLOCK TABLES"); +echo "All done"; } else {