Beispiel #1
0
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,
    )
Beispiel #2
0
    def __broadcast_block(self, block: Block) -> None:
        """
        Broadcast the current block to all nodes on the network that this node
        is aware of.

        This block has already been validated and approved

        This ensures synchronicity across all nodes on the network.
        """
        logger.debug("Broadcasting blocks to following nodes: %s", self.nodes)
        for node in self.nodes:
            url = f"{node}/broadcast-block"
            logger.debug("Broadcasting new block %s to %s", block, url)
            try:
                response = requests.post(
                    url, json={"block": block.SerializeToHex()})
                if response.status_code == 400 or response.status_code == 500:
                    logger.error("Block declined, needs resolving: %s",
                                 response.json())
            except requests.exceptions.ConnectionError:
                continue
Beispiel #3
0
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],
    )