Skip to content

Latest commit

Β 

History

History
100 lines (63 loc) Β· 3.17 KB

File metadata and controls

100 lines (63 loc) Β· 3.17 KB

markdown Copy Edit

πŸ› οΈ Debugging Report: Smart Contract Hashing & EVM Opcode Issues

This document outlines the debugging process between Bonsu and Claude on resolving two key issues in the Tinc-Blockchain project:

  1. NumberFormatException due to improper hexadecimal parsing.
  2. IllegalArgumentException from unknown EVM opcodes.

βœ… Issue 1: NumberFormatException in SmartContractProcessTest

πŸ’₯ Problem

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

πŸ” Root Cause

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.