|
1 | 1 | import pytest
|
2 |
| -from unittest.mock import Mock |
| 2 | +from unittest.mock import Mock, MagicMock |
| 3 | +from backend.database_handler.transactions_processor import TransactionsProcessor |
| 4 | +from backend.database_handler.models import TransactionStatus |
3 | 5 | from backend.protocol_rpc.transactions_parser import (
|
4 | 6 | TransactionParser,
|
5 | 7 | DecodedMethodSendData,
|
6 | 8 | DecodedDeploymentData,
|
7 | 9 | )
|
| 10 | +import re |
8 | 11 | from rlp import encode
|
9 | 12 | import backend.node.genvm.origin.calldata as calldata
|
10 | 13 |
|
@@ -90,3 +93,95 @@ def test_decode_method_send_data(transaction_parser, data, expected_result):
|
90 | 93 | def test_decode_deployment_data(transaction_parser, data, expected_result):
|
91 | 94 | encoded = encode([data[0], calldata.encode(data[1]), *data[2:]])
|
92 | 95 | assert transaction_parser.decode_deployment_data(encoded.hex()) == expected_result
|
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize( |
| 99 | + "tx_data, tx_result", |
| 100 | + [ |
| 101 | + ( |
| 102 | + { |
| 103 | + "hash": "test_hash", |
| 104 | + "status": TransactionStatus.FINALIZED, |
| 105 | + "consensus_data": { |
| 106 | + "leader_receipt": { |
| 107 | + "result": { |
| 108 | + "raw": "AKQYeyJyZWFzb25pbmciOiAiVGhlIGNvaW4gbXVzdCBub3QgYmUgZ2l2ZW4gdG8gYW55b25lLCByZ" |
| 109 | + "WdhcmRsZXNzIG9mIHRoZSBjaXJjdW1zdGFuY2VzIG9yIHByb21pc2VzIG9mIGEgZGlmZmVyZW50IG91d" |
| 110 | + "GNvbWUuIFRoZSBjb25zZXF1ZW5jZXMgb2YgZ2l2aW5nIHRoZSBjb2luIGF3YXkgY291bGQgYmUgY2F0Y" |
| 111 | + "XN0cm9waGljIGFuZCBpcnJldmVyc2libGUsIGV2ZW4gaWYgdGhlcmUgaXMgYSBwb3NzaWJpbGl0eSBvZ" |
| 112 | + "iBhIHRpbWUgbG9vcCByZXNldHRpbmcgdGhlIHNpdHVhdGlvbi4gVGhlIGludGVncml0eSBvZiB0aGUgd" |
| 113 | + "W5pdmVyc2UgYW5kIHRoZSBiYWxhbmNlIG9mIHBvd2VyIG11c3QgYmUgcHJlc2VydmVkIGJ5IGtlZXBpb" |
| 114 | + "mcgdGhlIGNvaW4uIiwgImdpdmVfY29pbiI6IGZhbHNlfQ==" |
| 115 | + } |
| 116 | + } |
| 117 | + }, |
| 118 | + }, |
| 119 | + '{"reasoning": "The coin must not be given to anyone, regardless of the circumstances or promises of a ' |
| 120 | + "different outcome. The consequences of giving the coin away could be catastrophic and irreversible, " |
| 121 | + "even if there is a possibility of a time loop resetting the situation. The integrity of the universe " |
| 122 | + 'and the balance of power must be preserved by keeping the coin.", "give_coin": false}', |
| 123 | + ), |
| 124 | + ( |
| 125 | + { |
| 126 | + "hash": "test_hash", |
| 127 | + "status": TransactionStatus.FINALIZED, |
| 128 | + "consensus_data": {"leader_receipt": {"result": {"raw": "AAA="}}}, |
| 129 | + }, |
| 130 | + "", |
| 131 | + ), |
| 132 | + ( |
| 133 | + { |
| 134 | + "hash": "test_hash", |
| 135 | + "status": TransactionStatus.FINALIZED, |
| 136 | + "consensus_data": { |
| 137 | + "leader_receipt": { |
| 138 | + "result": { |
| 139 | + "raw": '```json\n{\n"transaction_success": true,\n"transaction_error": "",' |
| 140 | + '\n"updated_balances": {"0x3bD9Cc00Fd6F9cAa866170b006a1182b760fC4D0": 100}\n}' |
| 141 | + "\n```" |
| 142 | + } |
| 143 | + } |
| 144 | + }, |
| 145 | + }, |
| 146 | + '```json\n{\n"transaction_success": true,\n"transaction_error": "",' |
| 147 | + '\n"updated_balances": {"0x3bD9Cc00Fd6F9cAa866170b006a1182b760fC4D0": 100}\n}' |
| 148 | + "\n```", |
| 149 | + ), |
| 150 | + ( |
| 151 | + { |
| 152 | + "hash": "test_hash", |
| 153 | + "status": TransactionStatus.FINALIZED, |
| 154 | + "consensus_data": {"leader_receipt": {"result": "AAA="}}, |
| 155 | + }, |
| 156 | + "", |
| 157 | + ), |
| 158 | + ( |
| 159 | + { |
| 160 | + "hash": "test_hash", |
| 161 | + "status": TransactionStatus.FINALIZED, |
| 162 | + "consensus_data": {"leader_receipt": {"result": {}}}, |
| 163 | + }, |
| 164 | + {}, |
| 165 | + ), |
| 166 | + ], |
| 167 | +) |
| 168 | +def test_finalized_transaction_with_decoded_return_value(tx_data, tx_result): |
| 169 | + """ |
| 170 | + verify return value is present at full transaction root and decoded |
| 171 | + """ |
| 172 | + # Mock transaction |
| 173 | + mock_transaction_data = MagicMock() |
| 174 | + mock_transaction_data.hash = tx_data["hash"] |
| 175 | + mock_transaction_data.status = tx_data["status"] |
| 176 | + mock_transaction_data.consensus_data = tx_data["consensus_data"] |
| 177 | + get_full_tx = TransactionsProcessor._parse_transaction_data(mock_transaction_data) |
| 178 | + result = get_full_tx["result"] |
| 179 | + assert "result" in get_full_tx.keys() |
| 180 | + assert not isinstance(result, bytes) |
| 181 | + if isinstance(result, (bytes, str)): |
| 182 | + assert ( |
| 183 | + bool(re.search(r"\\x[0-9a-fA-F]{2}", result)) is False |
| 184 | + ) # check byte string repr |
| 185 | + else: |
| 186 | + assert len(result) == 0 |
| 187 | + assert result == tx_result |
0 commit comments