Skip to content

Commit

Permalink
Pre-release of 0.2.0 (#3)
Browse files Browse the repository at this point in the history
* update build CI

* [feat] Add metadata v15 class (#1)

* add metadata v15 class and example in docstring

* fix docstring

* serde full

* add portable registry

* add registry from metadata directly

* add clone

* add clone to metadata

* Feat/decode by type string (#2)

* add decoding well-known types (i.e. in the type registry)

* not well-known types work for arr, tup?, vec

* add compact; fix primitive load

* bump edition

* add scale info type string format

* add tests

* fix primitives (char not in registry)

* check length first

* handle variants better

* handle options

* bump version

* fix options

* fix tests

* fix option parsing to return None or val

* add more tests

* use prerelease
  • Loading branch information
camfairchild authored Sep 12, 2024
1 parent 1d09e0b commit 7c1aa79
Show file tree
Hide file tree
Showing 9 changed files with 24,410 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
#
# maturin generate-ci github
#
name: CI
name: Build wheels

on:
push:
branches:
- main
tags:
- '*'
pull_request:
workflow_dispatch:

permissions:
Expand Down
115 changes: 113 additions & 2 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "bt_decode"
version = "0.0.1"
edition = "2018"
edition = "2021"

[lib]
name = "bt_decode"
Expand All @@ -20,10 +20,13 @@ features = ["extension-module"]
path = "libs/custom-derive"

[dependencies]
frame-metadata = { version = "16.0.0", features = [ "current", "decode" ], default-features = false }
frame-metadata = { version = "16.0.0", features = [ "current", "decode", "serde_full" ], default-features = false }
scale-decode = { version = "0.13.0", default-features = false }

codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [
"derive",
] }
scale-info = { version = "2.11.2", default-features = false }
scale-info = { version = "2.11.2", features = [ "serde" ], default-features = false }
serde_json = { version = "1.0.127", features = [ "alloc" ], default-features = false }
scale-bits = { version = "0.4.0", default-features = false }
scale-value = { version = "0.16.2", default-features = false }
68 changes: 67 additions & 1 deletion bt_decode.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Tuple
from typing import Any, List, Optional, Tuple

class AxonInfo:
# Axon serving block.
Expand Down Expand Up @@ -218,3 +218,69 @@ class DelegateInfo:
@staticmethod
def decode_delegated(encoded: bytes) -> List[Tuple["DelegateInfo", int]]:
pass

class MetadataV15:
"""
MetadataV15 is the 15th version-style of metadata for the chain.
It contains information about all the chain types, including the type signatures
of the Runtime API functions.
Example:
>>> import bittensor, bt_decode, scalecodec
>>> sub = bittensor.subtensor()
>>> v15_int = scalecodec.U32()
>>> v15_int.value = 15
>>> metadata_rpc_result = sub.substrate.rpc_request("state_call", [
... "Metadata_metadata_at_version",
... v15_int.encode().to_hex(),
... sub.substrate.get_chain_finalised_head()
])
>>> 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)
>>> print(metadata_v15.to_json())
"""

@staticmethod
def decode_from_metadata_option(encoded_metadata_v15: bytes) -> "MetadataV15":
pass
def to_json(self) -> str:
"""
Returns a JSON representation of the metadata.
"""
pass

class PortableRegistry:
"""
PortableRegistry is a portable for of the chains registry that
can be used to serialize and deserialize the registry to and from JSON.
Example:
>>> import bittensor, bt_decode, scalecodec
>>> sub = bittensor.subtensor()
>>> v15_int = scalecodec.U32()
>>> v15_int.value = 15
>>> metadata_rpc_result = sub.substrate.rpc_request("state_call", [
... "Metadata_metadata_at_version",
... v15_int.encode().to_hex(),
... sub.substrate.get_chain_finalised_head()
])
>>> 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)
>>> bt_decode.PortableRegistry.from_metadata_v15( metadata_v15 )
"""

registry: str # JSON encoded PortableRegistry

@staticmethod
def from_json(json_str: str) -> "PortableRegistry":
pass
@staticmethod
def from_metadata_v15(metadata_v15: MetadataV15) -> "PortableRegistry":
pass

def decode(
type_string: str, portable_registry: PortableRegistry, encoded: bytes
) -> Any:
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.0.1a"
version = "0.2.0a"
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 7c1aa79

Please sign in to comment.