anchor
anchor copied to clipboard
Move to getLatestBlockhash, and don't overwrite an existing blockhash
This PR migrates from the deprecated getRecentBlockhash to its replacement getLatestBlockhash. In my testing this means that each transaction gets a unique blockhash, which has the benefit that you can submit calls to the same instruction w/ the same parameters + accounts when using the program.methods[...].rpc() API.
I've also updated the logic to only set recentBlockhash if it isn't already set. This means that if the blockhash logic changes again in future, or somebody wants to use a different one for some reason, then there will be a workaround of using .transaction() instead of .rpc(), setting it yourself and then calling sendAndConfirm or sendAll. I'm happy to remove this if it adds too much complexity to the behaviour though.
Note that in the issue I mentioned we could also set lastValidBlockHeight which is returned by getLatestBlockhash. The version of @solana/web3.js currently used by Anchor doesn't have that on the Transaction class so I've left that out. I don't think it's important enough to justify upgrading that library just for this.
Closes #2091
@mcintyre94 is attempting to deploy a commit to the coral-xyz Team on Vercel.
A member of the Team first needs to authorize it.
It looks like the failing tests here are all with "Error: failed to get recent blockhash: Error: failed to get latest blockhash: Method not found" on the miscNonRentExempt test suite.
Looking at the CI script here: https://github.com/coral-xyz/anchor/blob/master/tests/misc/ci.sh - it looks like these are intentionally run on an older validator:
The latter needs to be on a validator with a version < 1.9, so it can test whether anchor's rent-exemption checks work for legacy accounts which dont have to be rent-exempt
Inconveniently, getLatestBlockhash is new in version 1.9, see here: https://docs.solana.com/developing/clients/jsonrpc-api#getlatestblockhash
NEW: This method is only available in solana-core v1.9 or newer. Please use getRecentBlockhash for solana-core v1.8
Is there an approach already used in the Anchor codebase to deal with using features added after 1.9 while this test suite uses an earlier version?
I've updated the implementation to use a new helper function which uses getLatestBlockhash and then falls back to getRecentBlockhash if that fails. Not super happy to be adding that technical debt, but I think it's the best way to support the <1.9 validator the CI requires and hopefully the documentation makes it clear what's going on/when it can be simplified.
Latest test failure seems to be unrelated to TS:
error[E0658]: destructuring assignments are unstable
--> /home/runner/.cargo/registry/src/github.com-1ecc6299db9ec823/spl-token-3.3.1/src/instruction.rs:1712:13
|
1712 | _ = TokenInstruction::unpack(&expect[0..2]);
| ^
|
= note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
= help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable
For more information about this error, try `rustc --explain E0658`.
error: could not compile `spl-token` due to previous error
warning: build failed, waiting for other jobs to finish...
error: build failed
Error: Process completed with exit code 1.
https://github.com/coral-xyz/anchor/runs/7674721636?check_suite_focus=true#step:16:136
Equivalent error on another PR: https://github.com/coral-xyz/anchor/runs/7672255984?check_suite_focus=true#step:16:136
@callensm
Any chance I could get an updated review on this? I think the only failing check is the one failing on all builds and it should be good to go.
Just pushed another rebase on latest master branch
There were some small conflicts with some other changes already made. Let's see how the tests go now.
Actually this might already be fixed by another PR that was merged recently. https://github.com/coral-xyz/anchor/pull/2205
Awesome, thankyou!! :)