From f618a628ffc20389cbfbb41fb01e5161e68f9943 Mon Sep 17 00:00:00 2001 From: alvrs Date: Thu, 21 Sep 2023 00:16:40 +0100 Subject: [PATCH] don't render getter methods in offchain tables --- .../src/codegen/tables/MessageTable.sol | 36 ----- .../contracts/src/codegen/tables/Offchain.sol | 66 --------- packages/store/ts/codegen/field.ts | 140 +++++++++--------- packages/store/ts/codegen/record.ts | 42 +++--- packages/store/ts/codegen/tableOptions.ts | 1 + packages/store/ts/codegen/types.ts | 2 + 6 files changed, 98 insertions(+), 189 deletions(-) diff --git a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol index 405c6a00f2..ba92627410 100644 --- a/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol +++ b/examples/minimal/packages/contracts/src/codegen/tables/MessageTable.sol @@ -76,42 +76,6 @@ library MessageTable { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Get the full data */ - function get() internal view returns (string memory value) { - bytes32[] memory _keyTuple = new bytes32[](0); - - (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - - /** Get the full data */ - function _get() internal view returns (string memory value) { - bytes32[] memory _keyTuple = new bytes32[](0); - - (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - - /** Get the full data (using the specified store) */ - function get(IStore _store) internal view returns (string memory value) { - bytes32[] memory _keyTuple = new bytes32[](0); - - (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - /** Set the full data using individual values */ function set(string memory value) internal { bytes memory _staticData; diff --git a/packages/cli/contracts/src/codegen/tables/Offchain.sol b/packages/cli/contracts/src/codegen/tables/Offchain.sol index 5a988615ec..31767a067d 100644 --- a/packages/cli/contracts/src/codegen/tables/Offchain.sol +++ b/packages/cli/contracts/src/codegen/tables/Offchain.sol @@ -78,33 +78,6 @@ library Offchain { _store.registerTable(_tableId, _fieldLayout, getKeySchema(), getValueSchema(), getKeyNames(), getFieldNames()); } - /** Get value */ - function getValue(bytes32 key) internal view returns (uint256 value) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** Get value */ - function _getValue(bytes32 key) internal view returns (uint256 value) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - - /** Get value (using the specified store) */ - function getValue(IStore _store, bytes32 key) internal view returns (uint256 value) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - bytes32 _blob = _store.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); - } - /** Set value */ function setValue(bytes32 key, uint256 value) internal { bytes32[] memory _keyTuple = new bytes32[](1); @@ -129,45 +102,6 @@ library Offchain { _store.setField(_tableId, _keyTuple, 0, abi.encodePacked((value)), _fieldLayout); } - /** Get the full data */ - function get(bytes32 key) internal view returns (uint256 value) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - - /** Get the full data */ - function _get(bytes32 key) internal view returns (uint256 value) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - - /** Get the full data (using the specified store) */ - function get(IStore _store, bytes32 key) internal view returns (uint256 value) { - bytes32[] memory _keyTuple = new bytes32[](1); - _keyTuple[0] = key; - - (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = _store.getRecord( - _tableId, - _keyTuple, - _fieldLayout - ); - return decode(_staticData, _encodedLengths, _dynamicData); - } - /** Set the full data using individual values */ function set(bytes32 key, uint256 value) internal { bytes memory _staticData = encodeStatic(value); diff --git a/packages/store/ts/codegen/field.ts b/packages/store/ts/codegen/field.ts index 4367a55f84..6fad585ca1 100644 --- a/packages/store/ts/codegen/field.ts +++ b/packages/store/ts/codegen/field.ts @@ -21,36 +21,38 @@ export function renderFieldMethods(options: RenderTableOptions) { // For dynamic fields, compute the field index relative to the end of the static fields const _typedFieldName = `${field.typeWithLocation} ${field.name}`; - result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => - renderWithStore( - storeArgument, - (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` - /** Get ${field.name}${_commentSuffix} */ - function ${_methodNamePrefix}get${_methodNameSuffix}(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - ])}) internal view returns (${_typedFieldName}) { - ${_keyTupleDefinition} - ${ - field.isDynamic - ? `bytes memory _blob = ${_store}.getDynamicField( - _tableId, - _keyTuple, - ${schemaIndex - options.staticFields.length} - );` - : `bytes32 _blob = ${_store}.getStaticField( - _tableId, - _keyTuple, - ${schemaIndex}, - _fieldLayout - );` + if (options.withGetters) { + result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => + renderWithStore( + storeArgument, + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` + /** Get ${field.name}${_commentSuffix} */ + function ${_methodNamePrefix}get${_methodNameSuffix}(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal view returns (${_typedFieldName}) { + ${_keyTupleDefinition} + ${ + field.isDynamic + ? `bytes memory _blob = ${_store}.getDynamicField( + _tableId, + _keyTuple, + ${schemaIndex - options.staticFields.length} + );` + : `bytes32 _blob = ${_store}.getStaticField( + _tableId, + _keyTuple, + ${schemaIndex}, + _fieldLayout + );` + } + return ${renderDecodeFieldSingle(field)}; } - return ${renderDecodeFieldSingle(field)}; - } ` - ) - ); + ) + ); + } result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => renderWithStore( @@ -73,40 +75,41 @@ export function renderFieldMethods(options: RenderTableOptions) { if (field.isDynamic) { const portionData = fieldPortionData(field); - result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => - renderWithStore( - storeArgument, - (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` - /** Get the length of ${field.name}${_commentSuffix} */ - function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - ])}) internal view returns (uint256) { - ${_keyTupleDefinition} - uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, _fieldLayout); - unchecked { - return _byteLength / ${portionData.elementLength}; + if (options.withGetters) { + result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => + renderWithStore( + storeArgument, + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` + /** Get the length of ${field.name}${_commentSuffix} */ + function ${_methodNamePrefix}length${_methodNameSuffix}(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal view returns (uint256) { + ${_keyTupleDefinition} + uint256 _byteLength = ${_store}.getFieldLength(_tableId, _keyTuple, ${schemaIndex}, _fieldLayout); + unchecked { + return _byteLength / ${portionData.elementLength}; + } } - } ` - ) - ); + ) + ); - result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => - renderWithStore( - storeArgument, - (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` - /** - * Get an item of ${field.name}${_commentSuffix} - * (unchecked, returns invalid data if index overflows) - */ - function ${_methodNamePrefix}getItem${_methodNameSuffix}(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - "uint256 _index", - ])}) internal view returns (${portionData.typeWithLocation}) { + result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => + renderWithStore( + storeArgument, + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` + /** + * Get an item of ${field.name}${_commentSuffix} + * (unchecked, returns invalid data if index overflows) + */ + function ${_methodNamePrefix}getItem${_methodNameSuffix}(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + "uint256 _index", + ])}) internal view returns (${portionData.typeWithLocation}) { ${_keyTupleDefinition} unchecked { bytes memory _blob = ${_store}.getFieldSlice( @@ -116,20 +119,21 @@ export function renderFieldMethods(options: RenderTableOptions) { _fieldLayout, _index * ${portionData.elementLength}, (_index + 1) * ${portionData.elementLength} - ); - return ${portionData.decoded}; + ); + return ${portionData.decoded}; + } } - } - ` - ) - ); + ` + ) + ); + } result += renderWithFieldSuffix(options.withSuffixlessFieldMethods, field.name, (_methodNameSuffix) => renderWithStore( storeArgument, (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` - /** Push ${portionData.title} to ${field.name}${_commentSuffix} */ - function ${_methodNamePrefix}push${_methodNameSuffix}(${renderArguments([ + /** Push ${portionData.title} to ${field.name}${_commentSuffix} */ + function ${_methodNamePrefix}push${_methodNameSuffix}(${renderArguments([ _typedStore, _typedTableId, _typedKeyArgs, @@ -138,7 +142,7 @@ export function renderFieldMethods(options: RenderTableOptions) { ${_keyTupleDefinition} ${_store}.pushToField(_tableId, _keyTuple, ${schemaIndex}, ${portionData.encoded}, _fieldLayout); } - ` + ` ) ); diff --git a/packages/store/ts/codegen/record.ts b/packages/store/ts/codegen/record.ts index 522e39676f..bca8884879 100644 --- a/packages/store/ts/codegen/record.ts +++ b/packages/store/ts/codegen/record.ts @@ -12,26 +12,30 @@ export function renderRecordMethods(options: RenderTableOptions) { const { structName, storeArgument } = options; const { _tableId, _typedTableId, _keyArgs, _typedKeyArgs, _keyTupleDefinition } = renderCommonData(options); - let result = renderWithStore( - storeArgument, - (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` - /** Get the full data${_commentSuffix} */ - function ${_methodNamePrefix}get(${renderArguments([ - _typedStore, - _typedTableId, - _typedKeyArgs, - ])}) internal view returns (${renderDecodedRecord(options)}) { - ${_keyTupleDefinition} + let result = ""; - ( - bytes memory _staticData, - PackedCounter _encodedLengths, - bytes memory _dynamicData - ) = ${_store}.getRecord(_tableId, _keyTuple, _fieldLayout); - return decode(_staticData, _encodedLengths, _dynamicData); - } - ` - ); + if (options.withGetters) { + result += renderWithStore( + storeArgument, + (_typedStore, _store, _commentSuffix, _untypedStore, _methodNamePrefix) => ` + /** Get the full data${_commentSuffix} */ + function ${_methodNamePrefix}get(${renderArguments([ + _typedStore, + _typedTableId, + _typedKeyArgs, + ])}) internal view returns (${renderDecodedRecord(options)}) { + ${_keyTupleDefinition} + + ( + bytes memory _staticData, + PackedCounter _encodedLengths, + bytes memory _dynamicData + ) = ${_store}.getRecord(_tableId, _keyTuple, _fieldLayout); + return decode(_staticData, _encodedLengths, _dynamicData); + } + ` + ); + } result += renderWithStore( storeArgument, diff --git a/packages/store/ts/codegen/tableOptions.ts b/packages/store/ts/codegen/tableOptions.ts index 79404c23ee..c1f19d91ce 100644 --- a/packages/store/ts/codegen/tableOptions.ts +++ b/packages/store/ts/codegen/tableOptions.ts @@ -97,6 +97,7 @@ export function getTableOptions(config: StoreConfig): TableOptions[] { fields, staticFields, dynamicFields, + withGetters: !tableData.offchainOnly, withRecordMethods, withDynamicFieldMethods: !tableData.offchainOnly, withSuffixlessFieldMethods, diff --git a/packages/store/ts/codegen/types.ts b/packages/store/ts/codegen/types.ts index 222ab6bc1a..96ea355f53 100644 --- a/packages/store/ts/codegen/types.ts +++ b/packages/store/ts/codegen/types.ts @@ -22,6 +22,8 @@ export interface RenderTableOptions { fields: RenderField[]; staticFields: RenderStaticField[]; dynamicFields: RenderDynamicField[]; + /** Whether to render getter functions */ + withGetters: boolean; /** Whether to render dynamic field methods (push, pop, update) */ withDynamicFieldMethods: boolean; /** Whether to render get/set methods for the whole record */