alloy-rs / alloy-rs/core

[Feature] Make UInt panic on overflow if overflow_checks is enabled

Aberta
#828 3 comentários 0 reações 0 responsáveis Ver no GitHub
enhancement
Linguagem predominante
Rust
Estrelas
967
Forks
274
Merge médio
1d 21h
PRs com merge (30d)
15

Descrição

### Component

primitives

### Describe the feature you would like

Currently `UInt` wraps on overflow. Please, make it panic on overflow if `cfg(overflow_checks)` is enabled, i. e. exactly same thing rustc does with standard numbers. Here is why.

First and foremost, `UInt` is used mainly for representing monetary values. Wrapping behavior is unnatural for monetary values. So, `UInt` should panic on overflow. But still some users may want to skip checks for speed reasons. So, we should provide way to configure behavior. The most natural thing to do is to make `UInt` panic when standard numbers panic and wrap when standard types wrap. So, we should use `cfg(overflow_checks)`.

Of course, this is breaking change, so it should be done in next major version.

I use `alloy` for managing my personal cryptocurrency, i. e. for transferring it around. Unnoticed overflows may cause loss of money, I absolutely don't want this. So, I plan to create my wrapper around `U256`, which will panic on overflow. And I also will use `clippy::arithmetic_side_effects` to make sure that I will not use arithmetic directly on `ruint`/`alloy`'s `U256`. But ideally this should be fixed in `alloy` itself.

Also, currently Solidity throws exception on integer overflow by default ( https://docs.soliditylang.org/en/v0.8.28/types.html#integers ), so currently `alloy` is less safe than Solidity!

I already reported this problem to `ruint` ( https://github.com/recmo/uint/issues/408 ), but they rejected my request. So, I propose to create `alloy`'s wrapper, which will panic on overflow (if `cfg(overflow_checks)`).

If you don't want to replace `UInt` everywhere, then at least create panicking wrapper and use it in places, where we clearly know that we are dealing with money. For example, `Provider::get_balance`. This method clearly returns monetary value, there is no any sense for wrapping behavior for this value (but I still suggest checking `cfg(overflow_checks)` for consistency with standard types)

### Additional context

_No response_

Guia de contribuição

Abrir o guia de contribuição

Avaliação

Esta issue ainda não foi avaliada.

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.