markdown Copy Edit
This document outlines the debugging process between Bonsu and Claude on resolving two key issues in the Tinc-Blockchain project:
NumberFormatExceptiondue to improper hexadecimal parsing.IllegalArgumentExceptionfrom unknown EVM opcodes.
During test execution, the following error was observed:
NumberFormatException: For input string: "96c384bc87c5c3db" under radix 16
markdown Copy Edit
This occurred at:
- Line 42
- Line 98
The error originated in the isHashBelowTarget method of HashUtils.java:
return Long.parseLong(hash.substring(0, 16), 16) < difficulty;
Although 16 characters is the maximum length for a hexadecimal string to be parsed as a long, certain values (like "96c384bc87c5c3db") can still exceed the safe range of a long, causing a NumberFormatException.
β
Solution
A safer generateValidHexHash method was implemented, and logic was added to ensure any parsing uses BigInteger or avoids unsafe conversions entirely.
π Relevant Files for Hashing
The following files were examined to trace and fix the hashing issue:
Block.java β For hash structure.
HashUtils.java β Core hashing logic.
BlockchainState.java β State management for blocks and transactions.
TxHasher.java and BlockHasher.java β Custom hash logic.
Consensus.java β Validation rules tied to hashing and difficulty.
βοΈ Issue 2: EVM Opcode Execution Error
π₯ Problem
A separate error was encountered during EVM bytecode execution:
makefile
Copy
Edit
java.lang.IllegalArgumentException: Unknown opcode: 0x60
The opcode 0x60 corresponds to PUSH1 β a basic EVM operation. This failure was triggered during smart contract tests.
π Root Cause
There were two separate Opcode enum definitions:
An inner enum inside Evm.java which did not include PUSH1.
A standalone Opcode.java file which correctly included PUSH1.
Since Evm.java referenced its own enum, it could not recognize 0x60.
β
Solution
Evm.java was updated to:
Include PUSH1 in the internal enum.
Add bytecode execution logic for PUSH1.
Improve error propagation for debugging.
π§ Future recommendation: Refactor to ensure only one centralized Opcode enum is used across the entire project to prevent inconsistencies.
π Relevant Files for EVM Debugging
To fix the opcode issue, these files were critical:
org.example.app.core.smartcontract.Evm.java β EVM logic and bytecode processing.
org.example.app.core.smartcontract.Opcode.java β Central list of supported opcodes.
π Summary Table
Issue Root Cause Fix Applied
NumberFormatException Hash value exceeds Java's long capacity Improved hash parsing with safe methods
IllegalArgumentException PUSH1 opcode not found in internal enum Unified opcode handling in Evm.java
π¨βπ» Maintainer Notes
Always use a centralized utility or enum for definitions like opcodes to prevent duplication.
When working with hexadecimal values in Java, prefer BigInteger over Long to avoid range errors.
Ensure all hashing methods are deterministic and length-safe across the system.