Migrate to native u256 and fix critical architectural bugs#38
Merged
Conversation
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.
🚀 Migrate to Native u256 and Fix Critical Bugs
This PR completes the migration from a custom
U256type to Zig's nativeu256, along with fixing critical architectural issues discovered during the migration.✅ Changes Summary
1. Native u256 Migration
Replaced custom
U256struct with Zig's nativeu256type throughout the codebase.Created Ethereum utility functions:
u256FromBytes()- Convert from big-endian bytes (Ethereum format)u256ToBytes()- Convert to big-endian bytesu256FromHex()- Parse from hex stringu256ToHex()- Format as hex stringu256ToU64()- Safe conversion to u64Updated all type definitions:
AbiValuenow usesu256instead ofU256u256u256u256u256Simplified arithmetic:
value.add(fee)→ New:value + feebalance.gte(required)→ New:balance >= requiredamount.mulScalar(10)→ New:amount * 10Kept legacy
U256wrapper for backwards compatibility (thin wrapper around nativeu256)2. Fixed Critical Architectural Bug (Issues #35, #37)
Problem: Dangling pointer in
Providerstructure causing segfaults and bus errors.Root Cause:
Solution: On-demand namespace creation
All Provider methods now create namespaces on-demand, eliminating dangling pointers.
3. Fixed Memory Leak in ABI Types (Issue #36)
Problem:
AbiType.toString()allocated memory for constant strings that were never freed.Solution: Return const string literals (no allocation needed)
4. Added JSON Value Cleanup
Created
src/rpc/free_json.zigwithfreeJsonValue()function to properly clean up JSON values returned from RPC calls, preventing memory leaks in network operations.📊 Files Changed (22 files)
Core Primitives & Types
src/primitives/uint.zig- New u256 utilities, legacy U256 wrappersrc/root.zig- Updated exports, deprecated U256src/abi/types.zig- u256 in AbiValue, fixed toString()src/abi/encode.zig- Native u256 encodingsrc/abi/decode.zig- Native u256 decodingsrc/abi/packed.zig- Packed encoding with u256src/types/transaction.zig- All gas/value fields use u256src/types/block.zig- Difficulty and fees use u256src/types/receipt.zig- Gas price uses u256RPC & Provider Layer
src/rpc/client.zig- Fixed JSON array handlingsrc/rpc/eth.zig- u256 return types, added defer cleanupsrc/rpc/types.zig- All u256 typessrc/rpc/free_json.zig- NEW: JSON cleanup utilitiessrc/providers/provider.zig- On-demand namespaces, u256 methodssrc/providers/http.zig- Pointer-based methodssrc/rlp/packed.zig- u256 byte conversionMiddleware
src/middleware/gas.zig- Native u256 arithmetic, fixed provider accessExamples
examples/02_query_blockchain.zig- Format specifiers for u256examples/03_send_transaction.zig- Format specifiers, native u256examples/05_transaction_receipts.zig- Native u256 arithmeticexamples/07_complete_workflow.zig- Format specifiersrun_all_examples.sh- NEW: Helper script✅ Testing & Verification
All Checks Pass
Examples Status
01_wallet_creation- Runs successfully02_query_blockchain- RPC calls work (was crashing, now fixed)03_send_transaction- Middleware works (was crashing, now fixed)04_smart_contracts- No memory leaks (was leaking, now fixed)05_transaction_receipts- Runs successfully06_event_monitoring- Runs successfully07_complete_workflow- Runs successfully💡 Benefits
Code Quality
+,-,*,/,==,<,>operatorsPerformance
u256better than custom typeMaintainability
🐛 Issues Resolved
Closes #35 - Bus error in RPC client (dangling pointer)
Closes #36 - Memory leak in AbiType.toString()
Closes #37 - Segmentation fault in middleware (dangling pointer)
🔗 Related Issues
Account Abstraction (ERC-4337) roadmap created: Issues #27-34
📝 Migration Notes
For Users
The API remains mostly compatible. If you were using
U256:Old way:
New way:
For Ethereum-specific conversions, use the utility functions:
The legacy
U256wrapper is still available for backwards compatibility.🧪 Testing
Tested on:
All core functionality verified working with native
u256.