refactor: centralize SCALE codec primitives in utils/scale.py#52
Merged
LandynDev merged 2 commits intoentrius:testfrom Apr 17, 2026
Merged
refactor: centralize SCALE codec primitives in utils/scale.py#52LandynDev merged 2 commits intoentrius:testfrom
LandynDev merged 2 commits intoentrius:testfrom
Conversation
Collaborator
|
Hi @fansilas , thanks for the contribution. I think this could make the codebase cleaner from a quick skim, can you fix conflicts before I look more into it? |
Contributor
Author
|
Hi @LandynDev |
LandynDev
approved these changes
Apr 17, 2026
2 tasks
plind-junior
added a commit
to plind-junior/allways
that referenced
this pull request
Apr 18, 2026
…ak assertions - test_subtensor.py: remove TestDecodeCompact and TestIsValidAddress (already in test_scale.py from entrius#52); keep only TestProviderBasics - test_axon_handlers.py: TestRejectSynapse — patch bt.logging.debug and assert (no_context → not called; with_context → called once with '{context}: {reason}'); drop the three keccak property tests that exercise pycryptodome rather than our wrapper - test_misc.py: drop test_preserves_function_metadata (functools.wraps) and test_returns_current_block_from_subtensor (mock pass-through) - test_logging.py: drop test_returns_logger_at_events_level, test_registers_event_level_name, test_adds_handler — all just verify stdlib logging plumbing we passed through - test_config.py: drop pure-default argparse tests; keep override tests which actually exercise our flag-name + type wiring - test_metadata.py: deleted (only checks pathlib behavior on a Path(__file__).parent constant) 363 tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SCALE primitive encoders and decoders were duplicated across three modules:
contract_client.py,validator/event_watcher.py, andvalidator/axon_handlers.py.Each copy carried its own magic byte widths, hex-prefix stripping, and compact-length encoders. This collapses them into a single
allways/utils/scale.pythat all three modules now import from.No behavior change for well-formed contract or RPC data. Where the three copies disagreed on error handling (out-of-bounds
decode_string, mode-2 compact address length, stray0xsubstrings), the extracted module picks the more defensive variant so malformed input fails gracefully rather than silently corrupting output.Changes
New module:
allways/utils/scale.pySS58_PREFIX,U32_BYTES,U64_BYTES,U128_BYTES,ACCOUNT_ID_BYTESstrip_hex_prefix,compact_encode_len,encode_bytes,encode_str,encode_u128(value, new_offset):decode_u32,decode_u64,decode_u128,decode_bool,decode_account_id,decode_stringallways/contract_client.pycompact_encode_lendefinition and inline hex-prefix / u128 / byte-width logicencode_value,extract_*,decode_swap_data,get_reservation_data, andraw_contract_readnow delegate to the shared helpersdecode_stringmethod that just forwarded to the utility4,8,16,32literalsallways/validator/event_watcher.pydecode_u32/u64/u128/bool/account_id/stringin favor of imports fromutils/scaletopic_account_iddelegates todecode_account_id, and the hex-prefix strip into_bytesusesstrip_hex_prefixstructandss58_encodeimports removed (no longer needed locally)allways/validator/axon_handlers.pyscale_encode_*_hash_inputfunctions now composeencode_bytes,encode_str, andencode_u128instead of inliningcompact_encode_len(len(...)) + ...and.to_bytes(16, 'little')encode_strlambda insidescale_encode_initiate_hash_inputis gonetests/test_scale.pyTestDecodeStringexercisesdecode_stringfrom the utility directly now that the client shim is gonecompact_encode_lenimported from its new homeTesting
pytest)ruff check allways/ tests/cleanruff format --check allways/ tests/cleanstruct.pack('<QQ', lo, hi)across the range[0, 2**128 - 1]including boundaries at2**64and2**127ss58_encode(bytes, 42)produces the same output as the previoussubstrate.ss58_encode(bytes.hex())path on Bittensor