Question re. onchain PDA derivation & compute budget

Hi All,

I am exploring the possibility (max unit cost) of generating a single PDA onchain. I am wondering if the default compute budget is large enough to allow for the theoretical maximum number of derivation attempts (256 tries [?] unless I am mistaken).

Does anybody have a better intuition to draw from regarding compute budget? Additionally, I am curious to know if there are many/any projects with pda implementations where the addresses are derived onchain because of some design need, etc. --I can’t think of many (any) reasons why this might be.

If not I will perform some experiments/sleuth and report back …otherwise I am trying to keep the side-projects down to a minimum and would appreciate the wisdoms.

Thanks for the thought,
Blair

I’m not sure honestly, I’d have to test it out, to be sure. Somebody on Discord might know, but I haven’t tested that yet.

My guess, which could be terribly wrong, is that you shouldn’t run into a compute limit issue for doing that, but again, that’s just a total guess.

1 Like

I threw together a quick and ugly benchmark.

Running a few longer trials on my computer, for practical purposes most PDAs take between 1-2 attempts.

Onchain, disregarding the loop and message cost, for that seed set [0…50] I generated 51 PDAs in 105 tries, at a cost of 172000 compute units.

…comes out to roughly 3400 units per PDA generated, and 1650 units per attempt.

    // create dummy ID
    let fake_program_id: Pubkey = Pubkey::from_str("5S5wzXehfG8NQvopbfvEVv6dvmHvxofWguTdQFfeeuCd").unwrap();

    let mut j = 0;          // track total number of generations
    //let mut k: u8 = 0;    // track attempt number max


    for i in 0..=50 {

        let (key, seed) = Pubkey::find_program_address(&[&[i]], &fake_program_id);

        j += 1 + (255 - seed);

        //msg!("key = {}, seedno= {}", key, (255-seed));
        //if (255 - seed) > k {
        //    k = 255 - seed;
        //}

    }

    msg!("generated 51 pdas in {} steps.", j);
    //  msg!("Longest attempt was {} tries.", i);
    
    Ok(())

2 Likes