-
Notifications
You must be signed in to change notification settings - Fork 215
/
boot.js
156 lines (141 loc) · 4.2 KB
/
boot.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// @ts-check
import { E, Far } from '@endo/far';
import * as simBehaviors from '@agoric/inter-protocol/src/proposals/sim-behaviors.js';
import {
makeAgoricNamesAccess,
makePromiseSpace,
runModuleBehaviors,
} from './utils.js';
import {
CLIENT_BOOTSTRAP_MANIFEST,
CHAIN_BOOTSTRAP_MANIFEST,
SIM_CHAIN_BOOTSTRAP_MANIFEST,
} from './manifest.js';
import * as behaviors from './behaviors.js';
import * as clientBehaviors from './client-behaviors.js';
import * as utils from './utils.js';
const { details: X, quote: q } = assert;
// Choose a manifest based on runtime configured argv.ROLE.
const roleToManifest = harden({
chain: CHAIN_BOOTSTRAP_MANIFEST,
'sim-chain': SIM_CHAIN_BOOTSTRAP_MANIFEST,
client: CLIENT_BOOTSTRAP_MANIFEST,
});
const roleToBehaviors = harden({
'sim-chain': { ...behaviors, ...simBehaviors },
// copy to avoid trying to harden a module namespace
client: { ...clientBehaviors },
});
/**
* Build root object of the bootstrap vat.
*
* @param {{
* D: DProxy,
* logger: (msg) => void,
* }} vatPowers
* @param {{
* argv: { ROLE: string },
* bootstrapManifest?: Record<string, Record<string, unknown>>,
* coreProposalCode?: string,
* }} vatParameters
*/
const buildRootObject = (vatPowers, vatParameters) => {
const log = vatPowers.logger || console.info;
const { produce, consume } = makePromiseSpace(log);
const { agoricNames, agoricNamesAdmin, spaces } = makeAgoricNamesAccess(log);
produce.agoricNames.resolve(agoricNames);
produce.agoricNamesAdmin.resolve(agoricNamesAdmin);
const {
argv: { ROLE },
bootstrapManifest,
} = vatParameters;
console.debug(`${ROLE} bootstrap starting`);
const bootManifest = bootstrapManifest || roleToManifest[ROLE];
const bootBehaviors = roleToBehaviors[ROLE] || behaviors;
assert(bootManifest, X`no configured bootstrapManifest for role ${ROLE}`);
assert(bootBehaviors, X`no configured bootstrapBehaviors for role ${ROLE}`);
/**
* Bootstrap vats and devices.
*
* @param {SwingsetVats} vats
* @param {SoloDevices | ChainDevices} devices
*/
const rawBootstrap = async (vats, devices) => {
// Complete SwingSet wiring.
const { D } = vatPowers;
D(devices.mailbox).registerInboundHandler(vats.vattp);
await E(vats.vattp).registerMailboxDevice(devices.mailbox);
const runBehaviors = manifest => {
return runModuleBehaviors({
// eslint-disable-next-line no-use-before-define
allPowers,
behaviors: bootBehaviors,
manifest,
makeConfig: (name, permit) => {
log(`bootstrap: ${name}(${q(permit)}`);
return vatParameters[name];
},
});
};
// TODO: Aspires to be BootstrapPowers, but it's too specific.
const allPowers = harden({
vatPowers,
vatParameters,
vats,
devices,
produce,
consume,
...spaces,
runBehaviors,
// These module namespaces might be useful for core eval governance.
modules: {
clientBehaviors: { ...clientBehaviors },
simBehaviors: { ...simBehaviors },
behaviors: { ...behaviors },
utils: { ...utils },
},
});
await runBehaviors(bootManifest);
const { coreProposalCode } = vatParameters;
if (!coreProposalCode) {
return;
}
// Start the governance from the core proposals.
const coreEvalMessage = {
type: 'CORE_EVAL',
evals: [
{
json_permits: 'true',
js_code: coreProposalCode,
},
],
};
/** @type {any} */
const { coreEvalBridgeHandler } = consume;
await E(coreEvalBridgeHandler).fromBridge(
'arbitrary srcID',
coreEvalMessage,
);
};
return Far('bootstrap', {
bootstrap: (vats, devices) =>
rawBootstrap(vats, devices).catch(e => {
console.error('BOOTSTRAP FAILED:', e);
throw e;
}),
consumeItem: name => {
assert.typeof(name, 'string');
return consume[name];
},
produceItem: (name, resolution) => {
assert.typeof(name, 'string');
produce[name].resolve(resolution);
},
resetItem: name => {
assert.typeof(name, 'string');
produce[name].reset();
},
});
};
harden({ buildRootObject });
export { buildRootObject };