Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Add SDK support for creating transactions with address table lookups #23728

Merged
merged 8 commits into from
Mar 31, 2022

Conversation

jstarry
Copy link
Contributor

@jstarry jstarry commented Mar 17, 2022

Problem

Rust SDK doesn't provide any convenience APIs for creating transactions which make use of on-chain lookup tables.

Summary of Changes

  • Added CompileError error type to differentiate types of message compilation failures
  • Added Message::try_compile method to compile messages using address lookup tables
  • Added CompiledKeys::try_extract_table_lookup to remove any non-signer compiled keys that can be replaced with an address table lookup
  • AccountKeys::compile_instructions is now fallible and was renamed to AccountKeys::try_compile_instructions. It returns an error when compiling instructions with an unknown key or if u8 conversion fails
  • CompiledKeys::try_into_message_components now returns a Result instead of an Option to make use of the new CompileError

Fixes #

@jstarry jstarry force-pushed the lut/create-v0-message branch from 5a331a9 to 924ede5 Compare March 17, 2022 16:14
@jstarry jstarry force-pushed the lut/create-v0-message branch 7 times, most recently from 6ad1047 to e1673e1 Compare March 24, 2022 09:37
@jstarry jstarry changed the title Add API for creating transactions with address table lookups Add SDK support for creating transactions with address table lookups Mar 24, 2022
@jstarry jstarry marked this pull request as ready for review March 24, 2022 09:38
@jstarry jstarry requested a review from t-nelson March 24, 2022 09:38
@jstarry jstarry force-pushed the lut/create-v0-message branch from e1673e1 to 39e1242 Compare March 24, 2022 10:10
sdk/program/src/message/account_keys.rs Show resolved Hide resolved
) -> Result<Vec<CompiledInstruction>, CompileError> {
let mut account_index_map = BTreeMap::<&Pubkey, u8>::new();
for (index, key) in self.iter().enumerate() {
let index = u8::try_from(index).map_err(|_| CompileError::TooManyAccountKeys)?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's improve self-doc with an explicit constant for max supported, rather than implicitly relying on u8::MAX.

Also seems like we should be preventing a too large AccountKeys from ever being created rather than checking it here (and presumably other places in the future)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a runtime limit of 64 but this compile error is for correctness not for runtime validity. I just want to make sure that the index actually fits in a u8 because that's how we serialize it in a transaction. There's many ways to create an invalid transaction on the client side so I think it's ok for now to allow large AccountKeys to be created because they will be rejected during sanitization and the runtime check could change in the future so I don't want to hardcode that into the client.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, the error seems misleading in that case since we can have too many account keys without getting a TooManyAccountKeys 🤔

i typically prefer to fail asap rather than let pathology fester until some other process notices later. killing it at the client, especially in the first implementation, which the others will copy seems like the less wrong thing to do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, the error seems misleading in that case since we can have too many account keys without getting TooManyAccountKeys 🤔

Agreed, that was a bad name. I updated to AccountIndexOverflow and AddressTableLookupIndexOverflow.

i typically prefer to fail asap rather than let pathology fester until some other process notices later. killing it at the client, especially in the first implementation, which the others will copy seems like the less wrong thing to do

I'm hesitant to do runtime checks on the client because runtime rules can change over time. For example, the serialized size of a transaction could change, the total number of accounts lockable by a transaction (currently 64) could be updated later, etc.

sdk/program/src/message/account_keys.rs Outdated Show resolved Hide resolved
sdk/program/src/message/compiled_keys.rs Show resolved Hide resolved
@jstarry jstarry force-pushed the lut/create-v0-message branch 5 times, most recently from d090ae5 to 05c90a5 Compare March 25, 2022 09:54
@jstarry jstarry added the CI Pull Request is ready to enter CI label Mar 25, 2022
@solana-grimes solana-grimes removed the CI Pull Request is ready to enter CI label Mar 25, 2022
@jstarry jstarry force-pushed the lut/create-v0-message branch from 05c90a5 to e066c6a Compare March 25, 2022 14:48
@jstarry
Copy link
Contributor Author

jstarry commented Mar 27, 2022

@t-nelson can you take another look? CI failure is unrelated to this change

t-nelson
t-nelson previously approved these changes Mar 30, 2022
Copy link
Contributor

@t-nelson t-nelson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just a nit

a rebase should get you past that downstream build error

sdk/program/src/message/compiled_keys.rs Outdated Show resolved Hide resolved
@jstarry jstarry force-pushed the lut/create-v0-message branch from 800170d to dece769 Compare March 31, 2022 03:13
@mergify mergify bot dismissed t-nelson’s stale review March 31, 2022 03:13

Pull request has been modified.

@jstarry jstarry added the automerge Merge this Pull Request automatically once CI passes label Mar 31, 2022
@mergify
Copy link
Contributor

mergify bot commented Mar 31, 2022

automerge label removed due to a CI failure

@mergify mergify bot removed the automerge Merge this Pull Request automatically once CI passes label Mar 31, 2022
@jstarry
Copy link
Contributor Author

jstarry commented Mar 31, 2022

Yay new downstream build failure:

error[E0432]: unresolved imports `bytemuck_derive::AnyBitPattern`, `bytemuck_derive::CheckedBitPattern`, `bytemuck_derive::NoUninit`
--


    <span class="term-fgx12 term-fg1">| </span>  <span class="term-fgx9 term-fg1">no `AnyBitPattern` in the root</span>

@jstarry jstarry force-pushed the lut/create-v0-message branch from dece769 to 04a7a8a Compare March 31, 2022 05:10
@jstarry jstarry added the automerge Merge this Pull Request automatically once CI passes label Mar 31, 2022
@mergify mergify bot removed the automerge Merge this Pull Request automatically once CI passes label Mar 31, 2022
@mergify
Copy link
Contributor

mergify bot commented Mar 31, 2022

automerge label removed due to a CI failure

@jstarry jstarry force-pushed the lut/create-v0-message branch from 04a7a8a to e0771ee Compare March 31, 2022 07:07
@jstarry jstarry merged commit 8832653 into solana-labs:master Mar 31, 2022
@jstarry jstarry deleted the lut/create-v0-message branch March 31, 2022 09:44
ngundotra pushed a commit to ngundotra/solana that referenced this pull request Apr 2, 2022
…olana-labs#23728)

* Add SDK support for creating transactions with address table lookups

* fix bpf compilation

* rename compile error variants to indicate overflow

* Add doc tests

* fix bpf compatibility

* use constant for overflow tests

* Use cfg_attr for dead code attribute

* resolve merge conflict
ngundotra pushed a commit to ngundotra/solana that referenced this pull request Apr 2, 2022
…olana-labs#23728)

* Add SDK support for creating transactions with address table lookups

* fix bpf compilation

* rename compile error variants to indicate overflow

* Add doc tests

* fix bpf compatibility

* use constant for overflow tests

* Use cfg_attr for dead code attribute

* resolve merge conflict
ngundotra pushed a commit to ngundotra/solana that referenced this pull request Apr 2, 2022
…olana-labs#23728)

* Add SDK support for creating transactions with address table lookups

* fix bpf compilation

* rename compile error variants to indicate overflow

* Add doc tests

* fix bpf compatibility

* use constant for overflow tests

* Use cfg_attr for dead code attribute

* resolve merge conflict
ngundotra pushed a commit to ngundotra/solana that referenced this pull request Apr 2, 2022
…olana-labs#23728)

* Add SDK support for creating transactions with address table lookups

* fix bpf compilation

* rename compile error variants to indicate overflow

* Add doc tests

* fix bpf compatibility

* use constant for overflow tests

* Use cfg_attr for dead code attribute

* resolve merge conflict
ngundotra pushed a commit to ngundotra/solana that referenced this pull request Apr 2, 2022
…olana-labs#23728)

* Add SDK support for creating transactions with address table lookups

* fix bpf compilation

* rename compile error variants to indicate overflow

* Add doc tests

* fix bpf compatibility

* use constant for overflow tests

* Use cfg_attr for dead code attribute

* resolve merge conflict
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants