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

fix(cli): account for getRecord's trimming #574

Merged
merged 1 commit into from
Apr 2, 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
321 changes: 295 additions & 26 deletions packages/cli/contracts/src/tables/Dynamics.sol

Large diffs are not rendered by default.

144 changes: 133 additions & 11 deletions packages/cli/contracts/src/tables/Singleton.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,23 @@ library Singleton {
StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema());
}

/** Register the table's schema (using the specified store) */
function registerSchema(IStore _store) internal {
_store.registerSchema(_tableId, getSchema(), getKeySchema());
}

/** Set the table's metadata */
function setMetadata() internal {
(string memory _tableName, string[] memory _fieldNames) = getMetadata();
StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames);
}

/** Set the table's metadata (using the specified store) */
function setMetadata(IStore _store) internal {
(string memory _tableName, string[] memory _fieldNames) = getMetadata();
_store.setMetadata(_tableId, _tableName, _fieldNames);
}

/** Get v1 */
function getV1() internal view returns (int256 v1) {
bytes32[] memory _primaryKeys = new bytes32[](0);
Expand All @@ -67,13 +78,28 @@ library Singleton {
return (int256(uint256(Bytes.slice32(_blob, 0))));
}

/** Get v1 (using the specified store) */
function getV1(IStore _store) internal view returns (int256 v1) {
bytes32[] memory _primaryKeys = new bytes32[](0);

bytes memory _blob = _store.getField(_tableId, _primaryKeys, 0);
return (int256(uint256(Bytes.slice32(_blob, 0))));
}

/** Set v1 */
function setV1(int256 v1) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.setField(_tableId, _primaryKeys, 0, abi.encodePacked((v1)));
}

/** Set v1 (using the specified store) */
function setV1(IStore _store, int256 v1) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.setField(_tableId, _primaryKeys, 0, abi.encodePacked((v1)));
}

/** Get v2 */
function getV2() internal view returns (uint32[2] memory v2) {
bytes32[] memory _primaryKeys = new bytes32[](0);
Expand All @@ -82,20 +108,42 @@ library Singleton {
return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}

/** Get v2 (using the specified store) */
function getV2(IStore _store) internal view returns (uint32[2] memory v2) {
bytes32[] memory _primaryKeys = new bytes32[](0);

bytes memory _blob = _store.getField(_tableId, _primaryKeys, 1);
return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}

/** Set v2 */
function setV2(uint32[2] memory v2) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.setField(_tableId, _primaryKeys, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)));
}

/** Set v2 (using the specified store) */
function setV2(IStore _store, uint32[2] memory v2) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.setField(_tableId, _primaryKeys, 1, EncodeArray.encode(fromStaticArray_uint32_2(v2)));
}

/** Push an element to v2 */
function pushV2(uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.pushToField(_tableId, _primaryKeys, 1, abi.encodePacked((_element)));
}

/** Push an element to v2 (using the specified store) */
function pushV2(IStore _store, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.pushToField(_tableId, _primaryKeys, 1, abi.encodePacked((_element)));
}

/** Get v3 */
function getV3() internal view returns (uint32[2] memory v3) {
bytes32[] memory _primaryKeys = new bytes32[](0);
Expand All @@ -104,20 +152,42 @@ library Singleton {
return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}

/** Get v3 (using the specified store) */
function getV3(IStore _store) internal view returns (uint32[2] memory v3) {
bytes32[] memory _primaryKeys = new bytes32[](0);

bytes memory _blob = _store.getField(_tableId, _primaryKeys, 2);
return toStaticArray_uint32_2(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}

/** Set v3 */
function setV3(uint32[2] memory v3) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.setField(_tableId, _primaryKeys, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)));
}

/** Set v3 (using the specified store) */
function setV3(IStore _store, uint32[2] memory v3) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.setField(_tableId, _primaryKeys, 2, EncodeArray.encode(fromStaticArray_uint32_2(v3)));
}

/** Push an element to v3 */
function pushV3(uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.pushToField(_tableId, _primaryKeys, 2, abi.encodePacked((_element)));
}

/** Push an element to v3 (using the specified store) */
function pushV3(IStore _store, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.pushToField(_tableId, _primaryKeys, 2, abi.encodePacked((_element)));
}

/** Get v4 */
function getV4() internal view returns (uint32[1] memory v4) {
bytes32[] memory _primaryKeys = new bytes32[](0);
Expand All @@ -126,20 +196,42 @@ library Singleton {
return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}

/** Get v4 (using the specified store) */
function getV4(IStore _store) internal view returns (uint32[1] memory v4) {
bytes32[] memory _primaryKeys = new bytes32[](0);

bytes memory _blob = _store.getField(_tableId, _primaryKeys, 3);
return toStaticArray_uint32_1(SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}

/** Set v4 */
function setV4(uint32[1] memory v4) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.setField(_tableId, _primaryKeys, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)));
}

/** Set v4 (using the specified store) */
function setV4(IStore _store, uint32[1] memory v4) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.setField(_tableId, _primaryKeys, 3, EncodeArray.encode(fromStaticArray_uint32_1(v4)));
}

/** Push an element to v4 */
function pushV4(uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

StoreSwitch.pushToField(_tableId, _primaryKeys, 3, abi.encodePacked((_element)));
}

/** Push an element to v4 (using the specified store) */
function pushV4(IStore _store, uint32 _element) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.pushToField(_tableId, _primaryKeys, 3, abi.encodePacked((_element)));
}

/** Get the full data */
function get() internal view returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) {
bytes32[] memory _primaryKeys = new bytes32[](0);
Expand All @@ -148,6 +240,16 @@ library Singleton {
return decode(_blob);
}

/** Get the full data (using the specified store) */
function get(
IStore _store
) internal view returns (int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) {
bytes32[] memory _primaryKeys = new bytes32[](0);

bytes memory _blob = _store.getRecord(_tableId, _primaryKeys, getSchema());
return decode(_blob);
}

/** Set the full data using individual values */
function set(int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) internal {
bytes memory _data = encode(v1, v2, v3, v4);
Expand All @@ -157,6 +259,15 @@ library Singleton {
StoreSwitch.setRecord(_tableId, _primaryKeys, _data);
}

/** Set the full data using individual values (using the specified store) */
function set(IStore _store, int256 v1, uint32[2] memory v2, uint32[2] memory v3, uint32[1] memory v4) internal {
bytes memory _data = encode(v1, v2, v3, v4);

bytes32[] memory _primaryKeys = new bytes32[](0);

_store.setRecord(_tableId, _primaryKeys, _data);
}

/** Decode the tightly packed blob using this table's schema */
function decode(
bytes memory _blob
Expand All @@ -166,20 +277,24 @@ library Singleton {

v1 = (int256(uint256(Bytes.slice32(_blob, 0))));

uint256 _start;
uint256 _end = 64;
// Store trims the blob if dynamic fields are all empty
if (_blob.length > 32) {
uint256 _start;
// skip static data length + dynamic lengths word
uint256 _end = 64;

_start = _end;
_end += _encodedLengths.atIndex(0);
v2 = toStaticArray_uint32_2(SliceLib.getSubslice(_blob, _start, _end).decodeArray_uint32());
_start = _end;
_end += _encodedLengths.atIndex(0);
v2 = toStaticArray_uint32_2(SliceLib.getSubslice(_blob, _start, _end).decodeArray_uint32());

_start = _end;
_end += _encodedLengths.atIndex(1);
v3 = toStaticArray_uint32_2(SliceLib.getSubslice(_blob, _start, _end).decodeArray_uint32());
_start = _end;
_end += _encodedLengths.atIndex(1);
v3 = toStaticArray_uint32_2(SliceLib.getSubslice(_blob, _start, _end).decodeArray_uint32());

_start = _end;
_end += _encodedLengths.atIndex(2);
v4 = toStaticArray_uint32_1(SliceLib.getSubslice(_blob, _start, _end).decodeArray_uint32());
_start = _end;
_end += _encodedLengths.atIndex(2);
v4 = toStaticArray_uint32_1(SliceLib.getSubslice(_blob, _start, _end).decodeArray_uint32());
}
}

/** Tightly pack full data using this table's schema */
Expand Down Expand Up @@ -211,6 +326,13 @@ library Singleton {

StoreSwitch.deleteRecord(_tableId, _primaryKeys);
}

/* Delete all data for given keys (using the specified store) */
function deleteRecord(IStore _store) internal {
bytes32[] memory _primaryKeys = new bytes32[](0);

_store.deleteRecord(_tableId, _primaryKeys);
}
}

function toStaticArray_uint32_2(uint32[] memory _value) pure returns (uint32[2] memory _result) {
Expand Down
Loading