def new_transaction(client, payload): transaction = json.loads(payload) transaction = Transaction.create_object(transaction) blockchain.add_transaction(transaction) count = 0 mine_count = 0 for tx in blockchain.get_open_transactions(): if tx.sender != SUN.PUBLIC_KEY: count += 1 else: mine_count += 1 if count >= 3 or mine_count >= 10: node_id, proof = blockchain.proof_of_stake() if node_id == blockchain.public_key: mined_block = blockchain.mine_block() signature = wallet.sign_block(mined_block) mined_block.add_signature(signature) mined_block.transactions = [ tx.__dict__ for tx in mined_block.transactions ] mined_block = mined_block.__dict__ client.publish("new_block", json.dumps(mined_block), qos=2) if count >= 5 or mine_count >= 15: proof += 10 helper_trans = Transaction(SUN.PUBLIC_KEY, SUN.PUBLIC_KEY, proof, "MINING FIX") signature = wallet.sign_transaction(helper_trans) helper_trans.add_signature(signature) client.publish("new_transaction", json.dumps(helper_trans), qos=2)
def sign_transaction(): requested_object = request.get_json() wall = requested_object['wallet'] wallet_id = wall['wallet_id'] private_key = wall['private_key'] public_key = wall['public_key'] wallet = Wallet(wallet_id, private_key, public_key) trans = requested_object['transaction'] sender = trans['sender'] recipient = trans['recipient'] amount = trans['amount'] description = trans['description'] transaction = Transaction(sender, recipient, amount, description) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) response = transaction.__dict__ return jsonify(response), 200
client.on_connect = on_connect #client.on_message = on_message #client.on_disconnect = on_disconnect client.connect(MQTT.SERVER, int(MQTT.SSL_PORT)) # client.loop_forever() def on_connect(client, userdata, flags, rc): client.publish("new_transaction", ) t2 = Transaction(SUN.PUBLIC_KEY, wallet1.public_key, 50) t1 = Transaction(wallet1.public_key, wallet2.public_key, 20, "Ceva") signature = wallet1.sign_transaction(t1) t1.add_signature(signature) t1 = Transaction("Eu", "tu", 200) t2 = Transaction("El", "ea", 100) t3 = Transaction("EU", "mie", 50) b1 = Block(1, "eu", 100, [t1, t2], 'ceva') b2 = Block(1, "EU", 50, [t3], 'cevass') chain = [b1, b2] # # print(json.dumps(b1.__dict__)) # [block.__dict__ for block in chain] # block.transactions = [tx.__dict__ for tx in block.transactions] for block # b1.transactions = [tx.__dict__ for tx in b1.transactions] # print(json.dumps(b1.__dict__)) new_chain = []