2024-06-11 11:36:20 +00:00
|
|
|
import json
|
2024-09-16 08:23:57 +00:00
|
|
|
import threading
|
2024-09-25 12:27:04 +00:00
|
|
|
import time
|
2024-10-04 12:55:28 +00:00
|
|
|
import pytest
|
2024-10-07 15:40:18 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from conftest import option, user_1, user_2
|
|
|
|
from clients.signals import SignalClient
|
|
|
|
from clients.status_backend import RpcClient, StatusBackend
|
2024-10-04 12:55:28 +00:00
|
|
|
from collections import namedtuple
|
2024-06-11 11:36:20 +00:00
|
|
|
|
2024-10-07 15:40:18 +00:00
|
|
|
class StatusDTestCase:
|
|
|
|
|
2024-10-04 12:55:28 +00:00
|
|
|
network_id = 31337
|
2024-06-11 11:36:20 +00:00
|
|
|
|
|
|
|
def setup_method(self):
|
2024-10-07 15:40:18 +00:00
|
|
|
self.rpc_client = RpcClient(
|
|
|
|
option.rpc_url_statusd
|
|
|
|
)
|
2024-06-11 11:36:20 +00:00
|
|
|
|
2024-10-07 15:40:18 +00:00
|
|
|
class WalletTestCase(StatusDTestCase):
|
|
|
|
|
2024-10-04 12:55:28 +00:00
|
|
|
def setup_method(self):
|
|
|
|
super().setup_method()
|
2024-06-11 11:36:20 +00:00
|
|
|
|
2024-09-18 16:54:17 +00:00
|
|
|
def wallet_create_multi_transaction(self, **kwargs):
|
2024-06-11 11:36:20 +00:00
|
|
|
method = "wallet_createMultiTransaction"
|
2024-09-18 16:54:17 +00:00
|
|
|
transferTx_data = {
|
2024-09-25 12:27:04 +00:00
|
|
|
"data": "",
|
|
|
|
"from": user_1.address,
|
|
|
|
"gas": "0x5BBF",
|
|
|
|
"input": "",
|
|
|
|
"maxFeePerGas": "0xbcc0f04fd",
|
|
|
|
"maxPriorityFeePerGas": "0xbcc0f04fd",
|
|
|
|
"to": user_2.address,
|
|
|
|
"type": "0x02",
|
|
|
|
"value": "0x5af3107a4000",
|
|
|
|
}
|
2024-09-18 16:54:17 +00:00
|
|
|
for key, new_value in kwargs.items():
|
|
|
|
if key in transferTx_data:
|
|
|
|
transferTx_data[key] = new_value
|
|
|
|
else:
|
2024-10-07 15:40:18 +00:00
|
|
|
logging.info(
|
2024-09-25 12:27:04 +00:00
|
|
|
f"Warning: The key '{key}' does not exist in the transferTx parameters and will be ignored.")
|
2024-06-11 11:36:20 +00:00
|
|
|
params = [
|
|
|
|
{
|
|
|
|
"fromAddress": user_1.address,
|
|
|
|
"fromAmount": "0x5af3107a4000",
|
|
|
|
"fromAsset": "ETH",
|
2024-10-04 12:55:28 +00:00
|
|
|
"type": 0, # MultiTransactionSend
|
2024-06-11 11:36:20 +00:00
|
|
|
"toAddress": user_2.address,
|
|
|
|
"toAsset": "ETH",
|
|
|
|
},
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"bridgeName": "Transfer",
|
|
|
|
"chainID": 31337,
|
2024-09-18 16:54:17 +00:00
|
|
|
"transferTx": transferTx_data
|
2024-06-11 11:36:20 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
f"{option.password}",
|
|
|
|
]
|
2024-10-07 15:40:18 +00:00
|
|
|
return self.rpc_client.rpc_request(method, params)
|
2024-10-04 12:55:28 +00:00
|
|
|
|
|
|
|
def send_valid_multi_transaction(self, **kwargs):
|
|
|
|
response = self.wallet_create_multi_transaction(**kwargs)
|
2024-06-11 11:36:20 +00:00
|
|
|
|
2024-10-04 12:55:28 +00:00
|
|
|
tx_hash = None
|
2024-10-07 15:40:18 +00:00
|
|
|
self.rpc_client.verify_is_valid_json_rpc_response(response)
|
2024-10-04 12:55:28 +00:00
|
|
|
try:
|
|
|
|
tx_hash = response.json(
|
|
|
|
)["result"]["hashes"][str(self.network_id)][0]
|
|
|
|
except (KeyError, json.JSONDecodeError):
|
|
|
|
raise Exception(response.content)
|
|
|
|
return tx_hash
|
|
|
|
|
|
|
|
class TransactionTestCase(WalletTestCase):
|
2024-10-07 15:40:18 +00:00
|
|
|
|
2024-06-11 11:36:20 +00:00
|
|
|
def setup_method(self):
|
|
|
|
super().setup_method()
|
2024-10-04 12:55:28 +00:00
|
|
|
self.tx_hash = self.send_valid_multi_transaction()
|
|
|
|
|
2024-10-07 15:40:18 +00:00
|
|
|
class EthRpcTestCase(WalletTestCase):
|
|
|
|
|
2024-10-04 12:55:28 +00:00
|
|
|
@pytest.fixture(autouse=True, scope='class')
|
|
|
|
def tx_data(self):
|
2024-10-07 15:40:18 +00:00
|
|
|
self.rpc_client = RpcClient(
|
|
|
|
option.rpc_url_statusd
|
|
|
|
)
|
|
|
|
|
2024-10-04 12:55:28 +00:00
|
|
|
tx_hash = self.send_valid_multi_transaction()
|
|
|
|
self.wait_until_tx_not_pending(tx_hash)
|
|
|
|
|
|
|
|
receipt = self.get_transaction_receipt(tx_hash)
|
2024-06-11 11:36:20 +00:00
|
|
|
try:
|
2024-10-04 12:55:28 +00:00
|
|
|
block_number = receipt.json()["result"]["blockNumber"]
|
|
|
|
block_hash = receipt.json()["result"]["blockHash"]
|
2024-06-11 11:36:20 +00:00
|
|
|
except (KeyError, json.JSONDecodeError):
|
2024-10-04 12:55:28 +00:00
|
|
|
raise Exception(receipt.content)
|
|
|
|
|
|
|
|
TxData = namedtuple("TxData", ["tx_hash", "block_number", "block_hash"])
|
|
|
|
return TxData(tx_hash, block_number, block_hash)
|
|
|
|
|
|
|
|
def get_block_header(self, block_number):
|
|
|
|
method = "ethclient_headerByNumber"
|
|
|
|
params = [self.network_id, block_number]
|
2024-10-07 15:40:18 +00:00
|
|
|
return self.rpc_client.rpc_valid_request(method, params)
|
2024-10-04 12:55:28 +00:00
|
|
|
|
|
|
|
def get_transaction_receipt(self, tx_hash):
|
|
|
|
method = "ethclient_transactionReceipt"
|
|
|
|
params = [self.network_id, tx_hash]
|
2024-10-07 15:40:18 +00:00
|
|
|
return self.rpc_client.rpc_valid_request(method, params)
|
2024-10-04 12:55:28 +00:00
|
|
|
|
|
|
|
def wait_until_tx_not_pending(self, tx_hash, timeout=10):
|
|
|
|
method = "ethclient_transactionByHash"
|
|
|
|
params = [self.network_id, tx_hash]
|
2024-10-07 15:40:18 +00:00
|
|
|
response = self.rpc_client.rpc_valid_request(method, params)
|
2024-06-11 11:36:20 +00:00
|
|
|
|
2024-10-04 12:55:28 +00:00
|
|
|
start_time = time.time()
|
|
|
|
while response.json()["result"]["isPending"] == True:
|
|
|
|
time_passed = time.time() - start_time
|
|
|
|
if time_passed >= timeout:
|
|
|
|
raise TimeoutError(
|
|
|
|
f"Tx {tx_hash} is still pending after {timeout} seconds")
|
|
|
|
time.sleep(0.5)
|
2024-10-07 15:40:18 +00:00
|
|
|
response = self.rpc_client.rpc_valid_request(method, params)
|
2024-10-04 12:55:28 +00:00
|
|
|
return response.json()["result"]["tx"]
|
2024-09-16 08:23:57 +00:00
|
|
|
|
2024-10-07 15:40:18 +00:00
|
|
|
class SignalTestCase(StatusDTestCase):
|
2024-09-16 08:23:57 +00:00
|
|
|
|
2024-09-25 12:27:04 +00:00
|
|
|
await_signals = []
|
2024-09-16 08:23:57 +00:00
|
|
|
|
|
|
|
def setup_method(self):
|
|
|
|
super().setup_method()
|
2024-10-07 15:40:18 +00:00
|
|
|
self.signal_client = SignalClient(option.ws_url_statusd, self.await_signals)
|
2024-09-16 08:23:57 +00:00
|
|
|
|
2024-10-07 15:40:18 +00:00
|
|
|
websocket_thread = threading.Thread(target=self.signal_client._connect)
|
2024-09-16 08:23:57 +00:00
|
|
|
websocket_thread.daemon = True
|
|
|
|
websocket_thread.start()
|
2024-10-07 15:40:18 +00:00
|
|
|
|
|
|
|
class StatusBackendTestCase:
|
|
|
|
|
|
|
|
def setup_method(self):
|
|
|
|
pass
|