Skip to content

Commit

Permalink
chore(swingset): add kernelKeeper.getImporters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
warner committed Jun 1, 2021
1 parent a9bde7b commit e9a7a4e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/SwingSet/src/kernel/state/kernelKeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down Expand Up @@ -1013,6 +1025,7 @@ export default function makeKernelKeeper(kvStore, streamStore, kernelSlog) {
addKernelObject,
ownerOfKernelObject,
ownerOfKernelDevice,
getImporters,
deleteKernelObject,

addKernelPromise,
Expand Down
14 changes: 14 additions & 0 deletions packages/SwingSet/src/kernel/state/vatKeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -422,6 +435,7 @@ export function makeVatKeeper(
return harden({
setSourceAndOptions,
getSourceAndOptions,
importsKernelSlot,
mapVatSlotToKernelSlot,
mapKernelSlotToVatSlot,
getReachableFlag,
Expand Down

0 comments on commit e9a7a4e

Please sign in to comment.