Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

abi/bind: Fix bugs #195

Merged
merged 8 commits into from
Jan 6, 2025
Merged

abi/bind: Fix bugs #195

merged 8 commits into from
Jan 6, 2025

Conversation

ian0371
Copy link
Collaborator

@ian0371 ian0371 commented Dec 31, 2024

Proposed changes

This PR brings some abi/bind PRs from geth. It is expected to fix panic in CI failure, although another error may occur.

These are considered for future PR: (NameConflict, NumericMethodName tests)

Types of changes

  • Bugfix
  • New feature or enhancement
  • Others

Checklist

  • I have read the CONTRIBUTING GUIDELINES doc
  • I have read the CLA and signed by comment I have read the CLA Document and I hereby sign the CLA in first time contribute
  • Lint and unit tests pass locally with my changes ($ make test)
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)
  • Any dependent changes have been merged and published in downstream modules

Further comments

There are nine contracts whose compiled bytecode changed more than 32 bytes, which are all unrelated to hardforks:

  • service_chain/Bridge.go: last go generate was ed74650 (2024-04-01), but last solidity modification was 91aad89 (2024-04-21).
    • BridgeMetaData: affected by Bridge -> BridgeTransferERC20 -> BridgeTokens
    • BridgeCounterPartMetaData
    • BridgeTokensMetaData
  • testing/extbridge/ext_bridge.go: last go generate was 83331ce (2024-04-01), but last solidity modification was 3f06cb6 (2024-04-21).
    • BridgeTokensMetaData
    • ExtBridgeMetaData: affected by ExtBridge -> BridgeTransferERC20 -> BridgeTransfer -> BridgeOperator
  • system_contracts/consensus/Kip163.go: last go generate was 2ab8803 (2024-06-17), but last solidity modification was 30ffe48 (2024-06-21).
    • PublicDelegationMetaData
    • PublicDelegationFactoryMetaData
  • testing/system_contracts/all.go (TreasuryRebalanceV2): last solidity modification was 0760656 (2024-06-24). Note that only the bytecode for tests is updated, and now it became equal to that of system_contracts/rebalance/all.go.
    • TreasuryRebalanceV2MetaData: last go generate was 69351f5 (2024-05-12)
    • TreasuryRebalanceMockV2MetaData: last go generate was 4032e53 (2024-05-16)
Filtering contracts with 32+ bytes bytecode changes
git diff 977e1348 4e7f8d7c > input.diff
python run.py input.diff

run.py:

#!/usr/bin/env python
import argparse, re

global args

minus_pattern = re.compile(r'-\s*Bin:\s*"0x([0-9a-f]+)"')
plus_pattern = re.compile(r'\+\s*Bin:\s*"0x([0-9a-f]+)"')

def process_diff(input_filename):
    with open(input_filename, "r") as f:
        lines = f.readlines()

    # Split diff into each file diff
    file_diffs = "".join(lines).split("diff --git")
    assert file_diffs[0] == "", "improper diff file"

    # Process each chunk (skip first empty chunk)
    for diff in file_diffs[1:]:
        # Extract file name from first line: diff --git a/... b/...
        filename = diff.split(" ")[1].lstrip("a/")
        check_bin_diff(diff, filename)

def check_bin_diff(file_diff, file_name):
    # Split file diff into chunks based on @@ markers.
    chunks = []
    chunks = ["@@" + "@@".join(p) for p in zip(*[iter("".join(file_diff).split("@@")[1:])] * 2)]
    for chunk in chunks:
        minus_match = minus_pattern.findall(chunk)
        if not minus_match:
            continue
        minus_bytes = bytes.fromhex(minus_match[0])

        plus_match = plus_pattern.findall(chunk)
        plus_bytes = bytes.fromhex(plus_match[0])
        
        # Count differing bits
        offsets = get_diff_offsets(minus_bytes, plus_bytes)
        if len(offsets) > args.threshold:
            var_match = re.findall(r'var ([a-zA-Z0-9]+)', chunk)
            var_name = var_match[0]
            print(f"{file_name}:{var_name} has {len(offsets)} different bytes. First diff offset: {min(offsets) - len(minus_bytes)}")

def get_diff_offsets(b1, b2):
    # Pad shorter bytes with zeros to match longer length
    max_len = max(len(b1), len(b2))
    b1 = b1.ljust(max_len, b'\x00')
    b2 = b2.ljust(max_len, b'\x00')
    
    offsets = []
    for i, (c1, c2) in enumerate(zip(b1, b2)):
        if c1 != c2:
            offsets.append(i)
    return offsets

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Process git diff file')
    parser.add_argument('diff_file', help='Git diff file (e.g. from: git diff HEAD HEAD~5 > input.diff)')
    parser.add_argument('-t', '--threshold', type=int, default=32, help='Byte diff threshold (default: 32)')
    args = parser.parse_args()
    process_diff(args.diff_file)
Finding the last `go generate` and solidity modification

Note the contract dependencies.

contract Bridge is BridgeCounterPart, BridgeTransferKLAY, BridgeTransferERC20, BridgeTransferERC721
contract ExtBridge is BridgeTransferERC20, BridgeTransferERC721
contract BridgeTransferERC20 is BridgeTokens, IERC20BridgeReceiver, BridgeTransfer
contract BridgeTransfer is BridgeHandledRequests, BridgeFee, BridgeOperator

Checkout the git blame of "*MetaData.Bin" objects:

# Bridge.go
git log contracts/contracts/service_chain/bridge/*.sol
* 91aad896 - contracts: Remove comparison against boolean literal (2024-04-21) <ollie.j>
git blame 7c60b7e40~ -- contracts/contracts/service_chain/bridge/Bridge.go | rg 'var BridgeMetaData' -A100 | rg "Bin:" | cut -c -80
ed7465076 (ollie.j      2024-04-01 21:43:24 +0900   266)        Bin: "0x60806040819052
git blame 7c60b7e40~ -- contracts/contracts/service_chain/bridge/Bridge.go | rg 'var BridgeCounterPartMetaData' -A100 | rg "Bin:" | cut -c -80
ed7465076 (ollie.j      2024-04-01 21:43:24 +0900   266)        Bin: "0x60806040819052
git blame 7c60b7e40~ -- contracts/contracts/service_chain/bridge/Bridge.go | rg 'var BridgeTokensMetaData' -A100 | rg "Bin:" | cut -c -80
ed7465076 (ollie.j      2024-04-01 21:43:24 +0900  5792)        Bin: "0x60806040819052

# ext_bridge.go: affected by BridgeOperator.sol
git log contracts/contracts/service_chain/bridge/BridgeOperator.sol
* 3f06cb69 - contracts: Emit events in Bridge configuration changes (2024-04-21) <ollie.j>
git blame 7c60b7e40~ -- contracts/contracts/testing/extbridge/ext_bridge.go | rg 'var BridgeTokensMetaData' -A100 | rg "Bin:" | cut -c -80
83331ceff (Chihyun Song 2024-04-01 23:29:59 +0900  1810)        Bin: "0x60806040819052
git blame 7c60b7e40~ -- contracts/contracts/testing/extbridge/ext_bridge.go | rg 'var ExtBridgeMetaData' -A100 | rg "Bin:" | cut -c -80
83331ceff (Chihyun Song 2024-04-01 23:29:59 +0900 18193)       Bin: "0x60806040819052

# Kip163.go
git log contracts/contracts/system_contracts/consensus/PublicDelegation/*.sol
* 30ffe48d - (upstream/pd-commission-rate) Set commission limit to 100% (2024-06-21) <hyeonLewis>
git blame 7c60b7e40~ -- contracts/contracts/system_contracts/consensus/Kip163.go | rg 'var PublicDelegationMetaData' -A100 | rg "Bin:" | cut -c -80
2ab88035f (hyeonLewis   2024-06-17 11:40:02 +0900 38363)        Bin: "0x60a06040523480
git blame 7c60b7e40~ -- contracts/contracts/system_contracts/consensus/Kip163.go | rg 'var PublicDelegationFactoryMetaData' -A100 | rg "Bin:" | cut -c -80
2ab88035f (hyeonLewis   2024-06-17 11:40:02 +0900 41679)        Bin: "0x60806040523480

# testing/system_contracts/all.go (TreasuryRebalanceV2)
git log contracts/contracts/system_contracts/rebalance/TreasuryRebalanceV2.sol
* 07606565 - add setPendingMemo at trV2 (2024-06-24) <yumiel.ko>
git blame 7c60b7e40~ -- contracts/contracts/testing/system_contracts/all.go | rg 'var TreasuryRebalanceMockV2MetaData' -A100 | rg "Bin:" | cut -c -80
4032e53a1 (Chihyun Song 2024-05-16 10:52:25 +0900 16154)        Bin: "0x60806040523480
git blame 7c60b7e40~ -- contracts/contracts/testing/system_contracts/all.go | rg 'var TreasuryRebalanceV2MetaData' -A100 | rg "Bin:" | cut -c -80
69351f5bc (hyeonLewis   2024-05-12 13:21:40 +0900 18430)        Bin: "0x60806040523480

@ian0371 ian0371 self-assigned this Dec 31, 2024
@blukat29
Copy link
Contributor

blukat29 commented Jan 3, 2025

Generated Bin has changed. Some cases are just compiler metadata change at the end like consensus/consensus.go, but some cases involve opcode change in the middle like consensus/kip163.go:38363. It might affect some hardforks. Please check.

@ian0371
Copy link
Collaborator Author

ian0371 commented Jan 3, 2025

@blukat29 Please see further comments section. No contracts are related to HF. (Thankfully TRv2 for production is unaffected)

@ian0371 ian0371 merged commit 7b4a6f3 into kaiachain:dev Jan 6, 2025
11 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Jan 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants