From e9a7a4ed404cc66c31c75a11552c39b9dcd53bac Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Mon, 31 May 2021 21:26:20 -0700 Subject: [PATCH] chore(swingset): add kernelKeeper.getImporters This function takes an object kref and returns a list of vatIDs which have imported that object. As mentioned in #3223, this would be more efficient with an index. --- packages/SwingSet/src/kernel/state/kernelKeeper.js | 13 +++++++++++++ packages/SwingSet/src/kernel/state/vatKeeper.js | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/SwingSet/src/kernel/state/kernelKeeper.js b/packages/SwingSet/src/kernel/state/kernelKeeper.js index a25ee63f1da9..ddf82e13570b 100644 --- a/packages/SwingSet/src/kernel/state/kernelKeeper.js +++ b/packages/SwingSet/src/kernel/state/kernelKeeper.js @@ -303,6 +303,18 @@ export default function makeKernelKeeper(kvStore, streamStore, kernelSlog) { return owner; } + function getImporters(koid) { + // TODO maintain an index instead of scanning every single vat + const importers = []; + function doesImport(vatID) { + return getVatKeeper(vatID).importsKernelSlot(koid); + } + importers.push(...getDynamicVats().filter(doesImport)); + importers.push(...getStaticVats().map(nvID => nvID[1]).filter(doesImport)); + importers.sort(); + return importers; + } + function deleteKernelObject(koid) { kvStore.delete(`${koid}.owner`); kvStore.delete(`${koid}.refCount`); @@ -1013,6 +1025,7 @@ export default function makeKernelKeeper(kvStore, streamStore, kernelSlog) { addKernelObject, ownerOfKernelObject, ownerOfKernelDevice, + getImporters, deleteKernelObject, addKernelPromise, diff --git a/packages/SwingSet/src/kernel/state/vatKeeper.js b/packages/SwingSet/src/kernel/state/vatKeeper.js index 6b0b0b19f335..544f2f0f6b83 100644 --- a/packages/SwingSet/src/kernel/state/vatKeeper.js +++ b/packages/SwingSet/src/kernel/state/vatKeeper.js @@ -145,6 +145,19 @@ export function makeVatKeeper( } } + function importsKernelSlot(kernelSlot) { + const kernelKey = `${vatID}.c.${kernelSlot}`; + const data = kvStore.get(kernelKey); + if (data) { + const { vatSlot } = parseReachableAndVatSlot(data); + const { allocatedByVat } = parseVatSlot(vatSlot); + if (!allocatedByVat) { + return true; + } + } + return false; + } + /** * Provide the kernel slot corresponding to a given vat slot, allocating a * new one (for exports only) if it doesn't already exist. If we're allowed @@ -422,6 +435,7 @@ export function makeVatKeeper( return harden({ setSourceAndOptions, getSourceAndOptions, + importsKernelSlot, mapVatSlotToKernelSlot, mapKernelSlotToVatSlot, getReachableFlag,