Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

U256 deserialization fails for dec_str #2353

Closed
0xrainbowtrout opened this issue Apr 15, 2023 · 2 comments · Fixed by #2354
Closed

U256 deserialization fails for dec_str #2353

0xrainbowtrout opened this issue Apr 15, 2023 · 2 comments · Fixed by #2354
Labels
bug Something isn't working

Comments

@0xrainbowtrout
Copy link
Contributor

Version
List the versions of all ethers-rs crates you are using. The easiest way to get this information is using cargo-tree.

├── ethers v2.0.3
│   ├── ethers-addressbook v2.0.3
│   │   ├── ethers-core v2.0.3
│   ├── ethers-contract v2.0.3
│   │   ├── ethers-contract-abigen v2.0.3
│   │   │   ├── ethers-core v2.0.3 (*)
│   │   │   ├── ethers-etherscan v2.0.3
│   │   │   │   ├── ethers-core v2.0.3 (*)
│   │   │   │   ├── ethers-solc v2.0.3
│   │   │   │   │   ├── ethers-core v2.0.3 (*)
│   │   ├── ethers-contract-derive v2.0.3 (proc-macro)
│   │   │   ├── ethers-contract-abigen v2.0.3
│   │   │   │   ├── ethers-core v2.0.3 (*)
│   │   │   ├── ethers-core v2.0.3 (*)
│   │   ├── ethers-core v2.0.3 (*)
│   │   ├── ethers-providers v2.0.3
│   │   │   ├── ethers-core v2.0.3 (*)
│   ├── ethers-core v2.0.3 (*)
│   ├── ethers-etherscan v2.0.3 (*)
│   ├── ethers-middleware v2.0.3
│   │   ├── ethers-contract v2.0.3 (*)
│   │   ├── ethers-core v2.0.3 (*)
│   │   ├── ethers-etherscan v2.0.3 (*)
│   │   ├── ethers-providers v2.0.3 (*)
│   │   ├── ethers-signers v2.0.3
│   │   │   ├── ethers-core v2.0.3 (*)
│   ├── ethers-providers v2.0.3 (*)
│   ├── ethers-signers v2.0.3 (*)
│   └── ethers-solc v2.0.3 (*)

Platform
The output of uname -a (UNIX), or version and 32 or 64-bit (Windows)

Linux XXXXXXXX 6.2.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 30 Mar 2023 14:51:14 +0000 x86_64

Description

I was trying to read data from a json that consists of a number of U256s and kept getting unexpected results (the numbers were getting deserialized incorrectly). It appears related to #1507. See a simplified code sample below:

use ethers::core::types::U256;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{Result, Value};


#[test]
fn deserialize_u256() {

    #[derive(Debug, Deserialize, Serialize)]
    struct TestValues {
        value_1: U256,
        value_2: U256,
        value_3: U256,
        value_4: U256,
    }

    let data = r#"
        {
            "value_1": "750000000000000000",
            "value_2": "21000000000000000",
            "value_3": "0",
            "value_4": "1"
        }
    "#;
            
    let data_json: TestValues = serde_json::from_str(&data).unwrap();

    println!("Expected {:?}, got {:?}", U256::from_dec_str("750000000000000000").unwrap(), data_json.value_1);
    println!("Expected {:?}, got {:?}", U256::from_dec_str("21000000000000000").unwrap(), data_json.value_2);
    println!("Expected {:?}, got {:?}", U256::from_dec_str("0").unwrap(), data_json.value_3);
    println!("Expected {:?}, got {:?}", U256::from_dec_str("1").unwrap(), data_json.value_4);

    assert_eq!(data_json.value_1, U256::from_dec_str("750000000000000000").unwrap());
    assert_eq!(data_json.value_2, U256::from_dec_str("21000000000000000").unwrap());
    assert_eq!(data_json.value_3, U256::zero());
    assert_eq!(data_json.value_4, U256::one());
}

The output of this is:

---- deserialize_u256 stdout ----
Expected 750000000000000000, got 2158269056624017539072
Expected 21000000000000000, got 38046409652025950208
Expected 0, got 0
Expected 1, got 1
thread 'deserialize_u256' panicked at 'assertion failed: `(left == right)`
  left: `2158269056624017539072`,
 right: `750000000000000000`', tests/deserialize_u256.rs:33:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- deserialize_rate_model stdout ----
rateModel: RateModelConfig { rateModel: RateModel { u_bar: 2158269056624017539072, r_0: 38046409652025950208, r_1: 0, r_2: 1770887431076116955136 }, routeRateModel: RouteRateModel { models: {"1-10": RateModel { u_bar: 0, r_0: 0, r_1: 0, r_2: 0 }, "1-42161": RateModel { u_bar: 0, r_0: 0, r_1: 0, r_2: 0 }} }, transferThreshold: "0", spokeTargetBalances: SpokeTargetBalances { targets: {"42161": SpokeTarget { threshold: 6044629098073145873530880, target: 4004566777473459141214208 }, "10": SpokeTarget { threshold: 6044629098073145873530880, target: 4004566777473459141214208 }} } }
thread 'deserialize_rate_model' panicked at 'assertion failed: `(left == right)`
  left: `2158269056624017539072`,
 right: `750000000000000000`', tests/deserialize_u256.rs:92:5

It seems similar to #1507 because HEX(750000000000000000) -> 2158269056624017539072. I took a look at what I think are the relevant lines of code and I'm not sure why it's landing on else if s.starts_with("0x") -- It could be me doing something dumb but the behaviour seemed unexpected.

@0xrainbowtrout 0xrainbowtrout added the bug Something isn't working label Apr 15, 2023
@mattsse
Copy link
Collaborator

mattsse commented Apr 15, 2023

I took a look at what I think are the relevant lines of code a

but that's not even used here

see #2354

@0xrainbowtrout
Copy link
Contributor Author

got it. thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants