Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[x/programs] Simulator should fail if a key that is used was not created #861

Closed
iFrostizz opened this issue Apr 25, 2024 · 0 comments · Fixed by #883
Closed

[x/programs] Simulator should fail if a key that is used was not created #861

iFrostizz opened this issue Apr 25, 2024 · 0 comments · Fixed by #883

Comments

@iFrostizz
Copy link
Collaborator

  1. Change the counter program
#[public]
pub fn init(_: Context, _addr: Address) {
    //
}

#[public]
pub fn init2(_: Context) {
    //
}
  1. Add this test in the counter program
    #[test]
    fn init_program() {
        let simulator = simulator::Client::new();

        let owner_key = String::from("owner");
        let [alice_key, bob_key] = ["alice", "bob"]
            .map(String::from)
            .map(Key::Ed25519)
            .map(Param::Key);

        let mut plan = Plan::new(owner_key.clone());

        plan.add_step(Step::create_key(Key::Ed25519(owner_key)));

        let program_id = plan.add_step(Step {
            endpoint: Endpoint::Execute,
            method: "program_create".into(),
            max_units: 1000000,
            params: vec![Param::String(PROGRAM_PATH.into())],
            require: None,
        });

        plan.add_step(Step {
            endpoint: Endpoint::Execute,
            method: "init2".into(),
            max_units: 10000000000,
            params: vec![program_id.into()],
            require: None,
        });

        plan.add_step(Step {
            endpoint: Endpoint::Execute,
            method: "init".into(),
            max_units: 10000000000,
            params: vec![program_id.into(), alice_key.clone()],
            require: None,
        });

        // run plan
        let plan_responses = simulator.run_plan(&plan).unwrap();

        // ensure no errors
        assert!(
            plan_responses.iter().all(|resp| resp.error.is_none()),
            "error: {:?}",
            plan_responses
                .iter()
                .filter_map(|resp| resp.error.as_ref())
                .next()
        );
    }

The test fails with an unreachable error from WASM, but that's only happening at the call to the init function, and it only happens when calling a function with more than one parameter. The fix here is to register the key:

        plan.add_step(Step {
            endpoint: Endpoint::Key,
            method: "key_create".into(),
            params: vec![alice_key.clone()],
            max_units: 0,
            require: None,
        });

We could try to error if the key has not been registered before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants