Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | 1x 1x 17x 17x 17x 27x 27x 35x 35x 35x 2x 33x 1x 32x 32x 32x 32x 32x 104x 104x 104x 22x 22x 22x 82x 82x 104x 80x 80x 77x 104x 27x 27x 4x 4x 8x 8x 4x 4x 4x 8x 23x 17x 17x 17x 107x 2x 105x 1x 104x 104x 104x 104x 10x 10x 104x 104x 104x 22x 22x 22x 11x 22x 82x 82x 82x 17x 17x 32x 32x 30x 105x 2x 17x 17x 17x | import { ContractSchema, ContractStorageField } from "../contractSchema";
import { Generator, ind } from "../generator";
export class PackFuncGen implements Generator {
gen(schema: ContractSchema): string {
const packMetadataFunction = generatePackMetadataFunction(schema.storage.fields);
function generatePackMetadataFunction(entries: ContractStorageField[]) {
let slots = [];
for (let slotIdx = 0; slotIdx < schema.storage.slots.length; slotIdx++) {
let slotEntries: string[] = [];
for (let fieldIdx = 0; fieldIdx < schema.storage.slots[slotIdx].fieldIDs.length; fieldIdx++) {
let fieldID = schema.storage.slots[slotIdx].fieldIDs[fieldIdx];
let field = schema.getField(fieldID);
if (field.arrayLength === 0) {
continue;
}
if (field.totalBits === 0) {
continue;
}
let arrayElementsPerSlot = 256 / field.elementBits;
let arrayIdxStartInSlot = field.arrayLength == 1 ? 0 : (slotIdx - field.slot) * arrayElementsPerSlot;
let arrayIdxEndInSlot = field.arrayLength == 1 ? 1 : arrayIdxStartInSlot + arrayElementsPerSlot;
let offsetInSlot = field.slot == slotIdx ? field.offset : 0;
for (let arrayIdx = arrayIdxStartInSlot; arrayIdx < arrayIdxEndInSlot; arrayIdx++) {
// the starting offset in this slot - if not the starting slot for the field then 0
let arrayIdxStr = field.arrayLength > 1 ? `[${arrayIdx}]` : "";
let conversion = `data.${field.key}`;
if (field.isString) {
const stringOffsetBits = 256 - field.elementBits;
const stringOffset = stringOffsetBits > 0 ? ` >> ${stringOffsetBits}` : "";
conversion = `PatchworkUtils.strToUint256(${conversion}${arrayIdxStr})${stringOffset}`;
} else Iif (field.fieldTypeSolidityEnum == `ADDRESS`) {
conversion = `uint256(uint160(${conversion}${arrayIdxStr}))`;
} else {
conversion = `uint256(${conversion}${arrayIdxStr})`;
}
if (offsetInSlot > 0 || arrayIdx > 0) {
let offset = field.offset + field.elementBits * (arrayIdx % arrayElementsPerSlot);
if (offset !== 0) {
conversion += ` << ${offset}`;
}
}
slotEntries.push(conversion);
}
}
// Will run out of stack, max 8
const maxArrayPerSlot = 8;
if (slotEntries.length > maxArrayPerSlot) {
let index = 0;
while (index < slotEntries.length) {
const group = slotEntries.slice(index, index + maxArrayPerSlot);
if (index === 0) {
slots.push(`uint256 slot${slotIdx} = ` + group.join(" | ") + ';');
} else Iif (index + maxArrayPerSlot < slotEntries.length) {
slots.push(`slot${slotIdx} = slot${slotIdx} | ` + group.join(" | ") + ';');
} else {
slots.push(`slots[${slotIdx}] = slot${slotIdx} | ` + group.join(" | ") + ';');
}
index += maxArrayPerSlot;
}
} else {
slots.push(`slots[${slotIdx}] = ` + slotEntries.join(" | ") + ';');
}
}
return `` +
`function packMetadata(${schema.getMetadataStructName()} memory data) public pure returns (uint256[] memory slots) {\n` +
ind(4, `slots = new uint256[](${schema.storage.slots.length});\n`) +
ind(4, `${slots.join("\n")}\n`) +
ind(4, `return slots;\n`) +
`}\n`;
}
function generateUnpackMetadataFunction(entries: ContractStorageField[]) {
let slotIndex = 0;
let unpackLines: string[] = [];
function unpackField(field: ContractStorageField, arrayIdx: number) {
if (field.arrayLength === 0) {
return;
}
if (field.totalBits === 0) {
return;
}
const naiveOffset = field.offset + (field.elementBits * arrayIdx);
const slot = field.slot + Math.floor((field.offset + ((field.elementBits) * arrayIdx)) / 256);
const offset = naiveOffset - ((slot - field.slot) * 256);
if (slot > slotIndex) {
slotIndex = slot;
unpackLines.push(`slot = slots[${slotIndex}];`);
}
let arrayIdxStr = field.arrayLength > 1 ? `[${arrayIdx}]` : "";
let shift = offset === 0 ? `` : ` >> ${offset}`;
if (field.isString) {
let strBytes = field.elementBits / 8;
let slotExpr = `slot${shift}`;
if (field.elementBits != 256) {
slotExpr = `uint${field.elementBits}(${slotExpr})`;
}
unpackLines.push(`data.${field.key}${arrayIdxStr} = PatchworkUtils.toString${strBytes}(${slotExpr});`);
} else Iif (field.fieldTypeSolidityEnum == `ADDRESS`) {
unpackLines.push(`data.${field.key}${arrayIdxStr} = address(uint160(slot${shift}));`);
} else {
let unpackedValue = `${field.solidityType}(slot${shift})`;
unpackLines.push(`data.${field.key}${arrayIdxStr} = ${unpackedValue};`);
}
}
unpackLines.push(`uint256 slot = slots[0];`);
for (let i = 0; i < entries.length; i++) {
let entry = entries[i];
if (entry.arrayLength > 0) {
for (let j = 0; j < entry.arrayLength; j++) {
unpackField(entry, j);
}
} else {
unpackField(entry, 0);
}
}
return `` +
`function unpackMetadata(uint256[] memory slots) public pure returns (${schema.getMetadataStructName()} memory data) {\n` +
` ${unpackLines.join("\n ")}\n` +
` return data;\n` +
`}\n`;
}
const unpackMetadataFunction = generateUnpackMetadataFunction(schema.storage.fields);
return ind(4, `` +
`${packMetadataFunction}\n` +
`${unpackMetadataFunction}\n`);
}
} |