def get_transaction(self, data, node): dprint("Getting the transactions") system = GetBlock() system.createTrans(sequance_number=data["sequance_number"], signature=data["signature"], fromUser=data["fromUser"], toUser=data["to_user"], data=data["data"], amount=data["amount"], transaction_fee=data["transaction_fee"], transaction_sender=node, transaction_time=data["transaction_time"])
def send(my_public_key, my_private_key, to_user, password, data=None, amount=None): """ The main function for sending the transaction. Inputs: * my_public_key: Sender's public key. * my_private_key: Sender's private key. * to_user: Receiver's address. * data: A text that can be written into the transaction. (Can be None) * amount: A int or float amount to be sent. (Can be None) """ my_public_key = "".join( [ l.strip() for l in my_public_key.splitlines() if l and not l.startswith("-----") ] ) system = GetBlock() sequance_number = GetSequanceNumber(my_public_key, system) + 1 # Get the current fee transaction_fee = system.transaction_fee tx_time = int(time.time()) the_tx = system.createTrans( sequance_number=sequance_number, signature=Ecdsa.sign( str(sequance_number) + str(my_public_key) + str(to_user) + str(data) + str(amount) + str(transaction_fee) + str(tx_time), PrivateKey.fromPem(my_private_key), ).toBase64(), fromUser=str(my_public_key), toUser=str(to_user), data=data, amount=amount, transaction_fee=transaction_fee, transaction_sender=None, transaction_time=tx_time, ) SavetoMyTransaction(the_tx)
def print_balance(): """ Prints the current wallet balance. """ balance = GetBalance(Wallet_Import(-1, 0), GetBlock()) print(balance) return balance
def send_the_coin(receiver, temp_coin_amount, password): try: temp_coin_amount = float(temp_coin_amount) except ValueError: print("This is not float coin amount.") return False if not temp_coin_amount < GetBlock().minumum_transfer_amount: if Wallet_Import(int(the_settings()["wallet"]), 2) == sha256(password.encode("utf-8")).hexdigest(): send_coin(float(temp_coin_amount), receiver, password) else: print("Password is not correct")
def sent_the_coins(self, widget): from transactions.send_coin import send_coin from blockchain.block.get_block import GetBlock text_list = self.get_send_coin_dialog_text() receiver_adress = text_list[1] amount = text_list[0] if not float(amount) < GetBlock().minumum_transfer_amount: send_coin(float(amount), receiver_adress) self.send_coin_dialog.dismiss()
def consensus_trigger(): """ Consensus process consists of 2 stages. This function makes the necessary redirects according to the situation and works to shorten the block time. """ dprint("Consensus Trigger") block = GetBlock() if block.validated: true_time = (block.block_time_change_time + block.block_time + ((block.block_time_change_block - block.sequance_number) * block.block_time)) if block.newly: true_time -= 1 if not int(time.time()) < true_time: block.newly = False block.reset_the_block() else: if block.raund_1_starting_time is None: block.raund_1_starting_time = int(time.time()) if not block.raund_1: consensus_round_1(block) elif not block.raund_2: consensus_round_2(block)
def Status(): first_block = GetBlock() start_time = time.time() while True: time.sleep(5) new_time = time.time() new_block = GetBlock() difference = int(new_time - start_time) if not (first_block.sequance_number + first_block.empty_block_number) == ( new_block.sequance_number + new_block.empty_block_number): if difference <= 6: return "Good" elif difference <= 10: return "Not bad" elif difference <= 20: return "Bad" elif difference >= 20: return "Very bad" else: if difference >= 20: return "Not work"
def get_full_chain(self, data, node): get_ok = False if not os.path.exists(TEMP_BLOCK_PATH): get_ok = True else: system = GetBlock() if node.id == system.dowload_true_block: get_ok = True if get_ok: if str(data["byte"]) == "end": os.rename(LOADING_BLOCK_PATH, TEMP_BLOCK_PATH) from blockchain.block.block_main import apps_starter from consensus.consensus_main import consensus_trigger from lib.perpetualtimer import perpetualTimer from app.app_main import apps_starter system = GetBlock() system.newly = True system.change_transaction_fee() system.exclude_validators = [] dprint(system.sequance_number) perpetualTimer(system.consensus_timer, consensus_trigger).start() apps_starter() system.save_block() else: file = open(LOADING_BLOCK_PATH, "ab") file.write((data["byte"].encode(encoding='iso-8859-1'))) file.close()
def CreateBlock(): """ If test mode is on, creates genesis block and send the connected nodes, if it is off, it calls get_block() function. """ if the_settings()["test_mode"]: dprint("Creating the genesis block") Block(0, Wallet_Import(0, 3)) mynode.main_node.send_full_accounts() mynode.main_node.send_full_chain() else: dprint("Getting block from nodes") GetBlock()
def get_full_accounts(self, data, node): get_ok = False if not os.path.exists(TEMP_ACCOUNTS_PATH): get_ok = True else: system = GetBlock() if node.id == system.dowload_true_block: get_ok = True if get_ok: file = open(TEMP_ACCOUNTS_PATH, "ab") file.write((data["byte"].encode(encoding='iso-8859-1'))) file.close()
def GetBalance(user, block): balance = -GetBlock().minumum_transfer_amount user = "".join([ l.strip() for l in user.splitlines() if l and not l.startswith("-----") ]) user = Address(user) for Accounts in GetAccounts(): if Accounts.Address == user: balance += Accounts.balance break for trans in block.pendingTransaction + block.validating_list: if user == trans.fromUser: balance -= trans.amount + trans.transaction_fee return balance
def get_candidate_block_hash(self, data, node): dprint("Getting the candidate block hash") from node.unl import node_is_unl if node_is_unl(node.id) and GetBlock( ).sequance_number == data["sequance_number"]: dprint("is unl") if Ecdsa.verify( "myblockhash" + data["hash"] + str(data["sequance_number"]), Signature.fromBase64(data["signature"]), PublicKey.fromPem(node.id)): dprint("ecdsa true") data["sender"] = node.id node.candidate_block_hash = data
def sent_the_coins(self, widget): text_list = self.get_send_coin_dialog_text() receiver_adress = text_list[2] amount = text_list[1] if not float(amount) < GetBlock().minumum_transfer_amount: if Wallet_Import(int(the_settings()["wallet"]), 2) == sha256( text_list[0].encode("utf-8")).hexdigest(): send_coin(float(amount), receiver_adress, text_list[0]) else: SweetAlert().fire( "Password is not correct", type='failure', ) del text_list self.send_coin_dialog.dismiss()
def get_candidate_block(self, data, node): dprint("Getting the candidate block") from node.unl import node_is_unl if node_is_unl(node.id) and GetBlock( ).sequance_number == data["sequance_number"]: dprint("is unl") signature_list = [] for element in data["transaction"]: signature_list.append(element["signature"]) dprint("signature_list: " + str(signature_list)) merkle_root_of_signature_list = MerkleTree( signature_list).getRootHash( ) if len(signature_list) != 0 else "0" dprint("signatureverify: " + str( Ecdsa.verify("myblock" + merkle_root_of_signature_list, Signature.fromBase64(data["signature"]), PublicKey.fromPem(node.id)))) dprint("publickey from pem: " + str(node.id)) dprint("merkleroot: " + merkle_root_of_signature_list) if Ecdsa.verify( "myblock" + merkle_root_of_signature_list + str(data["sequance_number"]), Signature.fromBase64(data["signature"]), PublicKey.fromPem(node.id)): dprint("ecdsa true") temp_tx = [] for element in data["transaction"]: temp_tx.append(Transaction.load_json(element)) data["transaction"] = temp_tx node.candidate_block = data
def send_my_block_hash(self, nodes): system = GetBlock() if system.raund_1 and not system.raund_2: data = { "action": "myblockhash", "hash": system.hash, "sequance_number": system.sequance_number, "signature": Ecdsa.sign( "myblockhash" + system.hash + str(system.sequance_number), PrivateKey.fromPem(Wallet_Import(0, 1))).toBase64() } for each_node in nodes: dprint("Raund 2: second ok of get candidate block hashes: " + str(each_node.__dict__)) self.send_data_to_node(each_node, data)
def send_my_block(self, nodes): system = GetBlock() new_list = [] signature_list = [] for element in system.validating_list: new_list.append(element.dump_json()) signature_list.append(element.signature) dprint("signature_list: " + str(signature_list)) dprint("publickey from pem: " + str(Wallet_Import(0, 1))) Merkle_signature_list = MerkleTree( signature_list).getRootHash() if len(signature_list) != 0 else "0" dprint("\nmerkleroot: " + Merkle_signature_list) data = { "action": "myblock", "transaction": new_list, "sequance_number": system.sequance_number, "signature": Ecdsa.sign( "myblock" + Merkle_signature_list + str(system.sequance_number), PrivateKey.fromPem(Wallet_Import(0, 1))).toBase64() } for each_node in nodes: dprint("Raund 1: second ok of get candidate block: " + str(each_node.__dict__)) self.send_data_to_node(each_node, data)
def menu(): """ The main structure of the cli mode, this function prints the menu, listens to the entries, makes the directions. """ while True: show_menu() choices_input = question_maker(mode="main") if choices_input == "w": all_wallets = list(get_saved_wallet()) if not len(all_wallets) == 0: current_wallet = the_settings()["wallet"] for wallet in all_wallets: number = str(all_wallets.index(wallet)) address = Wallet_Import(all_wallets.index(wallet),3) if not current_wallet == number: print(menu_maker(menu_number=number, menu_text=address)) else: print(menu_maker(menu_number=number, menu_text=address + " - CURRENTLY USED")) while True: try: new_wallet = input("Please select wallet: ") if int(new_wallet) in list(range(len(all_wallets))): change_wallet(new_wallet) break else: print("There is no such wallet") except: print("This is not a number") else: print("There is no wallet") if choices_input == "connectmainnetwork": connect_to_main_network() if choices_input == "cw": Wallet_Create() if choices_input == "sc": temp_coin_amount = input("Coin Amount (ex. 1.0): ") type_control = False try: float(temp_coin_amount) type_control = True except: print("This is not float coin amount.") if type_control and not float(temp_coin_amount) < GetBlock().minumum_transfer_amount: send_coin(float(temp_coin_amount), input("Please write receiver adress: ")) if choices_input == "gb": print(GetBalance(Wallet_Import(-1,0), GetBlock())) if choices_input == "help": show_menu() if choices_input == "ndstart": ndstart(str(input("ip: ")), int(input("port: "))) if choices_input == "ndstop": ndstop() if choices_input == "ndconnect": ndconnect(str(input("node ip: ")), int(input("node port: "))) if choices_input == "ndconnectmixdb": ndconnectmixdb() if choices_input == "ndnewunl": save_new_unl_node(input("Please write ID of the node: ")) if choices_input == "testmodeon": test_mode(True) if choices_input == "testmodeoff": test_mode(False) if choices_input == "debugmodeon": debug_mode(True) # from node.myownp2pn import mynode # mynode.main_node.debug = True if choices_input == "debugmodeoff": debug_mode(False) # from node.myownp2pn import mynode # mynode.main_node.debug = False if choices_input == "getfullnodelist": GetNodeList() if choices_input == "getblock": if the_settings()["test_mode"]: CreateBlock() else: GetBlockFromOtherNode() if choices_input == "0": exit()
def menu(): """ The main structure of the cli mode, this function prints the menu, listens to the entries, makes the directions. """ while True: show_menu() choices_input = question_maker(mode="main") if choices_input == "connectmainnetwork": connect_to_main_network() if choices_input == "cw": Wallet_Create() if choices_input == "sc": temp_coin_amount = input("Coin Amount (ex. 1.0): ") type_control = False try: float(temp_coin_amount) type_control = True except: print("This is not float coin amount.") if type_control and not float(temp_coin_amount) < GetBlock().minumum_transfer_amount: send_coin(float(temp_coin_amount), input("Please write receiver adress: ")) if choices_input == "gb": print(GetBalance(Wallet_Import(0,0), GetBlock())) print(Wallet_Import(0,3)) if choices_input == "help": show_menu() if choices_input == "ndstart": ndstart(str(input("ip: ")), int(input("port: "))) if choices_input == "ndstop": ndstop() if choices_input == "ndconnect": ndconnect(str(input("node ip: ")), int(input("node port: "))) if choices_input == "ndconnectmixdb": ndconnectmixdb() if choices_input == "ndnewunl": save_new_unl_node(input("Please write ID of the node: ")) if choices_input == "testmodeon": test_mode(True) if choices_input == "testmodeoff": test_mode(False) if choices_input == "debugmodeon": debug_mode(True) # from node.myownp2pn import mynode # mynode.main_node.debug = True if choices_input == "debugmodeoff": debug_mode(False) # from node.myownp2pn import mynode # mynode.main_node.debug = False if choices_input == "getfullnodelist": GetNodeList() if choices_input == "getblock": if the_settings()["test_mode"]: CreateBlock() else: GetBlockFromOtherNode() if choices_input == "0": exit()
def reflesh_balance(self): self.text = "Balance: " + str( GetBalance(Wallet_Import(-1, 0), GetBlock()))