@cogcoin/client 1.1.9 → 1.1.10
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/README.md +1 -1
- package/dist/bitcoind/managed-bitcoind-service-config.d.ts +13 -0
- package/dist/bitcoind/managed-bitcoind-service-config.js +165 -0
- package/dist/bitcoind/managed-bitcoind-service-lifecycle.d.ts +28 -0
- package/dist/bitcoind/managed-bitcoind-service-lifecycle.js +290 -0
- package/dist/bitcoind/managed-bitcoind-service-process.d.ts +8 -0
- package/dist/bitcoind/managed-bitcoind-service-process.js +48 -0
- package/dist/bitcoind/managed-bitcoind-service-replica.d.ts +8 -0
- package/dist/bitcoind/managed-bitcoind-service-replica.js +142 -0
- package/dist/bitcoind/managed-bitcoind-service-status.d.ts +42 -0
- package/dist/bitcoind/managed-bitcoind-service-status.js +178 -0
- package/dist/bitcoind/managed-bitcoind-service-types.d.ts +36 -0
- package/dist/bitcoind/managed-bitcoind-service-types.js +1 -0
- package/dist/bitcoind/service.d.ts +7 -63
- package/dist/bitcoind/service.js +7 -797
- package/dist/wallet/mining/engine-types.d.ts +1 -0
- package/dist/wallet/mining/engine-types.js +9 -1
- package/dist/wallet/mining/publish.js +3 -6
- package/dist/wallet/mining/runner.js +30 -18
- package/dist/wallet/mining/visualizer.js +7 -6
- package/dist/wallet/read/context.d.ts +4 -10
- package/dist/wallet/read/context.js +4 -227
- package/dist/wallet/read/local-state.d.ts +28 -0
- package/dist/wallet/read/local-state.js +233 -0
- package/dist/wallet/read/managed-bitcoind.d.ts +30 -0
- package/dist/wallet/read/managed-bitcoind.js +138 -0
- package/dist/wallet/read/managed-indexer.d.ts +23 -0
- package/dist/wallet/read/managed-indexer.js +87 -0
- package/dist/wallet/read/managed-services.d.ts +6 -21
- package/dist/wallet/read/managed-services.js +23 -196
- package/package.json +1 -1
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { access, constants } from "node:fs/promises";
|
|
2
|
+
import { normalizeWalletStateRecord, persistWalletCoinControlStateIfNeeded } from "../coin-control.js";
|
|
3
|
+
import { persistNormalizedWalletDescriptorStateIfNeeded } from "../descriptor-normalization.js";
|
|
4
|
+
import { attachOrStartManagedBitcoindService } from "../../bitcoind/service.js";
|
|
5
|
+
import { createRpcClient } from "../../bitcoind/node.js";
|
|
6
|
+
import { normalizeMiningStateRecord } from "../mining/state.js";
|
|
7
|
+
import { resolveWalletRootIdFromLocalArtifacts } from "../root-resolution.js";
|
|
8
|
+
import { resolveWalletRuntimePathsForTesting } from "../runtime.js";
|
|
9
|
+
import { extractWalletRootIdHintFromWalletStateEnvelope, loadRawWalletStateEnvelope, loadWalletState, } from "../state/storage.js";
|
|
10
|
+
import { createDefaultWalletSecretProvider, createWalletSecretReference, inspectClientPasswordSetupReadiness, } from "../state/provider.js";
|
|
11
|
+
import { describeClientPasswordLockedMessage, describeClientPasswordMigrationMessage, describeClientPasswordSetupMessage, } from "../state/client-password.js";
|
|
12
|
+
const defaultWalletLocalStateDeps = {
|
|
13
|
+
attachOrStartManagedBitcoindService,
|
|
14
|
+
createRpcClient,
|
|
15
|
+
};
|
|
16
|
+
function btcAmountToSats(value) {
|
|
17
|
+
return BigInt(Math.round(value * 100_000_000));
|
|
18
|
+
}
|
|
19
|
+
function isSpendableFundingUtxo(entry, fundingScriptPubKeyHex) {
|
|
20
|
+
return entry.scriptPubKey === fundingScriptPubKeyHex
|
|
21
|
+
&& entry.confirmations >= 1
|
|
22
|
+
&& entry.spendable !== false
|
|
23
|
+
&& entry.safe !== false;
|
|
24
|
+
}
|
|
25
|
+
async function pathExists(path) {
|
|
26
|
+
try {
|
|
27
|
+
await access(path, constants.F_OK);
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function isWalletAccessError(error) {
|
|
35
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
36
|
+
return message.startsWith("wallet_secret_missing_")
|
|
37
|
+
|| message.startsWith("wallet_secret_provider_")
|
|
38
|
+
|| message.startsWith("wallet_client_password_")
|
|
39
|
+
|| message === "wallet_state_legacy_envelope_unsupported";
|
|
40
|
+
}
|
|
41
|
+
function describeWalletAccessMessage(options) {
|
|
42
|
+
const message = options.accessError instanceof Error ? options.accessError.message : String(options.accessError ?? "");
|
|
43
|
+
if (message === "wallet_state_legacy_envelope_unsupported") {
|
|
44
|
+
return "Wallet state exists but was created by an older Cogcoin wallet format that this version no longer loads directly.";
|
|
45
|
+
}
|
|
46
|
+
if (message === "wallet_client_password_setup_required") {
|
|
47
|
+
return describeClientPasswordSetupMessage();
|
|
48
|
+
}
|
|
49
|
+
if (message === "wallet_client_password_migration_required") {
|
|
50
|
+
return describeClientPasswordMigrationMessage();
|
|
51
|
+
}
|
|
52
|
+
if (message === "wallet_client_password_locked") {
|
|
53
|
+
return describeClientPasswordLockedMessage();
|
|
54
|
+
}
|
|
55
|
+
if (message.startsWith("wallet_secret_provider_")) {
|
|
56
|
+
return "Wallet state exists but the local secret provider is unavailable.";
|
|
57
|
+
}
|
|
58
|
+
if (message.startsWith("wallet_secret_missing_")) {
|
|
59
|
+
return "Wallet state exists but its local secret-provider material is unavailable.";
|
|
60
|
+
}
|
|
61
|
+
return message.length > 0
|
|
62
|
+
? message
|
|
63
|
+
: "Wallet state exists but could not be loaded from the local secret provider.";
|
|
64
|
+
}
|
|
65
|
+
async function normalizeLoadedWalletStateForRead(options) {
|
|
66
|
+
if (options.dataDir === undefined) {
|
|
67
|
+
return options.loaded;
|
|
68
|
+
}
|
|
69
|
+
const node = await options.dependencies.attachOrStartManagedBitcoindService({
|
|
70
|
+
dataDir: options.dataDir,
|
|
71
|
+
chain: "main",
|
|
72
|
+
startHeight: 0,
|
|
73
|
+
walletRootId: options.loaded.state.walletRootId,
|
|
74
|
+
});
|
|
75
|
+
try {
|
|
76
|
+
const access = {
|
|
77
|
+
provider: options.access.provider,
|
|
78
|
+
secretReference: createWalletSecretReference(options.loaded.state.walletRootId),
|
|
79
|
+
};
|
|
80
|
+
const normalized = await persistNormalizedWalletDescriptorStateIfNeeded({
|
|
81
|
+
state: options.loaded.state,
|
|
82
|
+
access,
|
|
83
|
+
paths: options.paths,
|
|
84
|
+
nowUnixMs: options.now,
|
|
85
|
+
replacePrimary: options.loaded.source === "backup",
|
|
86
|
+
rpc: options.dependencies.createRpcClient(node.rpc),
|
|
87
|
+
});
|
|
88
|
+
const coinControl = await persistWalletCoinControlStateIfNeeded({
|
|
89
|
+
state: normalized.state,
|
|
90
|
+
access,
|
|
91
|
+
paths: options.paths,
|
|
92
|
+
nowUnixMs: options.now,
|
|
93
|
+
replacePrimary: (normalized.changed ? "primary" : options.loaded.source) === "backup",
|
|
94
|
+
rpc: options.dependencies.createRpcClient(node.rpc),
|
|
95
|
+
});
|
|
96
|
+
return {
|
|
97
|
+
source: coinControl.changed ? "primary" : normalized.changed ? "primary" : options.loaded.source,
|
|
98
|
+
state: coinControl.state,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
await node.stop?.().catch(() => undefined);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export async function inspectWalletLocalStateWithDependencies(options = {}, dependencies = defaultWalletLocalStateDeps) {
|
|
106
|
+
const paths = options.paths ?? resolveWalletRuntimePathsForTesting();
|
|
107
|
+
const now = options.now ?? Date.now();
|
|
108
|
+
const provider = options.secretProvider ?? createDefaultWalletSecretProvider();
|
|
109
|
+
const [hasPrimaryStateFile, hasBackupStateFile] = await Promise.all([
|
|
110
|
+
pathExists(paths.walletStatePath),
|
|
111
|
+
pathExists(paths.walletStateBackupPath),
|
|
112
|
+
]);
|
|
113
|
+
const clientPasswordReadiness = await inspectClientPasswordSetupReadiness(provider).catch(() => "ready");
|
|
114
|
+
if (!hasPrimaryStateFile && !hasBackupStateFile) {
|
|
115
|
+
return {
|
|
116
|
+
availability: "uninitialized",
|
|
117
|
+
clientPasswordReadiness,
|
|
118
|
+
unlockRequired: false,
|
|
119
|
+
walletRootId: null,
|
|
120
|
+
state: null,
|
|
121
|
+
source: null,
|
|
122
|
+
hasPrimaryStateFile,
|
|
123
|
+
hasBackupStateFile,
|
|
124
|
+
message: "Wallet state has not been initialized yet.",
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
if (clientPasswordReadiness !== "ready") {
|
|
128
|
+
const rawEnvelope = await loadRawWalletStateEnvelope({
|
|
129
|
+
primaryPath: paths.walletStatePath,
|
|
130
|
+
backupPath: paths.walletStateBackupPath,
|
|
131
|
+
}).catch(() => null);
|
|
132
|
+
if (rawEnvelope?.envelope.secretProvider == null) {
|
|
133
|
+
return {
|
|
134
|
+
availability: "local-state-corrupt",
|
|
135
|
+
clientPasswordReadiness: "ready",
|
|
136
|
+
unlockRequired: false,
|
|
137
|
+
walletRootId: extractWalletRootIdHintFromWalletStateEnvelope(rawEnvelope?.envelope ?? null),
|
|
138
|
+
state: null,
|
|
139
|
+
source: null,
|
|
140
|
+
hasPrimaryStateFile,
|
|
141
|
+
hasBackupStateFile,
|
|
142
|
+
message: "Wallet state exists but was created by an older Cogcoin wallet format that this version no longer loads directly.",
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
const resolvedRoot = await resolveWalletRootIdFromLocalArtifacts({
|
|
146
|
+
paths,
|
|
147
|
+
provider,
|
|
148
|
+
}).catch(() => null);
|
|
149
|
+
return {
|
|
150
|
+
availability: "local-state-corrupt",
|
|
151
|
+
clientPasswordReadiness,
|
|
152
|
+
unlockRequired: false,
|
|
153
|
+
walletRootId: resolvedRoot?.walletRootId ?? null,
|
|
154
|
+
state: null,
|
|
155
|
+
source: null,
|
|
156
|
+
hasPrimaryStateFile,
|
|
157
|
+
hasBackupStateFile,
|
|
158
|
+
message: clientPasswordReadiness === "migration-required"
|
|
159
|
+
? describeClientPasswordMigrationMessage()
|
|
160
|
+
: describeClientPasswordSetupMessage(),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const loaded = await loadWalletState({
|
|
165
|
+
primaryPath: paths.walletStatePath,
|
|
166
|
+
backupPath: paths.walletStateBackupPath,
|
|
167
|
+
}, {
|
|
168
|
+
provider,
|
|
169
|
+
});
|
|
170
|
+
const normalized = await normalizeLoadedWalletStateForRead({
|
|
171
|
+
loaded,
|
|
172
|
+
access: { provider },
|
|
173
|
+
dataDir: options.dataDir,
|
|
174
|
+
now,
|
|
175
|
+
paths,
|
|
176
|
+
dependencies,
|
|
177
|
+
});
|
|
178
|
+
return {
|
|
179
|
+
availability: "ready",
|
|
180
|
+
clientPasswordReadiness,
|
|
181
|
+
unlockRequired: false,
|
|
182
|
+
walletRootId: normalized.state.walletRootId,
|
|
183
|
+
state: normalizeWalletStateRecord({
|
|
184
|
+
...normalized.state,
|
|
185
|
+
miningState: normalizeMiningStateRecord(normalized.state.miningState),
|
|
186
|
+
}),
|
|
187
|
+
source: normalized.source,
|
|
188
|
+
hasPrimaryStateFile,
|
|
189
|
+
hasBackupStateFile,
|
|
190
|
+
message: null,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
const resolvedRoot = await resolveWalletRootIdFromLocalArtifacts({
|
|
195
|
+
paths,
|
|
196
|
+
provider,
|
|
197
|
+
}).catch(() => null);
|
|
198
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
199
|
+
return {
|
|
200
|
+
availability: "local-state-corrupt",
|
|
201
|
+
clientPasswordReadiness,
|
|
202
|
+
unlockRequired: message === "wallet_client_password_locked",
|
|
203
|
+
walletRootId: resolvedRoot?.walletRootId ?? null,
|
|
204
|
+
state: null,
|
|
205
|
+
source: null,
|
|
206
|
+
hasPrimaryStateFile,
|
|
207
|
+
hasBackupStateFile,
|
|
208
|
+
message: isWalletAccessError(error)
|
|
209
|
+
? describeWalletAccessMessage({ accessError: error })
|
|
210
|
+
: error instanceof Error
|
|
211
|
+
? error.message
|
|
212
|
+
: String(error),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export async function inspectWalletLocalState(options = {}) {
|
|
217
|
+
return inspectWalletLocalStateWithDependencies(options);
|
|
218
|
+
}
|
|
219
|
+
export async function readFundingSpendableSats(options) {
|
|
220
|
+
if (options.state === null || options.rpc === null) {
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
const state = options.state;
|
|
224
|
+
try {
|
|
225
|
+
const utxos = await options.rpc.listUnspent(state.managedCoreWallet.walletName, 1);
|
|
226
|
+
return utxos.reduce((sum, entry) => isSpendableFundingUtxo(entry, state.funding.scriptPubKeyHex)
|
|
227
|
+
? sum + btcAmountToSats(entry.amount)
|
|
228
|
+
: sum, 0n);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { loadBundledGenesisParameters } from "@cogcoin/indexer";
|
|
2
|
+
import type { ManagedWalletNodeConnection } from "../../bitcoind/managed-runtime/types.js";
|
|
3
|
+
import { createRpcClient } from "../../bitcoind/node.js";
|
|
4
|
+
import { attachOrStartManagedBitcoindService, probeManagedBitcoindService } from "../../bitcoind/service.js";
|
|
5
|
+
import type { ManagedBitcoindNodeHandle } from "../../bitcoind/types.js";
|
|
6
|
+
import { verifyManagedCoreWalletReplica } from "../lifecycle.js";
|
|
7
|
+
import type { WalletBitcoindStatus, WalletLocalStateStatus, WalletNodeStatus, WalletServiceHealth } from "./types.js";
|
|
8
|
+
export type ManagedWalletBitcoindReadDeps = {
|
|
9
|
+
loadBundledGenesisParameters: typeof loadBundledGenesisParameters;
|
|
10
|
+
probeManagedBitcoindService: typeof probeManagedBitcoindService;
|
|
11
|
+
attachOrStartManagedBitcoindService: typeof attachOrStartManagedBitcoindService;
|
|
12
|
+
createRpcClient: typeof createRpcClient;
|
|
13
|
+
verifyManagedCoreWalletReplica: typeof verifyManagedCoreWalletReplica;
|
|
14
|
+
};
|
|
15
|
+
export interface ManagedWalletBitcoindReadState {
|
|
16
|
+
node: ManagedWalletNodeConnection<ManagedBitcoindNodeHandle, ReturnType<typeof createRpcClient>>;
|
|
17
|
+
bitcoind: WalletBitcoindStatus;
|
|
18
|
+
nodeHealth: WalletServiceHealth;
|
|
19
|
+
nodeMessage: string | null;
|
|
20
|
+
}
|
|
21
|
+
export declare function deriveNodeHealthForTesting(status: WalletNodeStatus | null, bitcoindHealth: WalletBitcoindStatus["health"]): {
|
|
22
|
+
health: WalletServiceHealth;
|
|
23
|
+
message: string | null;
|
|
24
|
+
};
|
|
25
|
+
export declare function openManagedWalletBitcoindReadState(options: {
|
|
26
|
+
dataDir: string;
|
|
27
|
+
walletRootId: string;
|
|
28
|
+
localState: WalletLocalStateStatus;
|
|
29
|
+
startupTimeoutMs: number;
|
|
30
|
+
}, dependencies?: ManagedWalletBitcoindReadDeps): Promise<ManagedWalletBitcoindReadState>;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { loadBundledGenesisParameters } from "@cogcoin/indexer";
|
|
2
|
+
import { deriveManagedBitcoindWalletStatus, resolveManagedBitcoindProbeDecision, } from "../../bitcoind/managed-runtime/bitcoind-policy.js";
|
|
3
|
+
import { createRpcClient } from "../../bitcoind/node.js";
|
|
4
|
+
import { resolveCogcoinProcessingStartHeight } from "../../bitcoind/processing-start-height.js";
|
|
5
|
+
import { attachOrStartManagedBitcoindService, probeManagedBitcoindService, } from "../../bitcoind/service.js";
|
|
6
|
+
import { verifyManagedCoreWalletReplica } from "../lifecycle.js";
|
|
7
|
+
const TOLERATED_NODE_HEADER_LEAD_BLOCKS = 2;
|
|
8
|
+
const TOLERATED_NODE_HEADER_LEAD_MESSAGE = "Bitcoin headers can briefly lead validated blocks; a short 1-2 block lead is normal and is being tolerated.";
|
|
9
|
+
const NODE_CATCHING_UP_MESSAGE = "Bitcoin Core is still catching up to headers.";
|
|
10
|
+
const defaultManagedWalletBitcoindReadDeps = {
|
|
11
|
+
loadBundledGenesisParameters,
|
|
12
|
+
probeManagedBitcoindService,
|
|
13
|
+
attachOrStartManagedBitcoindService,
|
|
14
|
+
createRpcClient,
|
|
15
|
+
verifyManagedCoreWalletReplica,
|
|
16
|
+
};
|
|
17
|
+
function deriveNodeHealth(status, bitcoindHealth) {
|
|
18
|
+
if (bitcoindHealth !== "ready" || status === null || !status.ready) {
|
|
19
|
+
return {
|
|
20
|
+
health: "catching-up",
|
|
21
|
+
message: NODE_CATCHING_UP_MESSAGE,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const headerLead = status.nodeBestHeight !== null && status.nodeHeaderHeight !== null
|
|
25
|
+
? status.nodeHeaderHeight - status.nodeBestHeight
|
|
26
|
+
: null;
|
|
27
|
+
if (headerLead !== null && headerLead > 0) {
|
|
28
|
+
if (headerLead <= TOLERATED_NODE_HEADER_LEAD_BLOCKS) {
|
|
29
|
+
return {
|
|
30
|
+
health: "synced",
|
|
31
|
+
message: TOLERATED_NODE_HEADER_LEAD_MESSAGE,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
health: "catching-up",
|
|
36
|
+
message: NODE_CATCHING_UP_MESSAGE,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
health: "synced",
|
|
41
|
+
message: null,
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export function deriveNodeHealthForTesting(status, bitcoindHealth) {
|
|
45
|
+
return deriveNodeHealth(status, bitcoindHealth);
|
|
46
|
+
}
|
|
47
|
+
async function attachNodeStatus(options, dependencies) {
|
|
48
|
+
try {
|
|
49
|
+
const probe = await dependencies.probeManagedBitcoindService({
|
|
50
|
+
dataDir: options.dataDir,
|
|
51
|
+
chain: "main",
|
|
52
|
+
startHeight: 0,
|
|
53
|
+
walletRootId: options.walletRootId,
|
|
54
|
+
startupTimeoutMs: options.startupTimeoutMs,
|
|
55
|
+
});
|
|
56
|
+
const decision = resolveManagedBitcoindProbeDecision(probe);
|
|
57
|
+
if (decision.action === "reject") {
|
|
58
|
+
return {
|
|
59
|
+
handle: null,
|
|
60
|
+
rpc: null,
|
|
61
|
+
status: null,
|
|
62
|
+
observedStatus: probe.status,
|
|
63
|
+
error: decision.error,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
const genesis = await dependencies.loadBundledGenesisParameters();
|
|
67
|
+
const handle = await dependencies.attachOrStartManagedBitcoindService({
|
|
68
|
+
dataDir: options.dataDir,
|
|
69
|
+
chain: "main",
|
|
70
|
+
startHeight: resolveCogcoinProcessingStartHeight(genesis),
|
|
71
|
+
walletRootId: options.walletRootId,
|
|
72
|
+
startupTimeoutMs: options.startupTimeoutMs,
|
|
73
|
+
});
|
|
74
|
+
const rpc = dependencies.createRpcClient(handle.rpc);
|
|
75
|
+
const [chainInfo, serviceStatus] = await Promise.all([
|
|
76
|
+
rpc.getBlockchainInfo(),
|
|
77
|
+
handle.refreshServiceStatus?.(),
|
|
78
|
+
]);
|
|
79
|
+
const status = {
|
|
80
|
+
ready: true,
|
|
81
|
+
chain: chainInfo.chain,
|
|
82
|
+
pid: handle.pid,
|
|
83
|
+
walletRootId: handle.walletRootId ?? null,
|
|
84
|
+
nodeBestHeight: chainInfo.blocks,
|
|
85
|
+
nodeBestHashHex: chainInfo.bestblockhash,
|
|
86
|
+
nodeHeaderHeight: chainInfo.headers,
|
|
87
|
+
serviceUpdatedAtUnixMs: serviceStatus?.updatedAtUnixMs ?? null,
|
|
88
|
+
serviceStatus: serviceStatus ?? null,
|
|
89
|
+
walletReplica: serviceStatus?.walletReplica ?? null,
|
|
90
|
+
walletReplicaMessage: serviceStatus?.walletReplica?.message ?? null,
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
handle,
|
|
94
|
+
rpc,
|
|
95
|
+
status,
|
|
96
|
+
observedStatus: serviceStatus ?? null,
|
|
97
|
+
error: null,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
return {
|
|
102
|
+
handle: null,
|
|
103
|
+
rpc: null,
|
|
104
|
+
status: null,
|
|
105
|
+
observedStatus: null,
|
|
106
|
+
error: error instanceof Error ? error.message : String(error),
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
export async function openManagedWalletBitcoindReadState(options, dependencies = defaultManagedWalletBitcoindReadDeps) {
|
|
111
|
+
const node = await attachNodeStatus({
|
|
112
|
+
dataDir: options.dataDir,
|
|
113
|
+
walletRootId: options.walletRootId,
|
|
114
|
+
startupTimeoutMs: options.startupTimeoutMs,
|
|
115
|
+
}, dependencies);
|
|
116
|
+
if (options.localState.state !== null && node.status !== null) {
|
|
117
|
+
const verifiedReplica = await dependencies.verifyManagedCoreWalletReplica(options.localState.state, options.dataDir, {
|
|
118
|
+
nodeHandle: node.handle ?? undefined,
|
|
119
|
+
});
|
|
120
|
+
node.status = {
|
|
121
|
+
...node.status,
|
|
122
|
+
walletReplica: verifiedReplica,
|
|
123
|
+
walletReplicaMessage: verifiedReplica.message ?? null,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const bitcoind = deriveManagedBitcoindWalletStatus({
|
|
127
|
+
status: node.observedStatus,
|
|
128
|
+
nodeStatus: node.status,
|
|
129
|
+
startupError: node.error,
|
|
130
|
+
});
|
|
131
|
+
const nodeDerived = deriveNodeHealth(node.status, bitcoind.health);
|
|
132
|
+
return {
|
|
133
|
+
node,
|
|
134
|
+
bitcoind,
|
|
135
|
+
nodeHealth: nodeDerived.health,
|
|
136
|
+
nodeMessage: nodeDerived.message,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { attachOrStartIndexerDaemon, probeIndexerDaemon, readObservedIndexerDaemonStatus, readSnapshotWithRetry, type IndexerDaemonClient } from "../../bitcoind/indexer-daemon.js";
|
|
2
|
+
import type { ManagedBitcoindNodeHandle } from "../../bitcoind/types.js";
|
|
3
|
+
import type { WalletIndexerStatus, WalletSnapshotView } from "./types.js";
|
|
4
|
+
export type ManagedWalletIndexerReadDeps = {
|
|
5
|
+
probeIndexerDaemon: typeof probeIndexerDaemon;
|
|
6
|
+
attachOrStartIndexerDaemon: typeof attachOrStartIndexerDaemon;
|
|
7
|
+
readSnapshotWithRetry: typeof readSnapshotWithRetry;
|
|
8
|
+
readObservedIndexerDaemonStatus: typeof readObservedIndexerDaemonStatus;
|
|
9
|
+
};
|
|
10
|
+
export interface ManagedWalletIndexerReadState {
|
|
11
|
+
daemonClient: IndexerDaemonClient | null;
|
|
12
|
+
indexer: WalletIndexerStatus;
|
|
13
|
+
snapshot: WalletSnapshotView | null;
|
|
14
|
+
}
|
|
15
|
+
export declare function openManagedWalletIndexerReadState(options: {
|
|
16
|
+
dataDir: string;
|
|
17
|
+
databasePath: string;
|
|
18
|
+
walletRootId: string;
|
|
19
|
+
startupTimeoutMs: number;
|
|
20
|
+
expectedIndexerBinaryVersion: string | null;
|
|
21
|
+
now: number;
|
|
22
|
+
nodeHandle: ManagedBitcoindNodeHandle | null;
|
|
23
|
+
}, dependencies?: ManagedWalletIndexerReadDeps): Promise<ManagedWalletIndexerReadState>;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { deserializeIndexerState } from "@cogcoin/indexer";
|
|
2
|
+
import { attachOrStartIndexerDaemon, INDEXER_DAEMON_BACKGROUND_FOLLOW_RECOVERY_FAILED, probeIndexerDaemon, readObservedIndexerDaemonStatus, readSnapshotWithRetry, } from "../../bitcoind/indexer-daemon.js";
|
|
3
|
+
import { deriveManagedIndexerWalletStatus, resolveIndexerDaemonProbeDecision, } from "../../bitcoind/managed-runtime/indexer-policy.js";
|
|
4
|
+
const defaultManagedWalletIndexerReadDeps = {
|
|
5
|
+
probeIndexerDaemon,
|
|
6
|
+
attachOrStartIndexerDaemon,
|
|
7
|
+
readSnapshotWithRetry,
|
|
8
|
+
readObservedIndexerDaemonStatus,
|
|
9
|
+
};
|
|
10
|
+
export async function openManagedWalletIndexerReadState(options, dependencies = defaultManagedWalletIndexerReadDeps) {
|
|
11
|
+
let daemonClient = null;
|
|
12
|
+
let daemonStatus = null;
|
|
13
|
+
let observedDaemonStatus = null;
|
|
14
|
+
let snapshot = null;
|
|
15
|
+
let indexerSource = "none";
|
|
16
|
+
let daemonError = null;
|
|
17
|
+
try {
|
|
18
|
+
const probe = await dependencies.probeIndexerDaemon({
|
|
19
|
+
dataDir: options.dataDir,
|
|
20
|
+
walletRootId: options.walletRootId,
|
|
21
|
+
});
|
|
22
|
+
const probeDecision = resolveIndexerDaemonProbeDecision({
|
|
23
|
+
probe,
|
|
24
|
+
expectedBinaryVersion: options.expectedIndexerBinaryVersion,
|
|
25
|
+
});
|
|
26
|
+
if (probeDecision.action !== "reject") {
|
|
27
|
+
await probe.client?.close().catch(() => undefined);
|
|
28
|
+
daemonClient = await dependencies.attachOrStartIndexerDaemon({
|
|
29
|
+
dataDir: options.dataDir,
|
|
30
|
+
databasePath: options.databasePath,
|
|
31
|
+
walletRootId: options.walletRootId,
|
|
32
|
+
startupTimeoutMs: options.startupTimeoutMs,
|
|
33
|
+
ensureBackgroundFollow: true,
|
|
34
|
+
expectedBinaryVersion: options.expectedIndexerBinaryVersion,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
observedDaemonStatus = probe.status;
|
|
39
|
+
indexerSource = probe.status === null ? "none" : "probe";
|
|
40
|
+
daemonError = probeDecision.error;
|
|
41
|
+
}
|
|
42
|
+
if (daemonClient !== null) {
|
|
43
|
+
const lease = await dependencies.readSnapshotWithRetry(daemonClient, options.walletRootId);
|
|
44
|
+
daemonStatus = lease.status;
|
|
45
|
+
observedDaemonStatus = lease.status;
|
|
46
|
+
snapshot = {
|
|
47
|
+
tip: lease.payload.tip,
|
|
48
|
+
state: deserializeIndexerState(Buffer.from(lease.payload.stateBase64, "base64")),
|
|
49
|
+
source: "lease",
|
|
50
|
+
daemonInstanceId: lease.payload.daemonInstanceId,
|
|
51
|
+
snapshotSeq: lease.payload.snapshotSeq,
|
|
52
|
+
openedAtUnixMs: lease.payload.openedAtUnixMs,
|
|
53
|
+
};
|
|
54
|
+
indexerSource = "lease";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
daemonError = error instanceof Error ? error.message : String(error);
|
|
59
|
+
if (daemonError === INDEXER_DAEMON_BACKGROUND_FOLLOW_RECOVERY_FAILED) {
|
|
60
|
+
await daemonClient?.close().catch(() => undefined);
|
|
61
|
+
await options.nodeHandle?.stop().catch(() => undefined);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
if (observedDaemonStatus === null) {
|
|
65
|
+
observedDaemonStatus = await dependencies.readObservedIndexerDaemonStatus({
|
|
66
|
+
dataDir: options.dataDir,
|
|
67
|
+
walletRootId: options.walletRootId,
|
|
68
|
+
}).catch(() => null);
|
|
69
|
+
if (observedDaemonStatus !== null) {
|
|
70
|
+
indexerSource = "status-file";
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const indexer = deriveManagedIndexerWalletStatus({
|
|
75
|
+
daemonStatus,
|
|
76
|
+
observedStatus: observedDaemonStatus,
|
|
77
|
+
snapshot,
|
|
78
|
+
source: indexerSource,
|
|
79
|
+
now: options.now,
|
|
80
|
+
startupError: daemonError,
|
|
81
|
+
});
|
|
82
|
+
return {
|
|
83
|
+
daemonClient,
|
|
84
|
+
indexer,
|
|
85
|
+
snapshot,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
@@ -1,26 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { attachOrStartIndexerDaemon, probeIndexerDaemon, readObservedIndexerDaemonStatus, readSnapshotWithRetry, type IndexerDaemonClient } from "../../bitcoind/indexer-daemon.js";
|
|
1
|
+
import { type IndexerDaemonClient } from "../../bitcoind/indexer-daemon.js";
|
|
3
2
|
import type { ManagedWalletReadServiceBundle } from "../../bitcoind/managed-runtime/types.js";
|
|
4
3
|
import { createRpcClient } from "../../bitcoind/node.js";
|
|
5
|
-
import { attachOrStartManagedBitcoindService, probeManagedBitcoindService } from "../../bitcoind/service.js";
|
|
6
4
|
import type { ManagedBitcoindNodeHandle } from "../../bitcoind/types.js";
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
probeManagedBitcoindService: typeof probeManagedBitcoindService;
|
|
12
|
-
attachOrStartManagedBitcoindService: typeof attachOrStartManagedBitcoindService;
|
|
13
|
-
createRpcClient: typeof createRpcClient;
|
|
14
|
-
verifyManagedCoreWalletReplica: typeof verifyManagedCoreWalletReplica;
|
|
15
|
-
probeIndexerDaemon: typeof probeIndexerDaemon;
|
|
16
|
-
attachOrStartIndexerDaemon: typeof attachOrStartIndexerDaemon;
|
|
17
|
-
readSnapshotWithRetry: typeof readSnapshotWithRetry;
|
|
18
|
-
readObservedIndexerDaemonStatus: typeof readObservedIndexerDaemonStatus;
|
|
19
|
-
};
|
|
20
|
-
export declare function deriveNodeHealthForTesting(status: WalletNodeStatus | null, bitcoindHealth: WalletBitcoindStatus["health"]): {
|
|
21
|
-
health: WalletServiceHealth;
|
|
22
|
-
message: string | null;
|
|
23
|
-
};
|
|
5
|
+
import type { WalletLocalStateStatus } from "./types.js";
|
|
6
|
+
import { deriveNodeHealthForTesting, type ManagedWalletBitcoindReadDeps } from "./managed-bitcoind.js";
|
|
7
|
+
import { type ManagedWalletIndexerReadDeps } from "./managed-indexer.js";
|
|
8
|
+
type ManagedWalletReadServiceDeps = ManagedWalletBitcoindReadDeps & ManagedWalletIndexerReadDeps;
|
|
24
9
|
export declare function openManagedWalletReadServiceBundle(options: {
|
|
25
10
|
dataDir: string;
|
|
26
11
|
databasePath: string;
|
|
@@ -30,4 +15,4 @@ export declare function openManagedWalletReadServiceBundle(options: {
|
|
|
30
15
|
expectedIndexerBinaryVersion: string | null;
|
|
31
16
|
now: number;
|
|
32
17
|
}, dependencies?: ManagedWalletReadServiceDeps): Promise<ManagedWalletReadServiceBundle<ManagedBitcoindNodeHandle, ReturnType<typeof createRpcClient>, IndexerDaemonClient>>;
|
|
33
|
-
export {};
|
|
18
|
+
export { deriveNodeHealthForTesting };
|