This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat: add getAddressLookupTable method to Connection #27127
Merged
mergify
merged 1 commit into
solana-labs:master
from
jstarry:web3/get-address-lookup-table
Aug 14, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as BufferLayout from '@solana/buffer-layout'; | ||
|
||
export interface IAccountStateData { | ||
readonly typeIndex: number; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export type AccountType<TInputData extends IAccountStateData> = { | ||
/** The account type index (from solana upstream program) */ | ||
index: number; | ||
/** The BufferLayout to use to build data */ | ||
layout: BufferLayout.Layout<TInputData>; | ||
}; | ||
|
||
/** | ||
* Decode account data buffer using an AccountType | ||
* @internal | ||
*/ | ||
export function decodeData<TAccountStateData extends IAccountStateData>( | ||
type: AccountType<TAccountStateData>, | ||
data: Uint8Array, | ||
): TAccountStateData { | ||
let decoded: TAccountStateData; | ||
try { | ||
decoded = type.layout.decode(data); | ||
} catch (err) { | ||
throw new Error('invalid instruction; ' + err); | ||
} | ||
|
||
if (decoded.typeIndex !== type.index) { | ||
throw new Error( | ||
`invalid account data; account type mismatch ${decoded.typeIndex} != ${type.index}`, | ||
); | ||
} | ||
|
||
return decoded; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 8 additions & 6 deletions
14
web3.js/src/programs/address-lookup-table.ts → ...rc/programs/address-lookup-table/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import * as BufferLayout from '@solana/buffer-layout'; | ||
|
||
import assert from '../../utils/assert'; | ||
import * as Layout from '../../layout'; | ||
import {PublicKey} from '../../publickey'; | ||
import {u64} from '../../utils/bigint'; | ||
import {decodeData} from '../../account-data'; | ||
|
||
export type AddressLookupTableState = { | ||
deactivationSlot: bigint; | ||
lastExtendedSlot: number; | ||
lastExtendedSlotStartIndex: number; | ||
authority?: PublicKey; | ||
addresses: Array<PublicKey>; | ||
}; | ||
|
||
export type AddressLookupTableAccountArgs = { | ||
key: PublicKey; | ||
state: AddressLookupTableState; | ||
}; | ||
|
||
/// The serialized size of lookup table metadata | ||
const LOOKUP_TABLE_META_SIZE = 56; | ||
|
||
export class AddressLookupTableAccount { | ||
key: PublicKey; | ||
state: AddressLookupTableState; | ||
|
||
constructor(args: AddressLookupTableAccountArgs) { | ||
this.key = args.key; | ||
this.state = args.state; | ||
} | ||
|
||
isActive(): boolean { | ||
const U64_MAX = 2n ** 64n - 1n; | ||
return this.state.deactivationSlot === U64_MAX; | ||
} | ||
|
||
static deserialize(accountData: Uint8Array): AddressLookupTableState { | ||
const meta = decodeData(LookupTableMetaLayout, accountData); | ||
|
||
const serializedAddressesLen = accountData.length - LOOKUP_TABLE_META_SIZE; | ||
assert(serializedAddressesLen >= 0, 'lookup table is invalid'); | ||
assert(serializedAddressesLen % 32 === 0, 'lookup table is invalid'); | ||
|
||
const numSerializedAddresses = serializedAddressesLen / 32; | ||
const {addresses} = BufferLayout.struct<{addresses: Array<Uint8Array>}>([ | ||
BufferLayout.seq(Layout.publicKey(), numSerializedAddresses, 'addresses'), | ||
]).decode(accountData.slice(LOOKUP_TABLE_META_SIZE)); | ||
|
||
return { | ||
deactivationSlot: meta.deactivationSlot, | ||
lastExtendedSlot: meta.lastExtendedSlot, | ||
lastExtendedSlotStartIndex: meta.lastExtendedStartIndex, | ||
authority: | ||
meta.authority.length !== 0 | ||
? new PublicKey(meta.authority[0]) | ||
: undefined, | ||
addresses: addresses.map(address => new PublicKey(address)), | ||
}; | ||
} | ||
} | ||
|
||
const LookupTableMetaLayout = { | ||
index: 1, | ||
layout: BufferLayout.struct<{ | ||
typeIndex: number; | ||
deactivationSlot: bigint; | ||
lastExtendedSlot: number; | ||
lastExtendedStartIndex: number; | ||
authority: Array<Uint8Array>; | ||
}>([ | ||
BufferLayout.u32('typeIndex'), | ||
u64('deactivationSlot'), | ||
BufferLayout.nu64('lastExtendedSlot'), | ||
BufferLayout.u8('lastExtendedStartIndex'), | ||
BufferLayout.u8(), // option | ||
BufferLayout.seq( | ||
Layout.publicKey(), | ||
BufferLayout.offset(BufferLayout.u8(), -1), | ||
'authority', | ||
), | ||
]), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun fact: this change made
web3.js
incompatible with React Native because:**
) so there's a transform that converts2n ** 64n
toMath.pow(2n, 64n)
and then you're in trouble becauseMath.pow()
doesn't work withbigint
(runtime fatal)bigint
yet.No change necessary – I'm working on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://twitter.com/steveluscher/status/1563940218444840960
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch, thanks for the heads up. Seems worthwhile to use a literal here then, or do you think it's not worth the trouble?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are now three places in
web3.js
that useBigInt
If we inline the computation and convince the noble libs to do the same (eg. just literally change this to
BigInt('18446744073709551616')
and comment it) then:BigInt
BigInt
polyfill: still fucked because polyfills don't permit arithmetic (require('big-integer')(1) + require('big-integer')(1)
is actually nonsense, because it's basicallyObject + Object
)BigInt
supportI'm going to post about this on September 1st, but my recommendation going forward for anyone who wants to use Solana libraries in React Native going forward will be to upgrade to React Native 0.70 and switch to Hermes. Big integers are critical for most crypto use cases, Hermes is now the default in React Native from 0.70 onward, and the way out of this mess is forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, really appreciate the breakdown on this. I'll leave as-is then