Skip to content

Conversation

@yihuang
Copy link
Contributor

@yihuang yihuang commented Nov 1, 2025

Description

  • use go-abi for much better efficiency and error handling

Closes: #789


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • tackled an existing issue or discussed with a team member
  • left instructions on how to review the changes
  • targeted the main branch

dynamicSize := 0
dynamicSize += abi.SizeString(t.Bech32Address)

return Bech32ToHexCallStaticSize + dynamicSize

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
// EncodeTo encodes Bech32ToHexCall to ABI bytes in the provided buffer
func (value Bech32ToHexCall) EncodeTo(buf []byte) (int, error) {
// Encode tuple fields
dynamicOffset := Bech32ToHexCallStaticSize // Start dynamic data after static section

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
func (t Bech32ToHexReturn) EncodedSize() int {
dynamicSize := 0

return Bech32ToHexReturnStaticSize + dynamicSize

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
// EncodeTo encodes Bech32ToHexReturn to ABI bytes in the provided buffer
func (value Bech32ToHexReturn) EncodeTo(buf []byte) (int, error) {
// Encode tuple fields
dynamicOffset := Bech32ToHexReturnStaticSize // Start dynamic data after static section

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
return nil, err
}
bz, err = result.Encode()
case Bech32ToHexID:

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
evmd/go.mod Outdated
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/ulikunitz/xz v0.5.15 // indirect
github.com/yihuang/go-abi v0.0.0-20251031110513-0571d8dc7aa0 // indirect
Copy link
Contributor Author

@yihuang yihuang Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO, make a release before mark this PR as ready for review.


// Big endian integer versions of function selectors
const (
Bech32ToHexID = 3873392158

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values

var _ abi.Method = (*Bech32ToHexCall)(nil)

const Bech32ToHexCallStaticSize = 32

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
return result, nil
}

const Bech32ToHexReturnStaticSize = 32

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
Solution:
- use go-abi for much better efficiency and error handling

update go-abi stdlib support

fix: convert interface{} to generated statically typed structs in precompile tests

Refactored test files to use generated statically typed structs from go-abi
instead of interface{}:

- gov/types_test.go: Updated 5 test functions
  * TestNewMsgDeposit → DepositCall
  * TestNewMsgCancelProposal → CancelProposalCall
  * TestNewMsgVote → VoteCall
  * TestParseVoteArgs → GetVoteCall
  * TestParseDepositArgs → GetDepositCall

- distribution/types_test.go: Updated 8 test functions
  * TestNewMsgSetWithdrawAddress → SetWithdrawAddressCall
  * TestNewMsgWithdrawDelegatorReward → WithdrawDelegatorRewardsCall
  * TestNewMsgFundCommunityPool → FundCommunityPoolCall
  * TestNewMsgDepositValidatorRewardsPool → DepositValidatorRewardsPoolCall
  * TestNewDelegationRewardsRequest → DelegationRewardsCall
  * TestNewDelegationTotalRewardsRequest → DelegationTotalRewardsCall
  * TestNewDelegatorValidatorsRequest → DelegatorValidatorsCall
  * TestNewDelegatorWithdrawAddressRequest → DelegatorWithdrawAddressCall

- slashing/types_test.go: Updated 1 test function
  * TestParseSigningInfoArgs: Simplified to remove unnecessary type checks

Removed type validation test cases (now handled by Go's type system)
and kept only business logic tests. Removed unused fmt imports where applicable.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

fix test

fix: convert interface{} to generated statically typed structs in precompile tests

Convert all precompile test files to use generated statically typed structs
instead of []interface{}, providing better type safety and developer experience.

Changes:
- distribution/types_test.go: Convert test arguments from []interface{} to generated structs
- staking/types_test.go: Convert all test cases to use generated statically typed structs:
  * CreateValidatorCall, DelegateCall, UndelegateCall, RedelegateCall
  * CancelUnbondingDelegationCall, DelegationCall, UnbondingDelegationCall
  * Updated struct field names to match generated types (Commission → CommissionRates)
  * Removed unused imports
- slashing/types_test.go: Convert test arguments from []interface{} to GetSigningInfoCall
- slashing/query.go: Updated query functions to use generated struct types
- slashing/tx.go: Updated transaction functions to use generated struct types
- slashing/types.go: Updated ParseSigningInfoArgs and ParseSigningInfosArgs
- slashing/slashing.go: Added PageRequest mapping to go:generate directive
- slashing/slashing.abi.go: Regenerated to use cmn.PageRequest instead of duplicate type

Results:
- All tests pass successfully
- 517 lines of code removed
- Type safety improved with compile-time checking
- Better IDE autocomplete and type hints

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

perf: remove redundant *Output structs and move FromResponse to *Return structs

Refactor precompile code to remove redundant *Output structs (SigningInfoOutput,
SigningInfosOutput, ParamsOutput) and their corresponding custom types. The
FromResponse methods now directly populate the generated *Return structs.

Benefits:
- Removed 78 lines of redundant code
- Eliminated type duplication between *Output and *Return structs
- Simplified query functions by removing unnecessary conversions
- Direct use of generated statically typed structs

Changes in slashing precompile:
- Deleted SlashingSigningInfo, SigningInfoOutput, SigningInfosOutput structs
- Deleted SlashingParams, ParamsOutput structs
- Moved FromResponse method receivers to *GetSigningInfoReturn, *GetSigningInfosReturn, *GetParamsReturn
- Simplified query.go functions to directly use *Return structs
- All tests pass successfully

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

feat: migrate remaining precompiles to use generated statically typed structs

Convert bank, bech32, erc20, and werc20 precompiles to use generated
statically typed structs instead of []interface{}, following the pattern
established in staking, distribution, and slashing precompiles.

Changes:
- bank/types.go: Removed unused ParseBalancesArgs and ParseSupplyOfArgs functions
- bech32/methods.go: Updated HexToBech32 and Bech32ToHex to use generated Call/Return structs
- bech32/bech32.go: Updated Run function to decode args directly using generated structs
- erc20/approve.go: Updated Approve to use ApproveCall and ApproveReturn
- erc20/erc20.go: Updated Execute to use cmn.Run and cmn.RunWithStateDB with ID constants
- erc20/query.go: Converted all query functions to use generated return types
- erc20/tx.go: Updated Transfer/TransferFrom to use generated Call/Return structs
- erc20/types.go: Removed all Parse*Args functions (replaced by generated structs)
- werc20/tx.go: Updated Withdraw to use WithdrawCall and WithdrawReturn
- werc20/werc20.go: Updated Execute and IsTransaction to use ID constants

Benefits:
- ~500+ lines of code removed across all precompiles
- Type safety improved with compile-time checking
- Better IDE autocomplete and type hints
- Consistent patterns across all precompiles

Build Status:
✅ All migrated precompiles build successfully
✅ All tests pass (staking, distribution, slashing)

Note: ics20 precompile deferred (already builds successfully but uses old pattern)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

update go-abi

cleanup

changelog

fix build

feat: migrate bank precompile integration tests to go-abi

Migrate bank precompile tests from go-ethereum ABI to custom go-abi library:

- Updated ContractData struct to remove precompileABI field
- Replaced UnpackIntoInterface/Unpack calls with manual decoding helpers
- Updated getTxAndCallArgs to use EncodeWithSelector() for direct precompile calls
- Rewrote test_query.go unit tests to use typed call structs (BalancesCall, SupplyOfCall, TotalSupplyCall)
- Created decoder helper functions for type-safe result decoding
- Tests now use fully type-safe generated types instead of reflection

Created comprehensive MIGRATION_GUIDE.md with step-by-step instructions
for migrating other precompile tests to go-abi.

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

feat: migrate bech32 and slashing precompile integration tests to go-abi

Migrated bech32 and slashing precompile tests from go-ethereum ABI to custom go-abi library:

Bech32 precompile (tests/integration/precompiles/bech32/):
- Updated all test cases to use typed call structs (HexToBech32Call, Bech32ToHexCall)
- Replaced Pack() calls with EncodeWithSelector() for input encoding
- Replaced Unpack() calls with direct Decode() on result types
- Rewrote unit tests to use direct precompile methods instead of generic interface

Slashing precompile (tests/integration/precompiles/slashing/):
- Updated TestGetSigningInfo to use GetSigningInfoCall with ConsAddress field
- Updated TestGetSigningInfos to use GetSigningInfosCall with Pagination field
- Updated TestGetParams to use GetParamsCall with EmptyTuple
- Replaced all UnpackIntoInterface calls with direct result type decoding
- Tests now use fully type-safe generated types instead of reflection

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

cleanup bank test

refactor tests

fix tests

fix: update factory to use abi.Method from CallArgs

Update ExecuteContractCall and QueryContract to use callArgs.Method
instead of callArgs directly to match the new GenerateContractCallArgs
signature that expects abi.Method interface.

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

docs: update MIGRATION_GUIDE.md with status and troubleshooting

- Added migration status table for all 10 precompiles
- Updated decode examples to show recommended approach (direct Decode)
- Added troubleshooting section with common issues and solutions:
  * s.precompile.Methods undefined
  * s.precompile.ABI undefined
  * Too many arguments in calls
  * Factory calls with CallArgs
- Added references to Bech32 and Slashing migrations
- Updated examples to be more comprehensive

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

fix: variable declarations in BeforeEach blocks

- Use short declaration operator (:=) in BeforeEach blocks instead of separate var declarations
- Fix DepositEvent and WithdrawalEvent to use pointers: &werc20.DepositEvent{}
- Fix ERC20 return type field access (Field1 instead of Balance, Name, Symbol, Decimals)
- Remove unused crypto import from test_events.go

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Co-Authored-By: Happy <[email protected]>

fix gov tests

fix gov events test

fix gov test build

remove unused methods

fix build bank tests

fix: migrate distribution and erc20 precompiles to go-abi

Migrate precompile tests from go-ethereum to go-abi following the gov precompile pattern.

Changes:
- Distribution precompile: Regenerated ABI and updated test files to use new go-abi encoding pattern
- ERC20 precompile: Updated test files to use new call struct pattern and method interface
- Updated imports from ethereum/accounts/abi to yihuang/go-abi
- Replaced precompile.Methods[] and Pack() with call structs and EncodeWithSelector()
- Updated IsTransaction() calls to use method.GetMethodID()

This enables the precompiles to build successfully with the new go-abi framework.

🤖 Generated with [Claude Code](https://claude.ai/claude-code)

Co-Authored-By: Claude <[email protected]>

fix: migrate slashing precompile to go-abi

Migrate slashing precompile tests from go-ethereum to go-abi following the established pattern.

Changes:
- test_tx.go: Updated to use new call struct pattern (UnjailCall) instead of Methods[] and Pack()
- test_query.go: Updated to use common.PageRequest and slashing.PageResponse types
- Fixed method signatures to use pointer receivers for call structs
- Removed unused imports (fmt, testutil)

This enables the slashing precompile main test files to build successfully with the new go-abi framework.

🤖 Generated with [Claude Code](https://claude.ai/claude-code)

Co-Authored-By: Claude <[email protected]>

temp

fix erc20 tests

temp

// GetMethodID returns the function id
func (t Bech32ToHexCall) GetMethodID() uint32 {
return Bech32ToHexID

Check warning

Code scanning / CodeQL

Directly using the bech32 constants Warning

Directly using the bech32 constants instead of the configuration values
Comment on lines +62 to +64
for k, v := range generator.ParseExternalTuples(*extTuplesFlag) {
extTuples[k] = v
}

Check warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism
@yihuang yihuang changed the title Problem: geth abi library is not suitable for precompile feat: use go-abi to replace go-ethereum abi library Nov 4, 2025
@yihuang yihuang marked this pull request as ready for review November 4, 2025 05:17
@yihuang yihuang marked this pull request as draft November 4, 2025 06:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: use go-abi to replace go-ethereum abi library

1 participant