Exemplo n.º 1
0
    def test_serialization(self):
        """
        Tests that a message successfully serializes and deserializes. The deserialized object should have the same
        properties as the original one before it was serialized.
        """
        tx_hashes = ['A' * 64, 'B' * 64, 'C' * 64]
        original = TransactionRequest.create(tx_hashes)
        original_binary = original.serialize()
        clone = TransactionRequest.from_bytes(original_binary)  # deserialize byte object

        self.assertEqual(original.tx_hashes, clone.tx_hashes)
Exemplo n.º 2
0
    def test_tx_request_replies_with_correct_data(self):
        """
        Tests that a delegate who receives a TransactionRequest in Consensus state replies with the correct data
        """
        mock_sm = MagicMock()
        state = DelegateConsensusState(mock_sm)

        # Build a merkle tree and attach it to the state
        tx_objects = [build_test_transaction() for _ in range(5)]
        tx_blobs = [tx.serialize() for tx in tx_objects]
        tx_hashes = [Hasher.hash(blob) for blob in tx_blobs]
        merkle_tree = MerkleTree.from_raw_transactions(tx_blobs)

        state.merkle = merkle_tree

        # Build a TransactionRequest for a few of these transactions
        requested_hashes = tx_hashes[1:4]
        expected_blobs = tx_blobs[1:4]
        request = TransactionRequest.create(
            transaction_hashes=requested_hashes)

        # Finally, ensure the reply is what we expect it to be
        reply = state.handle_tx_request(request)

        self.assertEquals(reply.raw_transactions, expected_blobs)
Exemplo n.º 3
0
 def test_creation_fail(self):
     """
     Tests that a created block data request has the expected properties
     """
     tx_hashes = ['A' * 64, 'B' * 64, 'C' * 67]
     with self.assertRaises(Exception) as e:
         bdr = TransactionRequest.create(tx_hashes)
Exemplo n.º 4
0
 def test_creation(self):
     """
     Tests that a created block data request has the expected properties
     """
     tx_hashes = ['A' * 64, 'B' * 64, 'C' * 64]
     bdr = TransactionRequest.create(tx_hashes)
     self.assertEqual(bdr.tx_hashes, tx_hashes)
Exemplo n.º 5
0
    def _fetch_tx_for_current_block(self):
        """
        Fetches the transactions for the current block being updated
        """
        assert self.current_block, "_fetch_tx_for_current_block called but self.current_block not set!"

        request = TransactionRequest.create(self.current_block.merkle_leaves)
        self.parent.composer.send_request_msg(message=request,
                                              vk=VKBook.get_masternodes()[0])
        self.current_request = request
Exemplo n.º 6
0
    def test_validate_matches_request_no_match(self):
        """
        Tests that a created block data reply has the expected properties
        """
        sk = wallet.new()[0]
        code_strs = ['some random binary', 'some deterministic binary', 'While True: self.eatAss()']
        contracts = [
            ContractTransactionBuilder.create_contract_tx(sk, code_str) \
            for code_str in code_strs
        ]
        tx_binaries = [c.serialize() for c in contracts]

        tx_hashes = [Hasher.hash(cs+b'bbb') for cs in tx_binaries]
        bdr_req = TransactionRequest.create(tx_hashes)

        bdr_rep = TransactionReply.create(tx_binaries)
        assert not bdr_rep.validate_matches_request(bdr_req)
Exemplo n.º 7
0
    def _request_from_delegate(self, tx_hashes: List[str], delegate_vk: str=''):
        """
        Helper method to request a transaction from a delegate via the transactions hash.
        :param tx_hash:  The hash of the transaction to request
        :param delegate_vk:  The verifying key of the delegate to request. If not specified,
        self._first_available_node() is used
        """
        if delegate_vk:
            assert delegate_vk in self.node_states, "Expected to request from a delegate state that known in node_states"
        else:
            delegate_vk = self._first_available_node()

        self.log.debug("Requesting {} tx hashes from VK {}".format(len(tx_hashes), delegate_vk))

        # TODO make this more optimal by requesting hashes in batch
        req = TransactionRequest.create(tx_hashes)
        self.parent.composer.send_request_msg(message=req, timeout=5, vk=delegate_vk)

        self.node_states[delegate_vk] = self.NODE_AWAITING