Add example of deserializing the SlotHash sysvar while remaining with…#3440
Add example of deserializing the SlotHash sysvar while remaining with…#3440SUMEETRM wants to merge 4 commits intosolana-labs:masterfrom
Conversation
…in the BPF compute limit
joncinque
left a comment
There was a problem hiding this comment.
Rather than adding a separate program in this file, how about adding the decoding to the existing process_instruction in processor.rs in this same directory? then you can merge this test with the functional test that already exists, just adding the slot hashes sysvar and file.
examples/rust/sysvar/src/slothash.rs
Outdated
| sysvar::slot_hashes::id(), | ||
| sol_to_lamports(1.), | ||
| Pubkey::default(), | ||
| "slot_hashes.bin", |
There was a problem hiding this comment.
We'll need to add this file somewhere in this repo so the test can pick it up. Typically we put it in tests/fixtures. Check out https://github.com/mvines/solana-bpf-program-template/pull/4/files#diff-d78b61a40495314ce899038afe34465a2619be35049c25c487854e970c07ba1e
There was a problem hiding this comment.
Tried to fix it in my most recent commit but not sure if that's what needs to be done. Couldn't find the tests/fixtures directory in rust examples so I added slot_hashes.bin to the same directory as processor.rs
I've added the code from slothashes.rs to processor.rs and have changed the function name. Is this how it's supposed to be done?
…rs along with slot_hashes.bin in the same dir
| //Code for deserializing the SlotHash sysvar while remaining within the BPF compute limit | ||
|
|
||
| //entrypoint!(process_instruction2); | ||
| fn process_instruction2( | ||
| _program_id: &Pubkey, | ||
| accounts: &[AccountInfo], | ||
| _instruction_data: &[u8], | ||
| ) -> ProgramResult { |
There was a problem hiding this comment.
What I meant was to have all this code happen after line 43, in the normal process_instruction function. Written this way, the processor will never be triggered
| }; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_transaction() { |
There was a problem hiding this comment.
The idea is to merge this test with the one in examples/rust/sysvar/tests/functional.rs since they exercise the same code underneath. You can change the transaction to also include the slot hashes sysvar and rest should work very nicely
| sysvar::slot_hashes::id(), | ||
| sol_to_lamports(1.), | ||
| Pubkey::default(), | ||
| "slot_hashes.bin", |
There was a problem hiding this comment.
Once this test is moved to examples/rust/sysvar/tests/functional.rs, then you can move the slot hashes file to examples/rust/sysvar/tests/fixtures/slot_hashes.bin
joncinque
left a comment
There was a problem hiding this comment.
I can't comment on it, but be sure to remove examples/rust/sysvar/src/slot_hashes.bin. Otherwise, just run cargo fmt and clippy, clean up those errors, and we should be good to merge!
| }; | ||
|
|
||
| /// Instruction processor | ||
| entrypoint(process_instruction); |
There was a problem hiding this comment.
This should be removed since the entrypoint is already defined in entrypoint.rs. Also, it's a macro, so it should normally be called as entrypoint!(process_instruction)
Normal decoding of the SlotHash sysvar will exceed the current BPF compute limit. This PR demonstrates how to manually decode the sysvar to avoid this issue: mvines/solana-bpf-program-template#4
It has been added to fix issue #945 in the examples folder and can be located by examples/rust/sysvar/src/slothash.rs