Skip to content

Commit 65e9d2d

Browse files
authored
Feat/rao changes (#9)
* update lib and pyi for NeuronInfo fields * bump ver * change ver to pre-release * chore: ruff * chore: ruff * use tuple instead * use older version of bittensor for testing * add more logging to test and attr fix
1 parent 817d79d commit 65e9d2d

12 files changed

+100
-79
lines changed

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bt_decode"
3-
version = "0.4.0"
3+
version = "0.5.0-a0"
44
edition = "2021"
55

66
[lib]

bt_decode.pyi

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class NeuronInfo:
6060
stake: List[
6161
Tuple[bytes, int]
6262
] # map of coldkey to stake on this neuron/hotkey (includes delegations)
63+
total_stake: int
64+
alpha_stake: int
65+
tao_stake: int
6366
rank: int
6467
emission: int
6568
incentive: int
@@ -94,6 +97,9 @@ class NeuronInfoLite:
9497
stake: List[
9598
Tuple[bytes, int]
9699
] # map of coldkey to stake on this neuron/hotkey (includes delegations)
100+
total_stake: int
101+
alpha_stake: int
102+
tao_stake: int
97103
rank: int
98104
emission: int
99105
incentive: int

pyproject.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "bt-decode"
3-
version = "0.4.0"
3+
version = "0.5.0-a0"
44
description = "A wrapper around the scale-codec crate for fast scale-decoding of Bittensor data structures."
55
readme = "README.md"
66
license = {file = "LICENSE"}
@@ -41,7 +41,7 @@ build-backend = "maturin"
4141
[project.optional-dependencies]
4242
dev = ["black==23.7.0","maturin", "ruff==0.4.7"]
4343
test = [
44-
"bittensor==8.2.0",
44+
"bittensor==7.3.1",
4545
"pytest==7.2.0",
4646
"pytest-asyncio==0.23.7",
4747
"pytest-mock==3.12.0",

src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ mod bt_decode {
9898
axon_info: AxonInfo,
9999
prometheus_info: PrometheusInfo,
100100
stake: Vec<(AccountId, Compact<u64>)>, // map of coldkey to stake on this neuron/hotkey (includes delegations)
101+
total_stake: Compact<u64>,
102+
alpha_stake: Compact<u64>,
103+
tao_stake: Compact<u64>,
101104
rank: Compact<u16>,
102105
emission: Compact<u64>,
103106
incentive: Compact<u16>,
@@ -127,6 +130,9 @@ mod bt_decode {
127130
axon_info: AxonInfo,
128131
prometheus_info: PrometheusInfo,
129132
stake: Vec<(AccountId, Compact<u64>)>, // map of coldkey to stake on this neuron/hotkey (includes delegations)
133+
total_stake: Compact<u64>,
134+
alpha_stake: Compact<u64>,
135+
tao_stake: Compact<u64>,
130136
rank: Compact<u16>,
131137
emission: Compact<u64>,
132138
incentive: Compact<u16>,

tests/test_decode_by_type_string.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
from typing import Any, Callable, Dict, Tuple
1+
from typing import Any, Dict, Tuple
22

3-
import dataclasses
4-
import unittest
53
import pytest
64

75
import bt_decode
8-
import substrateinterface, scalecodec
9-
import bittensor
10-
11-
from . import (
12-
get_file_bytes,
13-
fix_field as fix_field_fixes,
14-
py_getattr as py_getattr_fixes,
15-
)
166

177

188
TEST_TYPE_STRING_SCALE_INFO_DECODING: Dict[str, Tuple[str, Any]] = {

tests/test_decode_delegate_info.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@
2525
"take": lambda x: bittensor.U16_NORMALIZED_FLOAT(x),
2626
"nominators": lambda x: [(bittensor.u8_key_to_ss58(e[0]), e[1]) for e in x],
2727
}
28-
fix_field = lambda key, value, parent_key=None: fix_field_fixes(
29-
FIELD_FIXES, key, value, parent_key
30-
)
28+
29+
30+
def fix_field(key, value, parent_key=None):
31+
return fix_field_fixes(FIELD_FIXES, key, value, parent_key)
32+
3133

3234
ATTR_NAME_FIXES: Dict[str, str] = {
3335
# None
3436
"delegate_ss58": "hotkey_ss58",
3537
}
3638

37-
py_getattr = lambda obj, attr, parent_name=None: py_getattr_fixes(
38-
ATTR_NAME_FIXES, obj, attr, parent_name
39-
)
39+
40+
def py_getattr(obj, attr, parent_name=None):
41+
return py_getattr_fixes(ATTR_NAME_FIXES, obj, attr, parent_name)
4042

4143

4244
class TestDecodeDelegateInfo(unittest.TestCase):

tests/test_decode_neurons.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, Dict, List, Optional
1+
from typing import Callable, Dict, List
22

33
import dataclasses
44
import netaddr
@@ -44,18 +44,18 @@
4444
"weights": lambda x: [[e[0], e[1]] for e in x],
4545
}
4646

47-
fix_field = lambda key, value, parent_key=None: fix_field_fixes(
48-
FIELD_FIXES, key, value, parent_key
49-
)
47+
48+
def fix_field(key, value, parent_key=None):
49+
return fix_field_fixes(FIELD_FIXES, key, value, parent_key)
5050

5151

5252
ATTR_NAME_FIXES: Dict[str, str] = {
5353
# None
5454
}
5555

56-
py_getattr = lambda obj, attr, parent_name=None: py_getattr_fixes(
57-
ATTR_NAME_FIXES, obj, attr, parent_name
58-
)
56+
57+
def py_getattr(obj, attr, parent_name=None):
58+
return py_getattr_fixes(ATTR_NAME_FIXES, obj, attr, parent_name)
5959

6060

6161
class TestDecodeNeuronInfoLite(unittest.TestCase):

tests/test_decode_stake_info.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import bittensor
88

99
from . import (
10-
get_file_bytes,
1110
fix_field as fix_field_fixes,
1211
py_getattr as py_getattr_fixes,
1312
)
@@ -22,19 +21,21 @@
2221
"coldkey": lambda x: bittensor.u8_key_to_ss58(x),
2322
"hotkey": lambda x: bittensor.u8_key_to_ss58(x),
2423
}
25-
fix_field = lambda key, value, parent_key=None: fix_field_fixes(
26-
FIELD_FIXES, key, value, parent_key
27-
)
24+
25+
26+
def fix_field(key, value, parent_key=None):
27+
return fix_field_fixes(FIELD_FIXES, key, value, parent_key)
28+
2829

2930
ATTR_NAME_FIXES: Dict[str, str] = {
3031
# None
3132
"coldkey": "coldkey_ss58",
3233
"hotkey": "hotkey_ss58",
3334
}
3435

35-
py_getattr = lambda obj, attr, parent_name=None: py_getattr_fixes(
36-
ATTR_NAME_FIXES, obj, attr, parent_name
37-
)
36+
37+
def py_getattr(obj, attr, parent_name=None):
38+
return py_getattr_fixes(ATTR_NAME_FIXES, obj, attr, parent_name)
3839

3940

4041
class TestDecodeStakeInfo(unittest.TestCase):

tests/test_decode_subnet_hyp.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import bittensor
88

99
from . import (
10-
get_file_bytes,
1110
fix_field as fix_field_fixes,
1211
py_getattr as py_getattr_fixes,
1312
)
@@ -21,15 +20,17 @@
2120
FIELD_FIXES: Dict[str, Callable] = {
2221
# None
2322
}
24-
fix_field = lambda key, value, parent_key=None: fix_field_fixes(
25-
FIELD_FIXES, key, value, parent_key
26-
)
23+
24+
25+
def fix_field(key, value, parent_key=None):
26+
return fix_field_fixes(FIELD_FIXES, key, value, parent_key)
27+
2728

2829
ATTR_NAME_FIXES: Dict[str, str] = {"max_weights_limit": "max_weight_limit"}
2930

30-
py_getattr = lambda obj, attr, parent_name=None: py_getattr_fixes(
31-
ATTR_NAME_FIXES, obj, attr, parent_name
32-
)
31+
32+
def py_getattr(obj, attr, parent_name=None):
33+
return py_getattr_fixes(ATTR_NAME_FIXES, obj, attr, parent_name)
3334

3435

3536
class TestDecodeSubnetHyperparameters(unittest.TestCase):

tests/test_decode_subnet_info.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
},
2727
"owner": lambda x: bittensor.u8_key_to_ss58(x),
2828
}
29-
fix_field = lambda key, value, parent_key=None: fix_field_fixes(
30-
FIELD_FIXES, key, value, parent_key
31-
)
29+
30+
31+
def fix_field(key, value, parent_key=None):
32+
return fix_field_fixes(FIELD_FIXES, key, value, parent_key)
33+
3234

3335
ATTR_NAME_FIXES: Dict[str, str] = {
3436
"emission_values": "emission_value",
@@ -37,11 +39,12 @@
3739
"network_connect": "connection_requirements",
3840
"network_modality": "modality",
3941
"owner": "owner_ss58",
42+
"blocks_since_last_step": "blocks_since_epoch",
4043
}
4144

42-
py_getattr = lambda obj, attr, parent_name=None: py_getattr_fixes(
43-
ATTR_NAME_FIXES, obj, attr, parent_name
44-
)
45+
46+
def py_getattr(obj, attr, parent_name=None):
47+
return py_getattr_fixes(ATTR_NAME_FIXES, obj, attr, parent_name)
4548

4649

4750
class TestDecodeSubnetInfo(unittest.TestCase):
@@ -62,7 +65,13 @@ def test_decode_matches_python_impl(self):
6265
if not attr.startswith("__") and not callable(getattr(subnet_info, attr)):
6366
attr_count += 1
6467

65-
attr_py = py_getattr(subnet_info_py, attr)
68+
try:
69+
attr_py = py_getattr(subnet_info_py, attr)
70+
except AttributeError as e:
71+
print(f"Error getting attribute {attr}: {e}")
72+
print(subnet_info_py)
73+
raise e
74+
6675
if dataclasses.is_dataclass(attr_py):
6776
attr_rs = getattr(subnet_info, attr)
6877

@@ -112,7 +121,7 @@ def test_decode_vec_option_matches_python_impl(self):
112121

113122
subnet_info_list_py = (
114123
bittensor.SubnetInfo.list_from_vec_u8( # Option specified internally
115-
list(TEST_SUBNET_INFO_HEX["vec option normal"]())
124+
TEST_SUBNET_INFO_HEX["vec option normal"]()
116125
)
117126
)
118127

@@ -128,7 +137,13 @@ def test_decode_vec_option_matches_python_impl(self):
128137
):
129138
attr_count += 1
130139

131-
attr_py = py_getattr(subnet_info_py, attr)
140+
try:
141+
attr_py = py_getattr(subnet_info_py, attr)
142+
except AttributeError as e:
143+
print(f"Error getting attribute {attr}: {e}")
144+
print(subnet_info_py)
145+
raise e
146+
132147
if dataclasses.is_dataclass(attr_py):
133148
attr_rs = getattr(subnet_info, attr)
134149

tests/test_encode_by_type_string.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -162,39 +162,39 @@
162162
}
163163

164164

165-
TEST_TYPE_STRING_PLAIN_DECODING: Dict[str, Tuple[str, Any]] = {
166-
"bool": ("01", True),
167-
"bool ": ("00", False),
168-
"u8": ("01", 1),
169-
"u16": ("0100", 1),
170-
"u32": ("01000000", 1),
171-
"u64": ("0100000000000000", 1),
172-
"u128": ("01000000000000000000000000000000", 1),
173-
"Compact<u8>": ("00", 0),
174-
"Compact<u8> ": ("fd03", 2**8 - 1),
175-
"Compact<u16>": ("feff0300", 2**16 - 1),
176-
"Compact<u32>": ("03ffffffff", 2**32 - 1),
177-
"Compact<u64>": ("13ffffffffffffffff", 2**64 - 1),
178-
"Option<u8>": ("010c", 12),
179-
"Option<u8>": ("00", None),
180-
"Option<u32>": ("00", None),
181-
"Option<u32> ": ("0101000000", 1),
182-
"()": ("", ()),
183-
"[u8; 4]": ("62616265", (98, 97, 98, 101)),
184-
"[u8; 4]": ("62616265", [98, 97, 98, 101]),
185-
"Vec<u8>": ("0c010203", (1, 2, 3)),
186-
"Vec<u8> ": ("00", []),
187-
"Vec<u8> ": ("00", ()),
188-
"(u8, u16) ": ("7bffff", (123, 2**16-1)),
189-
"str": ("0c666f6f", "foo"),
190-
}
165+
TEST_TYPE_STRING_PLAIN_DECODING: list[tuple[str, tuple[str, Any]]] = [
166+
("bool", ("01", True)),
167+
("bool ", ("00", False)),
168+
("u8", ("01", 1)),
169+
("u16", ("0100", 1)),
170+
("u32", ("01000000", 1)),
171+
("u64", ("0100000000000000", 1)),
172+
("u128", ("01000000000000000000000000000000", 1)),
173+
("Compact<u8>", ("00", 0)),
174+
("Compact<u8> ", ("fd03", 2**8 - 1)),
175+
("Compact<u16>", ("feff0300", 2**16 - 1)),
176+
("Compact<u32>", ("03ffffffff", 2**32 - 1)),
177+
("Compact<u64>", ("13ffffffffffffffff", 2**64 - 1)),
178+
("Option<u8>", ("010c", 12)),
179+
("Option<u8>", ("00", None)),
180+
("Option<u32>", ("00", None)),
181+
("Option<u32> ", ("0101000000", 1)),
182+
("()", ("", ())),
183+
("[u8; 4]", ("62616265", (98, 97, 98, 101))),
184+
("[u8; 4]", ("62616265", [98, 97, 98, 101])),
185+
("Vec<u8>", ("0c010203", (1, 2, 3))),
186+
("Vec<u8> ", ("00", [])),
187+
("Vec<u8> ", ("00", ())),
188+
("(u8, u16) ", ("7bffff", (123, 2**16 - 1))),
189+
("str", ("0c666f6f", "foo")),
190+
]
191191

192192
TEST_TYPES_JSON = "tests/test_types.json"
193193

194194

195195
@pytest.mark.parametrize(
196196
"type_string,test_hex,test_value",
197-
[(x, y, z) for x, (y, z) in TEST_TYPE_STRING_PLAIN_DECODING.items()],
197+
[(x, y, z) for x, (y, z) in TEST_TYPE_STRING_PLAIN_DECODING],
198198
)
199199
class TestEncodeByPlainTypeString:
200200
# Test combinations of human-readable type strings and hex-encoded values

0 commit comments

Comments
 (0)