Skip to content

abigen v2: expose package-level ErrEventSignatureMismatch for event unpacking errors #34075

@rglKali

Description

@rglKali

Rationale

Currently, UnpackXxxEvent methods return signature mismatch errors defined inline via errors.New("event signature mismatch"). This makes it impossible for callers to distinguish between a signature mismatch and other unpacking failures (e.g. malformed ABI data, topic parsing errors) using errors.Is, since the error value is unexported and not addressable.

A concrete use-case: Block-wide log processing via eth_getBlockReceipts. When consuming all logs from a block rather than pre-filtering with eth_getLogs, a caller receives logs for every contract and every event in that block. Pre-filtering by topic before calling UnpackXxxEvent would introduce redundant checks, so the idiomatic approach is to attempt unpacking every log and continue on mismatch. However, any other error should halt execution and trigger an alert -- meaning a reliable, typed distinction between wrong event and corrupt data is critical for correctness.
Without a sentinel error, the only option is brittle string matching, which is not acceptable in production monitoring code.

Implementation

Define a package-level sentinel error in the generated runtime and return it from all UnpackXxxEvent methods on signature mismatch:

var ErrEventSignatureMismatch = errors.New("event signature mismatch")

Then replace the inline errors.New(...) calls with this sentinel, allowing callers to write:

if len(log.Topics) == 0 || log.Topics[0] != usdt.abi.Events[event].ID {
	return nil, ErrXxxSignatureMismatch
}

The change is non-breaking, touches only the bind package and the abigen template, and the fix is straightforward. I'm willing to implement this.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions