def GetCandidateBlocks(): """ Collects candidate blocks and candidate block hashes from connected unl nodes and returns them in the candidate_block class """ nodes = get_as_node_type(get_unl_nodes()) the_candidate_blocks = [] the_candidate_block_hashes = [] for node in nodes: if node.candidate_block: already_in_list_candidate_blocks = False for other_blocks in the_candidate_blocks[:]: if other_blocks["signature"] == node.candidate_block["signature"]: already_in_list_candidate_blocks = True if not already_in_list_candidate_blocks: the_candidate_blocks.append(node.candidate_block) if node.candidate_block_hash: already_in_list_candidate_block_hashes = False for other_block_hashes in the_candidate_block_hashes[:]: if ( other_block_hashes["signature"] == node.candidate_block_hash["signature"] ): already_in_list_candidate_block_hashes = True if not already_in_list_candidate_block_hashes: the_candidate_block_hashes.append(node.candidate_block_hash) return candidate_block(the_candidate_blocks, the_candidate_block_hashes)
def test_node_connection_saving_and_unl(self): temp_private_key_class = Wallet_Create() node_1 = ndstart("127.0.0.1", 10001) temp_private_key = temp_private_key_class.toPem() saved_wallets = get_saved_wallet() for each_wallet in saved_wallets: if temp_private_key == saved_wallets[each_wallet]["privatekey"]: Wallet_Delete(each_wallet) temp_private_key_class2 = Wallet_Create() node_2 = ndstart("127.0.0.1", 10002) temp_private_key2 = temp_private_key_class2.toPem() saved_wallets2 = get_saved_wallet() for each_wallet in saved_wallets2: if temp_private_key2 == saved_wallets2[each_wallet]["privatekey"]: Wallet_Delete(each_wallet) ndconnect("127.0.0.1", 10001) finded_node = False in_unl_list = False get_as_node = False nodes_list = get_connected_node() for element in nodes_list: if element == node_1.id or element == node_2.id: finded_node = True save_new_unl_node(node_1.id) save_new_unl_node(node_2.id) temp_unl_node_list = get_unl_nodes() temp_get_as_node_type = get_as_node_type(temp_unl_node_list) for unl_element in temp_unl_node_list: if unl_element == node_1.id or unl_element == node_2.id: for node_element_of_unl in temp_get_as_node_type: if node_1.host == node_element_of_unl.host or node_2 == node_element_of_unl.host: if node_1.port == node_element_of_unl.port or node_2 == node_element_of_unl.port: get_as_node = True in_unl_list = True unl_node_delete(unl_element) connected_node_delete(element) node_1.stop() node_2.stop() self.assertEqual(finded_node, True, "Problem on connection saving system.") self.assertEqual(in_unl_list, True, "Problem on UNL node saving system.") self.assertEqual(get_as_node, True, "Problem on UNL get as node system.")
def GetCandidateBlocks(): nodes = get_as_node_type(get_unl_nodes()) the_candidate_blocks = [] the_candidate_block_hashes = [] for node in nodes: if node.candidate_block: the_candidate_blocks.append(node.candidate_block) if node.candidate_block_hash: the_candidate_block_hashes.append(node.candidate_block_hash) return candidate_block(the_candidate_blocks, the_candidate_block_hashes)
def __init__(self, sequance_number, signature, fromUser, toUser, data, amount, transaction_fee): self.sequance_number = sequance_number self.signature = signature self.fromUser = fromUser self.toUser = toUser self.data = data self.amount = amount self.transaction_fee = transaction_fee self.valid = [] from node.unl import get_unl_nodes self.total_validators = get_unl_nodes() self.already_asked_nodes = [] self.invalid = []
def propagating_the_tx(self, tx): """ Sends the given transaction to UNL nodes. """ items = { "transactionrequest": 1, "sequance_number": tx.sequance_number, "signature": tx.signature, "fromUser": tx.fromUser, "to_user": tx.toUser, "data": tx.data, "amount": tx.amount, "transaction_fee": tx.transaction_fee, "transaction_time":tx.time } for each_node in get_as_node_type(get_unl_nodes()): mynode.main_node.send_data_to_node(each_node, items)
def get_ledger_from_other_node(): from node.myownp2pn import MyOwnPeer2PeerNode from node.unl import get_unl_nodes, get_as_node_type node = MyOwnPeer2PeerNode.main_node unl_list = get_as_node_type(get_unl_nodes()) node.send_to_node(unl_list[0], "sendmefullledger")
def sendme_full_node_list(): from node.myownp2pn import MyOwnPeer2PeerNode from node.unl import get_unl_nodes, get_as_node_type node = MyOwnPeer2PeerNode.main_node unl_list = get_as_node_type(get_unl_nodes()) node.send_to_node(unl_list[0], "sendmefullnodelist")
def reset_the_block(self): """ When the block is verified and if block have a transaction and if block have at least half of the max_tx_number transaction,it saves the block and makes the edits for the new block. """ if (self.validated_time - self.start_time) >= self.block_time: self.block_time += 0.2 else: self.block_time -= 0.2 #Printing validated block. dprint("""\n _____ _ ____ _ ____ _____ _ __ / ____| | | | _ \| | / __ \ / ____| |/ / | | _ _ _ __ _ __ ___ _ __ | |_ | |_) | | | | | | | | ' / | | | | | | '__| '__/ _ \ '_ \| __| | _ <| | | | | | | | < | |___| |_| | | | | | __/ | | | |_ | |_) | |___| |__| | |____| . \ \_____\__,_|_| |_| \___|_| |_|\__| |____/|______\____/ \_____|_|\_\ """+str(self.__dict__)+"\n") self.start_time = int(time.time()) self.raund_1_starting_time = None self.raund_1 = False self.raund_1_node = False self.raund_2_starting_time = None self.raund_2 = False self.raund_2_node = False self.validated = False # Resetting the node candidate blocks. for node in get_as_node_type(get_unl_nodes()): node.candidate_block = None node.candidate_block_hash = None if not len(self.validating_list) == 0 and not len(self.validating_list) < (self.max_tx_number / 2): app_tigger(self) saveBlockstoBlockchainDB(self) # Resetting and setting the new elements. self.previous_hash = self.hash self.sequance_number = self.sequance_number + 1 self.validating_list = [] self.hash = None #Printing new block. dprint("""\n _ _ ____ _ ____ _____ _ __ | \ | | | _ \| | / __ \ / ____| |/ / | \| | _____ __ | |_) | | | | | | | | ' / | . ` |/ _ \ \ /\ / / | _ <| | | | | | | | < | |\ | __/\ V V / | |_) | |___| |__| | |____| . \ |_| \_|\___| \_/\_/ |____/|______\____/ \_____|_|\_\ """+str(self.__dict__)+"\n") else: self.empty_block_number += 1 # Adding self.pendingTransaction to the new/current block. PendinttoValidating(self) # Saving the new block. self.save_block()
def GetBlockFromOtherNode(): from node.myownp2pn import mynode from node.unl import get_unl_nodes, get_as_node_type node = mynode.main_node unl_list = get_as_node_type(get_unl_nodes()) node.send_data_to_node(unl_list[0], "sendmefullblock")
def GetNodeList(): node = mynode.main_node unl_list = get_as_node_type(get_unl_nodes()) node.send_data_to_node(unl_list[0], "sendmefullnodelist")
def consensus_round_2(block): """ At this stage of the consensus process, The blocks (block hashes) of our and the unl nodes are compared and the block accepted by 80 percent is approved. Inputs: * block: The block (class) we want consensus round 2 to be done """ unl_nodes = get_unl_nodes() if not block.raund_2_node: dprint("Raund 2: in get candidate block hashes\n") mynode.main_node.send_my_block_hash(get_as_node_type(unl_nodes)) block.raund_2_node = True block.save_block() candidate_class = GetCandidateBlocks() dprint("Raund 2 Conditions") dprint( len(candidate_class.candidate_block_hashes) > ( (len(unl_nodes) * 80) / 100)) dprint( (int(time.time()) - block.raund_2_starting_time) < block.raund_2_time) if len(candidate_class.candidate_block_hashes) > ( (len(unl_nodes) * 80) / 100): if not (int(time.time()) - block.raund_2_starting_time) < block.raund_2_time: dprint("Raund 2: first ok") for candidate_block in candidate_class.candidate_block_hashes[:]: tx_valid = 0 if block.hash == candidate_block["hash"]: tx_valid += 1 for other_block in candidate_class.candidate_block_hashes[:]: if candidate_block != other_block: if candidate_block["hash"] == other_block["hash"]: tx_valid += 1 if tx_valid > ((len(unl_nodes) * 80) / 100): dprint("Raund 2: second ok") if block.hash == candidate_block["hash"]: block.validated = True block.validated_time = int(time.time()) block.raund_2 = True else: print("Raund 2: my block is not valid") node = mynode.main_node unl_list = get_as_node_type( [candidate_block["sender"]]) node.send_data_to_node(unl_list[0], "sendmefullblock") block.dowload_true_block = candidate_block["sender"] block.save_block() else: if not block.decrease_the_time_2 == 3: block.decrease_the_time_2 += 1 block.increase_the_time_2 = 0 block.save_block() else: if not (int(time.time()) - block.raund_2_starting_time) < block.raund_2_time: if not block.increase_the_time_2 == 3: block.increase_the_time_2 += 1 block.decrease_the_time_2 = 0 block.save_block()
def consensus_round_1(block): """ At this stage of the consensus process, The transactions of our and the unl nodes are evaluated and transactions accepted by owned by more than 50 percent. Inputs: * block: The block (class) we want consensus round 1 to be done """ unl_nodes = get_unl_nodes() if not block.raund_1_node: dprint("Raund 1: in get candidate blocks\n") mynode.main_node.send_my_block(get_as_node_type(unl_nodes)) block.raund_1_node = True block.save_block() candidate_class = GetCandidateBlocks() dprint("Raund 1 Conditions") dprint( len(candidate_class.candidate_blocks) > ((len(unl_nodes) * 80) / 100)) dprint( (int(time.time()) - block.raund_1_starting_time) < block.raund_1_time) if len(candidate_class.candidate_blocks) > ((len(unl_nodes) * 80) / 100): if len(candidate_class.candidate_blocks) == len(unl_nodes) or not ( int(time.time()) - block.raund_1_starting_time) < block.raund_1_time: temp_validating_list = [] dprint("Raund 1: first ok") dprint(len(candidate_class.candidate_blocks)) for candidate_block in candidate_class.candidate_blocks[:]: for other_block_tx in candidate_block["transaction"]: tx_valid = 0 for my_txs in block.validating_list: if other_block_tx.signature == my_txs.signature: tx_valid += 1 if len(candidate_class.candidate_blocks) != 1: dprint("Raund 1: Test tx") for other_block in candidate_class.candidate_blocks[:]: if candidate_block["signature"] != other_block[ "signature"]: dprint("Raund 1: Test tx 2") for other_block_txs in other_block[ "transaction"]: if other_block_tx.signature == other_block_txs.signature: dprint("Raund 1: Test tx 3") tx_valid += 1 else: tx_valid += 1 if tx_valid > (len(unl_nodes) / 2): dprint("Raund 1: second ok") already_in_ok = False for alrady_tx in temp_validating_list[:]: if other_block_tx.signature == alrady_tx.signature: already_in_ok = True if not already_in_ok: dprint("Raund 1: third ok") temp_validating_list.append(other_block_tx) for my_validating_list in block.validating_list[:]: ok = False for my_temp_validating_list in temp_validating_list[:]: if my_validating_list.signature == my_temp_validating_list.signature: ok = True block.validating_list.remove(my_validating_list) if not ok: block.createTrans(my_validating_list.sequance_number, my_validating_list.signature, my_validating_list.fromUser, my_validating_list.toUser, my_validating_list.transaction_fee, my_validating_list.data, my_validating_list.amount, transaction_sender=None, transaction_time=my_validating_list.time) block.validating_list = temp_validating_list block.raund_1 = True block.raund_2_starting_time = int(time.time()) ProccesstheTransaction(block) block.hash = CalculateHash(block) block.save_block()
def reset_the_block(self): """ When the block is verified and if block have a transaction and if block have at least half of the max_tx_number transaction,it saves the block and makes the edits for the new block. """ if self.increase_the_time == 3: self.increase_the_time = 0 self.raund_1_time += 0.1 self.block_time_change_time = int(time.time()) self.block_time_change_block = self.sequance_number if self.decrease_the_time == 3: self.decrease_the_time = 0 if not self.raund_1_time <= 2: self.raund_1_time -= 0.1 self.block_time_change_time = int(time.time()) self.block_time_change_block = self.sequance_number if self.increase_the_time_2 == 3: self.increase_the_time_2 = 0 self.raund_2_time += 0.1 self.block_time_change_time = int(time.time()) self.block_time_change_block = self.sequance_number if self.decrease_the_time_2 == 3: self.decrease_the_time_2 = 0 if not self.raund_2_time <= 2: self.raund_2_time -= 0.1 self.block_time_change_time = int(time.time()) self.block_time_change_block = self.sequance_number self.block_time = self.raund_1_time + self.raund_2_time #Printing validated block. dprint("""\n _____ _ ____ _ ____ _____ _ __ / ____| | | | _ \| | / __ \ / ____| |/ / | | _ _ _ __ _ __ ___ _ __ | |_ | |_) | | | | | | | | ' / | | | | | | '__| '__/ _ \ '_ \| __| | _ <| | | | | | | | < | |___| |_| | | | | | __/ | | | |_ | |_) | |___| |__| | |____| . \ \_____\__,_|_| |_| \___|_| |_|\__| |____/|______\____/ \_____|_|\_\ """ + str(self.__dict__) + "\n") self.start_time = int(time.time()) self.raund_1_starting_time = None self.raund_1 = False self.raund_1_node = False self.raund_2_starting_time = None self.raund_2 = False self.raund_2_node = False self.validated = False # Resetting the node candidate blocks. for node in get_as_node_type(get_unl_nodes()): node.candidate_block = None node.candidate_block_hash = None if not len(self.validating_list) == 0 and not len( self.validating_list) < (self.max_tx_number / 2): app_tigger(self) my_address = Wallet_Import(-1, 3) for tx in self.validating_list: if tx.toUser == my_address: SavetoMyTransaction(tx) saveBlockstoBlockchainDB(self) # Resetting and setting the new elements. self.previous_hash = self.hash current_blockshash_list = GetBlockshash() current_blockshash_list.append(self.previous_hash) SaveBlockshash(current_blockshash_list) self.sequance_number = self.sequance_number + 1 self.validating_list = [] self.hash = None #Printing new block. dprint("""\n _ _ ____ _ ____ _____ _ __ | \ | | | _ \| | / __ \ / ____| |/ / | \| | _____ __ | |_) | | | | | | | | ' / | . ` |/ _ \ \ /\ / / | _ <| | | | | | | | < | |\ | __/\ V V / | |_) | |___| |__| | |____| . \ |_| \_|\___| \_/\_/ |____/|______\____/ \_____|_|\_\ """ + str(self.__dict__) + "\n") else: self.empty_block_number += 1 # Adding self.pendingTransaction to the new/current block. PendinttoValidating(self) # Saving the new block. self.save_block()