def receive_tx(): print(request.form) tx_data = request.json tx = Transaction.from_json(tx_data) if tx.verify(): mempool.push_transaction(tx) return jsonify('received') else: raise NotImplementedError("Not sure what to do if tx is not valid")
def from_json(cls, json_data): try: decoded = json.loads(json_data) except Exception as e: print(e) try: decoded_trans = json.loads(decoded['transactions']) transactions = [] for k in decoded_trans: transactions += [Transaction.from_json(decoded_trans[k])] return cls(decoded['index'], decoded['prev_hash'], None, transactions, decoded['root'], decoded['timestamp'], decoded['nonce']) except KeyError as ke: print(ke)
def read_json(filename): # function that opens and reads a json file with open(filename, "r") as file: if file.mode == "r": # checks that file is open in read mode content = read_open_file(filename, file) rows = re.findall( r"""".+": "(\d{4}-\d{2}-\d{2})",\n {4}".+": "(.+ ?.*)",\n {4}".+": "(.+ ?.*)",\n {4}".+": "(.+ ?.*)",\n {4}".+": (\d+.?\d*)""", content) # regex search that groups data from each column transactions = [] for json_transaction in rows: transactions.append(Transaction.from_json( json_transaction)) # formats data into standard internal # transaction class format logging.info(filename + " has been read and closed.") return transactions
def put_transaction(self, transaction): # create a Transaction object from the json data, it also validates it t = Transaction.from_json(transaction) if not self.miner.t_already_there(t): self.upcoming_transactions.append(t) return (True, 'Transaction added')