All files / kernel-test/src/vats resume-vat.ts

0% Statements 0/58
0% Branches 0/10
0% Functions 0/8
0% Lines 0/57

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                                                                                                                                                                                                                                                     
/* global harden */
import { E } from '@endo/eventual-send';
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import type { Baggage } from '@metamask/ocap-kernel';
 
import { unwrapTestLogger } from '../test-powers.ts';
import type { TestPowers } from '../test-powers.ts';
 
/**
 * Build function for generic test vat.
 *
 * @param vatPowers - Special powers granted to this vat.
 * @param vatPowers.logger - The logger for the vat.
 * @param parameters - Initialization parameters from the vat's config object.
 * @param parameters.name - The name of the vat.
 * @param baggage - Root of vat's persistent state.
 * @returns The root object for the new vat.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function buildRootObject(
  vatPowers: TestPowers,
  parameters: { name?: string },
  baggage: Baggage,
) {
  const name = parameters?.name ?? 'anonymous';
  const logger = unwrapTestLogger(vatPowers, name);
  const tlog = (message: string): void => logger(`${name}: ${message}`);
 
  /**
   * Print a message to the log.
   *
   * @param message - The message to print.
   */
  function log(message: string): void {
    // eslint-disable-next-line no-console
    console.log(`${name}: ${message}`);
  }
 
  log(`buildRootObject`);
 
  let startCount: number;
  if (baggage.has('name')) {
    const savedName = baggage.get('name') as string;
    tlog(`saved name is ${savedName}`);
 
    startCount = (baggage.get('startCount') as number) + 1;
    baggage.set('startCount', startCount);
  } else {
    baggage.init('name', name);
    tlog(`saving name`);
 
    baggage.init('startCount', 1);
    startCount = 1;
  }
  tlog(`start count: ${startCount}`);
 
  const me = makeDefaultExo('root', {
    async bootstrap(vats: { bob: unknown; carol: unknown }) {
      tlog(`bootstrap()`);
      // Explanation for the following bit of gymnastics: we'd like to save
      // `vats` itself in the baggage, but we can't because the entry for our
      // own root is a local reference and thus not durable, and we can't remove
      // this entry from `vats` directly because, being a parameter object, it
      // arrived hardened.  So instead we have to copy it sans the unwritable element.
      const writeVats: Record<string, unknown> = {};
      for (const [prop, value] of Object.entries(vats)) {
        if (value !== me) {
          writeVats[prop] = value;
        }
      }
      baggage.init('vats', harden(writeVats));
 
      const pIntroB = E(vats.bob).intro(me);
      const pIntroC = E(vats.carol).intro(me);
      const pGreetB = E(vats.bob).greet(`hello from ${name}`);
      const pGreetC = E(vats.carol).greet(`hello from ${name}`);
      const results = await Promise.all([pIntroB, pIntroC, pGreetB, pGreetC]);
      const [, , greetB, greetC] = results;
      tlog(`Bob answers greeting: '${greetB}'`);
      tlog(`Carol answers greeting: '${greetC}'`);
      tlog(`end bootstrap`);
      await E(vats.bob).loopback();
      return `bootstrap ${name}`;
    },
    intro(bootVat: unknown) {
      tlog(`intro()`);
      baggage.init('bootVat', bootVat);
    },
    greet(greeting: string) {
      tlog(`greet('${greeting}')`);
      return `${name} returns your greeting '${greeting}'`;
    },
    async resume() {
      tlog(`resume()`);
      if (baggage.has('vats')) {
        // I am the bootstrap vat
        tlog(`resumed vat is bootstrap`);
        const vats = baggage.get('vats') as { bob: unknown; carol: unknown };
        const pGreetB = E(vats.bob).greet(`hello again from ${name}`);
        const pGreetC = E(vats.carol).greet(`hello again from ${name}`);
        const [greetB, greetC] = await Promise.all([pGreetB, pGreetC]);
        tlog(`Bob answers greeting: '${greetB}'`);
        tlog(`Carol answers greeting: '${greetC}'`);
        await E(vats.bob).loopback();
      }
      if (baggage.has('bootVat')) {
        // I am Bob or Carol
        tlog(`resumed vat is not bootstrap`);
        const bootVat = baggage.get('bootVat');
        const greetBack = await E(bootVat).greet(`hello boot vat from ${name}`);
        tlog(`boot vat returns greeting with '${greetBack}'`);
        await E(bootVat).loopback();
      }
      tlog(`end resume`);
      return `resume ${name}`;
    },
    loopback() {
      return undefined;
    },
  });
  return me;
}