-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #419 from skalenetwork/hotfix/redis-dropped-tx
Hofix redis wallet adapter dropped tx
- Loading branch information
Showing
3 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from datetime import datetime | ||
|
||
import mock | ||
import pytest | ||
from freezegun import freeze_time | ||
|
||
from skale.wallets.redis_wallet import ( | ||
AdapterSendError, | ||
AdapterWaitError, | ||
DroppedError, | ||
EmptyStatusError, | ||
RedisWalletAdapter | ||
) | ||
|
||
from tests.helper import in_time | ||
|
||
|
||
@pytest.fixture | ||
def rdp(skale): | ||
return RedisWalletAdapter(mock.Mock(), 'transactions', skale.wallet) | ||
|
||
|
||
class RedisTestError(Exception): | ||
pass | ||
|
||
|
||
def test_make_raw_id(): | ||
tx_id = RedisWalletAdapter._make_raw_id() | ||
assert tx_id.startswith(b'tx-') | ||
assert len(tx_id) == 19 | ||
|
||
|
||
def test_make_score(): | ||
cts = 1623233060 | ||
dt = datetime.utcfromtimestamp(cts) | ||
with freeze_time(dt): | ||
score = RedisWalletAdapter._make_score(priority=5) | ||
assert score == 51623233060 | ||
|
||
|
||
def test_make_record(): | ||
tx = { | ||
'from': '0x1', | ||
'to': '0x2', | ||
'value': 1, | ||
'gasPrice': 1, | ||
'gas': 22000, | ||
'nonce': 1, | ||
'chainId': 1 | ||
} | ||
score = '51623233060' | ||
tx_id, r = RedisWalletAdapter._make_record(tx, score, 2) | ||
assert tx_id.startswith(b'tx-') and len(tx_id) == 19 | ||
assert r == b'{"status": "PROPOSED", "score": "51623233060", "multiplier": 2, "tx_hash": null, "from": "0x1", "to": "0x2", "value": 1, "gasPrice": 1, "gas": 22000, "nonce": 1, "chainId": 1}' # noqa | ||
|
||
|
||
def test_sign_and_send(rdp): | ||
tx = { | ||
'from': '0x1', | ||
'to': '0x2', | ||
'value': 1, | ||
'gasPrice': 1, | ||
'gas': 22000, | ||
'nonce': 1, | ||
'chainId': 1 | ||
} | ||
tx_id = rdp.sign_and_send(tx, multiplier=2, priority=5) | ||
assert tx_id.startswith('tx-') and len(tx_id) == 19 | ||
|
||
rdp.rs.pipeline = mock.Mock(side_effect=RedisTestError('rtest')) | ||
with pytest.raises(AdapterSendError): | ||
tx_id = rdp.sign_and_send(tx, multiplier=2, priority=5) | ||
|
||
|
||
def test_wait(rdp): | ||
tx_id = 'tx-tttttttttttttttt' | ||
rdp.get_status = mock.Mock(return_value=None) | ||
with in_time(3): | ||
with pytest.raises(EmptyStatusError): | ||
rdp.wait(tx_id, timeout=2) | ||
|
||
rdp.get_status = mock.Mock(return_value='DROPPED') | ||
with in_time(2): | ||
with pytest.raises(DroppedError): | ||
rdp.wait(tx_id, timeout=100) | ||
|
||
rdp.get_status = mock.Mock(side_effect=RedisTestError('test')) | ||
with in_time(2): | ||
with pytest.raises(AdapterWaitError): | ||
rdp.wait(tx_id, timeout=100) | ||
|
||
rdp.get_status = mock.Mock(return_value='SUCCESS') | ||
rdp.get_record = mock.Mock(return_value={'tx_hash': 'test'}) | ||
fake_receipt = {'test': 'test'} | ||
with mock.patch( | ||
'skale.wallets.redis_wallet.get_receipt', | ||
return_value=fake_receipt | ||
): | ||
with in_time(2): | ||
assert rdp.wait(tx_id, timeout=100) == fake_receipt |