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.
Rationale
Currently,
UnpackXxxEventmethods return signature mismatch errors defined inline viaerrors.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 witheth_getLogs, a caller receives logs for every contract and every event in that block. Pre-filtering by topic before callingUnpackXxxEventwould 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
UnpackXxxEventmethods on signature mismatch:Then replace the inline errors.New(...) calls with this sentinel, allowing callers to write:
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.