def broadcast_block(): # pylint: disable=unused-variable """ Broadcasts a new block to all the nodes Returns a status message Methods ----- POST Parameters ----- block : Block as hex Returns application/json ----- Return code : 201, 400, 409, 500 Response : message : str """ values = request.get_json() if not values: response = {"message": "No data found."} return jsonify(response), 400 if "block" not in values: response = {"message": "Some data is missing."} return jsonify(response), 400 block = Block.ParseFromHex(values["block"]) if block.index == blockchain.last_block.index + 1: added, message = blockchain.add_block(block) if added: response = {"message": "Block added"} return jsonify(response), 201 response = {"message": "Block seems invalid: " + message} return jsonify(response), 500 if block.index > blockchain.last_block.index: response = { "message": "Incoming block index higher than last block on current chain" } return jsonify(response), 500 response = { "message": "Blockchain seems to be shorter, block not added" } return jsonify(response), 409
def test_block_to_protobuf_and_back(): timestamp = datetime.utcfromtimestamp(0) transactions = [] header = Header( version=1, previous_hash="", timestamp=timestamp, transaction_merkle_root=get_merkle_root(transactions), difficulty=4, nonce=100, ) block = Block( index=0, block_hash="", size=0, header=header, transaction_count=len(transactions), transactions=transactions, ) p_block = block.SerializeToHex() assert p_block == "080010001a00220c080112001a002200280430642800" og_block = Block.ParseFromHex(p_block) assert og_block == Block( index=0, block_hash="", size=0, header=Header( version=1, previous_hash="", timestamp=timestamp, transaction_merkle_root=get_merkle_root(transactions), difficulty=4, nonce=100, ), transaction_count=len(transactions), transactions=transactions, )
def test_block_to_protobuf_and_back_with_transactions(): timestamp = datetime.utcfromtimestamp(0) transactions = [ FinalTransaction( transaction_hash="tx_hash_1", transaction_id="tx_hash_1", signed_transaction=SignedRawTransaction( details=Details( sender="test", recipient="test2", amount=4.5, nonce=0, timestamp=timestamp, public_key="pub_key", ), signature="sig", ), ) ] header = Header( version=1, previous_hash="", timestamp=timestamp, transaction_merkle_root=get_merkle_root( [t.signed_transaction for t in transactions]), difficulty=4, nonce=100, ) block = Block( index=0, block_hash="", size=0, header=header, transaction_count=len(transactions), transactions=[t.transaction_hash for t in transactions], ) p_block = block.SerializeToHex() assert ( p_block == "080010001a002260080112001a543061323330613034373436353733373431323035373436353733373433323139303030303030303030303030313234303230303032613030333230373730373536323566366236353739313230333733363936372200280430642801320974785f686173685f31" # noqa: E501 ) og_block = Block.ParseFromHex(p_block) assert og_block == Block( index=0, block_hash="", size=0, header=Header( version=1, previous_hash="", timestamp=timestamp, transaction_merkle_root=get_merkle_root( [t.signed_transaction for t in transactions]), difficulty=4, nonce=100, ), transaction_count=len(transactions), transactions=[t.transaction_hash for t in transactions], )