async def test_cancel_transaction_fails_if_not_queued(self): tx = create_transaction(nonce=0, gasprice=10 ** 10, startgas=21000, to=TEST_ADDRESS, value=10 ** 18) tx = sign_transaction(tx, FAUCET_PRIVATE_KEY) tx_hash = calculate_transaction_hash(tx) from_address = FAUCET_ADDRESS to_address = TEST_ADDRESS async with self.pool.acquire() as con: await con.execute("INSERT INTO transactions (hash, from_address, to_address, nonce, value, gas, gas_price, data, v, r, s, status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)", tx_hash, from_address, to_address, tx.nonce, hex(tx.value), hex(tx.startgas), hex(tx.gasprice), data_encoder(tx.data), hex(tx.v), hex(tx.r), hex(tx.s), 'unconfirmed') signature = personal_sign(FAUCET_PRIVATE_KEY, "Cancel transaction " + tx_hash) resp = await self.fetch("/tx/cancel", method="POST", body={"tx_hash": tx_hash, "signature": signature}) self.assertResponseCodeEqual(resp, 400) async with self.pool.acquire() as con: await con.execute("UPDATE transactions SET status = 'confirmed'") resp = await self.fetch("/tx/cancel", method="POST", body={"tx_hash": tx_hash, "signature": signature}) self.assertResponseCodeEqual(resp, 400) async with self.pool.acquire() as con: await con.execute("UPDATE transactions SET status = 'error'") resp = await self.fetch("/tx/cancel", method="POST", body={"tx_hash": tx_hash, "signature": signature}) self.assertResponseCodeEqual(resp, 400)
def test_personal_sign(self): msg = "Hello world!" signature = personal_sign(TEST_PRIVATE_KEY, msg) signature_bytes = data_decoder(signature) self.assertTrue(signature_bytes[-1] == 27 or signature_bytes[-1] == 28, "signature must be an ethereum signature") self.assertEqual("0x9ab94a7f9455231eabc3d8cb4e343e87d34d820dec276f4c89c56eb4c965cc855d63c7208cd72054e6f9bf792493debf8e03a80a511d508c4c2d3f8dff05655b1b", signature) self.assertTrue(personal_ecrecover(msg, signature, TEST_ADDRESS))
async def test_cancel_transaction(self): listener = MockTaskListener(self._app) listener.start_task_listener() tx = create_transaction(nonce=0, gasprice=10 ** 10, startgas=21000, to=TEST_ADDRESS, value=10 ** 18) tx = sign_transaction(tx, FAUCET_PRIVATE_KEY) tx_hash = calculate_transaction_hash(tx) from_address = FAUCET_ADDRESS to_address = TEST_ADDRESS async with self.pool.acquire() as con: await con.execute("INSERT INTO transactions (hash, from_address, to_address, nonce, value, gas, gas_price, data, v, r, s, status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)", tx_hash, from_address, to_address, tx.nonce, hex(tx.value), hex(tx.startgas), hex(tx.gasprice), data_encoder(tx.data), hex(tx.v), hex(tx.r), hex(tx.s), 'queued') signature = personal_sign(FAUCET_PRIVATE_KEY, "Cancel transaction " + tx_hash) resp = await self.fetch("/tx/cancel", method="POST", body={"tx_hash": tx_hash, "signature": signature}) self.assertResponseCodeEqual(resp, 204) tx_id, status = await listener.get() self.assertEqual(tx_id, 1) self.assertEqual(status, 'error') await listener.stop_task_listener()