Skip to content

Commit

Permalink
fix(swingset): initializeStoreKindInfo() earlier, unconditionally
Browse files Browse the repository at this point in the history
The liveslots "collection manager" assigns a virtual-object Kind ID to
each of the collection types it supports. Currently there are 8: (weak
vs strong) times (map vs set) times (virtual vs durable). These Kind
IDs live in the same numberspace as the virtual/durable Kinds that
userspace creates by calling `defineKind` or `defineDurableKind`. This
numberspace is also used for Export IDs, the `o+NN` values assigned to
exported Remotables.

Previously, the Kind IDs for the collections were allocated on-demand,
the first time anybody created a collection. The IDs they got could
vary, depend upon whether userspace created a collection first, or
used `defineKind` first. This also changed when we introduced
"baggage" (which is a durable collection, created by liveslots
itself).

This commit moves the allocation step: it is now done unconditionally
during startVat(), before userspace gets control. This should make the
allocated IDs more consistent across all vats.

This should behave correctly when we add new kinds of Stores in the
future: when the vat is upgraded to a new liveslots, it will read the
existing IDs from the DB, add any new ones, then write the updated
table back to the DB.

As a side-effect of this change, *all* vats now have kvStore keys to
record the IDs, not just the once which use collections. And the first
Export ID allocated after startup (either an object export or a Kind
definition) will get a different ID: instead of `o+1`, it is likely to
be `o+9`. Several unit tests needed to be updated as a result, however
now they won't have to be updated further if e.g. `startVat` changes
to create collections before userspace runs.
  • Loading branch information
warner committed Mar 28, 2022
1 parent d9d09f1 commit 28e500b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 162 deletions.
51 changes: 25 additions & 26 deletions packages/SwingSet/src/liveslots/collectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export function makeCollectionManager(
) {
const storeKindIDToName = new Map();

let storeKindInfoNeedsInitialization = true;
const storeKindInfo = {
scalarMapStore: {
hasWeakKeys: false,
Expand Down Expand Up @@ -95,33 +94,32 @@ export function makeCollectionManager(
return `vc.${collectionID}.${dbEntryKey}`;
}

function obtainStoreKindID(kindName) {
if (storeKindInfoNeedsInitialization) {
storeKindInfoNeedsInitialization = false;

let storeKindIDs = {};
const rawTable = syscall.vatstoreGet('storeKindIDTable');
if (rawTable) {
storeKindIDs = JSON.parse(rawTable);
}
for (const kind of Object.getOwnPropertyNames(storeKindInfo)) {
let kindID = storeKindIDs[kind];
if (!kindID) {
kindID = allocateExportID();
storeKindIDs[kind] = kindID;
}
storeKindInfo[kind].kindID = kindID;
storeKindIDToName.set(`${kindID}`, kind);
vrm.registerKind(
kindID,
storeKindInfo[kind].reanimator,
// eslint-disable-next-line no-use-before-define
deleteCollection,
storeKindInfo[kind].durable,
);
function initializeStoreKindInfo() {
let storeKindIDs = {};
const rawTable = syscall.vatstoreGet('storeKindIDTable');
if (rawTable) {
storeKindIDs = JSON.parse(rawTable);
}
for (const kind of Object.getOwnPropertyNames(storeKindInfo)) {
let kindID = storeKindIDs[kind];
if (!kindID) {
kindID = allocateExportID();
storeKindIDs[kind] = kindID;
}
syscall.vatstoreSet('storeKindIDTable', JSON.stringify(storeKindIDs));
storeKindInfo[kind].kindID = kindID;
storeKindIDToName.set(`${kindID}`, kind);
vrm.registerKind(
kindID,
storeKindInfo[kind].reanimator,
// eslint-disable-next-line no-use-before-define
deleteCollection,
storeKindInfo[kind].durable,
);
}
syscall.vatstoreSet('storeKindIDTable', JSON.stringify(storeKindIDs));
}

function obtainStoreKindID(kindName) {
return storeKindInfo[kindName].kindID;
}

Expand Down Expand Up @@ -809,6 +807,7 @@ export function makeCollectionManager(
const testHooks = { storeSizeInternal, makeCollection };

return harden({
initializeStoreKindInfo,
makeScalarBigMapStore,
makeScalarBigWeakMapStore,
makeScalarBigSetStore,
Expand Down
2 changes: 2 additions & 0 deletions packages/SwingSet/src/liveslots/liveslots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,8 @@ function build(
}

initializeIDCounters();
collectionManager.initializeStoreKindInfo();

const vatParameters = m.unserialize(vatParametersCapData);
baggage = collectionManager.provideBaggage();

Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/stores/test-storeGC.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ function validateCreateHolder(v, idx) {

function validateInit(v) {
validate(v, matchVatstoreGet('idCounters', NONE));
validate(v, matchVatstoreGet('baggageID', NONE));
validate(v, matchVatstoreGet('storeKindIDTable', NONE));
validate(
v,
Expand All @@ -475,6 +474,7 @@ function validateInit(v) {
'{"scalarMapStore":1,"scalarWeakMapStore":2,"scalarSetStore":3,"scalarWeakSetStore":4,"scalarDurableMapStore":5,"scalarDurableWeakMapStore":6,"scalarDurableSetStore":7,"scalarDurableWeakSetStore":8}',
),
);
validate(v, matchVatstoreGet('baggageID', NONE));
validateCreateBaggage(v, 1);
validateCreateHolder(v, 2);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-baggage.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function validateCreateBaggage(v, idx) {

function validateSetup(v) {
validate(v, matchVatstoreGet('idCounters', NONE));
validate(v, matchVatstoreGet('baggageID', NONE));
validate(v, matchVatstoreGet('storeKindIDTable', NONE));
validate(
v,
Expand All @@ -56,6 +55,7 @@ function validateSetup(v) {
'{"scalarMapStore":1,"scalarWeakMapStore":2,"scalarSetStore":3,"scalarWeakSetStore":4,"scalarDurableMapStore":5,"scalarDurableWeakMapStore":6,"scalarDurableSetStore":7,"scalarDurableWeakSetStore":8}',
),
);
validate(v, matchVatstoreGet('baggageID', NONE));
validateCreateBaggage(v, 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,6 @@ function validateCreateBaggage(v, idx) {

function validateSetup(v) {
validate(v, matchVatstoreGet('idCounters', NONE));
validate(v, matchVatstoreGet('baggageID', NONE));
validate(v, matchVatstoreGet('storeKindIDTable', NONE));
validate(
v,
Expand All @@ -470,6 +469,7 @@ function validateSetup(v) {
'{"scalarMapStore":1,"scalarWeakMapStore":2,"scalarSetStore":3,"scalarWeakSetStore":4,"scalarDurableMapStore":5,"scalarDurableWeakMapStore":6,"scalarDurableSetStore":7,"scalarDurableWeakSetStore":8}',
),
);
validate(v, matchVatstoreGet('baggageID', NONE));
validateCreateBaggage(v, 1);
validate(v, matchVatstoreSet(stateKey(cacheDisplacerVref), cacheObjValue));
validate(v, matchVatstoreSet(stateKey(fCacheDisplacerVref), cacheObjValue));
Expand Down
Loading

0 comments on commit 28e500b

Please sign in to comment.