Skip to content

feat: Yul bit-slicing engine, delegatecall forwarder, batch balance checker, G015 storage packer - #702

Merged
mijinummi merged 1 commit into
MDTechLabs:mainfrom
zoffunjunior381-jpg:feat/issues-690-691-692-695
Jul 29, 2026
Merged

feat: Yul bit-slicing engine, delegatecall forwarder, batch balance checker, G015 storage packer#702
mijinummi merged 1 commit into
MDTechLabs:mainfrom
zoffunjunior381-jpg:feat/issues-690-691-692-695

Conversation

@zoffunjunior381-jpg

Copy link
Copy Markdown
Contributor

Summary

  • [CORE] Build High-Performance Yul Bitmask & Array Slicing Engine #695contracts/utils/YulBitSlice.sol: pure-Yul library with extractBits(bytes calldata, bitOffset, bitLength) that reads arbitrary bit-length slices directly from calldata via calldataload, handling slices that straddle a 32-byte word boundary, without copying the buffer into memory. Also adds a byte-aligned sliceBytes convenience wrapper.
  • [CORE] Implement Zero-Overhead Universal Delegatecall Forwarder in Yul #691contracts/proxy/YulProxyForwarder.sol: minimal fallback/receive forwarder that does calldatacopydelegatecall(gas(), implementation, ...)returndatacopyreturn/revert, entirely in assembly, so proxied calls only pay for those four opcodes plus the delegatecall itself.
  • [PERF] Build Assembly Multi-Token Balance Query Loop #690contracts/utils/BatchBalanceChecker.sol: batchBalances(tokens, accounts) builds the balanceOf(address) selector once, then loops over the token x account matrix with staticcall, writing results into a flat memory array. Reverting or non-contract targets resolve to 0 instead of aborting the whole batch.
  • [CORE] Rule G015: Automated AST Storage Slot Re-Ordering Transformer #692gasguard-cli Rule G015: new transformers::storage_packer module that classifies state variable declarations (reusing the existing storage_model packing algorithm), then rewrites the contract source to group packable variables into shared 256-bit slots. Leading comments stay attached to their variable, and everything outside the declaration run (imports, structs, functions) is left byte-for-byte untouched. Wired up behind a new reorder-storage CLI command alongside the existing optimize-storage. Also added gasguard-cli/test/fixtures/storage_packing_transform.sol as a worked example (5 slots → 2 slots).

Assumptions / scope notes

  • The G015 transform only reorders the contiguous run of simple state-variable declarations (no initializer, no function/struct/mapping-with-body noise) in the first contract/library body — it intentionally does not attempt a full AST rewrite of arbitrarily interleaved declarations, matching the lightweight text-based parsing style already used by commands::optimize_storage.
  • gasguard-cli/Cargo.toml needed an explicit [workspace] table added — the crate wasn't previously buildable/testable at all (cargo test failed immediately with "current package believes it's in a workspace when it's not") because it isn't listed in the root Cargo.toml workspace members. This is a minimal, non-invasive fix so the new tests (and the crate generally) can actually run; it does not add gasguard-cli to the root workspace.
  • test/*.test.ts files for the new Solidity contracts follow the exact structure of the existing test/utils/MappingResolver.test.ts and other test/**/*.test.ts files in this repo — note that these files use vitest imports and are not currently picked up by the npm test (jest) script, whose testRegex only matches *.spec.ts$. That's a pre-existing repo-wide condition affecting all test/**/*.test.ts files, not something introduced here.

Test plan

  • forge build contracts/utils/YulBitSlice.sol contracts/proxy/YulProxyForwarder.sol contracts/utils/BatchBalanceChecker.sol — compiles cleanly (Solc 0.8.20)
  • cd gasguard-cli && cargo test — all new transformers::storage_packer tests pass (7/7); 2 pre-existing failures in commands::optimize_storage reproduce identically on main and are unrelated to this change (out of scope)
  • Maintainer review of the G015 reordering heuristic and Yul assembly safety comments

Closes #690
Closes #691
Closes #692
Closes #695

…ce checker, and G015 storage packer

- YulBitSlice: extracts arbitrary bit-length slices from calldata buffers
  using pure Yul assembly, handling unaligned offsets and 32-byte word
  boundary straddling without intermediate memory copies.
- YulProxyForwarder: minimal delegatecall forwarder that copies calldata,
  delegatecalls the implementation, and relays return data/revert reason
  verbatim, keeping proxy overhead to a fixed handful of opcodes.
- BatchBalanceChecker: queries balanceOf across a token x account matrix
  in one call using staticcall in an assembly loop, building the selector
  once and tolerating reverting/non-contract targets without halting.
- Rule G015 (gasguard-cli): storage_packer transformer that classifies
  state variables, reuses the existing storage_model packing algorithm,
  and rewrites contract source to group packable variables into shared
  slots while preserving comments and untouched code exactly. Wired up
  behind a new reorder-storage command alongside optimize-storage.

Closes MDTechLabs#690
Closes MDTechLabs#691
Closes MDTechLabs#692
Closes MDTechLabs#695
@drips-wave

drips-wave Bot commented Jul 28, 2026

Copy link
Copy Markdown

@zoffunjunior381-jpg Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@mijinummi
mijinummi merged commit 3aeed0e into MDTechLabs:main Jul 29, 2026
3 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants