paritytech / polkadot-sdk

The Parity Polkadot Blockchain SDK
https://polkadot.network/
1.85k stars 677 forks source link

Ease implementation of mock for pallets. #299

Open gui1117 opened 3 years ago

gui1117 commented 3 years ago

Currently there is very few provided function for doing some mocking of pallets.

People almost always implement some function like:

fn run_to_block(n: u32) {
    assert!(System::block_number() < n);
    while System::block_number() < n {
        let block_number = System::block_number();
        AllPalletsWithSystem::on_finalize(block_number);
        System::set_block_number(block_number + 1);
        maybe_new_session(block_number + 1);
        AllPalletsWithSystem::on_initialize(block_number + 1);
    }
}

Those function are sometime incomplete implementation of state transition.

I think maybe we can make use of frame_executive::Executive in tests, or provide some helper to avoid having to implement those function everywhere.

AurevoirXavier commented 2 hours ago

Indeed, we need this. There are too many such duplicated code.

@kianenigma Could you draft a simple "task list"? Where could be the best entry point to "inject" such run_to_block logic?

I'd love to take this issue.