Skip to content

Commit

Permalink
Merge pull request #2540 from opentensor/tests/update-tests-btwallet3
Browse files Browse the repository at this point in the history
Updates tests for btwallet 3.0.0
  • Loading branch information
ibraheem-opentensor authored Jan 17, 2025
2 parents b0ff5c2 + cd91cd8 commit 86f9b4d
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e-subtensor-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ jobs:

- name: Setup subtensor repo
working-directory: ${{ github.workspace }}/subtensor
run: git checkout testnet
run: git checkout main

- name: Run tests
run: |
Expand Down
5 changes: 4 additions & 1 deletion bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from scalecodec.exceptions import RemainingScaleBytesNotEmptyException
from scalecodec.type_registry import load_type_registry_preset
from scalecodec.types import ScaleType
from substrateinterface import Keypair
from substrateinterface.base import QueryMapResult, SubstrateInterface
from websockets.exceptions import InvalidStatus
from websockets.sync import client as ws_client
Expand Down Expand Up @@ -1532,9 +1533,11 @@ def get_transfer_fee(
call_params={"dest": dest, "value": value.rao},
)

temp_keypair = Keypair(ss58_address=wallet.coldkeypub.ss58_address)

try:
payment_info = self.substrate.get_payment_info(
call=call, keypair=wallet.coldkeypub
call=call, keypair=temp_keypair
)
except Exception as e:
logging.error(f"[red]Failed to get payment info.[/red] {e}")
Expand Down
2 changes: 1 addition & 1 deletion requirements/prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ scalecodec==1.2.11
substrate-interface~=1.7.9
uvicorn
websockets>=14.1
bittensor-wallet>=2.1.3
bittensor-wallet>=3.0.0
bittensor-commit-reveal>=0.1.0
3 changes: 3 additions & 0 deletions tests/e2e_tests/test_transfer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from bittensor.core.subtensor import Subtensor
from bittensor.utils.balance import Balance
from tests.e2e_tests.utils.e2e_test_utils import setup_wallet
from bittensor import logging

logging.set_trace()


def test_transfer(local_chain):
Expand Down
30 changes: 24 additions & 6 deletions tests/integration_tests/utils/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,51 @@
from bittensor import utils


def test_unlock_key(monkeypatch):
def test_unlock_through_env():
# Ensure path is clean before we run the tests
if os.path.exists("/tmp/bittensor-tests-wallets"):
if os.path.exists("/tmp/bittensor-tests-wallets/"):
shutil.rmtree("/tmp/bittensor-tests-wallets")

wallet = Wallet(path="/tmp/bittensor-tests-wallets")

# Set up the coldkey
cold_kf = Keyfile("/tmp/bittensor-tests-wallets/default/coldkey", name="default")
kp = Keypair.create_from_mnemonic(
"stool feel open east woman high can denial forget screen trust salt"
)
cold_kf.set_keypair(kp, False, False)
cold_kf.encrypt("1234password1234")
hot_kf = Keyfile("/tmp/bittensor-tests-wallets/default/hotkey", name="default")

# Set up the hotkey
hot_kf = Keyfile(
"/tmp/bittensor-tests-wallets/default/hotkeys/default", name="default"
)
hkp = Keypair.create_from_mnemonic(
"stool feel open east woman high can denial forget screen trust salt"
)
hot_kf.set_keypair(hkp, False, False)
hot_kf.encrypt("1234hotkey1234")
monkeypatch.setattr("getpass.getpass", lambda _: "badpassword1234")

# Save a wrong password to the environment for CK
cold_kf.save_password_to_env("badpassword")
result = utils.unlock_key(wallet)
assert result.success is False
monkeypatch.setattr("getpass.getpass", lambda _: "1234password1234")

# Save correct password to the environment for CK
cold_kf.save_password_to_env("1234password1234")
result = utils.unlock_key(wallet)
assert result.success is True
monkeypatch.setattr("getpass.getpass", lambda _: "badpassword1234")

# Save a wrong password to the environment for HK
hot_kf.save_password_to_env("badpassword")
result = utils.unlock_key(wallet, "hotkey")
assert result.success is False

# Save correct password to the environment for HK
hot_kf.save_password_to_env("1234hotkey1234")
result = utils.unlock_key(wallet, "hotkey")
assert result.success is True

with pytest.raises(ValueError):
utils.unlock_key(wallet, "mycoldkey")

Expand Down
9 changes: 7 additions & 2 deletions tests/unit_tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ def test_py_config_parsed_successfully_rust_wallet():
config.wallet.hotkey = "new_hotkey"
config.wallet.path = "/some/not_default/path"

# Pass in the whole bittensor config
wallet = bittensor.wallet(config=config)

# Asserts
assert wallet.name == config.wallet.name
assert wallet.hotkey_str == config.wallet.hotkey
assert wallet.path == config.wallet.path

# Pass in only the btwallet's config
wallet_two = bittensor.wallet(config=config.wallet)
assert wallet_two.name == config.wallet.name
assert wallet_two.hotkey_str == config.wallet.hotkey
assert wallet_two.path == config.wallet.path
4 changes: 3 additions & 1 deletion tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,8 @@ def test_get_transfer_fee(subtensor, mocker):
fake_payment_info = {"partialFee": int(2e10)}
subtensor.substrate.get_payment_info.return_value = fake_payment_info

mocker.patch.object(subtensor_module, "Keypair", return_value=mocker.MagicMock())

# Call
result = subtensor.get_transfer_fee(wallet=fake_wallet, dest=fake_dest, value=value)

Expand All @@ -1751,7 +1753,7 @@ def test_get_transfer_fee(subtensor, mocker):

subtensor.substrate.get_payment_info.assert_called_once_with(
call=subtensor.substrate.compose_call.return_value,
keypair=fake_wallet.coldkeypub,
keypair=subtensor_module.Keypair.return_value,
)

assert result == 2e10
Expand Down

0 comments on commit 86f9b4d

Please sign in to comment.