Exemplo n.º 1
0
    def test_interpret_currency(self):
        amount = 1260
        receiver = BOB_VK
        sender = ALICE_VK
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK,
                                                          receiver_vk=receiver,
                                                          amount=amount))

        self.interpreter = SenecaInterpreter()
        currency_contract = get_contract_exports(
            self.interpreter.ex,
            self.interpreter.contracts_table,
            contract_id='currency')

        sender_initial_balance = currency_contract.get_balance(sender)
        receiver_initial_balance = currency_contract.get_balance(receiver)

        self.interpreter.interpret(contract_tx)

        # Assert the contract ran and updated the expected rows
        self.assertEqual(currency_contract.get_balance(sender),
                         sender_initial_balance - amount)
        self.assertEqual(currency_contract.get_balance(receiver),
                         receiver_initial_balance + amount)

        # Assert the contract was added to the queue
        self.assertEqual(self.interpreter.queue_size, 1)
        self.assertEqual(self.interpreter.queue[0], contract_tx.transaction)
Exemplo n.º 2
0
    def test_run_bad_contract_reverts_to_last_successful_contract(self):
        """
        Tests that running a failing contract reverts any database changes it made before the point of failure
        """
        receiver = BOB_VK
        sender = ALICE_VK

        self.interpreter = SenecaInterpreter()
        currency_contract = get_contract_exports(
            self.interpreter.ex,
            self.interpreter.contracts_table,
            contract_id='currency')

        sender_initial_balance = currency_contract.get_balance(sender)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK,
                                                          receiver_vk=receiver,
                                                          amount=1000))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(currency_contract.get_balance(sender),
                         sender_initial_balance - 1000)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK,
                                                          receiver_vk=receiver,
                                                          amount=200))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(currency_contract.get_balance(sender),
                         sender_initial_balance - 1200)
        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK,
                                                          receiver_vk=receiver,
                                                          amount=60))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(currency_contract.get_balance(sender),
                         sender_initial_balance - 1260)

        contract_tx = self.ordered_tx(
            ContractTransactionBuilder.create_currency_tx(sender_sk=ALICE_SK,
                                                          receiver_vk=receiver,
                                                          amount=3696947))
        self.interpreter.interpret(contract_tx)
        self.assertEqual(currency_contract.get_balance(sender),
                         sender_initial_balance - 1260)
Exemplo n.º 3
0
    def test_create_currency_tx(self):
        sk, vk = wallet.new()
        sk2, vk2 = wallet.new()
        amount = 9000
        currency_code = ContractTemplate.interpolate_template('currency',
                                                              amount=amount,
                                                              receiver=vk2)

        contract_tx = ContractTransactionBuilder.create_currency_tx(
            sender_sk=sk, receiver_vk=vk2, amount=amount)

        self.assertEquals(contract_tx.sender, vk)
        self.assertEquals(contract_tx.code, currency_code)