Skip to content

Commit

Permalink
refactor(store): add StoreWrite and Store abstract contracts (#2411)
Browse files Browse the repository at this point in the history
Co-authored-by: alvrs <alvarius@lattice.xyz>
Co-authored-by: Kevin Ingersoll <kingersoll@gmail.com>
  • Loading branch information
3 people committed Mar 21, 2024
1 parent b0c45f8 commit 93390d8
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 269 deletions.
6 changes: 6 additions & 0 deletions .changeset/silly-mirrors-marry.md
@@ -0,0 +1,6 @@
---
"@latticexyz/store": minor
"@latticexyz/world": patch
---

Added an `abstract` `StoreKernel` contract, which includes all Store interfaces except for registration, and implements write methods, `protocolVersion` and initializes `StoreCore`. `Store` extends `StoreKernel` with the `IStoreRegistration` interface. `StoreData` is removed as a separate interface/contract. `World` now extends `StoreKernel` (since the registration methods are added via the `InitModule`).
32 changes: 3 additions & 29 deletions docs/pages/store/reference/store-core.mdx
Expand Up @@ -1015,44 +1015,18 @@ function _loadEncodedDynamicDataLength(
| -------- | ---------------- | ----------------------------------------------------------------------------------------- |
| `<none>` | `EncodedLengths` | The loaded encoded dynamic data length from storage for the given table ID and key tuple. |

## StoreData
## Store

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/StoreData.sol)
[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/Store.sol)

**Inherits:**
[IStoreData](/store/reference/store#istoredata), [StoreRead](/store/reference/store-core#storeread)
[IStore](/src/IStore.sol/interface.IStore.md), [StoreKernel](/src/StoreKernel.sol/abstract.StoreKernel.md)

This contract integrates the core storage functionalities and provides an interface for data storage.

_This abstract contract initializes `StoreCore`, implements `storeVersion`, and read methods,
but not write methods._

### Functions

#### constructor

Constructs the StoreData contract and initializes the StoreCore.

_Emits a HelloStore event upon creation._

```solidity
constructor();
```

#### storeVersion

Retrieves the protocol version of the Store.

```solidity
function storeVersion() public pure returns (bytes32);
```

**Returns**

| Name | Type | Description |
| -------- | --------- | ---------------------------------- |
| `<none>` | `bytes32` | The protocol version of the Store. |

## StoreRead

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/StoreRead.sol)
Expand Down
31 changes: 1 addition & 30 deletions docs/pages/store/reference/store.mdx
Expand Up @@ -5,9 +5,7 @@
[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStore.sol)

**Inherits:**
[IStoreData](/store/reference/store#istoredata), [IStoreRegistration](/store/reference/store#istoreregistration), [IStoreErrors](/store/reference/store#istoreerrors), [IFieldLayoutErrors](/src/IFieldLayoutErrors.sol/interface.IFieldLayoutErrors.md), [IEncodedLengthsErrors](/src/IEncodedLengthsErrors.sol/interface.IEncodedLengthsErrors.md), [ISchemaErrors](/src/ISchemaErrors.sol/interface.ISchemaErrors.md), [ISliceErrors](/src/ISliceErrors.sol/interface.ISliceErrors.md)

IStore implements the error interfaces for each library that it uses.
[IStoreKernel](/src/IStoreKernel.sol/interface.IStoreKernel.md), [IStoreRegistration](/store/reference/store#istoreregistration)

## IStoreEvents

Expand Down Expand Up @@ -313,33 +311,6 @@ error Store_InvalidSplice(uint40 startWithinField, uint40 deleteCount, uint40 fi
| `deleteCount` | `uint40` | The number of bytes to delete in the splice operation. |
| `fieldLength` | `uint40` | The field length for the splice operation. |

## IStoreData

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreData.sol)

**Inherits:**
[IStoreRead](/store/reference/store#istoreread), [IStoreWrite](/store/reference/store#istorewrite)

The IStoreData interface includes methods for reading and writing table values.

_These methods are frequently invoked during runtime, so it is essential to prioritize optimizing their gas cost._

### Functions

#### storeVersion

Returns the version of the Store contract.

```solidity
function storeVersion() external view returns (bytes32 version);
```

**Returns**

| Name | Type | Description |
| --------- | --------- | ---------------------------------- |
| `version` | `bytes32` | The version of the Store contract. |

## IStoreRead

[Git Source](https://github.com/latticexyz/mud/blob/main/packages/store/src/IStoreRead.sol)
Expand Down
18 changes: 7 additions & 11 deletions docs/pages/world/reference/world-external.mdx
Expand Up @@ -12,21 +12,17 @@ that are dynamically registered in the World during deployment.

_._

### Functions

#### storeVersion
## Store

Retrieves the protocol version of the Store.
[Git Source](https://github.com/latticexyz/mud/blob/main/packages/world/src/Store.sol)

```solidity
function storeVersion() public pure returns (bytes32);
```
**Inherits:**
[IStore](/src/IStore.sol/interface.IStore.md), [StoreKernel](/src/StoreKernel.sol/abstract.StoreKernel.md)

**Returns**
This contract integrates the core storage functionalities and provides an interface for data storage.

| Name | Type | Description |
| -------- | --------- | ---------------------------------- |
| `<none>` | `bytes32` | The protocol version of the Store. |
_This abstract contract initializes `StoreCore`, implements `storeVersion`, and read methods,
but not write methods._

#### registerTable

Expand Down
4 changes: 3 additions & 1 deletion docs/pages/world/reference/world.mdx
Expand Up @@ -5,11 +5,13 @@
[Git Source](https://github.com/latticexyz/mud/blob/main/packages/world/src/World.sol)

**Inherits:**
[StoreData](/store/reference/store-core#storedata), [IWorldKernel](/world/reference/world-external#iworldkernel)
StoreKernel, [IWorldKernel](/world/reference/world-external#iworldkernel)

_This contract is the core "World" contract containing various methods for
data manipulation, system calls, and dynamic function selector handling._

_World doesn't inherit `Store` because the `IStoreRegistration` methods are added via the `InitModule`._

### State Variables

#### creator
Expand Down
18 changes: 2 additions & 16 deletions packages/store/src/IStore.sol
@@ -1,25 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { IStoreErrors } from "./IStoreErrors.sol";
import { IStoreData } from "./IStoreData.sol";
import { IStoreKernel } from "./IStoreKernel.sol";
import { IStoreRegistration } from "./IStoreRegistration.sol";
import { IFieldLayoutErrors } from "./IFieldLayoutErrors.sol";
import { IEncodedLengthsErrors } from "./IEncodedLengthsErrors.sol";
import { ISchemaErrors } from "./ISchemaErrors.sol";
import { ISliceErrors } from "./ISliceErrors.sol";

/**
* @title IStore
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz)
* @notice IStore implements the error interfaces for each library that it uses.
*/
interface IStore is
IStoreData,
IStoreRegistration,
IStoreErrors,
IFieldLayoutErrors,
IEncodedLengthsErrors,
ISchemaErrors,
ISliceErrors
{}
interface IStore is IStoreKernel, IStoreRegistration {}
19 changes: 0 additions & 19 deletions packages/store/src/IStoreData.sol

This file was deleted.

31 changes: 31 additions & 0 deletions packages/store/src/IStoreKernel.sol
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { IStoreRead } from "./IStoreRead.sol";
import { IStoreWrite } from "./IStoreWrite.sol";
import { IStoreErrors } from "./IStoreErrors.sol";
import { IFieldLayoutErrors } from "./IFieldLayoutErrors.sol";
import { IEncodedLengthsErrors } from "./IEncodedLengthsErrors.sol";
import { ISchemaErrors } from "./ISchemaErrors.sol";
import { ISliceErrors } from "./ISliceErrors.sol";

/**
* @title IStoreKernel
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz)
* @notice IStoreKernel includes the error interfaces for each library that it uses.
*/
interface IStoreKernel is
IStoreRead,
IStoreWrite,
IStoreErrors,
IFieldLayoutErrors,
IEncodedLengthsErrors,
ISchemaErrors,
ISliceErrors
{
/**
* @notice Returns the protocol version of the Store contract.
* @return version The protocol version of the Store contract.
*/
function storeVersion() external view returns (bytes32 version);
}
18 changes: 18 additions & 0 deletions packages/store/src/Store.sol
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;

import { STORE_VERSION } from "./version.sol";
import { IStore } from "./IStore.sol";
import { StoreKernel } from "./StoreKernel.sol";
import { StoreRead } from "./StoreRead.sol";
import { StoreCore } from "./StoreCore.sol";
import { IStoreEvents } from "./IStoreEvents.sol";

/**
* @title Store Contract
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz)
* @notice This contract integrates the core storage functionalities and provides an interface for data storage.
* @dev This abstract contract initializes `StoreCore`, implements `storeVersion`, and read methods,
* but not write methods.
*/
abstract contract Store is IStore, StoreKernel {}
Expand Up @@ -2,21 +2,21 @@
pragma solidity >=0.8.24;

import { STORE_VERSION } from "./version.sol";
import { IStoreData } from "./IStoreData.sol";
import { IStore } from "./IStore.sol";
import { StoreRead } from "./StoreRead.sol";
import { StoreCore } from "./StoreCore.sol";
import { IStoreEvents } from "./IStoreEvents.sol";
import { IStoreKernel } from "./IStoreKernel.sol";

/**
* @title Store Data Contract
* @title StoreKernel Contract
* @author MUD (https://mud.dev) by Lattice (https://lattice.xyz)
* @notice This contract integrates the core storage functionalities and provides an interface for data storage.
* @dev This abstract contract initializes `StoreCore`, implements `storeVersion`, and read methods,
* but not write methods.
* @notice This contract integrates the storage functionalities except registration and provides an interface for data storage.
* @dev This abstract contract initializes `StoreCore`, implements `storeVersion`, and read methods.
*/
abstract contract StoreData is IStoreData, StoreRead {
abstract contract StoreKernel is IStoreKernel, StoreRead {
/**
* @notice Constructs the StoreData contract and initializes the StoreCore.
* @notice Constructs the StoreKernel contract and initializes the StoreCore.
* @dev Emits a HelloStore event upon creation.
*/
constructor() {
Expand Down

0 comments on commit 93390d8

Please sign in to comment.