Example #1
0
    def test_transaction_v3_invalid_nid(self):
        MockBlockchain = namedtuple("MockBlockchain",
                                    "find_nid find_tx_by_key")
        nids = list(range(0, 1000))
        random.shuffle(nids)

        tb = TransactionBuilder.new("0x3", None, self.tx_versioner)
        tb.step_limit = 1000000
        tb.value = 100000
        tb.signer = self.signer
        tb.to_address = ExternalAddress(os.urandom(20))
        tb.nid = nids[0]
        tb.nonce = random.randint(0, 100000)
        tb.data = "test"
        tb.data_type = "message"
        tx = tb.build()

        expected_nid = nids[1]
        mock_blockchain = MockBlockchain(find_nid=lambda: hex(expected_nid),
                                         find_tx_by_key=lambda _: False)

        tv = TransactionVerifier.new(tx.version, tx.type(), self.tx_versioner)
        self.assertRaises(TransactionInvalidNidError,
                          lambda: tv.verify(tx, mock_blockchain))
        self.assertRaises(TransactionInvalidNidError,
                          lambda: tv.pre_verify(tx, nid=expected_nid))
Example #2
0
 def _tx_item(tx_versioner: TransactionVersioner) -> Transaction:
     test_signer = Signer.from_prikey(os.urandom(32))
     tx_builder = TransactionBuilder.new("0x3", "", tx_versioner)
     tx_builder.signer = test_signer
     tx_builder.to_address = ExternalAddress.new()
     tx_builder.step_limit = random.randint(0, 10000)
     tx_builder.value = random.randint(0, 10000)
     tx_builder.nid = 2
     tx: Transaction = tx_builder.build()
     return tx
Example #3
0
def create_basic_tx(peer_auth: Signer) -> Transaction:
    """
    :param peer_auth:
    :return: transaction
    """
    tx_builder = TransactionBuilder.new("0x3", TransactionVersioner())
    tx_builder.private_key = peer_auth._private_key
    tx_builder.to_address = Address(
        "hx3f376559204079671b6a8df481c976e7d51b3c7c")
    tx_builder.value = 1
    tx_builder.step_limit = 100000000
    tx_builder.nid = 3
    return tx_builder.build()
Example #4
0
    def _tx_builder_factory(tx_version: str, type_=None) -> TransactionBuilder:
        tx_builder = TransactionBuilder.new(version=tx_version, type_=type_, versioner=TransactionVersioner())

        # Attributes that must be assigned
        tx_builder.signer = Signer.new()

        if tx_version == genesis.version:
            tx_builder = add_attrs_to_genesis_builder(tx_builder)
        elif tx_version == v2.version:
            tx_builder = add_attrs_to_v2_builder(tx_builder)
        elif tx_version == v3.version:
            tx_builder = add_attrs_to_v3_builder(tx_builder)

        return tx_builder
Example #5
0
    def test_transaction_v2(self):
        tb = TransactionBuilder.new("0x2", None, self.tx_versioner)
        tb.fee = 1000000
        tb.value = 100000
        tb.signer = self.signer
        tb.to_address = ExternalAddress(os.urandom(20))
        tb.nonce = random.randint(0, 100000)
        tx = tb.build()

        tv = TransactionVerifier.new("0x2", tx.type(), self.tx_versioner)
        tv.verify(tx)

        ts = TransactionSerializer.new("0x2", tx.type(), self.tx_versioner)
        tx_raw_data = ts.to_raw_data(tx)

        self.assertEqual(ts.from_(tx_raw_data), tx)
    def test_transaction_v2_duplicate_hash(self):
        MockBlockchain = namedtuple("MockBlockchain", "find_nid find_tx_by_key")

        tb = TransactionBuilder.new("0x2", None, self.tx_versioner)
        tb.fee = 1000000
        tb.value = 100000
        tb.signer = self.signer
        tb.to_address = ExternalAddress(os.urandom(20))
        tb.nonce = random.randint(0, 100000)
        tx = tb.build()

        mock_blockchain = MockBlockchain(find_nid=lambda: hex(3),
                                         find_tx_by_key=lambda _: True)

        tv = TransactionVerifier.new(tx.version, tx.type(), self.tx_versioner)
        self.assertRaises(TransactionDuplicatedHashError, lambda: tv.verify(tx, mock_blockchain))
Example #7
0
    def test_transaction_genesis(self):
        tb = TransactionBuilder.new("genesis", None, self.tx_versioner)
        tb.accounts = [{
            "name": "test0",
            "address": ExternalAddress(os.urandom(20)).hex_hx(),
            "balance": "0x12221231"
        }]
        tb.message = "Icon Loop"
        tx = tb.build(False)

        tv = TransactionVerifier.new("genesis", tx.type(), self.tx_versioner)
        tv.verify(tx)

        ts = TransactionSerializer.new("genesis", tx.type(), self.tx_versioner)
        tx_raw_data = ts.to_raw_data(tx)

        self.assertEqual(ts.from_(tx_raw_data), tx)
    def test_transaction_v2_unsigned(self):
        signer = Signer.new()

        tb = TransactionBuilder.new("0x2", None, self.tx_versioner)
        tb.fee = 1000000
        tb.value = 100000
        tb.from_address = ExternalAddress.fromhex_address(signer.address)
        tb.to_address = ExternalAddress(os.urandom(20))
        tb.nonce = random.randint(0, 100000)
        tx = tb.build(is_signing=False)

        tv = TransactionVerifier.new("0x2", tx.type(), self.tx_versioner)
        self.assertRaises(TransactionInvalidSignatureError, lambda: tv.verify(tx))
        self.assertRaises(TransactionInvalidSignatureError, lambda: tv.pre_verify(tx))

        tb.signer = signer
        signed_tx = tb.sign_transaction(tx)
        tv.verify(signed_tx)
        tv.pre_verify(signed_tx)
Example #9
0
        def block_maker(timestamp: int, height: int = 0, prev_hash=None):
            """Make dummy block"""
            tx_versioner = TransactionVersioner()

            dummy_receipts = {}
            block_builder = BlockBuilder.new("0.1a", tx_versioner)

            for i in range(1000):
                tx_builder = TransactionBuilder.new("0x3", None, tx_versioner)
                tx_builder.signer = test_signer
                tx_builder.to_address = ExternalAddress.new()
                tx_builder.step_limit = random.randint(0, 10000)
                tx_builder.value = random.randint(0, 10000)
                tx_builder.nid = 2
                tx = tx_builder.build()

                tx_serializer = TransactionSerializer.new(
                    tx.version, tx.type(), tx_versioner)
                block_builder.transactions[tx.hash] = tx
                dummy_receipts[tx.hash.hex()] = {
                    "dummy_receipt": "dummy",
                    "tx_dumped": tx_serializer.to_full_data(tx)
                }

            block_builder.signer = test_signer
            block_builder.prev_hash = prev_hash
            block_builder.height = height
            block_builder.state_hash = Hash32(bytes(Hash32.size))
            block_builder.receipts = dummy_receipts
            block_builder.reps = [
                ExternalAddress.fromhex_address(test_signer.address)
            ]
            block_builder.peer_id = ExternalAddress.fromhex(
                test_signer.address)
            block_builder.next_leader = ExternalAddress.fromhex(
                test_signer.address)
            block_builder.fixed_timestamp = timestamp

            b = block_builder.build()
            assert b.header.timestamp == timestamp

            return b
    def test_transaction_v3_unsigned(self):
        signer = Signer.new()

        tb = TransactionBuilder.new("0x3", None, self.tx_versioner)
        tb.step_limit = 1000000
        tb.value = 100000
        tb.from_address = ExternalAddress.fromhex_address(signer.address)
        tb.to_address = ExternalAddress(os.urandom(20))
        tb.nid = 3
        tb.nonce = random.randint(0, 100000)
        tb.data = "test"
        tb.data_type = "message"
        tx = tb.build(False)

        tv = TransactionVerifier.new("0x3", tx.type(), self.tx_versioner)
        self.assertRaises(TransactionInvalidSignatureError, lambda: tv.verify(tx))
        self.assertRaises(TransactionInvalidSignatureError, lambda: tv.pre_verify(tx, nid=3))

        tb.signer = signer
        signed_tx = tb.sign_transaction(tx)
        tv.verify(signed_tx)
        tv.pre_verify(signed_tx, nid=3)
Example #11
0
    def test_block_v0_4(self):
        block_version = "0.4"
        test_signer = Signer.from_prikey(os.urandom(32))
        tx_versioner = TransactionVersioner()

        dummy_receipts = {}
        block_builder = BlockBuilder.new(block_version, tx_versioner)
        for i in range(5):
            tx_builder = TransactionBuilder.new("0x3", None, tx_versioner)
            tx_builder.signer = test_signer
            tx_builder.to_address = ExternalAddress.new()
            tx_builder.step_limit = random.randint(0, 10000)
            tx_builder.value = random.randint(0, 10000)
            tx_builder.nid = 2
            tx = tx_builder.build()

            tx_serializer = TransactionSerializer.new(tx.version, tx.type(),
                                                      tx_versioner)
            block_builder.transactions[tx.hash] = tx
            dummy_receipts[tx.hash.hex()] = {
                "dummy_receipt": "dummy",
                "tx_dumped": tx_serializer.to_full_data(tx)
            }

        next_leader = ExternalAddress.fromhex(
            "hx00112233445566778899aabbccddeeff00112233")

        block_builder.signer = test_signer
        block_builder.height = 0
        block_builder.prev_hash = Hash32(bytes(Hash32.size))
        block_builder.state_hash = Hash32(bytes(Hash32.size))
        block_builder.receipts = dummy_receipts
        block_builder.reps = [
            ExternalAddress.fromhex_address(test_signer.address)
        ]
        block_builder.next_leader = next_leader
        block_builder.next_reps = []

        vote = BlockVote.new(test_signer, utils.get_time_stamp(),
                             block_builder.height - 1, 0,
                             block_builder.prev_hash)
        votes = BlockVotes(block_builder.reps, conf.VOTING_RATIO,
                           block_builder.height - 1, 0,
                           block_builder.prev_hash)
        votes.add_vote(vote)
        block_builder.prev_votes = votes.votes

        block = block_builder.build()
        block_verifier = BlockVerifier.new(block_version, tx_versioner)

        block_verifier.invoke_func = lambda b, prev_b: (block, dummy_receipts)
        reps_getter = lambda _: block_builder.reps
        generator = ExternalAddress.fromhex_address(test_signer.address)
        block_verifier.verify(block,
                              None,
                              None,
                              generator=generator,
                              reps_getter=reps_getter)

        block_serializer = BlockSerializer.new(block_version, tx_versioner)
        block_serialized = block_serializer.serialize(block)
        logging.info(json.dumps(block_serialized, indent=4))
        block_deserialized = block_serializer.deserialize(block_serialized)
        logging.info(
            json.dumps(block_serializer.serialize(block_deserialized),
                       indent=4))

        assert block.header == block_deserialized.header
        assert block.body == block_deserialized.body

        tx_hashes = list(block.body.transactions)
        tx_index = random.randrange(0, len(tx_hashes))

        block_prover = BlockProver.new(block.header.version, tx_hashes,
                                       BlockProverType.Transaction)
        tx_proof = block_prover.get_proof(tx_index)
        assert block_prover.prove(tx_hashes[tx_index],
                                  block.header.transactions_hash, tx_proof)

        block_prover = BlockProver.new(block.header.version,
                                       block_builder.receipts,
                                       BlockProverType.Receipt)
        receipt_proof = block_prover.get_proof(tx_index)
        receipts_hash = block_prover.to_hash32(
            block_builder.receipts[tx_index])
        assert block_prover.prove(receipts_hash, block.header.receipts_hash,
                                  receipt_proof)
    @pytest.mark.parametrize("rest_method", RestMethod)
    def test_url(self, rest_client: RestClient, rest_method: RestMethod):
        url = rest_client.create_url(rest_client._target, rest_method)
        assert url == request_urls[rest_method]

    @pytest.mark.parametrize("rest_method", RestMethod)
    def test_params(self, rest_client: RestClient, rest_method: RestMethod):
        params = rest_client.create_params(rest_method,
                                           request_params[rest_method])
        params.pop('id', None)
        assert params == request_params_results[rest_method]


tv = TransactionVersioner()
tb = TransactionBuilder.new(version="0x2", type_=None, versioner=tv)
tb.signer = Signer.new()
tb.to_address = ExternalAddress(os.urandom(20))
tb.fee = 10
tb.value = 1000
tb.nonce = 123
request_tx2 = tb.build()
request_tx2_param = TransactionSerializer.new("0x2", None,
                                              tv).to_raw_data(request_tx2)
request_tx2_param["from_"] = request_tx2_param.pop("from")

tb = TransactionBuilder.new(version="0x3", type_=None, versioner=tv)
tb.step_limit = 1000000
tb.value = 100000
tb.signer = Signer.new()
tb.to_address = ExternalAddress(os.urandom(20))