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

refactor: rename fileSelector to name #610

Merged
merged 2 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cli/src/render-solidity/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function renderWithStore(
}

export function renderTableId(staticResourceData: StaticResourceData) {
const hardcodedTableId = `uint256(bytes32(abi.encodePacked(bytes16("${staticResourceData.namespace}"), bytes16("${staticResourceData.fileSelector}"))))`;
const hardcodedTableId = `uint256(bytes32(abi.encodePacked(bytes16("${staticResourceData.namespace}"), bytes16("${staticResourceData.name}"))))`;

const tableIdDefinition = `
uint256 constant _tableId = ${hardcodedTableId};
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-solidity/tableOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function getTableOptions(config: StoreConfig): TableOptions[] {
return {
tableIdName: tableName + "TableId",
namespace: config.namespace,
fileSelector: tableData.fileSelector,
name: tableData.name,
};
}
})();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-solidity/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface StaticResourceData {
/** Name of the table id constant to render. */
tableIdName: string;
namespace: string;
fileSelector: string;
name: string;
}

export interface RenderTableType {
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/render-solidity/worldgen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export async function worldgen(
}));
const systemInterfaceName = `I${system.basename}`;
// create an interface using the external functions and imports
const { fileSelector } = config.systems[system.basename];
const { name } = config.systems[system.basename];
const output = renderSystemInterface({
name: systemInterfaceName,
functionPrefix: config.namespace === "" ? "" : `${config.namespace}_${fileSelector}_`,
functionPrefix: config.namespace === "" ? "" : `${config.namespace}_${name}_`,
functions,
imports,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-ts/recsV1TableOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function getRecsV1TableOptions(config: StoreConfig): RecsV1TableOptions {
if (tableData.tableIdArgument) continue;
const staticResourceData = {
namespace: config.namespace,
fileSelector: tableData.fileSelector,
name: tableData.name,
};

tableOptions.push({
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/render-ts/renderRecsV1Tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export function defineContractComponents(world: World) {
}

function renderDefineComponent(table: RecsV1TableOptions["tables"][number]) {
const { namespace, fileSelector } = table.staticResourceData;
const { namespace, name } = table.staticResourceData;
return `
(() => {
const tableId = new TableId("${namespace}", "${fileSelector}");
const tableId = new TableId("${namespace}", "${name}");
return defineComponent(world, {
${table.fields.map(({ name, recsTypeString }) => `${name}: ${recsTypeString},`).join("")}
}, {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface RecsV1TableOptions {
}[];
staticResourceData: {
namespace: string;
fileSelector: string;
name: string;
};
}[];
}
122 changes: 59 additions & 63 deletions packages/cli/src/utils/deploy-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ export async function deploy(mudConfig: MUDConfig, deployConfig: DeployConfig):
const tableIds: { [tableName: string]: Uint8Array } = {};
promises = [
...promises,
...Object.entries(mudConfig.tables).map(async ([tableName, { fileSelector, schema, primaryKeys }]) => {
console.log(chalk.blue(`Registering table ${tableName} at ${namespace}/${fileSelector}`));
...Object.entries(mudConfig.tables).map(async ([tableName, { name, schema, primaryKeys }]) => {
console.log(chalk.blue(`Registering table ${tableName} at ${namespace}/${name}`));

// Store the tableId for later use
tableIds[tableName] = toResourceSelector(namespace, fileSelector);
tableIds[tableName] = toResourceSelector(namespace, name);

// Register table
const schemaTypes = Object.values(schema).map((abiOrUserType) => {
Expand All @@ -141,88 +141,84 @@ export async function deploy(mudConfig: MUDConfig, deployConfig: DeployConfig):

await fastTxExecute(WorldContract, "registerTable", [
toBytes16(namespace),
toBytes16(fileSelector),
toBytes16(name),
encodeSchema(schemaTypes),
encodeSchema(keyTypes),
]);

// Register table metadata
await fastTxExecute(WorldContract, "setMetadata(bytes16,bytes16,string,string[])", [
toBytes16(namespace),
toBytes16(fileSelector),
toBytes16(name),
tableName,
Object.keys(schema),
]);

console.log(chalk.green(`Registered table ${tableName} at ${fileSelector}`));
console.log(chalk.green(`Registered table ${tableName} at ${name}`));
}),
];

// Register systems (using forEach instead of for..of to avoid blocking on async calls)
promises = [
...promises,
...Object.entries(mudConfig.systems).map(
async ([systemName, { fileSelector, openAccess, registerFunctionSelectors }]) => {
// Register system at route
console.log(chalk.blue(`Registering system ${systemName} at ${namespace}/${fileSelector}`));
await fastTxExecute(WorldContract, "registerSystem", [
toBytes16(namespace),
toBytes16(fileSelector),
await contractPromises[systemName],
openAccess,
]);
console.log(chalk.green(`Registered system ${systemName} at ${namespace}/${fileSelector}`));

// Register function selectors for the system
if (registerFunctionSelectors) {
const functionSignatures: FunctionSignature[] = await loadFunctionSignatures(systemName);
const isRoot = namespace === "";
// Using Promise.all to avoid blocking on async calls
await Promise.all(
functionSignatures.map(async ({ functionName, functionArgs }) => {
const functionSignature = isRoot
? functionName + functionArgs
: `${namespace}_${fileSelector}_${functionName}${functionArgs}`;

console.log(chalk.blue(`Registering function "${functionSignature}"`));
if (isRoot) {
const worldFunctionSelector = toFunctionSelector(
functionSignature === ""
? { functionName: systemName, functionArgs } // Register the system's fallback function as `<systemName>(<args>)`
: { functionName, functionArgs }
);
const systemFunctionSelector = toFunctionSelector({ functionName, functionArgs });
await fastTxExecute(WorldContract, "registerRootFunctionSelector", [
toBytes16(namespace),
toBytes16(fileSelector),
worldFunctionSelector,
systemFunctionSelector,
]);
} else {
await fastTxExecute(WorldContract, "registerFunctionSelector", [
toBytes16(namespace),
toBytes16(fileSelector),
functionName,
functionArgs,
]);
}
console.log(chalk.green(`Registered function "${functionSignature}"`));
})
);
}
...Object.entries(mudConfig.systems).map(async ([systemName, { name, openAccess, registerFunctionSelectors }]) => {
// Register system at route
console.log(chalk.blue(`Registering system ${systemName} at ${namespace}/${name}`));
await fastTxExecute(WorldContract, "registerSystem", [
toBytes16(namespace),
toBytes16(name),
await contractPromises[systemName],
openAccess,
]);
console.log(chalk.green(`Registered system ${systemName} at ${namespace}/${name}`));

// Register function selectors for the system
if (registerFunctionSelectors) {
const functionSignatures: FunctionSignature[] = await loadFunctionSignatures(systemName);
const isRoot = namespace === "";
// Using Promise.all to avoid blocking on async calls
await Promise.all(
functionSignatures.map(async ({ functionName, functionArgs }) => {
const functionSignature = isRoot
? functionName + functionArgs
: `${namespace}_${name}_${functionName}${functionArgs}`;

console.log(chalk.blue(`Registering function "${functionSignature}"`));
if (isRoot) {
const worldFunctionSelector = toFunctionSelector(
functionSignature === ""
? { functionName: systemName, functionArgs } // Register the system's fallback function as `<systemName>(<args>)`
: { functionName, functionArgs }
);
const systemFunctionSelector = toFunctionSelector({ functionName, functionArgs });
await fastTxExecute(WorldContract, "registerRootFunctionSelector", [
toBytes16(namespace),
toBytes16(name),
worldFunctionSelector,
systemFunctionSelector,
]);
} else {
await fastTxExecute(WorldContract, "registerFunctionSelector", [
toBytes16(namespace),
toBytes16(name),
functionName,
functionArgs,
]);
}
console.log(chalk.green(`Registered function "${functionSignature}"`));
})
);
}
),
}),
];

// Wait for resources to be registered before granting access to them
await Promise.all(promises); // ----------------------------------------------------------------------------------------------
promises = [];

// Grant access to systems
for (const [systemName, { fileSelector, accessListAddresses, accessListSystems }] of Object.entries(
mudConfig.systems
)) {
const resourceSelector = `${namespace}/${fileSelector}`;
for (const [systemName, { name, accessListAddresses, accessListSystems }] of Object.entries(mudConfig.systems)) {
const resourceSelector = `${namespace}/${name}`;

// Grant access to addresses
promises = [
Expand All @@ -231,10 +227,10 @@ export async function deploy(mudConfig: MUDConfig, deployConfig: DeployConfig):
console.log(chalk.blue(`Grant ${address} access to ${systemName} (${resourceSelector})`));
await fastTxExecute(WorldContract, "grantAccess(bytes16,bytes16,address)", [
toBytes16(namespace),
toBytes16(fileSelector),
toBytes16(name),
address,
]);
console.log(chalk.green(`Granted ${address} access to ${systemName} (${namespace}/${fileSelector})`));
console.log(chalk.green(`Granted ${address} access to ${systemName} (${namespace}/${name})`));
}),
];

Expand All @@ -245,7 +241,7 @@ export async function deploy(mudConfig: MUDConfig, deployConfig: DeployConfig):
console.log(chalk.blue(`Grant ${granteeSystem} access to ${systemName} (${resourceSelector})`));
await fastTxExecute(WorldContract, "grantAccess(bytes16,bytes16,address)", [
toBytes16(namespace),
toBytes16(fileSelector),
toBytes16(name),
await contractPromises[granteeSystem],
]);
console.log(chalk.green(`Granted ${granteeSystem} access to ${systemName} (${resourceSelector})`));
Expand Down
14 changes: 7 additions & 7 deletions packages/config/src/store/parseStoreConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ export interface TableConfig<
/** Output directory path for the file. Default is "tables" */
directory?: string;
/**
* The fileSelector is used with the namespace to register the table and construct its id.
* The table id will be uint256(bytes32(abi.encodePacked(bytes16(namespace), bytes16(fileSelector)))).
* The name is used with the namespace to register the table and construct its id.
* The table id will be uint256(bytes32(abi.encodePacked(bytes16(namespace), bytes16(name)))).
* Default is "<tableName>"
* */
fileSelector?: string;
name?: string;
/** Make methods accept `tableId` argument instead of it being a hardcoded constant. Default is false */
tableIdArgument?: boolean;
/** Include methods that accept a manual `IStore` argument. Default is true. */
Expand All @@ -79,7 +79,7 @@ export interface TableConfig<
const zFullTableConfig = z
.object({
directory: z.string().default("tables"),
fileSelector: zSelector.optional(),
name: zSelector.optional(),
tableIdArgument: z.boolean().default(false),
storeArgument: z.boolean().default(true),
primaryKeys: zPrimaryKeys,
Expand Down Expand Up @@ -118,14 +118,14 @@ export type TablesConfig<
> = Record<string, TableConfig<UserTypes, StaticUserTypes> | FieldData<UserTypes>>;

export const zTablesConfig = z.record(zTableName, zTableConfig).transform((tables) => {
// default fileSelector depends on tableName
// default name depends on tableName
for (const tableName of Object.keys(tables)) {
const table = tables[tableName];
table.fileSelector ??= tableName;
table.name ??= tableName;

tables[tableName] = table;
}
return tables as Record<string, RequireKeys<(typeof tables)[string], "fileSelector">>;
return tables as Record<string, RequireKeys<(typeof tables)[string], "name">>;
});

/************************************************************************
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/world/parseWorldConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const zSystemName = zObjectName;
const zModuleName = zObjectName;
const zSystemAccessList = z.array(zSystemName.or(zEthereumAddress)).default([]);

// The system config is a combination of a fileSelector config and access config
// The system config is a combination of a name config and access config
const zSystemConfig = z.intersection(
z.object({
fileSelector: zSelector,
name: zSelector,
registerFunctionSelectors: z.boolean().default(true),
}),
z.discriminatedUnion("openAccess", [
Expand Down
6 changes: 3 additions & 3 deletions packages/config/src/world/resolveWorldConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export function resolveWorldConfig(config: ParsedWorldConfig, existingContracts?
* @param config optional SystemConfig object, if none is provided the default config is used
* @param existingContracts optional list of existing contract names, used to validate system names in the access list. If not provided, no validation is performed.
* @returns ResolvedSystemConfig object
* Default value for fileSelector is `systemName`
* Default value for name is `systemName`
* Default value for registerFunctionSelectors is true
* Default value for openAccess is true
* Default value for accessListAddresses is []
* Default value for accessListSystems is []
*/
export function resolveSystemConfig(systemName: string, config?: SystemUserConfig, existingContracts?: string[]) {
const fileSelector = config?.fileSelector ?? systemName;
const name = config?.name ?? systemName;
const registerFunctionSelectors = config?.registerFunctionSelectors ?? true;
const openAccess = config?.openAccess ?? true;
const accessListAddresses: string[] = [];
Expand All @@ -76,5 +76,5 @@ export function resolveSystemConfig(systemName: string, config?: SystemUserConfi
}
}

return { fileSelector, registerFunctionSelectors, openAccess, accessListAddresses, accessListSystems };
return { name, registerFunctionSelectors, openAccess, accessListAddresses, accessListSystems };
}
4 changes: 2 additions & 2 deletions packages/config/src/world/userTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { DynamicResolution } from "../dynamicResolution.js";
// zod doesn't preserve doc comments
export type SystemUserConfig =
| {
/** The full resource selector consists of namespace and fileSelector */
fileSelector?: string;
/** The full resource selector consists of namespace and name */
name?: string;
/**
* Register function selectors for the system in the World.
* Defaults to true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function defineContractComponents(world: World) {
world,
{
namespace: RecsType.String,
file: RecsType.String,
name: RecsType.String,
systemFunctionSelector: RecsType.String,
},
{
Expand Down
Loading