Skip to content

Commit 3c39d69

Browse files
committed
path for compressed records
1 parent bb1a2d8 commit 3c39d69

File tree

7 files changed

+185
-390
lines changed

7 files changed

+185
-390
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"p-retry": "^5.1.2",
7070
"path": "^0.12.7",
7171
"rxjs": "7.5.5",
72+
"solady": "^0.1.26",
7273
"throttle-debounce": "^5.0.0",
7374
"toposort": "^2.0.2",
7475
"viem": "2.35.1",

packages/cli/src/mirror/createRecordHandler.ts

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { readContract, waitForTransactionReceipt } from "viem/actions";
1717
import chalk from "chalk";
1818
import { StoreRecord } from "./executeMirrorPlan";
1919
import { debug } from "./debug";
20+
import { LibZip } from "solady";
2021

2122
export function createRecordHandler({
2223
client,
@@ -127,8 +128,8 @@ Estimated L2 cost at 100k wei: ${parseFloat(formatEther(BigInt(estimatedGas) * 1
127128
});
128129

129130
const changedRecords = records.filter((record, i) => {
130-
const recordEncoded = encodeAbiParameters(tableRecordAbi, [record]);
131-
const existingRecordEncoded = encodeAbiParameters(tableRecordAbi, [existingRecords[i]]);
131+
const recordEncoded = encodeAbiParameters([tableRecordAbiItem], [record]);
132+
const existingRecordEncoded = encodeAbiParameters([tableRecordAbiItem], [existingRecords[i]]);
132133
if (recordEncoded === existingRecordEncoded) return false;
133134
// console.log("record changed in", record.tableId);
134135
// console.log(" ", recordEncoded);
@@ -139,12 +140,24 @@ Estimated L2 cost at 100k wei: ${parseFloat(formatEther(BigInt(estimatedGas) * 1
139140
return;
140141
}
141142

143+
const calldata = encodeAbiParameters(
144+
[{ type: "bytes32" }, tableRecordsAbiItem],
145+
[changedRecords[0].tableId, changedRecords],
146+
);
142147
const args = encodeSystemCall({
143148
systemId: batchStoreConfig.systems.BatchStoreSystem.systemId,
144149
abi: batchStoreSystemAbi,
145-
functionName: "_setTableRecords",
146-
args: [changedRecords[0].tableId, changedRecords],
150+
functionName: "_setTableRecords_flz",
151+
args: [LibZip.flzCompress(calldata) as Hex],
147152
});
153+
// console.log(
154+
// "flz compression",
155+
// size(calldata),
156+
// "=>",
157+
// size(LibZip.flzCompress(calldata) as Hex),
158+
// " = ",
159+
// size(LibZip.flzCompress(calldata) as Hex) / size(calldata),
160+
// );
148161
const hash = await writeContract(client, {
149162
chain: client.chain ?? null,
150163
address: worldAddress,
@@ -159,35 +172,59 @@ Estimated L2 cost at 100k wei: ${parseFloat(formatEther(BigInt(estimatedGas) * 1
159172
return { set, finalize };
160173
}
161174

162-
const tableRecordAbi = [
163-
{
164-
name: "record",
165-
type: "tuple",
166-
internalType: "struct TableRecord",
167-
components: [
168-
{
169-
name: "keyTuple",
170-
type: "bytes32[]",
171-
internalType: "bytes32[]",
172-
},
173-
{
174-
name: "staticData",
175-
type: "bytes",
176-
internalType: "bytes",
177-
},
178-
{
179-
name: "encodedLengths",
180-
type: "bytes32",
181-
internalType: "EncodedLengths",
182-
},
183-
{
184-
name: "dynamicData",
185-
type: "bytes",
186-
internalType: "bytes",
187-
},
188-
],
189-
},
190-
] as const;
175+
const tableRecordAbiItem = {
176+
type: "tuple",
177+
internalType: "struct TableRecord",
178+
components: [
179+
{
180+
name: "keyTuple",
181+
type: "bytes32[]",
182+
internalType: "bytes32[]",
183+
},
184+
{
185+
name: "staticData",
186+
type: "bytes",
187+
internalType: "bytes",
188+
},
189+
{
190+
name: "encodedLengths",
191+
type: "bytes32",
192+
internalType: "EncodedLengths",
193+
},
194+
{
195+
name: "dynamicData",
196+
type: "bytes",
197+
internalType: "bytes",
198+
},
199+
],
200+
} as const;
201+
202+
const tableRecordsAbiItem = {
203+
type: "tuple[]",
204+
internalType: "struct TableRecord[]",
205+
components: [
206+
{
207+
name: "keyTuple",
208+
type: "bytes32[]",
209+
internalType: "bytes32[]",
210+
},
211+
{
212+
name: "staticData",
213+
type: "bytes",
214+
internalType: "bytes",
215+
},
216+
{
217+
name: "encodedLengths",
218+
type: "bytes32",
219+
internalType: "EncodedLengths",
220+
},
221+
{
222+
name: "dynamicData",
223+
type: "bytes",
224+
internalType: "bytes",
225+
},
226+
],
227+
};
191228

192229
const batchStoreSystemAbi = [
193230
{
@@ -248,6 +285,19 @@ const batchStoreSystemAbi = [
248285
outputs: [],
249286
stateMutability: "nonpayable",
250287
},
288+
{
289+
type: "function",
290+
name: "_setTableRecords_flz",
291+
inputs: [
292+
{
293+
name: "data",
294+
type: "bytes",
295+
internalType: "bytes",
296+
},
297+
],
298+
outputs: [],
299+
stateMutability: "nonpayable",
300+
},
251301
{
252302
type: "function",
253303
name: "deleteTableRecords",

packages/world-module-batchstore/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
"dependencies": {
4545
"@latticexyz/schema-type": "workspace:*",
4646
"@latticexyz/store": "workspace:*",
47-
"@latticexyz/world": "workspace:*"
47+
"@latticexyz/world": "workspace:*",
48+
"solady": "^0.1.26"
4849
},
4950
"devDependencies": {
5051
"@latticexyz/abi-ts": "workspace:*",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
forge-std/=node_modules/forge-std/src/
22
@latticexyz/=node_modules/@latticexyz/
3+
solady/=node_modules/solady/src/

packages/world-module-batchstore/src/BatchStoreSystem.sol

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
99
import { EncodedLengths } from "@latticexyz/store/src/EncodedLengths.sol";
1010
import { FieldLayout } from "@latticexyz/store/src/FieldLayout.sol";
1111
import { TableRecord } from "./common.sol";
12+
import { LibZip } from "solady/utils/LibZip.sol";
13+
import { revertWithBytes } from "@latticexyz/world/src/revertWithBytes.sol";
14+
import { IBaseWorld } from "@latticexyz/world/src/codegen/interfaces/IBaseWorld.sol";
1215

1316
contract BatchStoreSystem is System {
1417
function getTableRecords(
@@ -61,7 +64,7 @@ contract BatchStoreSystem is System {
6164
}
6265
}
6366

64-
function _setTableRecords(ResourceId tableId, TableRecord[] memory records) external {
67+
function _setTableRecords(ResourceId tableId, TableRecord[] memory records) public {
6568
AccessControl.requireOwner(ROOT_NAMESPACE_ID, _msgSender());
6669

6770
FieldLayout fieldLayout = StoreCore.getFieldLayout(tableId);
@@ -87,4 +90,12 @@ contract BatchStoreSystem is System {
8790
StoreCore.deleteRecord(tableId, keyTuples[i], fieldLayout);
8891
}
8992
}
93+
94+
function _setTableRecords_flz(bytes calldata data) external {
95+
(ResourceId tableId, TableRecord[] memory records) = abi.decode(
96+
LibZip.flzDecompress(data),
97+
(ResourceId, TableRecord[])
98+
);
99+
_setTableRecords(tableId, records);
100+
}
90101
}

packages/world-module-batchstore/src/codegen/experimental/systems/BatchStoreSystemLib.sol

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)