Skip to content

Commit

Permalink
Merge pull request #87 from metaplex-foundation/feat/move-data-offset
Browse files Browse the repository at this point in the history
Move Data offset and length to the record level
  • Loading branch information
blockiosaurus authored Apr 25, 2024
2 parents 6d65cee + 2fbc51e commit 1c5583c
Show file tree
Hide file tree
Showing 24 changed files with 78 additions and 94 deletions.
8 changes: 1 addition & 7 deletions clients/js/src/generated/types/baseDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @see https://github.com/metaplex-foundation/kinobi
*/

import { Serializer, struct, u64 } from '@metaplex-foundation/umi/serializers';
import { Serializer, struct } from '@metaplex-foundation/umi/serializers';
import {
BasePluginAuthority,
BasePluginAuthorityArgs,
Expand All @@ -19,15 +19,11 @@ import {
export type BaseDataStore = {
dataAuthority: BasePluginAuthority;
schema: ExternalPluginSchema;
dataOffset: bigint;
dataLen: bigint;
};

export type BaseDataStoreArgs = {
dataAuthority: BasePluginAuthorityArgs;
schema: ExternalPluginSchemaArgs;
dataOffset: number | bigint;
dataLen: number | bigint;
};

export function getBaseDataStoreSerializer(): Serializer<
Expand All @@ -38,8 +34,6 @@ export function getBaseDataStoreSerializer(): Serializer<
[
['dataAuthority', getBasePluginAuthoritySerializer()],
['schema', getExternalPluginSchemaSerializer()],
['dataOffset', u64()],
['dataLen', u64()],
],
{ description: 'BaseDataStore' }
) as Serializer<BaseDataStoreArgs, BaseDataStore>;
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/generated/types/baseExternalPluginInitInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ export function baseExternalPluginInitInfo(
>['fields']
): GetDataEnumKind<BaseExternalPluginInitInfoArgs, 'DataStore'>;
export function baseExternalPluginInitInfo<
K extends BaseExternalPluginInitInfoArgs['__kind']
K extends BaseExternalPluginInitInfoArgs['__kind'],
>(kind: K, data?: any): Extract<BaseExternalPluginInitInfoArgs, { __kind: K }> {
return Array.isArray(data)
? { __kind: kind, fields: data }
: { __kind: kind, ...(data ?? {}) };
}
export function isBaseExternalPluginInitInfo<
K extends BaseExternalPluginInitInfo['__kind']
K extends BaseExternalPluginInitInfo['__kind'],
>(
kind: K,
value: BaseExternalPluginInitInfo
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/generated/types/baseExternalPluginKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ export function baseExternalPluginKey(
data: GetDataEnumKindContent<BaseExternalPluginKeyArgs, 'DataStore'>['fields']
): GetDataEnumKind<BaseExternalPluginKeyArgs, 'DataStore'>;
export function baseExternalPluginKey<
K extends BaseExternalPluginKeyArgs['__kind']
K extends BaseExternalPluginKeyArgs['__kind'],
>(kind: K, data?: any): Extract<BaseExternalPluginKeyArgs, { __kind: K }> {
return Array.isArray(data)
? { __kind: kind, fields: data }
: { __kind: kind, ...(data ?? {}) };
}
export function isBaseExternalPluginKey<
K extends BaseExternalPluginKey['__kind']
K extends BaseExternalPluginKey['__kind'],
>(
kind: K,
value: BaseExternalPluginKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function baseExternalPluginUpdateInfo(
>['fields']
): GetDataEnumKind<BaseExternalPluginUpdateInfoArgs, 'DataStore'>;
export function baseExternalPluginUpdateInfo<
K extends BaseExternalPluginUpdateInfoArgs['__kind']
K extends BaseExternalPluginUpdateInfoArgs['__kind'],
>(
kind: K,
data?: any
Expand All @@ -101,7 +101,7 @@ export function baseExternalPluginUpdateInfo<
: { __kind: kind, ...(data ?? {}) };
}
export function isBaseExternalPluginUpdateInfo<
K extends BaseExternalPluginUpdateInfo['__kind']
K extends BaseExternalPluginUpdateInfo['__kind'],
>(
kind: K,
value: BaseExternalPluginUpdateInfo
Expand Down
7 changes: 0 additions & 7 deletions clients/js/src/generated/types/baseLifecycleHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
option,
publicKey as publicKeySerializer,
struct,
u64,
} from '@metaplex-foundation/umi/serializers';
import {
BaseExtraAccount,
Expand All @@ -32,17 +31,13 @@ export type BaseLifecycleHook = {
extraAccounts: Option<Array<BaseExtraAccount>>;
dataAuthority: Option<BasePluginAuthority>;
schema: ExternalPluginSchema;
dataOffset: bigint;
dataLen: bigint;
};

export type BaseLifecycleHookArgs = {
hookedProgram: PublicKey;
extraAccounts: OptionOrNullable<Array<BaseExtraAccountArgs>>;
dataAuthority: OptionOrNullable<BasePluginAuthorityArgs>;
schema: ExternalPluginSchemaArgs;
dataOffset: number | bigint;
dataLen: number | bigint;
};

export function getBaseLifecycleHookSerializer(): Serializer<
Expand All @@ -55,8 +50,6 @@ export function getBaseLifecycleHookSerializer(): Serializer<
['extraAccounts', option(array(getBaseExtraAccountSerializer()))],
['dataAuthority', option(getBasePluginAuthoritySerializer())],
['schema', getExternalPluginSchemaSerializer()],
['dataOffset', u64()],
['dataLen', u64()],
],
{ description: 'BaseLifecycleHook' }
) as Serializer<BaseLifecycleHookArgs, BaseLifecycleHook>;
Expand Down
2 changes: 1 addition & 1 deletion clients/js/src/generated/types/basePluginAuthority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function basePluginAuthority(
data: GetDataEnumKindContent<BasePluginAuthorityArgs, 'Address'>
): GetDataEnumKind<BasePluginAuthorityArgs, 'Address'>;
export function basePluginAuthority<
K extends BasePluginAuthorityArgs['__kind']
K extends BasePluginAuthorityArgs['__kind'],
>(kind: K, data?: any): Extract<BasePluginAuthorityArgs, { __kind: K }> {
return Array.isArray(data)
? { __kind: kind, fields: data }
Expand Down
2 changes: 1 addition & 1 deletion clients/js/src/generated/types/baseUpdateAuthority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function baseUpdateAuthority(
data: GetDataEnumKindContent<BaseUpdateAuthorityArgs, 'Collection'>['fields']
): GetDataEnumKind<BaseUpdateAuthorityArgs, 'Collection'>;
export function baseUpdateAuthority<
K extends BaseUpdateAuthorityArgs['__kind']
K extends BaseUpdateAuthorityArgs['__kind'],
>(kind: K, data?: any): Extract<BaseUpdateAuthorityArgs, { __kind: K }> {
return Array.isArray(data)
? { __kind: kind, fields: data }
Expand Down
4 changes: 2 additions & 2 deletions clients/js/src/generated/types/baseValidationResultsOffset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function baseValidationResultsOffset(
>['fields']
): GetDataEnumKind<BaseValidationResultsOffsetArgs, 'Custom'>;
export function baseValidationResultsOffset<
K extends BaseValidationResultsOffsetArgs['__kind']
K extends BaseValidationResultsOffsetArgs['__kind'],
>(
kind: K,
data?: any
Expand All @@ -71,7 +71,7 @@ export function baseValidationResultsOffset<
: { __kind: kind, ...(data ?? {}) };
}
export function isBaseValidationResultsOffset<
K extends BaseValidationResultsOffset['__kind']
K extends BaseValidationResultsOffset['__kind'],
>(
kind: K,
value: BaseValidationResultsOffset
Expand Down
6 changes: 6 additions & 0 deletions clients/js/src/generated/types/externalRegistryRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export type ExternalRegistryRecord = {
authority: BasePluginAuthority;
lifecycleChecks: Option<Array<[HookableLifecycleEvent, ExternalCheckResult]>>;
offset: bigint;
dataOffset: Option<bigint>;
dataLen: Option<bigint>;
};

export type ExternalRegistryRecordArgs = {
Expand All @@ -44,6 +46,8 @@ export type ExternalRegistryRecordArgs = {
Array<[HookableLifecycleEventArgs, ExternalCheckResultArgs]>
>;
offset: number | bigint;
dataOffset: OptionOrNullable<number | bigint>;
dataLen: OptionOrNullable<number | bigint>;
};

export function getExternalRegistryRecordSerializer(): Serializer<
Expand All @@ -66,6 +70,8 @@ export function getExternalRegistryRecordSerializer(): Serializer<
),
],
['offset', u64()],
['dataOffset', option(u64())],
['dataLen', option(u64())],
],
{ description: 'ExternalRegistryRecord' }
) as Serializer<ExternalRegistryRecordArgs, ExternalRegistryRecord>;
Expand Down
5 changes: 5 additions & 0 deletions clients/js/src/hooked/pluginRegistryV1Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,18 @@ export function getExternalRegistryRecordSerializer(): Serializer<
buffer,
lifecycleChecksOffset
);

const [dataOffset, dataOffsetOffset] = option(u64()).deserialize(buffer, pluginOffsetOffset);
const [dataLen] = option(u64()).deserialize(buffer, dataOffsetOffset);
return [
{
pluginType,
authority,
lifecycleChecks,
offset: pluginOffset,
isUnknown,
dataOffset,
dataLen,
},
pluginOffsetOffset,
];
Expand Down
2 changes: 0 additions & 2 deletions clients/js/src/instructions/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ export const create = (
authority: plugin.initPluginAuthority || {
type: 'UpdateAuthority',
},
dataLen: 0n,
dataOffset: 0n,
type: 'LifecycleHook',
schema: plugin.schema || ExternalPluginSchema.Binary,
});
Expand Down
6 changes: 3 additions & 3 deletions clients/js/src/plugins/dataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
BaseDataStoreInitInfoArgs,
BaseDataStoreUpdateInfoArgs,
ExternalPluginSchema,
ExternalRegistryRecord,
} from '../generated';
import { ExternalPluginKey } from './externalPluginKey';
import { ExternalPluginManifest } from './externalPluginManifest';
Expand Down Expand Up @@ -66,14 +67,13 @@ export function dataStoreUpdateInfoArgsToBase(

export function dataStoreFromBase(
s: BaseDataStore,
r: ExternalRegistryRecord,
account: Uint8Array
): DataStore {
return {
...s,
dataAuthority: pluginAuthorityFromBase(s.dataAuthority),
dataOffset: s.dataOffset,
dataLen: s.dataLen,
data: parseExternalPluginData(s, account),
data: parseExternalPluginData(s, r, account),
};
}

Expand Down
3 changes: 2 additions & 1 deletion clients/js/src/plugins/externalPluginManifest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ExternalRegistryRecord } from 'src/generated';
import { ExternalPluginTypeString } from './externalPlugins';

export type ExternalPluginManifest<
Expand All @@ -9,7 +10,7 @@ export type ExternalPluginManifest<
UpdateBase extends Object
> = {
type: ExternalPluginTypeString;
fromBase: (input: Base, account: Uint8Array) => T;
fromBase: (input: Base, record: ExternalRegistryRecord, account: Uint8Array) => T;
initToBase: (input: Init) => InitBase;
updateToBase: (input: Update) => UpdateBase;
};
42 changes: 22 additions & 20 deletions clients/js/src/plugins/externalPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountMeta, Context, PublicKey } from '@metaplex-foundation/umi';
import { AccountMeta, Context, PublicKey, Option } from '@metaplex-foundation/umi';
import {
lifecycleHookFromBase,
LifecycleHookInitInfoArgs,
Expand Down Expand Up @@ -50,25 +50,25 @@ export type ExternalPluginsList = {

export type ExternalPluginInitInfoArgs =
| ({
type: 'Oracle';
} & OracleInitInfoArgs)
type: 'Oracle';
} & OracleInitInfoArgs)
| ({
type: 'LifecycleHook';
} & LifecycleHookInitInfoArgs)
type: 'LifecycleHook';
} & LifecycleHookInitInfoArgs)
| ({
type: 'DataStore';
} & DataStoreInitInfoArgs);
type: 'DataStore';
} & DataStoreInitInfoArgs);

export type ExternalPluginUpdateInfoArgs =
| ({
type: 'Oracle';
} & OracleUpdateInfoArgs)
type: 'Oracle';
} & OracleUpdateInfoArgs)
| ({
type: 'LifecycleHook';
} & LifecycleHookUpdateInfoArgs)
type: 'LifecycleHook';
} & LifecycleHookUpdateInfoArgs)
| ({
type: 'DataStore';
} & DataStoreUpdateInfoArgs);
type: 'DataStore';
} & DataStoreUpdateInfoArgs);

export const externalPluginManifests = {
Oracle: oracleManifest,
Expand Down Expand Up @@ -110,7 +110,7 @@ export function externalRegistryRecordsToExternalPluginList(
result.oracles.push({
type: 'Oracle',
...mappedPlugin,
...oracleFromBase(deserializedPlugin.fields[0], accountData),
...oracleFromBase(deserializedPlugin.fields[0], record, accountData),
});
} else if (deserializedPlugin.__kind === 'DataStore') {
if (!result.dataStores) {
Expand All @@ -119,7 +119,7 @@ export function externalRegistryRecordsToExternalPluginList(
result.dataStores.push({
type: 'DataStore',
...mappedPlugin,
...dataStoreFromBase(deserializedPlugin.fields[0], accountData),
...dataStoreFromBase(deserializedPlugin.fields[0], record, accountData),
});
} else if (deserializedPlugin.__kind === 'LifecycleHook') {
if (!result.lifecycleHooks) {
Expand All @@ -128,7 +128,7 @@ export function externalRegistryRecordsToExternalPluginList(
result.lifecycleHooks.push({
type: 'LifecycleHook',
...mappedPlugin,
...lifecycleHookFromBase(deserializedPlugin.fields[0], accountData),
...lifecycleHookFromBase(deserializedPlugin.fields[0], record, accountData),
});
}
});
Expand Down Expand Up @@ -232,15 +232,17 @@ export const findExtraAccounts = (
export function parseExternalPluginData(
plugin: {
schema: ExternalPluginSchema;
dataLen: bigint | number;
dataOffset: bigint | number;
},
record: {
dataLen: Option<bigint | number>;
dataOffset: Option<bigint | number>;
},
account: Uint8Array
): any {
let data;
const dataSlice = account.slice(
Number(plugin.dataOffset),
Number(plugin.dataOffset) + Number(plugin.dataLen)
Number(record.dataOffset),
Number(record.dataOffset) + Number(record.dataLen)
);

if (plugin.schema === ExternalPluginSchema.Binary) {
Expand Down
6 changes: 3 additions & 3 deletions clients/js/src/plugins/lifecycleHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BaseLifecycleHookInitInfoArgs,
BaseLifecycleHookUpdateInfoArgs,
ExternalPluginSchema,
ExternalRegistryRecord,
} from '../generated';
import { LifecycleChecks, lifecycleChecksToBase } from './lifecycleChecks';
import {
Expand Down Expand Up @@ -95,17 +96,16 @@ export function lifecycleHookUpdateInfoArgsToBase(

export function lifecycleHookFromBase(
s: BaseLifecycleHook,
r: ExternalRegistryRecord,
account: Uint8Array
): LifecycleHook {
return {
...s,
dataOffset: s.dataOffset,
dataLen: s.dataLen,
extraAccounts:
s.extraAccounts.__option === 'Some'
? s.extraAccounts.value.map(extraAccountFromBase)
: undefined,
data: parseExternalPluginData(s, account),
data: parseExternalPluginData(s, r, account),
dataAuthority:
s.dataAuthority.__option === 'Some'
? pluginAuthorityFromBase(s.dataAuthority.value)
Expand Down
3 changes: 2 additions & 1 deletion clients/js/src/plugins/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BaseOracle,
BaseOracleInitInfoArgs,
BaseOracleUpdateInfoArgs,
ExternalRegistryRecord,
} from '../generated';
import { LifecycleChecks, lifecycleChecksToBase } from './lifecycleChecks';
import { PluginAuthority, pluginAuthorityToBase } from './pluginAuthority';
Expand Down Expand Up @@ -82,7 +83,7 @@ export function oracleUpdateInfoArgsToBase(
};
}

export function oracleFromBase(s: BaseOracle, account: Uint8Array): Oracle {
export function oracleFromBase(s: BaseOracle, r: ExternalRegistryRecord, account: Uint8Array): Oracle {
return {
...s,
pda:
Expand Down
2 changes: 0 additions & 2 deletions clients/rust/src/generated/types/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ use borsh::BorshSerialize;
pub struct DataStore {
pub data_authority: PluginAuthority,
pub schema: ExternalPluginSchema,
pub data_offset: u64,
pub data_len: u64,
}
Loading

0 comments on commit 1c5583c

Please sign in to comment.