def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): """Append a new value as well as the last blockchain value to the blockchain. Arguments: :sender: The sender of the coins :recipient: The recipient of the coins :amount: The amount of coins sent with the transaction (default = 1.0) """ if self.__public_key is None: return False transaction = Transaction(sender, recipient, signature, amount) if verifier.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature }) if response.status_code == 400 or response.status_code == 500: print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False) : """ Append a new value as well as the last blockchain value to the blockchain. Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :amount: The amount of coins sent with the transaction (default = 1.0) """ # transaction = { # 'sender': sender, # 'recipient': recipient, # 'amount': amount # } # if self.public_key == None: # return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature}) if response.status_code == 400 or response.status_code == 500: print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = f"http://{node}/broadcast-transaction" try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'signature': signature, 'amount': amount }) if (response.status_code == 400 or response.status_code == 500): print('Transaction declined, needs resolving') except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): """ Return the last value of the current blockchain. Arguments: :sender: The sender of the coins :recipient: The recipient of the coins :amount: Amount of coins. Default to 1.0 """ transaction = Transaction(sender, recipient, amount, signature) if not Verification.verify_transaction(transaction, self.get_balance): raise TransactionError() self.__open_tansactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: try: self.__broadcast_transaction__(node, transaction) except ( requests.exceptions.ConnectionError, TransactionError) as error: print(error) continue return transaction
def add_transaction(self, recipient, sender, signature, amount=1.0): """ Append a new value as well as the last blockchain value to the blockchain Arguments: :sender: The sender of the coins :recipient: The recipient of the coins :amount: The amount of the coins sent with the transaction (default 1.0) """ # transaction = { # 'sender': sender, # 'recipient': recipient, # 'amount': amount # } # transaction = Transaction(sender, recipient, amount) # if Verification.verify_transaction(transaction, self.get_balance): # self.open_transactions.append(transaction) # self.save_data() # return True # return False if self.hosting_node == None: return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, amount = 1.0): """Append a new value as well as the last blockchain value to the blockchain Arguments: :Sender : the sender of the coins :Recipient : the recipient of the coins :Amount : the amount of coins sent (default = 1 coin) """ #transaction = { # 'Sender': sender, # 'Recipient': recipient, # 'Amount': amount #} transaction = Transaction(sender, recipient, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): """ Add a transaction to the blockchain and broadcasts the transactions into the network. :param sender str: the sender's name of the transaction :param recipient str: the recipient's name of the transaction :param signature str: Signature of the transaction. :param amount: the amount of the transaction, Default=1.0. :param is_receiving: False when creating a new transaction on this node, True when receiving a broadcast transaction :returns: True if transaction if verified, False if not. :raises ConnectionError: If the POST is not sent properly. """ transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: # Looping through all nodes to broadcast the infos: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature}) if response.status_code == 400 or response.status_code == 500: print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True else: return False
def add_transaction(self, recipient, sender, signature, amount = 1.0): """ Append a new value as well as the last blockchain value to the blockchain. Arguements: :sender :The sender of the coins. :recipient: The recipient of the coins :Amount : The amount of coins sent with the transaction (default is 1.0). """ # transaction = { # 'sender': sender, # 'recipient': recipient, # 'amount': amount # } if self.hosting_node == None: return False transaction = Transaction(sender, recipient, signature, amount) # if not Wallet.verify_transaction(transaction): # return False # transaction = OrderedDict( # [('sender', sender), ('recipient', recipient), ('amount', amount )]) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) # participants.add(sender) # participants.add(recipient) self.save_data() return True return False
def add_transaction( self, recipient, sender, signature, amount=1.0, ): """ Append a new value as well as the last blockchain value to the blockchain. Arguments: :sender: The sender of the coins that should be addeed. :recipient: The recipient of the coins :param: amount: The amount of coins sent with the transaction (default = 1.0) """ print("NODEEE, ", self.hosting_node) if self.hosting_node == None: return False # transaction = {"sender": sender, "recipient": recipient, "amount": amount} transaction = Transaction(sender, recipient, signature, amount) #transaction = OrderedDict([("sender", sender), ("recipient", recipient), ("amount", amount)]) if (Verification.verify_transaction(transaction, self.get_balance)): self.__open_transactions.append(transaction) #partecipants.add(sender) #partecipants.add(recipient) self.save_data() return True return False
def add_transaction(self, transaction): """ Add a new transaction to the open_transactions. """ if transaction.sender == SUN.PUBLIC_KEY: if Verification.verify_transaction(transaction, self.get_balance, False): self.__transactions.append(transaction) self.save_data() return True else: return False else: if Verification.verify_transaction(transaction, self.get_balance): self.__transactions.append(transaction) self.save_data() return True else: return False
def add_transaction(self, recipient, sender, amount=1.0): transaction = Transaction(sender, recipient, amount) if Verification.verify_transaction(transaction, self.get_balances): self.__open_transaction.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, amount=1.0): """ Append a new value as well as the last blockchain value to the blockchain. Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :amount: The amount of coins sent with the transaction (default = 1.0) """ # We will be using a dictionary for transactions as well, not to be confused with a blockchain block # transaction = { # 'sender': sender, # 'recipient': recipient, # 'amount': amount # } transaction = Transaction(sender, recipient, amount) # transaction = OrderedDict( # [('sender', sender), ('recipient', recipient), ('amount', amount)]) # This is where we pass the transaction to the next function if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) # participants.add(sender) # participants.add(recipient) print('Open transactions: ', self.__open_transactions) self.save_data() return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): # transaction = { # 'sender': sender, # 'recipient': recipient, # 'amount': amount # } # if self.public_key == None: # return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature}) if response.status_code == 400 or response.status_code == 500: print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, sender, recipient, public_key, signature, amount = 1.0, is_receiving = False): # transaction = { # "sender" : sender, # "recipient": recipient, # "amount": amount} transaction = Transaction(sender, recipient, signature, amount) if not Wallet.verify_transaction(transaction): print("Invalid signature.") return False if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.participants.add(sender) self.participants.add(recipient) self.save_data() if not is_receiving: # Broadcasting the new open transaction to other peer nodes for node in self.__peer_nodes: url = "http://{}/broadcast_transaction".format(node) try: response = requests.post(url, json = {"sender": sender, "recipient" : recipient, "amount" : amount, "signature" : signature}) if response.status_code == 400 or response.status_code == 500: print("Transaction declined. ") return False except requests.exceptions.ConnectionError: print("Connection failed with node : %s"%node) continue return True return False
def add_transaction(self, sender, recipient, signature, amount=1, is_receiving=False): """ Arguments: :sender: The sender of the coins :recipient: The recipient of the coins :amount: Amount of coins sent """ # default argument """ transaction = { 'sender': sender, 'recipient': recipient, 'amount': amount } """ # Initializing a dictionary # if self.public_key == None: #Public key is stored in the hosting node # return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={'sender': sender, 'recipient': recipient, 'amount': amount, 'signature':signature}) if response.status_code == 400 or response.status_code == 500: print('Transaction declined. Needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, receipent, sender, signature, amount=1.0, is_receiving=False): """Add a Transaction""" # Add Transaction if self.node_id is None: return False transaction = Transaction(sender, receipent, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_file() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'receipent': receipent, 'amount': amount, 'signature': signature }) if response.status_code == 400 or response.status_code == 500: print('Transaction Declined') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0): if self.hosting_node == None: return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0): """ Append a new value as well as the last blockchain value to the blockchain""" if self.hosting_node == None: return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipent, sender, amount=1.0): '''append new value to your blockchain''' if self.hosting_node == None: return False transaction = Transaction(sender, recipent, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transaction.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, amount=1.0): """ Append a new value as well as the last blockchain value to the blockchain """ transaction = Transaction(sender, recipient, amount) # Passes the get_balance function to verifier if Verification.verify_transaction(transaction, self.get_balance): # No longer appending directly to the blockchain, but instead appending to the open_transaction list self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): """Adds transactions to the open_transactions dictionary Arguments: :sender: the sender of the transaction :recipient: the receiver of the transaction :signature: the signature of the transaction :amount: the value of the transaction (default 1.0) """ # As the order of Dictionaries can change we need to work with # imported Ordereddics package # The order is important to us for the SHA256 hash algorithm # It's defined as a list of tuples # transaction = OrderedDict( # [('sender', sender), ('recipient', recipient), ('amount', amount)]) # prevent adding transaction when no wallet loaded # if self.public_key == None: # return None transaction = Transaction(sender, recipient, amount, signature) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() # Broadcast transaction to all peer nodes as long it is NOT a # received broadcast if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature }) if response.status_code == 400 or response.status_code == 500: print('Transaction declined. Need resolving') return False # continue with next peer node if connection error (node not online) except requests.exceptions.ConnectionError: continue return transaction else: return None
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): """Append a new transaction Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :amount: The amount of coins to be sent (default = 1.0) :is_receiving: Ist die Node nur ein Empfänger? """ # Überprüfung, ob Wallet korrekt initialisiert. # Es kann sein, wenn eine Transaktion an eine andere Node gesendet wird, # und diese nicht korrekt initalisiert ist, dass er auch hier return false zurückgibt. # Fehler muss noch behoben werden!!! if self.public_key == None: return False transaction = Transaction(sender, recipient, signature, amount) # get_balance ist eine Funktion, ohne Klammern heißt sie wird als Referenz übergeben, wo sie dann # in verify_transaction aufgerufen werden kann. if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() # Hier folgt der Broadcast zu den anderen Nodes. # Soll nur durch die Node ausgelöst werden, welche die Transaction erstellt. # Ansonsten würde es zu einem Endlosloop kommen, da jede Node die Transaktion hinzufügen würde, # und sie dann wieder broadcasted. if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature }) if response.status_code == 400 or response.status_code == 500: print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_recieving=False): """ Append a new tranction Arguments: : sender: The sender of the coins : recipient: The recipient of the coins : amount: The amount of coins sent with the transaction (default = 1.0) : Use OrderedDict to control the order of our dictionary to avoid invalid hash coincidence """ # transaction = { # "sender": sender, # "recipient": recipient, # "amount": amount # } if self.public_key == None: return False transaction = Transaction(sender, recipient, signature, amount) ver = Verification.verify_transaction(transaction, self.get_balance) print("Verification: ", ver) if ver: self.__open_transactions.append(transaction) self.save_data() if not is_recieving: for node in self.__peer_nodes: try: url = f"http://{node}/broadcast-transaction" response = requests.post(url, json={ "sender": sender, "recipient": recipient, "amount": amount, "signature": signature }) if response.status_code == 400 or response.status_code == 500: print("Transaction declined, needs resolving") return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, amount, signature): """Append a new value as wekk as the last blockchain value to the blockchain Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :last_transaction: The amount of coins sent with the transaction (defaults [1])""" if self.hosting_node == None: return None transaction = Transaction(sender, recipient, amount, signature) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0): """ Add new transaction to the transaction queue. Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :amount: Amount of coins sent with transaction (default = 1.0). """ if self.hosting_node == None: return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=False): ''' Add new transaction to the open transaction list Arguments: :sender -> str | Info about sender of coins \n :recipient -> str | Info about recipient of coins \n :signature -> str | Signature of the tx \n :amount -> float | Transaction amount ''' #--- We don't need public_key on node-2 for sending tx---# # if self.public_key == None: # return False new_transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction(new_transaction, self.get_balance): self.__open_transactions.append(new_transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature }) if response.status_code == 400 or response.status_code == 500: print('Transaction Declined, needs Resolving!') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, sender, recipient, signature, amount=1.0, is_receiving=False): """ Add a new transaction to the list of open transactions Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :signature: The signature of the transaction. :amount: The amount of coins sent with the transaction (default = 1.0) """ # if self.public_key == None: # return False # OrderedDict remembers the order in which its contents are added transaction = Transaction(sender, recipient, signature, amount) # If the sender has sufficient balance to send the amount in # the transaction, then add to the transaction to mempool if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() # Also inform our peer nodes if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature }) if (response.status_code == 400 or response.status_code == 500): print('Transaction declined, needs resolving') return False # Continue for the other nodes if there is an error # Don't return False and end the whole set except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, time=0, is_receiving=False): """ Append a new value as well as the last blockchain value to the blockchain. Arguments: :sender: The sender of the coins. :recipient: The recipient of the coins. :signature: The signature of the transaction. :amount: The amount of coins sent with the transaction :time: The time when the transaction was made (generated with time()) (default = 1.0) """ session = Session() transaction = Transaction(sender, recipient, signature, amount, timed=time) session.add(transaction) if Verification.verify_transaction(transaction, self.get_balance): session.commit() session.close() self.load_data() if not is_receiving: for node in self.__peer_nodes: url = 'http://{}/broadcast-transaction'.format(node['id']) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature, 'time': time }) if (response.status_code == 400 or response.status_code == 500): print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, receiver, sender, signature, amount=1.0, is_receiving=False): """Append a new transaction to the list of open transactions. Arguments: --------- receiver : str The receiver of the coins. sender : str The sender of the coins. amount : str The amount of coins sent with the transaction (default = 1.0) signature : str Signature """ transaction = Transaction(sender, receiver, signature, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transactions.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_nodes: url = f"http://{node}/broadcast_transaction" try: response = requests.post( url, json={ "sender": sender, "receiver": receiver, "amount": amount, "signature": signature, }, ) if (response.status_code == 400 or response.status_code == 500): print("Transaction declined. Needs resolving.") return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, signature, amount=1.0, is_receiving=True): """ This function adds the user input to the blockchain default arguments should come at the end. Arguments: :sender: The sender of the coins :recipient: The recipient of the coins :amount: amount of coins transfered, default is 1 """ #We want to add the transaction to the open_transaction from where we will do mining and dictionary will be best in this scenario. # transaction = { # 'sender':sender, # 'recipient':recipient, # 'amount':amount # } if self.public_key == None: return False transaction = Transaction(sender, recipient, signature, amount) if Verification.verify_transaction( transaction, self.get_balance ): #as verify_transaction is returning True or False self.__open_transaction.append(transaction) self.save_data() if not is_receiving: for node in self.__peer_node: url = 'http://{}/broadcast-transaction'.format(node) try: response = requests.post(url, json={ 'sender': sender, 'recipient': recipient, 'amount': amount, 'signature': signature }) if response.status_code == 400 or response.status_code == 500: print('Transaction declined, needs resolving') return False except requests.exceptions.ConnectionError: continue return True return False
def add_transaction(self, recipient, sender, amount=1.0): """ Append a new value and the last blockchain value to the blockchain Arguments: :transaction_amount: The ampount that shuld be added. :last_transaction: The last blockchain transaction (default[1]). """ # transaction = { # 'sender': sender, # 'recipient': recipient, # 'amount': amount # } transaction = Transaction(sender, recipient, amount) if Verification.verify_transaction(transaction, self.get_balance): self.__open_transaction.append(transaction) self.save_data() return True return False