Skip to content

Commit

Permalink
[Feat] encode by string (#8)
Browse files Browse the repository at this point in the history
* bump ver

* add encode method

* fix sequences

* fix tuple encode

* nump ver

* use prerelease

* add encode example to readme
  • Loading branch information
camfairchild authored Dec 5, 2024
1 parent 4b2a41d commit acad96a
Show file tree
Hide file tree
Showing 7 changed files with 903 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bt_decode"
version = "0.1.0"
version = "0.4.0-a0"
edition = "2021"

[lib]
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,41 @@ neurons_lite: List[NeuronInfoLite] = bt_decode.decode(
)
)
```

### encode by type string
*Note: This feature is unstable, but working for multiple types.*

You may also encode using a type-string formed from existing types by passing the metadata as pulled from a node (or formed manually).
```python
import bittensor, bt_decode, scalecodec
# Get subtensor connection
sub = bittensor.subtensor()
# Create a param for the RPC call, using v15 metadata
v15_int = scalecodec.U32()
v15_int.value = 15
# Make the RPC call to grab the metadata
metadata_rpc_result = sub.substrate.rpc_request("state_call", [
"Metadata_metadata_at_version",
v15_int.encode().to_hex(),
sub.substrate.get_chain_finalised_head()
])
# Decode the metadata into a PortableRegistry type
metadata_option_hex_str = metadata_rpc_result['result']
metadata_option_bytes = bytes.fromhex(metadata_option_hex_str[2:])
metadata_v15 = bt_decode.MetadataV15.decode_from_metadata_option(metadata_option_bytes)
registry = bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )


## Encode an integer as a compact u16
compact_u16: list[int] = bt_decode.encode(
"Compact<u16>", # type-string,
2**16-1,
registry
)
# [254, 255, 3, 0]
compact_u16_py_scale_codec = scalecodec.Compact()
compact_u16_py_scale_codec.value = 2**16-1
compact_u16_py_scale_codec.encode()

assert list(compact_u16_py_scale_codec.data.data) == compact_u16
```
18 changes: 18 additions & 0 deletions bt_decode.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,21 @@ def decode(
type_string: str, portable_registry: PortableRegistry, encoded: bytes
) -> Any:
pass

def encode(
type_string: str, portable_registry: PortableRegistry, to_encode: Any
) -> list[int]:
"""
Encode a python object to bytes.
Returns a list of integers representing the encoded bytes.
Example:
>>> import bittensor as bt
>>> res = bt.decode.encode("u128", bt.decode.PortableRegistry.from_json(...), 1234567890)
>>> res
[210, 2, 150, 73, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> bytes(res).hex()
'd2029649000000000000000000000000'
"""
pass
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "bt-decode"
version = "0.3.0a"
version = "0.4.0-a0"
description = "A wrapper around the scale-codec crate for fast scale-decoding of Bittensor data structures."
readme = "README.md"
license = {file = "LICENSE"}
Expand Down
Loading

0 comments on commit acad96a

Please sign in to comment.