forked from solana-labs/solana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getAddressLookupTable method to Connection (solana-labs#27127)
- Loading branch information
1 parent
4277a4e
commit 44647ba
Showing
5 changed files
with
242 additions
and
7 deletions.
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