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

Add a function to replace accounts in a transaction message using lookup tables #2969

Merged
merged 3 commits into from
Jul 23, 2024

Conversation

mcintyre94
Copy link
Collaborator

@mcintyre94 mcintyre94 commented Jul 22, 2024

This PR adds a new function useAddressLookupTablesForTransactionMessage to @solana/transaction-messages.

It takes as input a TransactionMessage and a mapping of lookup table address to its list of addresses. For each account that is not a signer of the input transaction message, and is not already a lookup, if it appears in one of the lookup tables then it is replaced by a lookup meta for that lookup table.

This means that a developer can build a transaction without worrying about lookup tables, and then call this with the lookup tables they want to use. This is similar to the legacy .compileToV0Message([lookupTable]) API.

This is not the same operation as is done when we compile a transaction message, this is simply replacing the accounts so that when the transaction message is compiled it will include the lookup tables.

Note: this is not as sophisticated as it could be, in terms of minimising the final transaction size. If you pass in multiple lookup tables, the output transaction message may use more of them than is optimal. Eg if we passed {lookup1: [abc], lookup2: [def], lookup3: [abc, def]} then it may use lookup1 and lookup2 instead of only using lookup3.

Also note that this doesn't guarantee that replaced accounts will use these lookup tables if the message is modified further. Eg if the message is modified such that an account becomes a signer after this is called, then compileTransactionMessage will change it back to a static account.

Copy link

changeset-bot bot commented Jul 22, 2024

🦋 Changeset detected

Latest commit: ea2f95d

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@mcintyre94 mcintyre94 force-pushed the add-lookup-tables-tx-message branch 3 times, most recently from e5c8f0a to 8c5e1dc Compare July 22, 2024 15:51
Copy link
Collaborator

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

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

I like it! I'd like to change the name though.

Address lookup tables are an optimization that lets you compress a transaction over the wire. We should use one of those words.

  • compressTransactionMessageUsingAddressLookupTables(message, luts)
  • optimizeTransactionMessageUsingAddressLookupTables(message, luts)

I was initially going to say we should call it ‘optimize’ but ‘compress’ is alluring because of how specific and revelatory it is. An optimization could be anything, but what's really going on here is that you're replacing IAccountMeta with IAccountLookupMeta that result in smaller compiled messages.

If you pass in multiple lookup tables, the output transaction message may use more of them than is optimal

Ooh, interesting. We should maybe move this functionality into the compileTransactionMessage. There should be an algorithm in there that minimizes the number of lookup tables used.

@mcintyre94
Copy link
Collaborator Author

Thanks @steveluscher! I like compressTransactionMessageUsingAddressLookupTables too, will rename to that.

@mcintyre94 mcintyre94 force-pushed the add-lookup-tables-tx-message branch from 8c5e1dc to bc57fe6 Compare July 22, 2024 17:21
Copy link
Collaborator

@lorisleiva lorisleiva left a comment

Choose a reason for hiding this comment

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

Perfect! 👌

>(
transactionMessage: TTransactionMessage,
addressesByLookupTableAddress: AddressesByLookupTableAddress,
): TTransactionMessage {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't exactly true, I guess. This function probably actually widens every account from whatever it is, back out to IAccountMeta<TAddress> | IAccountLookupMeta<TAddress>.

What would be the implications of mapping over this puppy and making that so?

// Pseudo TypeScript...
): 
  | TTransactionMessage  // Unchanged, or
  | TTransactionMessage & TTransactionMessage.instructions.map(i => ({
    ...i,
    accounts: i.accounts.map(a => IAccountMeta<typeof a.address> | IAccountLookupMeta<typeof a.address>)
  }))
{

Copy link
Collaborator

Choose a reason for hiding this comment

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

Haha I thought about this but the content of AddressesByLookupTableAddress would need to be fully static for this to even work which will probably never be the case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fair point! I did this just to keep ITransactionMessageWithFeePayer etc intact on the output message, but it isn't strictly accurate if you've got the accounts field typed more specifically.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not 100% on this type but I think this works? b643109

@mcintyre94 mcintyre94 force-pushed the add-lookup-tables-tx-message branch from b643109 to ea2f95d Compare July 22, 2024 19:31
@mcintyre94 mcintyre94 merged commit 419c12e into master Jul 23, 2024
8 checks passed
@mcintyre94 mcintyre94 deleted the add-lookup-tables-tx-message branch July 23, 2024 09:51
@github-actions github-actions bot mentioned this pull request Jul 23, 2024
Copy link
Contributor

🎉 This PR is included in version 1.95.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link
Contributor

Because there has been no activity on this PR for 14 days since it was merged, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants