nervosnetwork/ckb

A better implementation for `RationalU256`.

Open

#3543 aperta il 11 ago 2022

Vedi su GitHub
 (3 commenti) (0 reazioni) (0 assegnatari)Rust (260 fork)user submission
good first issuestalet:enhancement

Metriche repository

Star
 (1210 star)
Metriche merge PR
 (Merge medio 13g 23h) (29 PR mergiate in 30 g)

Descrizione

Issue

Panicked at 'U256: attempt to multiply with overflow' for some values.

https://github.com/nervosnetwork/ckb/blob/649389d90a142cf39cdbc2019b8fbde188383ca5/util/rational/src/lib.rs#L128-L140

Reproduce

Multiply two RationalU256 numbers, when both two numerators (or denominators) are big but their greatest common divisor is small.

For example:

use ckb_types::{core::RationalU256, u256, U256};
fn main() {
    let x = u256!("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
    let x_rational = RationalU256::from_u256(x.clone());
    let numerator = 33u32;
    let denominator = 100u32;
    let y_rational = RationalU256::new(U256::from(numerator), U256::from(denominator));
    let z_rational = x_rational * y_rational; // panicked in this line!
    let z = z_rational.into_u256();
    println!("{:#x} = {:#x} * {}/{}", z, x, numerator, denominator);
}

Ref: nervosnetwork/ckb-light-client#44

Guida contributor