@openzeppelin/compact-deployer 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +33 -0
- package/README.md +304 -0
- package/dist/config/compact-config.d.ts +31 -0
- package/dist/config/compact-config.js +123 -0
- package/dist/config/compact-config.js.map +1 -0
- package/dist/config/schema.d.ts +688 -0
- package/dist/config/schema.js +90 -0
- package/dist/config/schema.js.map +1 -0
- package/dist/deployer.d.ts +154 -0
- package/dist/deployer.js +916 -0
- package/dist/deployer.js.map +1 -0
- package/dist/deployments.d.ts +119 -0
- package/dist/deployments.js +144 -0
- package/dist/deployments.js.map +1 -0
- package/dist/errors.d.ts +94 -0
- package/dist/errors.js +124 -0
- package/dist/errors.js.map +1 -0
- package/dist/loaders/args.d.ts +22 -0
- package/dist/loaders/args.js +72 -0
- package/dist/loaders/args.js.map +1 -0
- package/dist/loaders/argv.d.ts +36 -0
- package/dist/loaders/argv.js +101 -0
- package/dist/loaders/argv.js.map +1 -0
- package/dist/loaders/artifact-path.d.ts +16 -0
- package/dist/loaders/artifact-path.js +35 -0
- package/dist/loaders/artifact-path.js.map +1 -0
- package/dist/loaders/artifact.d.ts +43 -0
- package/dist/loaders/artifact.js +115 -0
- package/dist/loaders/artifact.js.map +1 -0
- package/dist/loaders/constructor-meta.d.ts +17 -0
- package/dist/loaders/constructor-meta.js +99 -0
- package/dist/loaders/constructor-meta.js.map +1 -0
- package/dist/loaders/context.d.ts +14 -0
- package/dist/loaders/context.js +43 -0
- package/dist/loaders/context.js.map +1 -0
- package/dist/loaders/contract-resolve.d.ts +15 -0
- package/dist/loaders/contract-resolve.js +53 -0
- package/dist/loaders/contract-resolve.js.map +1 -0
- package/dist/loaders/init-state.d.ts +8 -0
- package/dist/loaders/init-state.js +37 -0
- package/dist/loaders/init-state.js.map +1 -0
- package/dist/loaders/ref-resolver.d.ts +8 -0
- package/dist/loaders/ref-resolver.js +27 -0
- package/dist/loaders/ref-resolver.js.map +1 -0
- package/dist/loaders/signing-key.d.ts +10 -0
- package/dist/loaders/signing-key.js +23 -0
- package/dist/loaders/signing-key.js.map +1 -0
- package/dist/providers/build.d.ts +20 -0
- package/dist/providers/build.js +41 -0
- package/dist/providers/build.js.map +1 -0
- package/dist/providers/network.d.ts +6 -0
- package/dist/providers/network.js +36 -0
- package/dist/providers/network.js.map +1 -0
- package/dist/providers/private-state-password.d.ts +12 -0
- package/dist/providers/private-state-password.js +27 -0
- package/dist/providers/private-state-password.js.map +1 -0
- package/dist/providers/proof-server.d.ts +29 -0
- package/dist/providers/proof-server.js +97 -0
- package/dist/providers/proof-server.js.map +1 -0
- package/dist/runDeploy.d.ts +167 -0
- package/dist/runDeploy.js +228 -0
- package/dist/runDeploy.js.map +1 -0
- package/dist/services/atomic-json.d.ts +7 -0
- package/dist/services/atomic-json.js +34 -0
- package/dist/services/atomic-json.js.map +1 -0
- package/dist/services/chain-state.d.ts +75 -0
- package/dist/services/chain-state.js +151 -0
- package/dist/services/chain-state.js.map +1 -0
- package/dist/services/contract-state.d.ts +29 -0
- package/dist/services/contract-state.js +54 -0
- package/dist/services/contract-state.js.map +1 -0
- package/dist/services/deploy-plan.d.ts +36 -0
- package/dist/services/deploy-plan.js +45 -0
- package/dist/services/deploy-plan.js.map +1 -0
- package/dist/services/deploy-tx.d.ts +189 -0
- package/dist/services/deploy-tx.js +297 -0
- package/dist/services/deploy-tx.js.map +1 -0
- package/dist/services/error-format.d.ts +10 -0
- package/dist/services/error-format.js +91 -0
- package/dist/services/error-format.js.map +1 -0
- package/dist/services/file-lock.d.ts +23 -0
- package/dist/services/file-lock.js +90 -0
- package/dist/services/file-lock.js.map +1 -0
- package/dist/services/maintenance-tx.d.ts +49 -0
- package/dist/services/maintenance-tx.js +43 -0
- package/dist/services/maintenance-tx.js.map +1 -0
- package/dist/services/wallet-cache.d.ts +99 -0
- package/dist/services/wallet-cache.js +239 -0
- package/dist/services/wallet-cache.js.map +1 -0
- package/dist/services/wallet-sync.d.ts +89 -0
- package/dist/services/wallet-sync.js +249 -0
- package/dist/services/wallet-sync.js.map +1 -0
- package/dist/wallet/handler.d.ts +64 -0
- package/dist/wallet/handler.js +151 -0
- package/dist/wallet/handler.js.map +1 -0
- package/dist/wallet/keystore.d.ts +55 -0
- package/dist/wallet/keystore.js +213 -0
- package/dist/wallet/keystore.js.map +1 -0
- package/dist/wallet/seeds.d.ts +41 -0
- package/dist/wallet/seeds.js +84 -0
- package/dist/wallet/seeds.js.map +1 -0
- package/package.json +133 -0
- package/proof-server.yml +16 -0
package/dist/deployer.js
ADDED
|
@@ -0,0 +1,916 @@
|
|
|
1
|
+
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
|
|
2
|
+
if (value !== null && value !== void 0) {
|
|
3
|
+
if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
|
|
4
|
+
var dispose, inner;
|
|
5
|
+
if (async) {
|
|
6
|
+
if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
|
|
7
|
+
dispose = value[Symbol.asyncDispose];
|
|
8
|
+
}
|
|
9
|
+
if (dispose === void 0) {
|
|
10
|
+
if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
|
|
11
|
+
dispose = value[Symbol.dispose];
|
|
12
|
+
if (async) inner = dispose;
|
|
13
|
+
}
|
|
14
|
+
if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
|
|
15
|
+
if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
|
|
16
|
+
env.stack.push({ value: value, dispose: dispose, async: async });
|
|
17
|
+
}
|
|
18
|
+
else if (async) {
|
|
19
|
+
env.stack.push({ async: true });
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
};
|
|
23
|
+
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
|
|
24
|
+
return function (env) {
|
|
25
|
+
function fail(e) {
|
|
26
|
+
env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
|
|
27
|
+
env.hasError = true;
|
|
28
|
+
}
|
|
29
|
+
var r, s = 0;
|
|
30
|
+
function next() {
|
|
31
|
+
while (r = env.stack.pop()) {
|
|
32
|
+
try {
|
|
33
|
+
if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
|
|
34
|
+
if (r.dispose) {
|
|
35
|
+
var result = r.dispose.call(r.value);
|
|
36
|
+
if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
|
|
37
|
+
}
|
|
38
|
+
else s |= 1;
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
fail(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
|
|
45
|
+
if (env.hasError) throw env.error;
|
|
46
|
+
}
|
|
47
|
+
return next();
|
|
48
|
+
};
|
|
49
|
+
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
50
|
+
var e = new Error(message);
|
|
51
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
52
|
+
});
|
|
53
|
+
import { createHash } from 'node:crypto';
|
|
54
|
+
import { CompactConfig } from "./config/compact-config.js";
|
|
55
|
+
import { Deployments } from "./deployments.js";
|
|
56
|
+
import { BlockLimitError, ConfigError, DeployError, DeploymentsFileError, FragmentDeployError, } from "./errors.js";
|
|
57
|
+
import { ConstructorArgs } from "./loaders/args.js";
|
|
58
|
+
import { Artifact } from "./loaders/artifact.js";
|
|
59
|
+
import { InitialPrivateState } from "./loaders/init-state.js";
|
|
60
|
+
import { SigningKey } from "./loaders/signing-key.js";
|
|
61
|
+
import { buildProviders } from "./providers/build.js";
|
|
62
|
+
import { applyNetwork } from "./providers/network.js";
|
|
63
|
+
import { ProofServer } from "./providers/proof-server.js";
|
|
64
|
+
import { assertResumable, awaitCircuitsOnChain, readSnapshot, signerIndex, verifyState, } from "./services/chain-state.js";
|
|
65
|
+
import { fragmentRemainder, planFragments, remaining, sortCircuits, } from "./services/deploy-plan.js";
|
|
66
|
+
import { awaitDeployFinalization, awaitDeployTxData, awaitFinalization, awaitFragmentFinalization, buildExplorerUrl, DEFAULT_TX_TIMEOUT_MS, persistDeployPrivateState, submitDeploy, submitInsert, toConfirmedRecord, toPartialRecord, toPendingRecord, } from "./services/deploy-tx.js";
|
|
67
|
+
import { formatError } from "./services/error-format.js";
|
|
68
|
+
import { buildInsertTx, buildInsertUpdate, verifyingKeyOf, } from "./services/maintenance-tx.js";
|
|
69
|
+
import { awaitDustSettled, DEFAULT_SYNC_TIMEOUT_MS, logWalletAddresses, readDustTip, syncAndVerifyFunds, } from "./services/wallet-sync.js";
|
|
70
|
+
import { WalletHandler } from "./wallet/handler.js";
|
|
71
|
+
import { resolveSeed } from "./wallet/seeds.js";
|
|
72
|
+
/**
|
|
73
|
+
* Stateful handle for one contract's deploy lifecycle. Always acquire
|
|
74
|
+
* with `await using`: `[Symbol.asyncDispose]` releases the proof-server
|
|
75
|
+
* container (if `"auto"`) and the wallet (if built here, not injected).
|
|
76
|
+
*/
|
|
77
|
+
export class Deployer {
|
|
78
|
+
/** Contract name as specified in opts. */
|
|
79
|
+
contractName;
|
|
80
|
+
/** Resolved network name (`opts.network` or `[profile].default_network`). */
|
|
81
|
+
networkName;
|
|
82
|
+
/** Hex of the deployer's coin public key. */
|
|
83
|
+
deployer;
|
|
84
|
+
/** Loaded artifact: zk config path + compiled-contract handle. */
|
|
85
|
+
artifact;
|
|
86
|
+
/** Per-contract signing key loaded from disk. */
|
|
87
|
+
signingKey;
|
|
88
|
+
#state;
|
|
89
|
+
constructor(state) {
|
|
90
|
+
this.#state = state;
|
|
91
|
+
this.contractName = state.opts.contract;
|
|
92
|
+
this.networkName = state.networkName;
|
|
93
|
+
this.deployer = state.deployer;
|
|
94
|
+
this.artifact = state.artifact;
|
|
95
|
+
this.signingKey = state.signingKey;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Load config + artifact + signing key, start proof server, build or
|
|
99
|
+
* adopt a wallet. Throws typed errors that map to CLI exit codes via
|
|
100
|
+
* {@link DeployError.exitCode}.
|
|
101
|
+
*/
|
|
102
|
+
static async prepare(opts) {
|
|
103
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
104
|
+
try {
|
|
105
|
+
const { logger } = opts;
|
|
106
|
+
const config = await CompactConfig.load(opts.configPath);
|
|
107
|
+
const { rootDir } = config;
|
|
108
|
+
const { networkName, network, contract } = resolveTargets(opts, config);
|
|
109
|
+
// Checked here so a bad budget fails before the wallet sync, which
|
|
110
|
+
// on a real network is tens of minutes.
|
|
111
|
+
assertBudget(opts.circuitsPerTx);
|
|
112
|
+
const budget = opts.circuitsPerTx ?? contract.circuits_per_tx;
|
|
113
|
+
const signingKey = await SigningKey.load(rootDir, contract.signing_key_file);
|
|
114
|
+
// One discriminated value instead of two parallel ones: every later
|
|
115
|
+
// use of the seed is reachable only through the `owned` arm, so the
|
|
116
|
+
// "injected wallet has no seed" invariant is checked by the compiler
|
|
117
|
+
// rather than re-asserted at runtime.
|
|
118
|
+
const walletSource = opts.walletProvider
|
|
119
|
+
? { kind: 'injected', provider: opts.walletProvider }
|
|
120
|
+
: {
|
|
121
|
+
kind: 'owned',
|
|
122
|
+
resolution: await resolveSeed({
|
|
123
|
+
config,
|
|
124
|
+
networkName,
|
|
125
|
+
network,
|
|
126
|
+
seedFile: opts.seedFile,
|
|
127
|
+
promptPassphrase: opts.promptPassphrase,
|
|
128
|
+
}),
|
|
129
|
+
};
|
|
130
|
+
if (walletSource.kind === 'owned') {
|
|
131
|
+
logger.debug(`Resolved deployer seed from: ${walletSource.resolution.origin}`);
|
|
132
|
+
}
|
|
133
|
+
// Hashed here because `prepare` is the only scope holding the seed, and
|
|
134
|
+
// the private-state password must derive from secret material.
|
|
135
|
+
const privateStateSecret = walletSource.kind === 'owned'
|
|
136
|
+
? createHash('sha256')
|
|
137
|
+
.update(walletSource.resolution.seed.value)
|
|
138
|
+
.digest('hex')
|
|
139
|
+
: undefined;
|
|
140
|
+
// Stack owns every resource acquired below. On any throw before
|
|
141
|
+
// the final `stack.move()`, `await using` disposes them in reverse
|
|
142
|
+
// order; on success, ownership transfers to the returned Deployer
|
|
143
|
+
// and the local `await using` becomes a no-op.
|
|
144
|
+
const stack = __addDisposableResource(env_1, new AsyncDisposableStack(), true);
|
|
145
|
+
const proofServer = await ProofServer.start({
|
|
146
|
+
cliOverride: opts.proofServer,
|
|
147
|
+
network,
|
|
148
|
+
logger,
|
|
149
|
+
});
|
|
150
|
+
stack.use(proofServer);
|
|
151
|
+
const { env } = applyNetwork(network, proofServer.url);
|
|
152
|
+
logger.debug(`Network ID: ${env.networkId}; proof server: ${env.proofServer}`);
|
|
153
|
+
const artifact = await Artifact.load({
|
|
154
|
+
rootDir,
|
|
155
|
+
artifactsDir: config.artifactsDir,
|
|
156
|
+
artifact: contract.artifact,
|
|
157
|
+
contractName: opts.contract,
|
|
158
|
+
witnesses: contract.witnesses,
|
|
159
|
+
});
|
|
160
|
+
logger.debug(`Artifact: ${artifact.artifactPath} (${artifact.circuitNames.length} circuits)`);
|
|
161
|
+
// Loaded before the wallet block: both depend only on the config and the
|
|
162
|
+
// artifact, and a bad args source or init-state ref must fail now rather
|
|
163
|
+
// than after a 30-60 min first sync.
|
|
164
|
+
const args = await ConstructorArgs.load(contract, rootDir, opts.argsOverride, opts.args, artifact.artifactPath);
|
|
165
|
+
const initialPrivateState = await InitialPrivateState.load(contract.init_private_state, rootDir);
|
|
166
|
+
let wallet;
|
|
167
|
+
if (walletSource.kind === 'injected') {
|
|
168
|
+
wallet = walletSource.provider;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
// Sync tuning precedence: CLI/programmatic option > [networks.X] TOML
|
|
172
|
+
// value > built-in default. `sync_batch_size` falls through to
|
|
173
|
+
// WalletHandler's 5000 default when neither is set.
|
|
174
|
+
const syncBatchSize = opts.syncBatchSize ?? network.sync_batch_size;
|
|
175
|
+
const syncTimeoutMs = opts.syncTimeoutMs ??
|
|
176
|
+
(network.sync_timeout !== undefined
|
|
177
|
+
? network.sync_timeout * 1000
|
|
178
|
+
: DEFAULT_SYNC_TIMEOUT_MS);
|
|
179
|
+
const owned = await WalletHandler.build(logger, env, walletSource.resolution.seed, {
|
|
180
|
+
rootDir,
|
|
181
|
+
skipWalletCache: opts.skipWalletCache,
|
|
182
|
+
seedCacheDust: opts.seedCacheDust,
|
|
183
|
+
seedCacheShielded: opts.seedCacheShielded,
|
|
184
|
+
seedCacheUnshielded: opts.seedCacheUnshielded,
|
|
185
|
+
syncBatchSize,
|
|
186
|
+
});
|
|
187
|
+
stack.use(owned);
|
|
188
|
+
wallet = owned.provider;
|
|
189
|
+
// Kick off the wallet's internal indexer subscription without
|
|
190
|
+
// blocking on testkit-js's 90 s `waitForFunds` gate (which is too
|
|
191
|
+
// short for real networks). Then drive sync ourselves with a
|
|
192
|
+
// configurable ceiling and surface a clear `UnfundedWalletError`
|
|
193
|
+
// if we reach chain tip and still have no shielded balance.
|
|
194
|
+
await wallet.start(false);
|
|
195
|
+
// Surface the wallet's derived bech32m addresses right away so
|
|
196
|
+
// the user can sanity-check they match the seed they intended
|
|
197
|
+
// *before* settling in for a long shielded sync.
|
|
198
|
+
await logWalletAddresses(wallet, logger);
|
|
199
|
+
await syncAndVerifyFunds({
|
|
200
|
+
wallet,
|
|
201
|
+
timeoutMs: syncTimeoutMs,
|
|
202
|
+
logger,
|
|
203
|
+
// Periodic checkpoint: every 5 min during sync, snapshot both
|
|
204
|
+
// sub-wallet caches. If the user interrupts a long first-run,
|
|
205
|
+
// the next attempt resumes from the most recent checkpoint.
|
|
206
|
+
onCheckpoint: () => owned.saveCache(),
|
|
207
|
+
});
|
|
208
|
+
// Snapshot the shielded + dust sub-wallets now that sync is
|
|
209
|
+
// complete. Best-effort: failures are warn-logged in
|
|
210
|
+
// `saveCache`'s caller; never block the deploy on a cache write.
|
|
211
|
+
try {
|
|
212
|
+
await owned.saveCache();
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
logger.warn({ err: formatError(e) }, 'Wallet cache save failed; next run will re-sync');
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const deployer = wallet.getCoinPublicKey();
|
|
219
|
+
return new Deployer({
|
|
220
|
+
opts,
|
|
221
|
+
logger,
|
|
222
|
+
config,
|
|
223
|
+
networkName,
|
|
224
|
+
network,
|
|
225
|
+
contract,
|
|
226
|
+
signingKey,
|
|
227
|
+
budget,
|
|
228
|
+
privateStateSecret,
|
|
229
|
+
artifact,
|
|
230
|
+
args,
|
|
231
|
+
initialPrivateState,
|
|
232
|
+
wallet,
|
|
233
|
+
deployer,
|
|
234
|
+
env,
|
|
235
|
+
resources: stack.move(),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
catch (e_1) {
|
|
239
|
+
env_1.error = e_1;
|
|
240
|
+
env_1.hasError = true;
|
|
241
|
+
}
|
|
242
|
+
finally {
|
|
243
|
+
const result_1 = __disposeResources(env_1);
|
|
244
|
+
if (result_1)
|
|
245
|
+
await result_1;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Deploy the contract and leave a `confirmed` record under
|
|
250
|
+
* `deployments/<network>.json`.
|
|
251
|
+
*
|
|
252
|
+
* A contract that fits one tx takes the single-tx path unchanged. A larger
|
|
253
|
+
* one is split: fragment 0's verifier keys ride the deploy tx and the rest
|
|
254
|
+
* arrive as batched maintenance updates, all inside this call. The record
|
|
255
|
+
* written after submission is what makes either path recoverable: every
|
|
256
|
+
* later failure leaves the address on disk and names it in the error.
|
|
257
|
+
*
|
|
258
|
+
* A `partial` head record resumes from chain state unless `--force`
|
|
259
|
+
* is set, in which case it is rotated into history and a new contract is
|
|
260
|
+
* deployed. Resuming is idempotent: only the circuits still missing on chain
|
|
261
|
+
* are inserted.
|
|
262
|
+
*/
|
|
263
|
+
async deploy() {
|
|
264
|
+
const s = this.#state;
|
|
265
|
+
const contractName = s.opts.contract;
|
|
266
|
+
const deployments = new Deployments({
|
|
267
|
+
rootDir: s.config.rootDir,
|
|
268
|
+
deploymentsDir: s.config.deploymentsDir,
|
|
269
|
+
network: s.networkName,
|
|
270
|
+
});
|
|
271
|
+
const force = s.opts.force === true;
|
|
272
|
+
const head = await deployments.getHead(contractName);
|
|
273
|
+
const providers = buildProviders({
|
|
274
|
+
env: s.env,
|
|
275
|
+
wallet: s.wallet,
|
|
276
|
+
contractName,
|
|
277
|
+
contract: s.contract,
|
|
278
|
+
zkConfigPath: s.artifact.zkConfigPath,
|
|
279
|
+
rootDir: s.config.rootDir,
|
|
280
|
+
privateStateProvider: s.opts.privateStateProvider,
|
|
281
|
+
privateStateSecret: s.privateStateSecret,
|
|
282
|
+
});
|
|
283
|
+
const txTimeoutMs = s.opts.txTimeoutMs ?? DEFAULT_TX_TIMEOUT_MS;
|
|
284
|
+
// A bundle missing a key fails here, before any transaction.
|
|
285
|
+
const keys = await s.artifact.verifierKeys(providers.zkConfigProvider);
|
|
286
|
+
// A partial head resumes; only --force starts a second contract.
|
|
287
|
+
const started = head?.status === 'partial' && !force
|
|
288
|
+
? await this.#resumeAt({ head, providers, keys, txTimeoutMs })
|
|
289
|
+
: await this.#deployFragmentZero({
|
|
290
|
+
providers,
|
|
291
|
+
deployments,
|
|
292
|
+
txTimeoutMs,
|
|
293
|
+
force,
|
|
294
|
+
});
|
|
295
|
+
const counts = started.kind === 'single'
|
|
296
|
+
? { fragments: 1, circuits: s.artifact.circuitNames.length }
|
|
297
|
+
: await this.#finishSplit({
|
|
298
|
+
providers,
|
|
299
|
+
deployments,
|
|
300
|
+
started,
|
|
301
|
+
keys,
|
|
302
|
+
txTimeoutMs,
|
|
303
|
+
});
|
|
304
|
+
const record = toConfirmedRecord({
|
|
305
|
+
previous: started.record,
|
|
306
|
+
txHash: started.txHash,
|
|
307
|
+
blockHeight: started.blockHeight,
|
|
308
|
+
});
|
|
309
|
+
const persisted = await this.#persist(record, () => deployments.confirm(contractName, record));
|
|
310
|
+
return {
|
|
311
|
+
contractName,
|
|
312
|
+
network: s.networkName,
|
|
313
|
+
address: record.address,
|
|
314
|
+
txHash: record.txHash,
|
|
315
|
+
txId: record.txId,
|
|
316
|
+
blockHeight: record.blockHeight,
|
|
317
|
+
deployer: record.deployer,
|
|
318
|
+
artifact: record.artifact,
|
|
319
|
+
deploymentsFile: persisted.head,
|
|
320
|
+
dryRun: false,
|
|
321
|
+
explorerUrl: buildExplorerUrl(s.network.explorer, record.address),
|
|
322
|
+
fragments: counts.fragments,
|
|
323
|
+
circuits: counts.circuits,
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Land every remaining fragment, then verify the address against the
|
|
328
|
+
* artifact. Only a pass here lets the caller write `confirmed`.
|
|
329
|
+
*/
|
|
330
|
+
async #finishSplit(args) {
|
|
331
|
+
const progress = await this.#insertRemaining(args);
|
|
332
|
+
// Strict verify gates the promotion to `confirmed`.
|
|
333
|
+
verifyState({
|
|
334
|
+
address: args.started.address,
|
|
335
|
+
artifactKeys: args.keys,
|
|
336
|
+
snapshot: progress.snapshot,
|
|
337
|
+
authority: progress.authority,
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
fragments: progress.fragments,
|
|
341
|
+
circuits: progress.snapshot.circuits.length,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Submit the deploy tx, record it, wait for it to land, and persist private
|
|
346
|
+
* state.
|
|
347
|
+
*/
|
|
348
|
+
async #deployFragmentZero(args) {
|
|
349
|
+
const { providers, deployments, txTimeoutMs, force } = args;
|
|
350
|
+
const s = this.#state;
|
|
351
|
+
const contractName = s.opts.contract;
|
|
352
|
+
const circuits = sortCircuits(s.artifact.circuitNames);
|
|
353
|
+
// Checked before proving: submitting a tx we would then refuse to record
|
|
354
|
+
// costs the user fees for nothing.
|
|
355
|
+
await deployments.assertRecordable(contractName, { force });
|
|
356
|
+
const attempt = await this.#submitHalving({
|
|
357
|
+
circuits,
|
|
358
|
+
size: s.budget ?? Math.max(circuits.length, 1),
|
|
359
|
+
what: 'Deploy tx',
|
|
360
|
+
submit: (batch) => submitDeploy({
|
|
361
|
+
providers,
|
|
362
|
+
contractName,
|
|
363
|
+
contract: s.contract,
|
|
364
|
+
artifact: s.artifact,
|
|
365
|
+
signingKey: s.signingKey.hex,
|
|
366
|
+
args: s.args.values,
|
|
367
|
+
initialPrivateState: s.initialPrivateState?.value,
|
|
368
|
+
circuits: batch,
|
|
369
|
+
}),
|
|
370
|
+
});
|
|
371
|
+
const submitted = attempt.result;
|
|
372
|
+
const fragments = planFragments(circuits, attempt.cap).fragments;
|
|
373
|
+
const split = fragments.length > 1;
|
|
374
|
+
// Nothing is written until the node has accepted the tx.
|
|
375
|
+
const record = split
|
|
376
|
+
? toPartialRecord({
|
|
377
|
+
address: submitted.address,
|
|
378
|
+
txId: submitted.txId,
|
|
379
|
+
deployer: s.deployer,
|
|
380
|
+
artifact: s.contract.artifact,
|
|
381
|
+
circuits,
|
|
382
|
+
circuitsOnChain: [],
|
|
383
|
+
})
|
|
384
|
+
: toPendingRecord({
|
|
385
|
+
submitted,
|
|
386
|
+
deployer: s.deployer,
|
|
387
|
+
artifact: s.contract.artifact,
|
|
388
|
+
});
|
|
389
|
+
await this.#persist(record, () => deployments.record(contractName, record, { force }));
|
|
390
|
+
const finalized = await awaitDeployFinalization({
|
|
391
|
+
providers,
|
|
392
|
+
contractName,
|
|
393
|
+
submitted,
|
|
394
|
+
txTimeoutMs,
|
|
395
|
+
recovery: record.status === 'partial' ? 'partial' : 'pending',
|
|
396
|
+
});
|
|
397
|
+
// Order copied from midnight-js's own post-success path, and reached only
|
|
398
|
+
// on `SucceedEntirely`. The signing key and the initial private
|
|
399
|
+
// state are stored once, for an address that now exists.
|
|
400
|
+
await persistDeployPrivateState({
|
|
401
|
+
providers,
|
|
402
|
+
contract: s.contract,
|
|
403
|
+
submitted,
|
|
404
|
+
});
|
|
405
|
+
const base = {
|
|
406
|
+
address: submitted.address,
|
|
407
|
+
txId: submitted.txId,
|
|
408
|
+
txHash: finalized.txHash,
|
|
409
|
+
blockHeight: finalized.blockHeight,
|
|
410
|
+
};
|
|
411
|
+
if (record.status !== 'partial') {
|
|
412
|
+
return { kind: 'single', ...base, record };
|
|
413
|
+
}
|
|
414
|
+
s.logger.info(`Deploy tx landed with ${attempt.batch.length}/${circuits.length} circuits, txId ${submitted.txId}, block ${finalized.blockHeight}`);
|
|
415
|
+
return {
|
|
416
|
+
kind: 'fresh',
|
|
417
|
+
...base,
|
|
418
|
+
record,
|
|
419
|
+
inserts: fragments.slice(1),
|
|
420
|
+
size: attempt.cap,
|
|
421
|
+
fragmentZero: attempt.batch,
|
|
422
|
+
tip: attempt.tip,
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
/**
|
|
426
|
+
* Adopt a `partial` head after checking the recorded address really is our
|
|
427
|
+
* unfinished contract. Nothing is submitted if any check fails.
|
|
428
|
+
*/
|
|
429
|
+
async #resumeAt(args) {
|
|
430
|
+
const { head, providers, keys, txTimeoutMs } = args;
|
|
431
|
+
const s = this.#state;
|
|
432
|
+
// Only the public half of the loaded key is compared or logged.
|
|
433
|
+
const verifyingKey = verifyingKeyOf(s.signingKey.hex);
|
|
434
|
+
s.logger.debug(`Resuming ${head.address}; maintenance verifying key ${verifyingKey}`);
|
|
435
|
+
// Settle the deploy transaction first, keyed by address. A run killed
|
|
436
|
+
// between submission and finalization leaves a record whose contract is
|
|
437
|
+
// still landing, and reading chain state before it does would report no
|
|
438
|
+
// contract and send the operator to --force, which deploys a second one.
|
|
439
|
+
const landed = await this.#settleDeployTx({ head, providers, txTimeoutMs });
|
|
440
|
+
// An insert the previous run submitted but never saw land may since have
|
|
441
|
+
// landed, so settle it before reading the state the plan is built from.
|
|
442
|
+
const settledInsert = head.pendingTxId === undefined
|
|
443
|
+
? undefined
|
|
444
|
+
: await this.#settlePendingInsert({
|
|
445
|
+
providers,
|
|
446
|
+
txId: head.pendingTxId,
|
|
447
|
+
circuits: head.pendingCircuits ?? [],
|
|
448
|
+
txTimeoutMs,
|
|
449
|
+
});
|
|
450
|
+
// A pending insert that turns out to have landed still has to be
|
|
451
|
+
// visible before the plan is built from chain state.
|
|
452
|
+
if (settledInsert !== undefined) {
|
|
453
|
+
await awaitCircuitsOnChain({
|
|
454
|
+
publicDataProvider: providers.publicDataProvider,
|
|
455
|
+
address: head.address,
|
|
456
|
+
expected: settledInsert,
|
|
457
|
+
timeoutMs: txTimeoutMs,
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
const read = await this.#readSnapshotOrFail(head, providers);
|
|
461
|
+
// No state and a timed-out watch is inconclusive: the deploy tx may still
|
|
462
|
+
// be landing. The --force hint is reserved for a watch that concluded.
|
|
463
|
+
if (read === undefined && landed === undefined) {
|
|
464
|
+
throw new FragmentDeployError({
|
|
465
|
+
address: head.address,
|
|
466
|
+
circuitsOnChain: head.circuitsOnChain,
|
|
467
|
+
circuitsPending: head.circuitsPending,
|
|
468
|
+
reason: `the deploy transaction of ${head.address} was not seen within ${txTimeoutMs} ms and no contract state is readable yet; check the address on an explorer before re-running with --force`,
|
|
469
|
+
timedOut: true,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
// Address exists, committee is ours, on-chain keys match the
|
|
473
|
+
// artifact. The record's own circuit lists are never consulted.
|
|
474
|
+
const snapshot = assertResumable({
|
|
475
|
+
address: head.address,
|
|
476
|
+
artifactKeys: keys,
|
|
477
|
+
snapshot: read,
|
|
478
|
+
verifyingKey,
|
|
479
|
+
});
|
|
480
|
+
const left = remaining([...keys.keys()], snapshot.circuits);
|
|
481
|
+
s.logger.info(`Resuming fragmented deploy of ${head.address}: ${snapshot.circuits.length}/${keys.size} circuits on chain`);
|
|
482
|
+
// A resume of a deploy started on another machine, or after the
|
|
483
|
+
// private-state store was cleared, has no key for this address.
|
|
484
|
+
await this.#ensureSigningKeyStored({ providers, address: head.address });
|
|
485
|
+
// Chain state exists, so the deploy landed. Prefer what the indexer just
|
|
486
|
+
// said over the record, and fall back to the record only when the
|
|
487
|
+
// address-keyed watch timed out.
|
|
488
|
+
const txHash = landed?.txHash ?? head.txHash;
|
|
489
|
+
const blockHeight = landed?.blockHeight ?? head.blockHeight;
|
|
490
|
+
if (txHash === undefined || blockHeight === undefined) {
|
|
491
|
+
throw new FragmentDeployError({
|
|
492
|
+
address: head.address,
|
|
493
|
+
circuitsOnChain: snapshot.circuits,
|
|
494
|
+
circuitsPending: left,
|
|
495
|
+
reason: `the deploy transaction of ${head.address} could not be identified, so the deploy cannot be confirmed; copy its txHash and blockHeight from an explorer into the partial record and re-run`,
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
const budget = s.budget ?? Math.max(left.length, 1);
|
|
499
|
+
return {
|
|
500
|
+
kind: 'resumed',
|
|
501
|
+
address: head.address,
|
|
502
|
+
txId: head.txId,
|
|
503
|
+
txHash,
|
|
504
|
+
blockHeight,
|
|
505
|
+
record: head,
|
|
506
|
+
// The resumed plan spans only what is left on chain.
|
|
507
|
+
inserts: planFragments(left, budget).fragments,
|
|
508
|
+
size: budget,
|
|
509
|
+
snapshot,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Wait for the recorded address to have a landed deploy transaction.
|
|
514
|
+
*
|
|
515
|
+
* `undefined` on timeout, so the caller can tell an inconclusive watch from
|
|
516
|
+
* a contract that is really absent. A recorded `txHash` that disagrees with
|
|
517
|
+
* the chain means the record points at someone else's deploy.
|
|
518
|
+
*/
|
|
519
|
+
async #settleDeployTx(args) {
|
|
520
|
+
const { head, providers, txTimeoutMs } = args;
|
|
521
|
+
const s = this.#state;
|
|
522
|
+
let landed;
|
|
523
|
+
try {
|
|
524
|
+
landed = await awaitDeployTxData({
|
|
525
|
+
providers,
|
|
526
|
+
address: head.address,
|
|
527
|
+
txTimeoutMs,
|
|
528
|
+
fail: (reason, detail) => detail?.timedOut === true
|
|
529
|
+
? new DeployError(reason)
|
|
530
|
+
: new FragmentDeployError({
|
|
531
|
+
address: head.address,
|
|
532
|
+
circuitsOnChain: head.circuitsOnChain,
|
|
533
|
+
circuitsPending: head.circuitsPending,
|
|
534
|
+
reason: `the deploy transaction of ${head.address} did not succeed: ${reason}`,
|
|
535
|
+
}, { cause: detail?.cause }),
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
catch (e) {
|
|
539
|
+
if (e instanceof FragmentDeployError)
|
|
540
|
+
throw e;
|
|
541
|
+
s.logger.debug(`No landed deploy transaction at ${head.address} yet: ${formatError(e)}`);
|
|
542
|
+
return undefined;
|
|
543
|
+
}
|
|
544
|
+
if (head.txHash !== undefined && head.txHash !== landed.txHash) {
|
|
545
|
+
throw new ConfigError(`Deployments ledger records txHash ${head.txHash} for ${head.address}, but the chain reports ${landed.txHash}. Repair the record, or re-run with --force to deploy fresh.`);
|
|
546
|
+
}
|
|
547
|
+
return landed;
|
|
548
|
+
}
|
|
549
|
+
/** Read chain state, reporting a read failure as the resumable error. */
|
|
550
|
+
async #readSnapshotOrFail(head, providers) {
|
|
551
|
+
try {
|
|
552
|
+
return await readSnapshot(providers.publicDataProvider, head.address);
|
|
553
|
+
}
|
|
554
|
+
catch (e) {
|
|
555
|
+
throw new FragmentDeployError({
|
|
556
|
+
address: head.address,
|
|
557
|
+
circuitsOnChain: head.circuitsOnChain,
|
|
558
|
+
circuitsPending: head.circuitsPending,
|
|
559
|
+
reason: `chain state at ${head.address} could not be read: ${formatError(e)}`,
|
|
560
|
+
}, { cause: e });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Wait out an insert the previous run left in flight, and report the circuits
|
|
565
|
+
* it carried if it landed.
|
|
566
|
+
*
|
|
567
|
+
* The chain read that follows is the truth either way, so a failure here only
|
|
568
|
+
* gets logged; the caller uses the returned circuits to make sure a landed one
|
|
569
|
+
* is visible before it plans.
|
|
570
|
+
*/
|
|
571
|
+
async #settlePendingInsert(args) {
|
|
572
|
+
const { providers, txId, circuits, txTimeoutMs } = args;
|
|
573
|
+
const { logger } = this.#state;
|
|
574
|
+
logger.info(`Settling the insert left in flight by the previous run: ${txId}`);
|
|
575
|
+
try {
|
|
576
|
+
await awaitFinalization({
|
|
577
|
+
providers,
|
|
578
|
+
txId,
|
|
579
|
+
txTimeoutMs,
|
|
580
|
+
fail: (reason) => new DeployError(reason),
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
catch (e) {
|
|
584
|
+
logger.info(`Insert ${txId} did not land: ${formatError(e)}`);
|
|
585
|
+
return undefined;
|
|
586
|
+
}
|
|
587
|
+
logger.info(`Insert ${txId} landed after the previous run stopped`);
|
|
588
|
+
return circuits;
|
|
589
|
+
}
|
|
590
|
+
/** Store the signing key for `address` when the private-state store lacks it. */
|
|
591
|
+
async #ensureSigningKeyStored(args) {
|
|
592
|
+
const { providers, address } = args;
|
|
593
|
+
const s = this.#state;
|
|
594
|
+
if ((await providers.privateStateProvider.getSigningKey(address)) !== null) {
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
s.logger.debug(`Storing the maintenance signing key for ${address}`);
|
|
598
|
+
providers.privateStateProvider.setContractAddress(address);
|
|
599
|
+
await providers.privateStateProvider.setSigningKey(address, s.signingKey.hex);
|
|
600
|
+
}
|
|
601
|
+
/**
|
|
602
|
+
* Submit the largest batch the node will take, halving on its refusal.
|
|
603
|
+
*
|
|
604
|
+
* The only halving site. Strictly decreasing, floor one circuit, and
|
|
605
|
+
* only when no explicit budget was given. Each attempt records the
|
|
606
|
+
* dust index the wallet must pass before it can have seen that spend.
|
|
607
|
+
*/
|
|
608
|
+
async #submitHalving(args) {
|
|
609
|
+
const s = this.#state;
|
|
610
|
+
// The cap survives a short batch: a fragment with two circuits left must
|
|
611
|
+
// not shrink the size every later fragment is allowed.
|
|
612
|
+
let cap = Math.max(1, args.size);
|
|
613
|
+
for (;;) {
|
|
614
|
+
const batch = args.circuits.slice(0, cap);
|
|
615
|
+
const tip = await readDustTip(s.wallet);
|
|
616
|
+
try {
|
|
617
|
+
return { batch, cap, tip, result: await args.submit(batch) };
|
|
618
|
+
}
|
|
619
|
+
catch (e) {
|
|
620
|
+
const halvable = e instanceof BlockLimitError &&
|
|
621
|
+
s.budget === undefined &&
|
|
622
|
+
batch.length > 1;
|
|
623
|
+
if (!halvable)
|
|
624
|
+
throw e;
|
|
625
|
+
cap = Math.floor(batch.length / 2);
|
|
626
|
+
s.logger.info(`${args.what} too large at ${batch.length} circuits; retrying with ${cap} per tx`);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* Insert the verifier keys that are still missing, one batched maintenance
|
|
632
|
+
* update per fragment, strictly sequentially.
|
|
633
|
+
*
|
|
634
|
+
* Every failure inside the loop leaves the `partial` record on disk
|
|
635
|
+
* and names the address and both circuit lists, so a re-run resumes.
|
|
636
|
+
*/
|
|
637
|
+
async #insertRemaining(args) {
|
|
638
|
+
const { providers, deployments, started, keys, txTimeoutMs } = args;
|
|
639
|
+
const s = this.#state;
|
|
640
|
+
const contractName = s.opts.contract;
|
|
641
|
+
const { address, record } = started;
|
|
642
|
+
const all = [...keys.keys()];
|
|
643
|
+
// The next fragment is built from a read that already shows
|
|
644
|
+
// fragment 0, never from a state the indexer has not caught up to. This
|
|
645
|
+
// read raises the resumable error itself, so it sits outside the catch.
|
|
646
|
+
let snapshot = started.kind === 'resumed'
|
|
647
|
+
? started.snapshot
|
|
648
|
+
: await awaitCircuitsOnChain({
|
|
649
|
+
publicDataProvider: providers.publicDataProvider,
|
|
650
|
+
address,
|
|
651
|
+
expected: started.fragmentZero,
|
|
652
|
+
timeoutMs: txTimeoutMs,
|
|
653
|
+
});
|
|
654
|
+
const authority = {
|
|
655
|
+
committee: snapshot.committee,
|
|
656
|
+
threshold: snapshot.threshold,
|
|
657
|
+
};
|
|
658
|
+
let size = started.size;
|
|
659
|
+
try {
|
|
660
|
+
// The wallet has to have applied the deploy tx's dust spend
|
|
661
|
+
// before the first insert is balanced against the same UTXO set. A
|
|
662
|
+
// resume has no spend of its own in flight.
|
|
663
|
+
await (started.kind === 'fresh'
|
|
664
|
+
? awaitDustSettled({
|
|
665
|
+
wallet: s.wallet,
|
|
666
|
+
appliedBeyond: started.tip,
|
|
667
|
+
timeoutMs: txTimeoutMs,
|
|
668
|
+
})
|
|
669
|
+
: Promise.resolve());
|
|
670
|
+
// The ledger checks each signature against a committee slot, so
|
|
671
|
+
// the slot comes from the same chain read as the counter.
|
|
672
|
+
const signer = signerIndex({
|
|
673
|
+
address,
|
|
674
|
+
snapshot,
|
|
675
|
+
verifyingKey: verifyingKeyOf(s.signingKey.hex),
|
|
676
|
+
});
|
|
677
|
+
await this.#recordProgress({
|
|
678
|
+
deployments,
|
|
679
|
+
record,
|
|
680
|
+
started,
|
|
681
|
+
snapshot,
|
|
682
|
+
all,
|
|
683
|
+
});
|
|
684
|
+
for (const fragment of started.inserts) {
|
|
685
|
+
// Chain-derived, so an insert never names a key already there.
|
|
686
|
+
let pending = fragmentRemainder(fragment, snapshot.circuits);
|
|
687
|
+
while (pending.length > 0) {
|
|
688
|
+
const landed = await this.#insertBatch({
|
|
689
|
+
providers,
|
|
690
|
+
deployments,
|
|
691
|
+
started,
|
|
692
|
+
keys,
|
|
693
|
+
all,
|
|
694
|
+
pending,
|
|
695
|
+
size,
|
|
696
|
+
snapshot,
|
|
697
|
+
signer,
|
|
698
|
+
txTimeoutMs,
|
|
699
|
+
});
|
|
700
|
+
snapshot = landed.snapshot;
|
|
701
|
+
size = landed.size;
|
|
702
|
+
pending = fragmentRemainder(fragment, snapshot.circuits);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
// Chain-derived: the CMA counter is 0 at deploy and rises by exactly one
|
|
706
|
+
// per landed update, including any from an earlier interrupted run.
|
|
707
|
+
return { fragments: 1 + Number(snapshot.counter), snapshot, authority };
|
|
708
|
+
}
|
|
709
|
+
catch (e) {
|
|
710
|
+
// A ledger-write failure and a config problem keep their own exit codes:
|
|
711
|
+
// neither is fixed by re-running. Everything else, block-limit
|
|
712
|
+
// exhaustion included, becomes the resumable error.
|
|
713
|
+
if (e instanceof DeploymentsFileError)
|
|
714
|
+
throw e;
|
|
715
|
+
if (e instanceof ConfigError)
|
|
716
|
+
throw e;
|
|
717
|
+
if (e instanceof FragmentDeployError)
|
|
718
|
+
throw e;
|
|
719
|
+
throw new FragmentDeployError({
|
|
720
|
+
address,
|
|
721
|
+
circuitsOnChain: snapshot.circuits,
|
|
722
|
+
circuitsPending: remaining(all, snapshot.circuits),
|
|
723
|
+
reason: `inserting verifier keys for "${contractName}" failed: ${formatError(e)}`,
|
|
724
|
+
}, { cause: e });
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
/**
|
|
728
|
+
* Land one batch of verifier keys: submit (halving on refusal), wait, confirm
|
|
729
|
+
* it on chain, let the wallet catch up, and rewrite the progress record.
|
|
730
|
+
*/
|
|
731
|
+
async #insertBatch(args) {
|
|
732
|
+
const { providers, deployments, started, keys, all, pending, size, signer, txTimeoutMs, } = args;
|
|
733
|
+
const s = this.#state;
|
|
734
|
+
const { address, record } = started;
|
|
735
|
+
let snapshot = args.snapshot;
|
|
736
|
+
const attempt = await this.#submitHalving({
|
|
737
|
+
circuits: pending,
|
|
738
|
+
size,
|
|
739
|
+
what: 'Insert',
|
|
740
|
+
submit: (batch) => submitInsert({
|
|
741
|
+
providers,
|
|
742
|
+
contractName: s.opts.contract,
|
|
743
|
+
circuits: batch,
|
|
744
|
+
// Counter read fresh from the snapshot above.
|
|
745
|
+
unprovenTx: buildInsertTx({
|
|
746
|
+
update: buildInsertUpdate({
|
|
747
|
+
address,
|
|
748
|
+
counter: snapshot.counter,
|
|
749
|
+
signingKey: s.signingKey.hex,
|
|
750
|
+
signerIndex: signer,
|
|
751
|
+
inserts: batch.map((circuitId) => ({
|
|
752
|
+
circuitId,
|
|
753
|
+
verifierKey: keyOf(keys, circuitId),
|
|
754
|
+
})),
|
|
755
|
+
}),
|
|
756
|
+
}),
|
|
757
|
+
}),
|
|
758
|
+
});
|
|
759
|
+
const txId = attempt.result;
|
|
760
|
+
try {
|
|
761
|
+
await awaitFragmentFinalization({
|
|
762
|
+
providers,
|
|
763
|
+
address,
|
|
764
|
+
txId,
|
|
765
|
+
txTimeoutMs,
|
|
766
|
+
circuitsOnChain: snapshot.circuits,
|
|
767
|
+
circuitsPending: remaining(all, snapshot.circuits),
|
|
768
|
+
});
|
|
769
|
+
}
|
|
770
|
+
catch (e) {
|
|
771
|
+
// Only a tx still in flight is worth settling on the next run.
|
|
772
|
+
// One the node ruled on is cleared, so a resume does not wait on it.
|
|
773
|
+
const stillInFlight = e instanceof FragmentDeployError && e.timedOut;
|
|
774
|
+
try {
|
|
775
|
+
await this.#recordProgress({
|
|
776
|
+
deployments,
|
|
777
|
+
record,
|
|
778
|
+
started,
|
|
779
|
+
snapshot,
|
|
780
|
+
all,
|
|
781
|
+
...(stillInFlight
|
|
782
|
+
? { pendingTxId: txId, pendingCircuits: attempt.batch }
|
|
783
|
+
: {}),
|
|
784
|
+
});
|
|
785
|
+
}
|
|
786
|
+
catch (writeFailure) {
|
|
787
|
+
// The insert failure is the one the operator has to act on, so the
|
|
788
|
+
// reporting write must not replace it.
|
|
789
|
+
s.logger.warn({ err: formatError(writeFailure) }, 'Could not record progress for the stopped insert');
|
|
790
|
+
}
|
|
791
|
+
throw e;
|
|
792
|
+
}
|
|
793
|
+
snapshot = await awaitCircuitsOnChain({
|
|
794
|
+
publicDataProvider: providers.publicDataProvider,
|
|
795
|
+
address,
|
|
796
|
+
expected: attempt.batch,
|
|
797
|
+
timeoutMs: txTimeoutMs,
|
|
798
|
+
});
|
|
799
|
+
const left = remaining(all, snapshot.circuits);
|
|
800
|
+
// Only when another insert follows; nothing is balanced after the
|
|
801
|
+
// last one.
|
|
802
|
+
if (left.length > 0) {
|
|
803
|
+
await awaitDustSettled({
|
|
804
|
+
wallet: s.wallet,
|
|
805
|
+
appliedBeyond: attempt.tip,
|
|
806
|
+
timeoutMs: txTimeoutMs,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
s.logger.info(`Inserted ${attempt.batch.length} circuit(s): ${snapshot.circuits.length}/${all.length} on chain, txId ${txId}`);
|
|
810
|
+
await this.#recordProgress({ deployments, record, started, snapshot, all });
|
|
811
|
+
return { snapshot, size: attempt.cap };
|
|
812
|
+
}
|
|
813
|
+
/** Refresh the `partial` head from a chain snapshot. Reporting only. */
|
|
814
|
+
async #recordProgress(args) {
|
|
815
|
+
const { deployments, record, started, snapshot, all, pendingTxId, pendingCircuits, } = args;
|
|
816
|
+
const progress = toPartialRecord({
|
|
817
|
+
address: started.address,
|
|
818
|
+
txId: started.txId,
|
|
819
|
+
deployer: record.deployer,
|
|
820
|
+
artifact: record.artifact,
|
|
821
|
+
circuits: all,
|
|
822
|
+
circuitsOnChain: snapshot.circuits,
|
|
823
|
+
submittedAt: record.submittedAt,
|
|
824
|
+
txHash: started.txHash,
|
|
825
|
+
blockHeight: started.blockHeight,
|
|
826
|
+
pendingTxId,
|
|
827
|
+
pendingCircuits,
|
|
828
|
+
});
|
|
829
|
+
await this.#persist(progress, () => deployments.updatePartial(this.#state.opts.contract, progress));
|
|
830
|
+
}
|
|
831
|
+
/** Log a "would deploy" event and return a synthetic result. No tx, no file. */
|
|
832
|
+
async dryRun() {
|
|
833
|
+
const s = this.#state;
|
|
834
|
+
s.logger.info({
|
|
835
|
+
contract: s.opts.contract,
|
|
836
|
+
network: s.networkName,
|
|
837
|
+
artifact: s.artifact.artifactPath,
|
|
838
|
+
argCount: s.args.length,
|
|
839
|
+
hasPrivateState: s.initialPrivateState !== undefined,
|
|
840
|
+
deployer: s.deployer,
|
|
841
|
+
}, 'dry-run: would deploy');
|
|
842
|
+
return {
|
|
843
|
+
contractName: s.opts.contract,
|
|
844
|
+
network: s.networkName,
|
|
845
|
+
address: '',
|
|
846
|
+
txHash: '',
|
|
847
|
+
txId: '',
|
|
848
|
+
blockHeight: 0,
|
|
849
|
+
deployer: s.deployer,
|
|
850
|
+
artifact: s.contract.artifact,
|
|
851
|
+
deploymentsFile: '',
|
|
852
|
+
dryRun: true,
|
|
853
|
+
explorerUrl: '',
|
|
854
|
+
fragments: 0,
|
|
855
|
+
circuits: s.artifact.circuitNames.length,
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* Log `record` at info, then write it. A ledger write can fail on a lock
|
|
860
|
+
* timeout, a permission error, or a corrupt `<network>.json`, none of which
|
|
861
|
+
* undo the tx, so the log line and the rethrown message both carry the
|
|
862
|
+
* on-chain identifiers.
|
|
863
|
+
*/
|
|
864
|
+
async #persist(record, write) {
|
|
865
|
+
this.#state.logger.info(record, `Deploy record (${record.status})`);
|
|
866
|
+
try {
|
|
867
|
+
return await write();
|
|
868
|
+
}
|
|
869
|
+
catch (e) {
|
|
870
|
+
// A refused record is the user's to fix, and its ConfigError already
|
|
871
|
+
// says how; wrapping it would hide the exit code and the instruction.
|
|
872
|
+
if (e instanceof ConfigError)
|
|
873
|
+
throw e;
|
|
874
|
+
throw new DeploymentsFileError(`Deploy of "${this.contractName}" was submitted but the deployments ledger write failed: ${formatError(e)}. Record it by hand: ${identifiers(record)}.`, { cause: e });
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
async [Symbol.asyncDispose]() {
|
|
878
|
+
await this.#state.resources.disposeAsync();
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
// ---------------------------------------------------------------------------
|
|
882
|
+
// Helpers
|
|
883
|
+
// ---------------------------------------------------------------------------
|
|
884
|
+
/** A fragment budget is a whole number of circuits, at least one. */
|
|
885
|
+
function assertBudget(budget) {
|
|
886
|
+
if (budget === undefined)
|
|
887
|
+
return;
|
|
888
|
+
if (!Number.isInteger(budget) || budget < 1) {
|
|
889
|
+
throw new ConfigError(`circuits_per_tx must be an integer >= 1; got ${budget}.`);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Verifier key bytes for `circuitId`. `Artifact.verifierKeys` refuses a bundle
|
|
894
|
+
* that is missing one, so every name in the plan has an entry.
|
|
895
|
+
*/
|
|
896
|
+
function keyOf(keys, circuitId) {
|
|
897
|
+
return keys.get(circuitId);
|
|
898
|
+
}
|
|
899
|
+
function identifiers(record) {
|
|
900
|
+
const base = `address ${record.address}, txId ${record.txId}`;
|
|
901
|
+
return record.status === 'confirmed'
|
|
902
|
+
? `${base}, txHash ${record.txHash}`
|
|
903
|
+
: base;
|
|
904
|
+
}
|
|
905
|
+
function resolveTargets(opts, config) {
|
|
906
|
+
const networkName = opts.network ?? config.defaultNetwork;
|
|
907
|
+
if (!networkName) {
|
|
908
|
+
throw new ConfigError('No network selected. Pass --network <name> or set [profile].default_network.');
|
|
909
|
+
}
|
|
910
|
+
return {
|
|
911
|
+
networkName,
|
|
912
|
+
network: config.network(networkName),
|
|
913
|
+
contract: config.contract(opts.contract),
|
|
914
|
+
};
|
|
915
|
+
}
|
|
916
|
+
//# sourceMappingURL=deployer.js.map
|