added logging system
This commit is contained in:
parent
65a6216eff
commit
d3973ca202
@ -269,9 +269,12 @@ def validateMinedBlock(self):
|
|||||||
for transaction in new_block.data:
|
for transaction in new_block.data:
|
||||||
if transaction.type != 1:
|
if transaction.type != 1:
|
||||||
fees += transaction.inputs[0][1] - transaction.outputs[0][1]
|
fees += transaction.inputs[0][1] - transaction.outputs[0][1]
|
||||||
|
self.db.createLog(transaction.inputs[0][0], time.time(), f"Transaction of {transaction.inputs[0][1]} is added in block id {new_block.id}")
|
||||||
|
self.db.createLog(transaction.outputs[0][0], time.time(), f"You have received new coins: {transaction.outputs[0][1]}, To see all info view block id {new_block.id}")
|
||||||
|
|
||||||
new_reward = Tx()
|
new_reward = Tx()
|
||||||
new_reward.createRewardTransaction(self.user.public_ser, self.user.private_ser, "MINE", fees)
|
new_reward.createRewardTransaction(self.user.public_ser, self.user.private_ser, "MINE", fees)
|
||||||
|
self.db.createLog(new_block.metadata['miner'], time.time(), f"Block id {new_block.id}, is validated!")
|
||||||
utilityHelper.saveFile("../data/transaction_pool.dat", new_reward)
|
utilityHelper.saveFile("../data/transaction_pool.dat", new_reward)
|
||||||
|
|
||||||
|
|
||||||
@ -290,6 +293,8 @@ def validateMinedBlock(self):
|
|||||||
else:
|
else:
|
||||||
new_block.metadata['false_validations'] += 1
|
new_block.metadata['false_validations'] += 1
|
||||||
if new_block.metadata['false_validations'] >= 3:
|
if new_block.metadata['false_validations'] >= 3:
|
||||||
|
# Create log for miner
|
||||||
|
self.db.createLog(new_block.metadata['miner'], time.time(), f"Block id {new_block.id}, was flagged invalid. Mine unsuccesful...")
|
||||||
# set transactions in block back to the pool
|
# set transactions in block back to the pool
|
||||||
for transaction in new_block.data:
|
for transaction in new_block.data:
|
||||||
utilityHelper.saveFile("../data/transaction_pool.dat", transaction)
|
utilityHelper.saveFile("../data/transaction_pool.dat", transaction)
|
||||||
|
@ -116,5 +116,54 @@ class DatabaseHelper:
|
|||||||
except sqlite3.Error as error:
|
except sqlite3.Error as error:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def createLog(self, public_key, date, info, unread = 0):
|
||||||
|
if not self.conn:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.cursor.execute("INSERT INTO `logs` (public_key, date, info, unread) VALUES (?, ?, ?, ?)", (public_key, date, info, unread,))
|
||||||
|
self.commit()
|
||||||
|
|
||||||
|
except sqlite3.Error as error:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def getLatestLogs(self):
|
||||||
|
if not self.conn:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.cursor.execute("SELECT * FROM `logs` ORDER BY `id` DESC LIMIT 10")
|
||||||
|
|
||||||
|
return self.cursor.fetchall()
|
||||||
|
|
||||||
|
except sqlite3.Error as error:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def getUreandLogs(self):
|
||||||
|
if not self.conn:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.cursor.execute("SELECT * FROM `logs` WHERE `unread` = ?", (0,))
|
||||||
|
|
||||||
|
return self.cursor.fetchall()
|
||||||
|
|
||||||
|
except sqlite3.Error as error:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def updateLogStatus(self, id, unread = 1):
|
||||||
|
if not self.conn:
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.cursor.execute("UPDATE `logs` SET `unread` = ? WHERE `id` = ?", (unread, id, ))
|
||||||
|
self.commit()
|
||||||
|
|
||||||
|
except sqlite3.Error as error:
|
||||||
|
return False
|
||||||
|
|
||||||
def checkMigrations(self):
|
def checkMigrations(self):
|
||||||
self.query("CREATE TABLE IF NOT EXISTS `users` ( `private_key` BLOB NOT NULL , `public_key` BLOB NOT NULL , `username` VARCHAR(255) NOT NULL , `password` VARCHAR(255) NOT NULL )")
|
self.query("CREATE TABLE IF NOT EXISTS `users` ( `private_key` BLOB NOT NULL , `public_key` BLOB NOT NULL , `username` VARCHAR(255) NOT NULL , `password` VARCHAR(255) NOT NULL )")
|
||||||
|
self.query("CREATE TABLE IF NOT EXISTS `logs` ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `public_key` BLOB NOT NULL , `date` VARCHAR(255) NOT NULL , `info` VARCHAR(255) NOT NULL , `unread` INTEGER NOT NULL )")
|
||||||
|
@ -36,9 +36,10 @@ class MenuHelper:
|
|||||||
|
|
||||||
self.user_settings_menu[0] = "Back"
|
self.user_settings_menu[0] = "Back"
|
||||||
self.user_settings_menu[1] = "View account info"
|
self.user_settings_menu[1] = "View account info"
|
||||||
self.user_settings_menu[2] = "Change username"
|
self.user_settings_menu[2] = "Show recent logs"
|
||||||
self.user_settings_menu[3] = "Change password"
|
self.user_settings_menu[3] = "Change username"
|
||||||
self.user_settings_menu[4] = "DELETE ACCOUNT"
|
self.user_settings_menu[4] = "Change password"
|
||||||
|
self.user_settings_menu[5] = "DELETE ACCOUNT"
|
||||||
|
|
||||||
self.opened_logs = False
|
self.opened_logs = False
|
||||||
|
|
||||||
@ -183,6 +184,9 @@ class MenuHelper:
|
|||||||
case "Back":
|
case "Back":
|
||||||
return
|
return
|
||||||
|
|
||||||
|
case "Show recent logs":
|
||||||
|
taskHelper.displayLogs(self)
|
||||||
|
|
||||||
case "View account info":
|
case "View account info":
|
||||||
self.user.printAccountInfo()
|
self.user.printAccountInfo()
|
||||||
|
|
||||||
|
@ -1,6 +1,35 @@
|
|||||||
from helpers import UtilityHelper as utilityHelper
|
from helpers import UtilityHelper as utilityHelper
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
def displayUnreadLogs(self):
|
||||||
|
logs = self.db.getUreandLogs
|
||||||
|
|
||||||
|
if len(logs) == 0:
|
||||||
|
return False
|
||||||
|
|
||||||
|
printLogs(logs)
|
||||||
|
|
||||||
|
for log in logs:
|
||||||
|
self.db.updateLog(log[0])
|
||||||
|
|
||||||
|
def displayLogs(self):
|
||||||
|
logs = self.db.getLatestLogs()
|
||||||
|
if len(logs) == 0:
|
||||||
|
print(f"{utilityHelper.warningMessage('No logs found')}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
printLogs(logs)
|
||||||
|
|
||||||
|
def printLogs(logs):
|
||||||
|
for log in logs:
|
||||||
|
print(f"------------------{log[0]}---------------------")
|
||||||
|
print(f"Log Date:")
|
||||||
|
print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(log[2]))}")
|
||||||
|
print(f"Log Message:")
|
||||||
|
print(f"{log[3]}")
|
||||||
|
|
||||||
|
print(f"------------------END---------------------")
|
||||||
|
|
||||||
def getBalance(self):
|
def getBalance(self):
|
||||||
balance = 0
|
balance = 0
|
||||||
blocks = utilityHelper.loadFile("../data/ledger.dat")
|
blocks = utilityHelper.loadFile("../data/ledger.dat")
|
||||||
|
@ -2,6 +2,7 @@ import pickle
|
|||||||
import hashlib
|
import hashlib
|
||||||
import os
|
import os
|
||||||
from helpers import BlockHelper as blockHelper
|
from helpers import BlockHelper as blockHelper
|
||||||
|
from helpers import TaskHelper as taskHelper
|
||||||
|
|
||||||
class bcolors:
|
class bcolors:
|
||||||
HEADER = '\033[95m'
|
HEADER = '\033[95m'
|
||||||
@ -69,6 +70,7 @@ def loginStartup(self):
|
|||||||
# TODO - check if there are unvalidated blocks
|
# TODO - check if there are unvalidated blocks
|
||||||
blockHelper.validateMinedBlock(self)
|
blockHelper.validateMinedBlock(self)
|
||||||
# TODO - check if there are any notifcations
|
# TODO - check if there are any notifcations
|
||||||
|
taskHelper.displayUnreadLogs(self)
|
||||||
|
|
||||||
def errorMessage(text):
|
def errorMessage(text):
|
||||||
return bcolors.FAIL + text + bcolors.ENDC
|
return bcolors.FAIL + text + bcolors.ENDC
|
||||||
|
Loading…
x
Reference in New Issue
Block a user