Repository navigation
Expand file tree
/
Copy pathrollback-contracts.cjs
More file actions
372 lines (343 loc) · 12.2 KB
/
Copy pathrollback-contracts.cjs
File metadata and controls
372 lines (343 loc) · 12.2 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env node
'use strict';
/**
* rollback-contracts.cjs
*
* Rolls the backend back to a previous set of Soroban contract IDs.
*
* IMPORTANT — Soroban contracts are immutable:
* Once a contract is deployed to the Stellar/Soroban network it cannot be
* undeployed, upgraded in place, or deleted. "Rolling back a deployment"
* therefore does NOT mean removing the new contracts from the chain. It
* means pointing the backend (environment variables / Kubernetes ConfigMap)
* back at the *previous* contract IDs so the application talks to the older,
* known-good contract instances again.
*
* The newly deployed contracts simply become unused. They remain on-chain
* forever and can be re-adopted later by pointing the backend at them again.
*
* What this script does:
* 1. Reads a previous contract-ids.env backup file.
* 2. Verifies each old contract still exists on-chain (via Soroban RPC).
* 3. Updates the backend environment / ConfigMap to the previous contract IDs.
*
* Contract-ID keys are normalised to the canonical `STELLAR_CONTRACT_*` names
* the backend reads, so backups written by either deploy path are supported:
* STELLAR_CONTRACT_VAULT_REGISTRY (canonical; both deploy paths)
* VAULT_REGISTRY_CONTRACT_ID (legacy SDK deployer)
* vault_registry (legacy bash deployer)
*
* The canonical keys are managed in Git in `k8s/configmap.yaml` (as
* placeholders). After a contract deploy, update those keys in the ConfigMap
* and re-apply it so the running config matches the committed state:
*
* # 1. Deploy contracts and capture the new IDs (writes a backup file).
* node scripts/deploy-contracts.cjs
*
* # 2. Update the contract-ID keys in k8s/configmap.yaml to the new IDs.
* # (Edit the STELLAR_CONTRACT_* values, then commit the change.)
*
* # 3. Re-apply the ConfigMap so the cluster matches Git.
* kubectl apply -f k8s/configmap.yaml
*
* # 4. Restart the backend so it picks up the new values.
* kubectl rollout restart deployment/backend -n synapsvault
*
* To roll back, run this script with `--configmap` (see below) and then
* re-apply the ConfigMap from Git once the committed IDs are reverted.
*
* Usage:
* node scripts/rollback-contracts.cjs \
* --backup ./backups/contract-ids.env.2024-01-01T00-00-00Z \
* [--target-env .env] \
* [--configmap my-backend-config] \
* [--namespace synapsvault] \
* [--rpc-url https://soroban-testnet.stellar.org] \
* [--network testnet] \
* [--dry-run]
*
* Exit codes:
* 0 success
* 1 usage / configuration error
* 2 verification failure (one or more old contracts missing on-chain)
* 3 environment / ConfigMap update failure
*/
const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');
// ---------------------------------------------------------------------------
// Minimal .env parser (no external dependencies).
// ---------------------------------------------------------------------------
function parseEnvFile(contents) {
const result = {};
const lines = contents.split(/\r?\n/);
for (const rawLine of lines) {
let line = rawLine.trim();
if (!line || line.startsWith('#')) continue;
// Strip leading 'export ' prefix (handles bash-style export KEY=value)
if (line.startsWith('export ')) {
line = line.slice(7).trim();
}
const eq = line.indexOf('=');
if (eq === -1) continue;
const key = line.slice(0, eq).trim();
let value = line.slice(eq + 1).trim();
// Strip surrounding quotes if present.
if (
(value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'"))
) {
value = value.slice(1, -1);
}
result[key] = value;
}
return result;
}
function readEnvFile(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`Env file not found: ${filePath}`);
}
return parseEnvFile(fs.readFileSync(filePath, 'utf8'));
}
// ---------------------------------------------------------------------------
// Argument parsing.
// ---------------------------------------------------------------------------
function parseArgs(argv) {
const args = {
backup: null,
envFile: '.env',
configmap: null,
namespace: 'synapsvault',
rpcUrl: process.env.SOROBAN_RPC_URL || null,
network: process.env.STELLAR_NETWORK || 'testnet',
dryRun: false,
};
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
const next = () => {
if (i + 1 >= argv.length) {
throw new Error(`Missing value for argument: ${arg}`);
}
return argv[++i];
};
switch (arg) {
case '--backup':
args.backup = next();
break;
// Node >= 22 intercepts a literal `--env-file` anywhere in argv, so
// `--target-env` is the portable spelling; `--env-file` is kept for Node 20.
case '--target-env':
case '--env-file':
args.envFile = next();
break;
case '--configmap':
args.configmap = next();
break;
case '--namespace':
args.namespace = next();
break;
case '--rpc-url':
args.rpcUrl = next();
break;
case '--network':
args.network = next();
break;
case '--dry-run':
args.dryRun = true;
break;
case '--help':
case '-h':
printUsage();
process.exit(0);
break;
default:
throw new Error(`Unknown argument: ${arg}`);
}
}
if (!args.backup) {
throw new Error('Missing required argument: --backup <path>');
}
if (!args.rpcUrl) {
args.rpcUrl = DEFAULT_RPC_URLS[args.network];
if (!args.rpcUrl) {
throw new Error(`No default RPC URL for network "${args.network}"; pass --rpc-url`);
}
}
return args;
}
function printUsage() {
process.stdout.write(
[
'Usage: node scripts/rollback-contracts.cjs --backup <path> [options]',
'',
'Options:',
' --backup <path> Path to the previous contract-ids.env backup (required)',
' --target-env <path> Backend .env file to update (default: .env)',
' (alias: --env-file, which newer Node versions intercept)',
' --configmap <name> Kubernetes ConfigMap to update (optional)',
' --namespace <ns> Kubernetes namespace (default: synapsvault)',
' --rpc-url <url> Soroban RPC endpoint (default: $SOROBAN_RPC_URL, or the',
' public RPC for testnet/futurenet; required for mainnet)',
' --network <name> Stellar network name (testnet|mainnet|futurenet)',
' --dry-run Verify and report without writing changes',
' -h, --help Show this help',
'',
'Note: Soroban contracts are immutable. This script does not undeploy',
'anything; it reverts the backend contract-ID references only.',
'',
'After a contract deploy, update the STELLAR_CONTRACT_* keys in',
'k8s/configmap.yaml and re-apply it (kubectl apply -f k8s/configmap.yaml)',
'so the committed ConfigMap matches the running config.',
'',
].join('\n')
);
}
// ---------------------------------------------------------------------------
// Contract ID validation & on-chain verification.
// ---------------------------------------------------------------------------
const CONTRACT_ID_RE = /^C[A-Z2-7]{55}$/;
const DEFAULT_RPC_URLS = {
testnet: 'https://soroban-testnet.stellar.org',
futurenet: 'https://rpc-futurenet.stellar.org',
};
function isValidContractId(id) {
return typeof id === 'string' && CONTRACT_ID_RE.test(id);
}
// Canonical env var prefix for deployed contract IDs (the names the backend
// reads), e.g. `vault_registry` -> `STELLAR_CONTRACT_VAULT_REGISTRY`.
const CONTRACT_ENV_PREFIX = 'STELLAR_CONTRACT_';
// Legacy keys whose canonical target is not a plain prefix/suffix rewrite:
// `REGISTRY_CONTRACT_ID` is the older name for the vault registry contract
// (see docs/ENVIRONMENT-VARIABLES.md).
const CONTRACT_KEY_ALIASES = {
REGISTRY_CONTRACT_ID: `${CONTRACT_ENV_PREFIX}VAULT_REGISTRY`,
REGISTRY_CONTRACT: `${CONTRACT_ENV_PREFIX}VAULT_REGISTRY`,
};
/**
* Map any recognised contract-ID key to its canonical `STELLAR_CONTRACT_*`
* name, so backups written by any deploy path can be rolled back.
*
* Returns null when `key` is not a contract-ID reference.
*/
function canonicalContractKey(key, value) {
if (typeof key !== 'string') return null;
if (key.startsWith(CONTRACT_ENV_PREFIX)) {
const stem = key.slice(CONTRACT_ENV_PREFIX.length);
return stem ? key : null;
}
if (Object.prototype.hasOwnProperty.call(CONTRACT_KEY_ALIASES, key)) {
return CONTRACT_KEY_ALIASES[key];
}
for (const suffix of ['_CONTRACT_ID', '_CONTRACT']) {
if (key.endsWith(suffix)) {
const stem = key.slice(0, -suffix.length);
return stem ? `${CONTRACT_ENV_PREFIX}${stem}` : null;
}
}
// Bare lower-case contract names (legacy bash deployer), e.g. `vault_registry`.
if (/^[a-z][a-z0-9_]*$/.test(key) && isValidContractId(value)) {
return `${CONTRACT_ENV_PREFIX}${key.toUpperCase()}`;
}
return null;
}
function updateEnvFile(filePath, ids, dryRun) {
const content = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : '';
const seen = new Set();
const lines = content
.replace(/\n$/, '')
.split('\n')
.filter((line, i) => content || i > 0);
const updated = lines.map((line) => {
const match = line.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=/);
if (!match) return line;
const key = match[1];
const canonical = canonicalContractKey(key, parseEnvFile(line)[key]);
if (!canonical || !Object.hasOwn(ids, canonical)) return line;
if (key === canonical) seen.add(key);
return `${key}=${ids[canonical]}`;
});
for (const [key, value] of Object.entries(ids)) {
if (!seen.has(key)) updated.push(`${key}=${value}`);
}
if (dryRun) {
console.log(`[dry-run] Would update ${path.resolve(filePath)}`);
return;
}
// Rename a protected temporary file so interrupted writes leave the original intact.
const temp = `${filePath}.${process.pid}.tmp`;
try {
fs.writeFileSync(temp, updated.join('\n') + '\n', { mode: 0o600, flag: 'wx' });
fs.renameSync(temp, filePath);
} finally {
if (fs.existsSync(temp)) fs.unlinkSync(temp);
}
}
async function main() {
const args = parseArgs(process.argv.slice(2));
const ids = {};
for (const [key, value] of Object.entries(readEnvFile(args.backup))) {
const canonical = canonicalContractKey(key, value);
if (!canonical) continue;
if (!/^STELLAR_CONTRACT_[A-Z0-9_]+$/.test(canonical) || !isValidContractId(value)) {
throw new Error(`Invalid contract reference: ${key}`);
}
if (ids[canonical] && ids[canonical] !== value)
throw new Error(`Conflicting IDs for ${canonical}`);
ids[canonical] = value;
}
if (!Object.keys(ids).length) throw new Error('Backup contains no contract IDs');
const { rpc, xdr } = require('@stellar/stellar-sdk');
const server = new rpc.Server(args.rpcUrl, { allowHttp: args.rpcUrl.startsWith('http://') });
try {
for (const id of Object.values(ids)) {
await server.getContractData(
id,
xdr.ScVal.scvLedgerKeyContractInstance(),
rpc.Durability.Persistent
);
}
} catch (err) {
process.exitCode = 2;
throw new Error(`On-chain verification failed: ${err.message}`);
}
try {
if (args.configmap && !args.dryRun) {
execFileSync(
'kubectl',
[
'--namespace',
args.namespace,
'patch',
'configmap',
args.configmap,
'--type=merge',
'-p',
JSON.stringify({ data: ids }),
],
{ stdio: 'inherit' }
);
}
updateEnvFile(args.envFile, ids, args.dryRun);
} catch (err) {
process.exitCode = 3;
throw err;
}
console.log(
args.dryRun
? 'Rollback verified (dry run).'
: 'Contract references restored; restart the backend to apply.'
);
}
if (require.main === module) {
main().catch((err) => {
console.error(err.message);
process.exitCode ||= 1;
});
}
module.exports = {
parseEnvFile,
isValidContractId,
canonicalContractKey,
parseArgs,
updateEnvFile,
};