stellar / soroban-examples

Example Soroban Contracts
Apache License 2.0
65 stars 68 forks source link

fuzzing example: fuzz_target_2 Config::setup is wrong #286

Open brson opened 11 months ago

brson commented 11 months ago

This example uses snapshots to reset the environment, but the way it initializes the first environment should not be recommended:

    fn setup() -> Env {
        let snapshot = {
            let init_ledger = LedgerInfo {
                protocol_version: 1,
                sequence_number: 10,
                timestamp: 12345,
                network_id: Default::default(),
                base_reserve: 10,
                min_temp_entry_ttl: u32::MAX,
                min_persistent_entry_ttl: u32::MAX,
                max_entry_ttl: u32::MAX,
            };

            LedgerSnapshot::from(init_ledger, None)
        };

        let env = Env::from_snapshot(snapshot);
        env.mock_all_auths();

        env
    }

Instead of calling Env::default it creates a snapshot from scratch and converts that to an env. This fails to do a bunch of initialization that Env::default does for the testutils config, and also configures the initial snapshot with strange values for ttl etc.

Instead, the initial snapshot should be created with Env::default, subsequent time advances can still use snapshots to destroy and reconstruct the env.

brson commented 11 months ago

There are additional revisions to be made to the fuzzing examples and I will get back to them soon.

brson commented 11 months ago

Using snapshots to destroy and recreate the environment may not be a good idea at all since Env::from_snapshot doesn't do the same test setup that Env::default does.