Skip to content

Commit

Permalink
Merge pull request #69 from opentensor/fix/roman/make-ketpair-class-s…
Browse files Browse the repository at this point in the history
…ubstrateinterface-keypair-compatable

Add base and mro updating logic before add Wallet Keypair class to the main_module
  • Loading branch information
roman-opentensor authored Nov 18, 2024
2 parents 7b40017 + 33b390e commit aab60f6
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
strategy:
matrix:
platform:
- runner: macos-12
- runner: macos-13
target: x86_64
- runner: macos-14
target: aarch64
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crate-type = ["cdylib"]

[dependencies]
sp-core = "34.0.0"
pyo3 = { version = "0.22.3", features = ["gil-refs"] }
pyo3 = { version = "0.22.6", features = ["gil-refs"] }
bip39 = { version = "2.0.0", features = ["rand"] }
rand = "0.8.5"
hex = "0.4.3"
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ dependencies = [
"ansible_vault~=2.1",
"munch~=2.5.0",
"rich",
"py-bip39-bindings==0.1.11"
"py-bip39-bindings==0.1.11",
"substrate-interface~=1.7.9"
]
requires-python = ">= 3.9"

Expand Down
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// Main module for declaration of python package structure
use pyo3::prelude::*;
use pyo3::types::{PyTuple, PyType};
use pyo3::ffi;

mod config;
mod constants;
Expand All @@ -10,7 +12,7 @@ mod utils;
mod wallet;

#[pymodule]
fn bittensor_wallet(module: &Bound<'_, PyModule>) -> PyResult<()> {
fn bittensor_wallet(py: Python<'_>,module: &Bound<'_, PyModule>) -> PyResult<()> {
// classes to main module
module.add_class::<config::Config>()?;
module.add_class::<keyfile::Keyfile>()?;
Expand All @@ -20,7 +22,7 @@ fn bittensor_wallet(module: &Bound<'_, PyModule>) -> PyResult<()> {
register_config_module(module)?;
register_errors_module(module)?;
register_keyfile_module(module)?;
register_keypair_module(module)?;
register_keypair_module(py, module)?;
register_utils_module(module)?;
register_wallet_module(module)?;
Ok(())
Expand Down Expand Up @@ -97,12 +99,34 @@ fn register_keyfile_module(main_module: &Bound<'_, PyModule>) -> PyResult<()> {
}

// keypair module with functions
fn register_keypair_module(main_module: &Bound<'_, PyModule>) -> PyResult<()> {
let keypair_module = PyModule::new_bound(main_module.py(), "keypair")?;
keypair_module.add_class::<keypair::Keypair>()?;
fn register_keypair_module(py: Python, main_module: &Bound<'_, PyModule>) -> PyResult<()> {
let keypair_module = PyModule::new_bound(py, "keypair")?;

// import python substrateinterface into the rust
let substrate_module = py.import_bound("substrateinterface")?;
let origin_keypair_class = substrate_module.getattr("Keypair")?;

// defining Wallet Keypair class
let keypair_type = PyType::new_bound::<keypair::Keypair>(py);

// update base and mro in Wallet Keypair type
unsafe {
(*keypair_type.as_type_ptr()).tp_base = origin_keypair_class.as_ptr() as *mut _;

let mro_tuple = PyTuple::new_bound(py, &[keypair_type.as_ref(), &origin_keypair_class]);
ffi::Py_INCREF(mro_tuple.as_ptr());
(*keypair_type.as_type_ptr()).tp_mro = mro_tuple.as_ptr() as *mut _;

if ffi::PyType_Ready(keypair_type.as_type_ptr()) != 0 {
return Err(PyErr::fetch(py));
}
}

keypair_module.add("Keypair", keypair_type)?;
main_module.add_submodule(&keypair_module)
}


// utils module with functions
fn register_utils_module(main_module: &Bound<'_, PyModule>) -> PyResult<()> {
let utils_module = PyModule::new_bound(main_module.py(), "utils")?;
Expand Down
29 changes: 29 additions & 0 deletions tests/test_keypair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from substrateinterface import Keypair
import pytest
from bittensor_wallet import Wallet
import time


@pytest.fixture
def mock_wallet():
wallet = Wallet(
name=f"mock-{str(time.time())}",
hotkey=f"mock-{str(time.time())}",
path="/tmp/tests_wallets/do_not_use",
)
wallet.create_new_coldkey(use_password=False, overwrite=True, suppress=True)
wallet.create_new_hotkey(use_password=False, overwrite=True, suppress=True)

return wallet


def test_keypair_type(mock_wallet):
"""Makes sure that the wallet fields coldkey, hotkey, coldkeypub are compatible with substrateinterface.Keypair."""
# Preps
wallet = mock_wallet

# Assertions

assert isinstance(wallet.coldkey, Keypair)
assert isinstance(wallet.hotkey, Keypair)
assert isinstance(wallet.coldkeypub, Keypair)
11 changes: 9 additions & 2 deletions tests/test_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,11 +552,15 @@ def test_wallet_string_representation_with_default_arguments():
w = Wallet()

# Asserts
assert str(w) == "Wallet (Name: 'default', Hotkey: 'default', Path: '~/.bittensor/wallets/')"
assert (
str(w)
== "Wallet (Name: 'default', Hotkey: 'default', Path: '~/.bittensor/wallets/')"
)
assert w.name == "default"
assert w.hotkey_str == "default"
assert w.path == "~/.bittensor/wallets/"


def test_wallet_string_representation_with_custom_arguments():
"""Tests wallet string representation with custom arguments."""
# Preps
Expand All @@ -568,7 +572,10 @@ def test_wallet_string_representation_with_custom_arguments():
w = Wallet(name="test_wallet", hotkey="test_hotkey", path="/tmp/tests_wallets/")

# Asserts
assert str(w) == f"Wallet (Name: '{wallet_name}', Hotkey: '{wallet_hotkey}', Path: '{wallet_path}')"
assert (
str(w)
== f"Wallet (Name: '{wallet_name}', Hotkey: '{wallet_hotkey}', Path: '{wallet_path}')"
)
assert w.name == wallet_name
assert w.hotkey_str == wallet_hotkey
assert w.path == wallet_path

0 comments on commit aab60f6

Please sign in to comment.