-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.ts
More file actions
253 lines (227 loc) · 10.1 KB
/
Copy pathconfig.ts
File metadata and controls
253 lines (227 loc) · 10.1 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
// Copyright (c) Aptos Labs
// SPDX-License-Identifier: Apache-2.0
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
const CONFIG_DIR = join(homedir(), '.ace');
const CONFIG_PATH = join(CONFIG_DIR, 'config.json');
export type Platform = 'gcp' | 'docker' | 'local';
/**
* Deployment mode:
* * `monolith` — one Cloud Run service does everything.
* * `microservices` — Maintainer (internal, min=max=1) + Handler (public,
* scales) pair, talking to each other via the Handler's
* service account + Maintainer's `/secrets`.
* * `metadata-management-only` — runtime is externally managed. The CLI
* persists credentials/on-chain metadata only and does
* not emit or run deployment commands.
*
* `docker` and `local` are always monolith today. Missing field defaults to
* `monolith` for backwards compat with v2.2.x-and-earlier tracked nodes.
*/
export type Mode = 'monolith' | 'microservices' | 'metadata-management-only';
export interface ChainRpcOverrides {
aptosMainnetApi?: string;
aptosMainnetApikey?: string;
aptosTestnetApi?: string;
aptosTestnetApikey?: string;
aptosLocalnetApi?: string;
aptosLocalnetApikey?: string;
aptosShelbynetApi?: string;
aptosShelbynetApikey?: string;
aptosShelbyPrivateBetaApi?: string;
aptosShelbyPrivateBetaApikey?: string;
solanaMainnetBetaRpc?: string;
solanaTestnetRpc?: string;
solanaDevnetRpc?: string;
}
export interface GcpConfig {
project: string;
region: string;
/** Monolith mode: name of the single Cloud Run service. Required iff mode='monolith'. */
serviceName?: string;
/** Microservices mode — internal-only Maintainer service. Required iff mode='microservices'. */
maintainerServiceName?: string;
/** Microservices mode — public Handler service. Required iff mode='microservices'. */
handlerServiceName?: string;
/** Microservices mode — Cloud Run autoscaling cap on the Handler. */
handlerMaxInstances?: number;
}
export interface DockerConfig {
containerName: string;
port: string;
}
export interface LocalConfig {
repoPath: string;
port: string;
pid?: number; // PID of the background process started by the CLI
logFile?: string; // Absolute path to the log file
logMaxMb?: number; // Max log file size in MB for logrotate config
}
/**
* An ACE deployment you administer. Persists the admin private key + RPC config.
* Created by `ace deployment new`; consumed by `ace deployment {update-contracts,edit,...}`.
*
* `aceAddr` and `adminAddress` are the same Aptos account in this codebase (the package
* is published *at* the admin address). They're stored as separate fields for clarity
* and so a future "delegated admin" deployment shape doesn't need a migration.
*/
export interface TrackedDeployment {
rpcUrl: string;
aceAddr: string;
adminAddress: string;
adminPrivateKey: string; // 0x-prefixed hex
sharedNodeApiKey?: string;
gasStationApiKey?: string;
alias?: string;
/** One of `mainnet | testnet | devnet | localnet | shelbynet | shelby-private-beta | custom`. Tags the deployment for display. */
network?: string;
/** Git tag (e.g. `v1.0.0`) at which the contracts were deployed; `deployment new` enforces strict tag+clean. */
deployedAtTag?: string;
/** Commit SHA at which the contracts were deployed. */
deployedAtCommit?: string;
/** ISO timestamp of the deploy (or last `update-contracts`). */
deployedAt?: string;
}
/** A node you control or watch. Network connection info is embedded directly. */
export interface TrackedNode {
// Network connection (formerly TrackedNetwork)
rpcUrl: string; // host-facing URL (used by the CLI)
nodeRpcUrl?: string; // node-facing URL (used by the container; differs from rpcUrl on localnet+Docker)
aceAddr: string;
rpcApiKey?: string;
// Node identity
accountAddr: string;
accountSk?: string;
pkeDk?: string;
pkeEk?: string;
alias?: string;
endpoint?: string;
image?: string;
platform?: Platform;
/** Deployment mode. Missing = `monolith` for backwards compat with older configs. */
mode?: Mode;
gcp?: GcpConfig;
docker?: DockerConfig;
local?: LocalConfig;
gasStationKey?: string;
chainRpc?: ChainRpcOverrides;
}
/** Helper: read the mode of a tracked node, defaulting to `monolith`. */
export function nodeMode(n: Pick<TrackedNode, 'mode'>): Mode {
return n.mode ?? 'monolith';
}
/**
* Per-network state for the load-test tool. `ace loadtest setup` populates this
* once per network (account generated, funded, ACL contract deployed); subsequent
* `ace loadtest run` invocations reuse it.
*/
export interface LoadTestState {
network: string; // 'testnet' | 'mainnet' | 'devnet' | 'localnet' | rpcLabel
rpcUrl: string;
rpcApiKey?: string;
accountAddr: string;
accountSk: string; // 0x-prefixed hex; needed by `aptos move publish`
/**
* Address the loadtest-acl Move module is published at. Same as `accountAddr`
* today — the contract is published from and at the test account. Stored
* explicitly so a future redesign with a separate admin doesn't migrate.
*
* Optional: `setup` writes the account-only partial state immediately after
* funding (so the funded account's private key is durable) and only fills in
* `contractAddr` after `aptos move publish` succeeds. A retried `setup` that
* finds an account-only entry resumes from the publish step.
*/
contractAddr?: string;
deployedAt?: string; // ISO timestamp of last publish
}
export interface Config {
defaultNode?: string;
defaultDeployment?: string;
nodes: Record<string, TrackedNode>;
deployments: Record<string, TrackedDeployment>;
/** Keyed by network name (`testnet`, `mainnet`, ...). At most one entry per network. */
loadtest?: Record<string, LoadTestState>;
}
// ── Key derivation ────────────────────────────────────────────────────────────
export function deriveRpcLabel(rpcUrl: string): string {
const url = rpcUrl.toLowerCase();
if (url.includes('shelbynet')) return 'shelbynet';
if (url.includes('shelby-private-beta')) return 'shelby-private-beta';
if (url.includes('mainnet')) return 'mainnet';
if (url.includes('testnet')) return 'testnet';
if (url.includes('devnet')) return 'devnet';
try {
const u = new URL(rpcUrl);
const isLocal = u.hostname === 'localhost' || u.hostname === '127.0.0.1';
if (isLocal) {
const port = u.port || '80';
return port === '8080' ? 'localnet' : `localnet:${port}`;
}
return u.port ? `${u.hostname}:${u.port}` : u.hostname;
} catch {
return rpcUrl;
}
}
export function makeNodeKey(rpcUrl: string, aceAddr: string, accountAddr: string): string {
return `${deriveRpcLabel(rpcUrl)}/${aceAddr}/${accountAddr}`;
}
export function displayNode(key: string, node: TrackedNode): string {
return node.alias ? `${key} (${node.alias})` : key;
}
export function makeDeploymentKey(rpcUrl: string, aceAddr: string): string {
return `${deriveRpcLabel(rpcUrl)}/${aceAddr}`;
}
export function displayDeployment(key: string, dep: TrackedDeployment): string {
return dep.alias ? `${key} (${dep.alias})` : key;
}
// ── Persistence ───────────────────────────────────────────────────────────────
export function loadConfig(): Config {
if (!existsSync(CONFIG_PATH)) return { nodes: {}, deployments: {} };
try {
const raw = JSON.parse(readFileSync(CONFIG_PATH, 'utf8')) as any;
// Migrate from old format: { networks: {...}, nodes: { networkKey, ... } }
if (raw.networks && raw.nodes) {
const migrated: Config = { defaultNode: raw.defaultNode, nodes: {}, deployments: {} };
for (const [nodeKey, n] of Object.entries(raw.nodes as Record<string, any>)) {
const net = (raw.networks as Record<string, any>)[n.networkKey];
if (!net) continue;
migrated.nodes[nodeKey] = {
rpcUrl: net.rpcUrl,
aceAddr: net.aceAddr,
rpcApiKey: n.rpcApiKey ?? net.rpcApiKey,
accountAddr: n.accountAddr,
accountSk: n.accountSk,
pkeDk: n.pkeDk,
pkeEk: n.pkeEk,
alias: n.alias,
endpoint: n.endpoint,
image: n.image,
platform: n.platform,
gcp: n.gcp,
docker: n.docker,
gasStationKey: n.gasStationKey,
};
}
saveConfig(migrated);
return migrated;
}
raw.nodes ??= {};
raw.deployments ??= {};
return raw as Config;
} catch {
return { nodes: {}, deployments: {} };
}
}
export function saveConfig(config: Config): void {
if (!existsSync(CONFIG_DIR)) mkdirSync(CONFIG_DIR, { recursive: true });
writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
}
export function resolveDefaultNode(config: Config): TrackedNode | undefined {
if (!config.defaultNode) return undefined;
return config.nodes[config.defaultNode];
}
export function resolveDefaultDeployment(config: Config): TrackedDeployment | undefined {
if (!config.defaultDeployment) return undefined;
return config.deployments[config.defaultDeployment];
}