gear-tech / gear

Web3 Ultimate Execution Engine
https://gear-tech.io
GNU General Public License v3.0
237 stars 105 forks source link

Gear program generator is not deterministic #2170

Closed techraed closed 1 year ago

techraed commented 1 year ago

Problem

Run on Linux

fn generate_gear_program<Rng: LoaderRng>(seed: Seed) -> Vec<u8> {
    let mut rng = Rng::seed_from_u64(seed);

    let mut buf = vec![0; 100_000];
    rng.fill_bytes(&mut buf);

    let mut u = Unstructured::new(&buf);

    let mut config = gear_wasm_gen::GearConfig::new_normal();
    config.print_test_info = Some(format!("Gear program seed = '{seed}'"));

    gear_wasm_gen::gen_gear_program_code(&mut u, config)
}

#[test]
fn code_generation_is_deterministic() {
    use rand::rngs::SmallRng;

    let seed = 11943721913250886906;

    let first_code = generate_gear_program::<SmallRng>(seed);
    let second_code = generate_gear_program::<SmallRng>(seed);

    assert_eq!(first_code, second_code); // panics
}
techraed commented 1 year ago

From docs:

The algorithm is deterministic but should not be considered reproducible due to dependence on platform and possible replacement in future library versions. For a reproducible generator, use a named PRNG from an external crate, e.g. rand_xoshiro or rand_chacha.

techraed commented 1 year ago

Found out that running the loader on different machines (different OS) gives different code seeds.