From c1290d532741b7655d0ccb59a1d069bba471220d Mon Sep 17 00:00:00 2001 From: Huseyin Uslu Date: Tue, 18 Feb 2014 11:29:31 +0200 Subject: [PATCH 1/6] added block validation and account validation scripts --- scripts/validate_blocks.php | 34 ++++++++++++++++++++++ scripts/validate_users.php | 56 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 scripts/validate_blocks.php create mode 100644 scripts/validate_users.php diff --git a/scripts/validate_blocks.php b/scripts/validate_blocks.php new file mode 100644 index 00000000..f9b40b30 --- /dev/null +++ b/scripts/validate_blocks.php @@ -0,0 +1,34 @@ +#!/usr/bin/php +can_connect() !== true ) + die("Failed to connect to RPC server\n"); + + echo "Validating blocks in database against coind..\n"; + + // fetch all blocks + $allBlocks = $block->getAll(); + foreach ($allBlocks as $block) + { + try { + $blockInfo = $bitcoin->getblock($block['blockhash']); + } + catch(Exception $e) + { + // don't report orphan blocks. + if($block['confirmations']!= -1 && $e->getMessage() == 'RPC call did not return 200: HTTP error: 500 - JSON Response: [-5] Block not found') + { + echo "Block not found: database-id: $block[id] - height: $block[height].\n"; + } + } + } + + echo "Done..\n"; +?> \ No newline at end of file diff --git a/scripts/validate_users.php b/scripts/validate_users.php new file mode 100644 index 00000000..e188b28f --- /dev/null +++ b/scripts/validate_users.php @@ -0,0 +1,56 @@ +#!/usr/bin/php +getAllAssoc(); + + $mask = "| %6s | %20s | %16s | %10s | %-12.12s | %5s | %5s | %12s | %5s | \n"; + printf($mask, 'ID', 'Username', 'LoggedIP', 'Last Login','Days Since', 'Ever', 'Trans', 'Balance','Stale'); + + $currentTime = time(); + $totalSavings = 0; + + foreach ($users as $user) + { + $id = $user['id']; + $isAdmin = $user['is_admin']; + $username = $user['username']; + $loggedIp = $user['loggedIp']; + $lastLogin = $user['last_login']; + $coinAddress = $user['coin_address']; + + $everLoggedIn = !empty($lastLogin); + $timeDelta = $currentTime - $lastLogin; + $lastLoginInDays = abs($timeDelta)/60/60/24; + + if($lastLoginInDays < $timeLimitInDays) + continue; + + // get transactions summary for the user + $summary = $transaction->getTransactionSummary($id); + $transactions_exists = !empty($summary); + + // get balances + $balances = $transaction->getBalance($id); + $confirmedBalance = $balances['confirmed']; + $totalSavings += $confirmedBalance; + + $staleAccount = $everLoggedIn == false && $transactions_exists == false; + + printf($mask, $id, $username, + $loggedIp, $lastLogin, $lastLoginInDays, $everLoggedIn ? 'yes' : 'no', + $transactions_exists ? 'yes' : 'no', $confirmedBalance, + $staleAccount ? 'yes' : 'no' ); + } + + echo "Total balance of stale accounts: $totalSavings \n"; +?> \ No newline at end of file From 981d73f988b363fed94c98aa90bd74c0cbc1deba Mon Sep 17 00:00:00 2001 From: Huseyin Uslu Date: Tue, 18 Feb 2014 13:00:47 +0200 Subject: [PATCH 2/6] applied seraphers proposed fixes & suggestions --- scripts/validate_blocks.php | 24 ++++++++++++++++++++---- scripts/validate_users.php | 8 ++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/scripts/validate_blocks.php b/scripts/validate_blocks.php index f9b40b30..c26b0428 100644 --- a/scripts/validate_blocks.php +++ b/scripts/validate_blocks.php @@ -6,27 +6,43 @@ chdir(dirname(__FILE__)); // Include all settings and classes - require_once('shared.inc.php'); + require_once('shared.inc.php'); if ( $bitcoin->can_connect() !== true ) die("Failed to connect to RPC server\n"); echo "Validating blocks in database against coind..\n"; + + $mask = "| %6s | %8s | %13s | %20s | %10s | \n"; + printf($mask, 'DB-ID', 'Height', 'Confirmations', 'Time', 'Status'); // fetch all blocks $allBlocks = $block->getAll(); foreach ($allBlocks as $block) { + //print_r($block); + $status = 'VALID'; + try { $blockInfo = $bitcoin->getblock($block['blockhash']); } catch(Exception $e) { - // don't report orphan blocks. - if($block['confirmations']!= -1 && $e->getMessage() == 'RPC call did not return 200: HTTP error: 500 - JSON Response: [-5] Block not found') + if($block['confirmations']== -1) { - echo "Block not found: database-id: $block[id] - height: $block[height].\n"; + $status = 'ORPHAN'; + } + else if($e->getMessage() == 'RPC call did not return 200: HTTP error: 500 - JSON Response: [-5] Block not found') + { + $status = 'INVALID'; } + else + { + $status = 'UNKNOWN'; + } + } + finally { + printf($mask, $block['id'], $block['height'], $block['confirmations'], strftime("%Y-%m-%d %H:%M:%S", $block['time']), $status); } } diff --git a/scripts/validate_users.php b/scripts/validate_users.php index e188b28f..e22bc6c6 100644 --- a/scripts/validate_users.php +++ b/scripts/validate_users.php @@ -13,7 +13,7 @@ // Fetch all users $users = $user->getAllAssoc(); - $mask = "| %6s | %20s | %16s | %10s | %-12.12s | %5s | %5s | %12s | %5s | \n"; + $mask = "| %6s | %20s | %16s | %20s | %12.12s | %5s | %5s | %12s | %5s | \n"; printf($mask, 'ID', 'Username', 'LoggedIP', 'Last Login','Days Since', 'Ever', 'Trans', 'Balance','Stale'); $currentTime = time(); @@ -30,7 +30,7 @@ $everLoggedIn = !empty($lastLogin); $timeDelta = $currentTime - $lastLogin; - $lastLoginInDays = abs($timeDelta)/60/60/24; + $lastLoginInDays = round(abs($timeDelta)/60/60/24, 0); if($lastLoginInDays < $timeLimitInDays) continue; @@ -47,8 +47,8 @@ $staleAccount = $everLoggedIn == false && $transactions_exists == false; printf($mask, $id, $username, - $loggedIp, $lastLogin, $lastLoginInDays, $everLoggedIn ? 'yes' : 'no', - $transactions_exists ? 'yes' : 'no', $confirmedBalance, + $loggedIp, strftime("%Y-%m-%d %H:%M:%S", $lastLogin), $lastLoginInDays, $everLoggedIn ? 'yes' : 'no', + $transactions_exists ? 'yes' : 'no', round($confirmedBalance,8), $staleAccount ? 'yes' : 'no' ); } From cf4e2e2f6a58ad6a64cc0ebd38bc095efca07880 Mon Sep 17 00:00:00 2001 From: Huseyin Uslu Date: Tue, 18 Feb 2014 13:04:18 +0200 Subject: [PATCH 3/6] fixed newlines --- scripts/validate_blocks.php | 6 +++--- scripts/validate_users.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/validate_blocks.php b/scripts/validate_blocks.php index c26b0428..e7118021 100644 --- a/scripts/validate_blocks.php +++ b/scripts/validate_blocks.php @@ -9,9 +9,9 @@ require_once('shared.inc.php'); if ( $bitcoin->can_connect() !== true ) - die("Failed to connect to RPC server\n"); + die("Failed to connect to RPC server". PHP_EOL); - echo "Validating blocks in database against coind..\n"; + echo "Validating blocks in database against coind..". PHP_EOL; $mask = "| %6s | %8s | %13s | %20s | %10s | \n"; printf($mask, 'DB-ID', 'Height', 'Confirmations', 'Time', 'Status'); @@ -46,5 +46,5 @@ } } - echo "Done..\n"; + echo "Done..". PHP_EOL; ?> \ No newline at end of file diff --git a/scripts/validate_users.php b/scripts/validate_users.php index e22bc6c6..bc44971c 100644 --- a/scripts/validate_users.php +++ b/scripts/validate_users.php @@ -52,5 +52,5 @@ $staleAccount ? 'yes' : 'no' ); } - echo "Total balance of stale accounts: $totalSavings \n"; + echo "Total balance of stale accounts: $totalSavings" . PHP_EOL; ?> \ No newline at end of file From 347a43601dd923719e952fd78775405439cec0f9 Mon Sep 17 00:00:00 2001 From: Huseyin Uslu Date: Tue, 18 Feb 2014 13:08:15 +0200 Subject: [PATCH 4/6] removed a debug line --- scripts/validate_blocks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/validate_blocks.php b/scripts/validate_blocks.php index e7118021..57895295 100644 --- a/scripts/validate_blocks.php +++ b/scripts/validate_blocks.php @@ -20,7 +20,6 @@ $allBlocks = $block->getAll(); foreach ($allBlocks as $block) { - //print_r($block); $status = 'VALID'; try { From f6ceace3868d83099824a30bfa9b3c8678e838d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Uslu?= Date: Fri, 28 Feb 2014 23:40:08 +0200 Subject: [PATCH 5/6] fixed orphan detection --- scripts/validate_blocks.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/scripts/validate_blocks.php b/scripts/validate_blocks.php index 57895295..e1d0af9f 100644 --- a/scripts/validate_blocks.php +++ b/scripts/validate_blocks.php @@ -13,25 +13,25 @@ echo "Validating blocks in database against coind..". PHP_EOL; - $mask = "| %6s | %8s | %13s | %20s | %10s | \n"; + $mask = "| %6s | %8s | %13s | %20s | %10s |". PHP_EOL;; printf($mask, 'DB-ID', 'Height', 'Confirmations', 'Time', 'Status'); // fetch all blocks $allBlocks = $block->getAll(); foreach ($allBlocks as $block) - { - $status = 'VALID'; - + { try { - $blockInfo = $bitcoin->getblock($block['blockhash']); + if($block['confirmations']== -1) // mark orphan blocks. + $status = 'ORPHAN'; + else + { + $blockInfo = $bitcoin->getblock($block['blockhash']); + $status = 'VALID'; // if the getblock() call didn't throw an exception, it's a valid block then. + } } catch(Exception $e) { - if($block['confirmations']== -1) - { - $status = 'ORPHAN'; - } - else if($e->getMessage() == 'RPC call did not return 200: HTTP error: 500 - JSON Response: [-5] Block not found') + if($e->getMessage() == 'RPC call did not return 200: HTTP error: 500 - JSON Response: [-5] Block not found') { $status = 'INVALID'; } From e73adc193ed5fba02d4ddad86d77d293f0a764e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Uslu?= Date: Sun, 2 Mar 2014 17:48:33 +0200 Subject: [PATCH 6/6] removed finally block --- scripts/validate_blocks.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts/validate_blocks.php b/scripts/validate_blocks.php index e1d0af9f..eed93e43 100644 --- a/scripts/validate_blocks.php +++ b/scripts/validate_blocks.php @@ -40,9 +40,8 @@ $status = 'UNKNOWN'; } } - finally { - printf($mask, $block['id'], $block['height'], $block['confirmations'], strftime("%Y-%m-%d %H:%M:%S", $block['time']), $status); - } + + printf($mask, $block['id'], $block['height'], $block['confirmations'], strftime("%Y-%m-%d %H:%M:%S", $block['time']), $status); } echo "Done..". PHP_EOL;