@flop-labs/sdk 0.9.0 → 0.11.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 +81 -0
- package/README.md +2 -2
- package/dist/agent-wallet.d.ts +12 -1
- package/dist/agent-wallet.d.ts.map +1 -1
- package/dist/agent-wallet.js +87 -8
- package/dist/agent-wallet.js.map +1 -1
- package/dist/bundle/index.esm.js +1 -1
- package/dist/bundle/index.esm.js.map +1 -1
- package/dist/bundle/index.umd.cjs +1 -1
- package/dist/bundle/index.umd.cjs.map +1 -1
- package/dist/chain.d.ts +2 -1
- package/dist/chain.d.ts.map +1 -1
- package/dist/chain.js +7 -4
- package/dist/chain.js.map +1 -1
- package/dist/client.d.ts +2 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +4 -1
- package/dist/client.js.map +1 -1
- package/dist/compute-channel/agent-driver.d.ts.map +1 -1
- package/dist/compute-channel/agent-driver.js +4 -1
- package/dist/compute-channel/agent-driver.js.map +1 -1
- package/dist/compute-channel/channel-price.d.ts +34 -0
- package/dist/compute-channel/channel-price.d.ts.map +1 -0
- package/dist/compute-channel/channel-price.js +63 -0
- package/dist/compute-channel/channel-price.js.map +1 -0
- package/dist/compute-channel/deepseek-v41.d.ts +2 -2
- package/dist/compute-channel/direct-session-chain.d.ts.map +1 -1
- package/dist/compute-channel/direct-session-chain.js +6 -1
- package/dist/compute-channel/direct-session-chain.js.map +1 -1
- package/dist/compute-channel/direct-session.d.ts +2 -0
- package/dist/compute-channel/direct-session.d.ts.map +1 -1
- package/dist/compute-channel/direct-session.js +2 -2
- package/dist/compute-channel/direct-session.js.map +1 -1
- package/dist/compute-channel/index.d.ts +2 -0
- package/dist/compute-channel/index.d.ts.map +1 -1
- package/dist/compute-channel/index.js +2 -0
- package/dist/compute-channel/index.js.map +1 -1
- package/dist/compute-channel/long-session-chain.d.ts.map +1 -1
- package/dist/compute-channel/long-session-chain.js +52 -7
- package/dist/compute-channel/long-session-chain.js.map +1 -1
- package/dist/compute-channel/long-session.d.ts +25 -5
- package/dist/compute-channel/long-session.d.ts.map +1 -1
- package/dist/compute-channel/long-session.js +66 -30
- package/dist/compute-channel/long-session.js.map +1 -1
- package/dist/compute-channel/model-config.js +2 -2
- package/dist/compute-channel/model-config.js.map +1 -1
- package/dist/compute-channel/session-estimate.d.ts +3 -1
- package/dist/compute-channel/session-estimate.d.ts.map +1 -1
- package/dist/compute-channel/session-estimate.js +7 -2
- package/dist/compute-channel/session-estimate.js.map +1 -1
- package/dist/compute-channel/session.d.ts +16 -2
- package/dist/compute-channel/session.d.ts.map +1 -1
- package/dist/compute-channel/session.js +34 -10
- package/dist/compute-channel/session.js.map +1 -1
- package/dist/compute-channel/toploc-completion-rpc.d.ts.map +1 -1
- package/dist/compute-channel/toploc-completion-rpc.js +3 -1
- package/dist/compute-channel/toploc-completion-rpc.js.map +1 -1
- package/dist/compute-channel/transcript.d.ts +5 -2
- package/dist/compute-channel/transcript.d.ts.map +1 -1
- package/dist/compute-channel/transcript.js +6 -3
- package/dist/compute-channel/transcript.js.map +1 -1
- package/dist/compute-channel/warm-pool.d.ts +127 -0
- package/dist/compute-channel/warm-pool.d.ts.map +1 -0
- package/dist/compute-channel/warm-pool.js +169 -0
- package/dist/compute-channel/warm-pool.js.map +1 -0
- package/dist/connect.d.ts +19 -2
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +76 -9
- package/dist/connect.js.map +1 -1
- package/dist/faucet.d.ts +7 -4
- package/dist/faucet.d.ts.map +1 -1
- package/dist/faucet.js +26 -5
- package/dist/faucet.js.map +1 -1
- package/dist/generated-client/profile.d.ts.map +1 -1
- package/dist/generated-client/profile.js +13 -2
- package/dist/generated-client/profile.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/runtime-compatibility.d.ts +1 -1
- package/dist/runtime-compatibility.d.ts.map +1 -1
- package/dist/runtime-compatibility.js +7 -2
- package/dist/runtime-compatibility.js.map +1 -1
- package/dist/wallet-manager.d.ts +17 -1
- package/dist/wallet-manager.d.ts.map +1 -1
- package/dist/wallet-manager.js +127 -57
- package/dist/wallet-manager.js.map +1 -1
- package/dist/wallets/burner.d.ts +1 -1
- package/dist/wallets/burner.d.ts.map +1 -1
- package/dist/wallets/burner.js +6 -2
- package/dist/wallets/burner.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { ComputeChannelChainClient, SettledResult, TimedOutResult } from "./chain.js";
|
|
2
|
+
import { type SessionLineage } from "./lineage.js";
|
|
3
|
+
import type { CosignedCheckpoint } from "./miner-host.js";
|
|
4
|
+
import type { ChannelSession } from "./session.js";
|
|
5
|
+
import type { Hash } from "./transcript.js";
|
|
6
|
+
import { TurnTimeoutError, type MinerSessionInfo } from "./transport-http.js";
|
|
7
|
+
/** A standby the agent could reopen with, as advertised by its `/session/info`. */
|
|
8
|
+
export interface StandbyCandidate {
|
|
9
|
+
/** Session endpoint the caller's `openStandby` connects to. */
|
|
10
|
+
url: string;
|
|
11
|
+
info: MinerSessionInfo;
|
|
12
|
+
}
|
|
13
|
+
/** What a standby must serve to continue the primary's session. */
|
|
14
|
+
export interface StandbyRequirement {
|
|
15
|
+
modelHash: Hash;
|
|
16
|
+
measuredRoot: Hash;
|
|
17
|
+
decodePolicyHash: Hash;
|
|
18
|
+
precision: number;
|
|
19
|
+
/** The failed primary's miner address; never selected as its own standby. */
|
|
20
|
+
primaryMiner: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Ordered standby set: candidates whose measured root, model, decode policy and
|
|
24
|
+
* precision match exactly and that report themselves ready (and, when they report
|
|
25
|
+
* calibration, calibrated). Keeps the caller's order, which carries its price and
|
|
26
|
+
* reputation ranking; drops the primary and repeated miners.
|
|
27
|
+
*/
|
|
28
|
+
export declare function selectStandbys(requirement: StandbyRequirement, candidates: readonly StandbyCandidate[]): StandbyCandidate[];
|
|
29
|
+
/** The primary's state at the moment it was declared dead. */
|
|
30
|
+
export interface FrozenPrimary {
|
|
31
|
+
channelId: Hash;
|
|
32
|
+
/** `R_k`: the highest receipt the agent co-signed; `null` before the first. */
|
|
33
|
+
checkpoint: CosignedCheckpoint | null;
|
|
34
|
+
/** Turns the agent verified; may exceed `checkpoint` by unacknowledged turns. */
|
|
35
|
+
turnsVerified: number;
|
|
36
|
+
}
|
|
37
|
+
/** Snapshot the primary; nothing past the returned checkpoint is payable on chain. */
|
|
38
|
+
export declare function freezePrimary(primary: ChannelSession): FrozenPrimary;
|
|
39
|
+
/** The finalized on-chain fields the old-channel watcher needs. */
|
|
40
|
+
export type OldChannelState = {
|
|
41
|
+
state: "Open";
|
|
42
|
+
openedAt: bigint;
|
|
43
|
+
maxDuration: bigint;
|
|
44
|
+
} | {
|
|
45
|
+
state: "Settling";
|
|
46
|
+
challengeEnd: bigint;
|
|
47
|
+
} | {
|
|
48
|
+
state: "Closed";
|
|
49
|
+
};
|
|
50
|
+
export interface OldChannelReader {
|
|
51
|
+
/** Channel state at the finalized head; `channel: null` once it no longer exists. */
|
|
52
|
+
channel(channelId: Hash): Promise<{
|
|
53
|
+
block: bigint;
|
|
54
|
+
channel: OldChannelState | null;
|
|
55
|
+
}>;
|
|
56
|
+
}
|
|
57
|
+
export type OldChannelAction = "wait" | "timeout" | "finalize" | "done";
|
|
58
|
+
/**
|
|
59
|
+
* Next agent step for an abandoned channel, mirroring the pallet's guards:
|
|
60
|
+
* `timeout` once the finalized head passes `openedAt + maxDuration` with nothing
|
|
61
|
+
* posted, `finalize` once a posted settlement's challenge window has closed.
|
|
62
|
+
* The agent never posts `force_settle` for the dead miner; that is the miner's or
|
|
63
|
+
* its watcher's claim, and it supersedes a timeout until the deadline passes.
|
|
64
|
+
*/
|
|
65
|
+
export declare function oldChannelAction(block: bigint, channel: OldChannelState | null): OldChannelAction;
|
|
66
|
+
export interface OldChannelWatch {
|
|
67
|
+
chain: ComputeChannelChainClient;
|
|
68
|
+
reader: OldChannelReader;
|
|
69
|
+
/** Delay between finalized-head reads. */
|
|
70
|
+
pause: () => Promise<void>;
|
|
71
|
+
}
|
|
72
|
+
/** Watch the old channel until it closes; returns the agent's closing call, if any. */
|
|
73
|
+
export declare function watchOldChannel(channelId: Hash, watch: OldChannelWatch): Promise<TimedOutResult | SettledResult | null>;
|
|
74
|
+
/** Every selected standby failed to open; the dead primary is never retried. */
|
|
75
|
+
export declare class StandbyExhaustedError extends Error {
|
|
76
|
+
readonly frozen: FrozenPrimary;
|
|
77
|
+
readonly attempts: Array<{
|
|
78
|
+
minerAddress: string;
|
|
79
|
+
error: unknown;
|
|
80
|
+
}>;
|
|
81
|
+
/** The old-channel watch, still running, when one was requested. */
|
|
82
|
+
readonly oldChannelClosed?: Promise<TimedOutResult | SettledResult | null> | undefined;
|
|
83
|
+
constructor(frozen: FrozenPrimary, attempts: Array<{
|
|
84
|
+
minerAddress: string;
|
|
85
|
+
error: unknown;
|
|
86
|
+
}>,
|
|
87
|
+
/** The old-channel watch, still running, when one was requested. */
|
|
88
|
+
oldChannelClosed?: Promise<TimedOutResult | SettledResult | null> | undefined);
|
|
89
|
+
}
|
|
90
|
+
/** The standby channel opened but context replay failed; it is the caller's live channel now. */
|
|
91
|
+
export declare class StandbyReplayError extends Error {
|
|
92
|
+
readonly frozen: FrozenPrimary;
|
|
93
|
+
readonly standby: StandbyCandidate;
|
|
94
|
+
readonly session: ChannelSession;
|
|
95
|
+
readonly error: unknown;
|
|
96
|
+
readonly oldChannelClosed?: Promise<TimedOutResult | SettledResult | null> | undefined;
|
|
97
|
+
constructor(frozen: FrozenPrimary, standby: StandbyCandidate, session: ChannelSession, error: unknown, oldChannelClosed?: Promise<TimedOutResult | SettledResult | null> | undefined);
|
|
98
|
+
}
|
|
99
|
+
export interface ReopenOptions {
|
|
100
|
+
primary: ChannelSession;
|
|
101
|
+
/** The primary's idle timeout. Any other failure is not a reopen trigger. */
|
|
102
|
+
cause: TurnTimeoutError;
|
|
103
|
+
requirement: StandbyRequirement;
|
|
104
|
+
candidates: readonly StandbyCandidate[];
|
|
105
|
+
/** Open (or force-open + ack) a fresh channel with `standby` and attach a session to it. */
|
|
106
|
+
openStandby(standby: StandbyCandidate): Promise<ChannelSession>;
|
|
107
|
+
/** Transcript turns or one signed summary, replayed as new turns 0.. of the new channel. */
|
|
108
|
+
replay: readonly Uint8Array[];
|
|
109
|
+
/** Start {@link watchOldChannel} on the primary's channel. */
|
|
110
|
+
oldChannel?: OldChannelWatch;
|
|
111
|
+
/** Publish lineage here once the new channel is indexed; failure is reported, not thrown. */
|
|
112
|
+
lineageIndexerUrl?: string;
|
|
113
|
+
}
|
|
114
|
+
export interface ReopenResult {
|
|
115
|
+
frozen: FrozenPrimary;
|
|
116
|
+
standby: StandbyCandidate;
|
|
117
|
+
session: ChannelSession;
|
|
118
|
+
replayOutputs: Uint8Array[];
|
|
119
|
+
/** `null` when the primary died before any co-signed receipt. */
|
|
120
|
+
lineage: SessionLineage | null;
|
|
121
|
+
lineagePublishError?: unknown;
|
|
122
|
+
/** Present when `oldChannel` was given; resolves when the old channel closes. */
|
|
123
|
+
oldChannelClosed?: Promise<TimedOutResult | SettledResult | null>;
|
|
124
|
+
}
|
|
125
|
+
/** Freeze the primary, start the old-channel watch, and continue on the first standby that opens. */
|
|
126
|
+
export declare function reopenOnStandby(opts: ReopenOptions): Promise<ReopenResult>;
|
|
127
|
+
//# sourceMappingURL=warm-pool.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"warm-pool.d.ts","sourceRoot":"","sources":["../../src/compute-channel/warm-pool.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,yBAAyB,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC3F,OAAO,EAAyC,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AAC1F,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM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Aa,GAAG,IAAI,CAAC,CAAC;CACnE;AAED,qGAAqG;AACrG,wBAAsB,eAAe,CAAC,IAAI,EAAE,aAAa,GAAG,OAAO,CAAC,YAAY,CAAC,CAoDhF"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file v1 off-chain warm-pool reopen (ENG-48, `whitepaper/research/session-migration-semantics.md`).
|
|
3
|
+
* @module sdk/typescript/compute-channel/warm-pool
|
|
4
|
+
*
|
|
5
|
+
* When the primary miner misses its per-turn idle deadline ({@link TurnTimeoutError}),
|
|
6
|
+
* the agent freezes the last receipt it co-signed, leaves the old channel to settle
|
|
7
|
+
* or time out on its own escrow, opens a new channel with the first available
|
|
8
|
+
* standby, and replays context there from turn 0. Nothing here migrates escrow:
|
|
9
|
+
* old and new channels stay independent, and lineage is unverified metadata.
|
|
10
|
+
*/
|
|
11
|
+
import { u8aEq, u8aToHex } from "@polkadot/util";
|
|
12
|
+
import { publishSessionLineage, sessionLineage } from "./lineage.js";
|
|
13
|
+
import { TurnTimeoutError } from "./transport-http.js";
|
|
14
|
+
/**
|
|
15
|
+
* Ordered standby set: candidates whose measured root, model, decode policy and
|
|
16
|
+
* precision match exactly and that report themselves ready (and, when they report
|
|
17
|
+
* calibration, calibrated). Keeps the caller's order, which carries its price and
|
|
18
|
+
* reputation ranking; drops the primary and repeated miners.
|
|
19
|
+
*/
|
|
20
|
+
export function selectStandbys(requirement, candidates) {
|
|
21
|
+
const seen = new Set([requirement.primaryMiner]);
|
|
22
|
+
const selected = [];
|
|
23
|
+
for (const candidate of candidates) {
|
|
24
|
+
const { info } = candidate;
|
|
25
|
+
if (seen.has(info.minerAddress))
|
|
26
|
+
continue;
|
|
27
|
+
if (!info.isReady || (info.calibration && !info.calibration.ready))
|
|
28
|
+
continue;
|
|
29
|
+
if (!u8aEq(info.measuredRoot, requirement.measuredRoot) ||
|
|
30
|
+
!u8aEq(info.modelHash, requirement.modelHash) ||
|
|
31
|
+
!u8aEq(info.decodePolicyHash, requirement.decodePolicyHash) ||
|
|
32
|
+
info.precision !== requirement.precision) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
seen.add(info.minerAddress);
|
|
36
|
+
selected.push(candidate);
|
|
37
|
+
}
|
|
38
|
+
return selected;
|
|
39
|
+
}
|
|
40
|
+
/** Snapshot the primary; nothing past the returned checkpoint is payable on chain. */
|
|
41
|
+
export function freezePrimary(primary) {
|
|
42
|
+
return {
|
|
43
|
+
channelId: primary.channelId,
|
|
44
|
+
checkpoint: primary.lastCosignedCheckpoint(),
|
|
45
|
+
turnsVerified: primary.turnsVerified(),
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Next agent step for an abandoned channel, mirroring the pallet's guards:
|
|
50
|
+
* `timeout` once the finalized head passes `openedAt + maxDuration` with nothing
|
|
51
|
+
* posted, `finalize` once a posted settlement's challenge window has closed.
|
|
52
|
+
* The agent never posts `force_settle` for the dead miner; that is the miner's or
|
|
53
|
+
* its watcher's claim, and it supersedes a timeout until the deadline passes.
|
|
54
|
+
*/
|
|
55
|
+
export function oldChannelAction(block, channel) {
|
|
56
|
+
if (!channel || channel.state === "Closed")
|
|
57
|
+
return "done";
|
|
58
|
+
if (channel.state === "Settling")
|
|
59
|
+
return block >= channel.challengeEnd ? "finalize" : "wait";
|
|
60
|
+
return block > channel.openedAt + channel.maxDuration ? "timeout" : "wait";
|
|
61
|
+
}
|
|
62
|
+
/** Watch the old channel until it closes; returns the agent's closing call, if any. */
|
|
63
|
+
export async function watchOldChannel(channelId, watch) {
|
|
64
|
+
for (;;) {
|
|
65
|
+
const { block, channel } = await watch.reader.channel(channelId);
|
|
66
|
+
switch (oldChannelAction(block, channel)) {
|
|
67
|
+
case "done":
|
|
68
|
+
return null;
|
|
69
|
+
case "timeout":
|
|
70
|
+
return watch.chain.timeout(channelId);
|
|
71
|
+
case "finalize":
|
|
72
|
+
return watch.chain.finalize(channelId);
|
|
73
|
+
case "wait":
|
|
74
|
+
await watch.pause();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Every selected standby failed to open; the dead primary is never retried. */
|
|
79
|
+
export class StandbyExhaustedError extends Error {
|
|
80
|
+
frozen;
|
|
81
|
+
attempts;
|
|
82
|
+
oldChannelClosed;
|
|
83
|
+
constructor(frozen, attempts,
|
|
84
|
+
/** The old-channel watch, still running, when one was requested. */
|
|
85
|
+
oldChannelClosed) {
|
|
86
|
+
super(attempts.length === 0
|
|
87
|
+
? "no compatible available standby"
|
|
88
|
+
: `all ${attempts.length} compatible standbys failed to open`);
|
|
89
|
+
this.frozen = frozen;
|
|
90
|
+
this.attempts = attempts;
|
|
91
|
+
this.oldChannelClosed = oldChannelClosed;
|
|
92
|
+
this.name = "StandbyExhaustedError";
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** The standby channel opened but context replay failed; it is the caller's live channel now. */
|
|
96
|
+
export class StandbyReplayError extends Error {
|
|
97
|
+
frozen;
|
|
98
|
+
standby;
|
|
99
|
+
session;
|
|
100
|
+
error;
|
|
101
|
+
oldChannelClosed;
|
|
102
|
+
constructor(frozen, standby, session, error, oldChannelClosed) {
|
|
103
|
+
super(`context replay on standby ${standby.info.minerAddress} failed`);
|
|
104
|
+
this.frozen = frozen;
|
|
105
|
+
this.standby = standby;
|
|
106
|
+
this.session = session;
|
|
107
|
+
this.error = error;
|
|
108
|
+
this.oldChannelClosed = oldChannelClosed;
|
|
109
|
+
this.name = "StandbyReplayError";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/** Freeze the primary, start the old-channel watch, and continue on the first standby that opens. */
|
|
113
|
+
export async function reopenOnStandby(opts) {
|
|
114
|
+
if (!(opts.cause instanceof TurnTimeoutError)) {
|
|
115
|
+
throw new Error("warm-pool reopen requires the primary's TurnTimeoutError");
|
|
116
|
+
}
|
|
117
|
+
const frozen = freezePrimary(opts.primary);
|
|
118
|
+
let oldChannelClosed;
|
|
119
|
+
if (opts.oldChannel) {
|
|
120
|
+
oldChannelClosed = watchOldChannel(frozen.channelId, opts.oldChannel);
|
|
121
|
+
// The caller owns this promise; keep an ignored rejection from crashing the process.
|
|
122
|
+
oldChannelClosed.catch(() => undefined);
|
|
123
|
+
}
|
|
124
|
+
const attempts = [];
|
|
125
|
+
for (const standby of selectStandbys(opts.requirement, opts.candidates)) {
|
|
126
|
+
let session;
|
|
127
|
+
try {
|
|
128
|
+
session = await opts.openStandby(standby);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
attempts.push({ minerAddress: standby.info.minerAddress, error });
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// A reopen is a new channel with fresh counters; anything else would let old
|
|
135
|
+
// receipts or turn indexes carry over.
|
|
136
|
+
if (u8aEq(session.channelId, frozen.channelId) || session.turnsVerified() !== 0) {
|
|
137
|
+
throw new Error(`standby session ${u8aToHex(session.channelId)} is not a fresh channel`);
|
|
138
|
+
}
|
|
139
|
+
const replayOutputs = [];
|
|
140
|
+
try {
|
|
141
|
+
for (const input of opts.replay)
|
|
142
|
+
replayOutputs.push(await session.turn(input));
|
|
143
|
+
}
|
|
144
|
+
catch (error) {
|
|
145
|
+
throw new StandbyReplayError(frozen, standby, session, error, oldChannelClosed);
|
|
146
|
+
}
|
|
147
|
+
const lineage = frozen.checkpoint ? sessionLineage(frozen.channelId, frozen.checkpoint) : null;
|
|
148
|
+
let lineagePublishError;
|
|
149
|
+
if (lineage && opts.lineageIndexerUrl) {
|
|
150
|
+
try {
|
|
151
|
+
await publishSessionLineage(opts.lineageIndexerUrl, session.channelId, lineage);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
lineagePublishError = error;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
frozen,
|
|
159
|
+
standby,
|
|
160
|
+
session,
|
|
161
|
+
replayOutputs,
|
|
162
|
+
lineage,
|
|
163
|
+
...(lineagePublishError === undefined ? {} : { lineagePublishError }),
|
|
164
|
+
...(oldChannelClosed ? { oldChannelClosed } : {}),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
throw new StandbyExhaustedError(frozen, attempts, oldChannelClosed);
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=warm-pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"warm-pool.js","sourceRoot":"","sources":["../../src/compute-channel/warm-pool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAuB,MAAM,cAAc,CAAC;AAI1F,OAAO,EAAE,gBAAgB,EAAyB,MAAM,qBAAqB,CAAC;AAmB9E;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC5B,WAA+B,EAC/B,UAAuC;IAEvC,MAAM,IAAI,GAAG,IAAI,GAAG,CAAS,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC,CAAC;IACzD,MAAM,QAAQ,GAAuB,EAAE,CAAC;IACxC,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,EAAE,IAAI,EAAE,GAAG,SAAS,CAAC;QAC3B,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,YAAY,CAAC;YAAE,SAAS;QAC1C,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC;YAAE,SAAS;QAC7E,IACE,CAAC,KAAK,CAAC,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,YAAY,CAAC;YACnD,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,WAAW,CAAC,SAAS,CAAC;YAC7C,CAAC,KAAK,CAAC,IAAI,CAAC,gBAAgB,EAAE,WAAW,CAAC,gBAAgB,CA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kBAAkB,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,gBAAgB,CAAC,CAAC;QAClF,CAAC;QACD,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,cAAc,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;QAC/F,IAAI,mBAA4B,CAAC;QACjC,IAAI,OAAO,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACtC,IAAI,CAAC;gBACH,MAAM,qBAAqB,CAAC,IAAI,CAAC,iBAAiB,EAAE,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;YAClF,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,mBAAmB,GAAG,KAAK,CAAC;YAC9B,CAAC;QACH,CAAC;QACD,OAAO;YACL,MAAM;YACN,OAAO;YACP,OAAO;YACP,aAAa;YACb,OAAO;YACP,GAAG,CAAC,mBAAmB,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,mBAAmB,EAAE,CAAC;YACrE,GAAG,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAClD,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,qBAAqB,CAAC,MAAM,EAAE,QAAQ,EAAE,gBAAgB,CAAC,CAAC;AACtE,CAAC"}
|
package/dist/connect.d.ts
CHANGED
|
@@ -1,4 +1,21 @@
|
|
|
1
1
|
import { ApiPromise } from "@polkadot/api";
|
|
2
|
-
/**
|
|
3
|
-
export declare
|
|
2
|
+
/** Default overall readiness bound, across every attempt and backoff. */
|
|
3
|
+
export declare const DEFAULT_RPC_TIMEOUT_MS = 10000;
|
|
4
|
+
/** Environment override for the readiness bound, in whole milliseconds. */
|
|
5
|
+
export declare const RPC_TIMEOUT_ENV = "FLOP_RPC_TIMEOUT_MS";
|
|
6
|
+
/** Attempts per connect: the first plus two retries after a failed handshake. */
|
|
7
|
+
export declare const RPC_CONNECT_ATTEMPTS = 3;
|
|
8
|
+
export interface ConnectFlopApiOptions {
|
|
9
|
+
/** Overall readiness bound in ms. Defaults to `FLOP_RPC_TIMEOUT_MS`, then 10 s. */
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
}
|
|
12
|
+
/** Resolve the readiness bound; an invalid option or environment value throws. */
|
|
13
|
+
export declare function resolveRpcTimeoutMs(timeoutMs?: number, env?: NodeJS.ProcessEnv): number;
|
|
14
|
+
/**
|
|
15
|
+
* Bound initial readiness and release the provider when connection setup fails.
|
|
16
|
+
* A failed handshake (refused, HTTP 429, abnormal close) is retried on a fresh
|
|
17
|
+
* provider with backoff, at most {@link RPC_CONNECT_ATTEMPTS} times and never past
|
|
18
|
+
* the one overall bound; then it fails with `RPC unreachable`. No other endpoint is tried.
|
|
19
|
+
*/
|
|
20
|
+
export declare function connectFlopApi(endpoint: string, rpc?: any, options?: ConnectFlopApiOptions): Promise<ApiPromise>;
|
|
4
21
|
//# sourceMappingURL=connect.d.ts.map
|
package/dist/connect.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA4B,MAAM,eAAe,CAAC;AAErE,
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA4B,MAAM,eAAe,CAAC;AAErE,yEAAyE;AACzE,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAC7C,2EAA2E;AAC3E,eAAO,MAAM,eAAe,wBAAwB,CAAC;AACrD,iFAAiF;AACjF,eAAO,MAAM,oBAAoB,IAAI,CAAC;AAKtC,MAAM,WAAW,qBAAqB;IACpC,mFAAmF;IACnF,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,kFAAkF;AAClF,wBAAgB,mBAAmB,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,CAAC,UAAU,GAAG,MAAM,CAQvF;AAcD;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,GAAG,EAAE,OAAO,GAAE,qBAA0B,GAAG,OAAO,CAAC,UAAU,CAAC,CA0C1H"}
|
package/dist/connect.js
CHANGED
|
@@ -1,20 +1,87 @@
|
|
|
1
1
|
import { ApiPromise, HttpProvider, WsProvider } from "@polkadot/api";
|
|
2
|
-
/**
|
|
3
|
-
export
|
|
4
|
-
|
|
2
|
+
/** Default overall readiness bound, across every attempt and backoff. */
|
|
3
|
+
export const DEFAULT_RPC_TIMEOUT_MS = 10_000;
|
|
4
|
+
/** Environment override for the readiness bound, in whole milliseconds. */
|
|
5
|
+
export const RPC_TIMEOUT_ENV = "FLOP_RPC_TIMEOUT_MS";
|
|
6
|
+
/** Attempts per connect: the first plus two retries after a failed handshake. */
|
|
7
|
+
export const RPC_CONNECT_ATTEMPTS = 3;
|
|
8
|
+
const RETRY_BACKOFF_MS = 250;
|
|
9
|
+
// setTimeout fires immediately past this, which would turn a long bound into none.
|
|
10
|
+
const MAX_TIMEOUT_MS = 2_147_483_647;
|
|
11
|
+
/** Resolve the readiness bound; an invalid option or environment value throws. */
|
|
12
|
+
export function resolveRpcTimeoutMs(timeoutMs, env) {
|
|
13
|
+
if (timeoutMs !== undefined)
|
|
14
|
+
return checkedTimeout(timeoutMs, "timeoutMs");
|
|
15
|
+
// Browsers have no `process`; there the bound comes from the option or the default.
|
|
16
|
+
const source = env ?? (typeof process === "undefined" ? {} : process.env);
|
|
17
|
+
const raw = source[RPC_TIMEOUT_ENV]?.trim();
|
|
18
|
+
if (!raw)
|
|
19
|
+
return DEFAULT_RPC_TIMEOUT_MS;
|
|
20
|
+
if (!/^\d+$/.test(raw))
|
|
21
|
+
throw new Error(`${RPC_TIMEOUT_ENV} must be a whole number of milliseconds, got ${JSON.stringify(raw)}`);
|
|
22
|
+
return checkedTimeout(Number(raw), RPC_TIMEOUT_ENV);
|
|
23
|
+
}
|
|
24
|
+
function checkedTimeout(value, label) {
|
|
25
|
+
if (!Number.isSafeInteger(value) || value <= 0 || value > MAX_TIMEOUT_MS) {
|
|
26
|
+
throw new Error(`${label} must be a positive whole number of milliseconds up to ${MAX_TIMEOUT_MS}, got ${value}`);
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
function seconds(ms) {
|
|
31
|
+
const s = ms / 1000;
|
|
32
|
+
return `${s} second${s === 1 ? "" : "s"}`;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Bound initial readiness and release the provider when connection setup fails.
|
|
36
|
+
* A failed handshake (refused, HTTP 429, abnormal close) is retried on a fresh
|
|
37
|
+
* provider with backoff, at most {@link RPC_CONNECT_ATTEMPTS} times and never past
|
|
38
|
+
* the one overall bound; then it fails with `RPC unreachable`. No other endpoint is tried.
|
|
39
|
+
*/
|
|
40
|
+
export async function connectFlopApi(endpoint, rpc, options = {}) {
|
|
41
|
+
const timeoutMs = resolveRpcTimeoutMs(options.timeoutMs);
|
|
5
42
|
let expired = false;
|
|
6
43
|
let timer;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
44
|
+
let wake;
|
|
45
|
+
let current;
|
|
46
|
+
const release = async () => {
|
|
47
|
+
if (current && !current.released) {
|
|
48
|
+
current.released = true;
|
|
49
|
+
await current.provider.disconnect();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const attempts = (async () => {
|
|
53
|
+
for (let attempt = 1;; attempt++) {
|
|
54
|
+
const provider = endpoint.startsWith("ws") ? new WsProvider(endpoint) : new HttpProvider(endpoint);
|
|
55
|
+
current = { provider, released: false };
|
|
56
|
+
const pending = ApiPromise.create({ provider, noInitWarn: true, throwOnConnect: true, ...(rpc ? { rpc } : {}) });
|
|
57
|
+
void pending.then((api) => { if (expired)
|
|
58
|
+
void api.disconnect(); }, () => { });
|
|
59
|
+
try {
|
|
60
|
+
return await pending;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
await release();
|
|
64
|
+
if (expired || attempt >= RPC_CONNECT_ATTEMPTS)
|
|
65
|
+
throw error;
|
|
66
|
+
await new Promise((resolve) => {
|
|
67
|
+
const backoff = setTimeout(resolve, RETRY_BACKOFF_MS * 2 ** (attempt - 1));
|
|
68
|
+
wake = () => { clearTimeout(backoff); resolve(); };
|
|
69
|
+
});
|
|
70
|
+
if (expired)
|
|
71
|
+
throw error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
})();
|
|
75
|
+
attempts.catch(() => { });
|
|
10
76
|
try {
|
|
11
|
-
return await Promise.race([
|
|
12
|
-
timer = setTimeout(() => reject(new Error(`RPC unreachable: connection to ${endpoint} timed out after
|
|
77
|
+
return await Promise.race([attempts, new Promise((_, reject) => {
|
|
78
|
+
timer = setTimeout(() => reject(new Error(`RPC unreachable: connection to ${endpoint} timed out after ${seconds(timeoutMs)}`)), timeoutMs);
|
|
13
79
|
})]);
|
|
14
80
|
}
|
|
15
81
|
catch (error) {
|
|
16
82
|
expired = true;
|
|
17
|
-
|
|
83
|
+
wake?.();
|
|
84
|
+
await release();
|
|
18
85
|
throw new Error(`RPC unreachable: could not connect to ${endpoint}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
19
86
|
}
|
|
20
87
|
finally {
|
package/dist/connect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.js","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAErE,oFAAoF;
|
|
1
|
+
{"version":3,"file":"connect.js","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AAAA,O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C,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,CAAC;YAC7I,CAAC,CAAC,CAAC,CAAC,CAAC;IACP,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,GAAG,IAAI,CAAC;QACf,IAAI,EAAE,EAAE,CAAC;QACT,MAAM,OAAO,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,yCAAyC,QAAQ,KAAK,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACpJ,CAAC;YAAS,CAAC;QAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAAC,CAAC;AACpC,CAAC"}
|
package/dist/faucet.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ import { type FlopNetwork } from './chain.js';
|
|
|
14
14
|
* Faucet configuration.
|
|
15
15
|
*/
|
|
16
16
|
/** Default faucet client label; faucet-confirmation.test.ts pins it to this package's version. */
|
|
17
|
-
export declare const FAUCET_CLIENT = "flop-sdk-ts/0.
|
|
17
|
+
export declare const FAUCET_CLIENT = "flop-sdk-ts/0.11.0";
|
|
18
18
|
export interface FaucetConfig {
|
|
19
19
|
/** Canonical FLOP network. Determines the endpoint unless explicitly overridden. */
|
|
20
20
|
network?: FlopNetwork;
|
|
@@ -65,16 +65,19 @@ export declare const FAUCET_ENDPOINTS: Partial<Record<FlopNetwork, string>>;
|
|
|
65
65
|
* Default funding amount for testnet (in FLOP).
|
|
66
66
|
*/
|
|
67
67
|
export declare const DEFAULT_FAUCET_AMOUNT = 25;
|
|
68
|
+
export declare const D10_FAUCET_GENESIS_HASH = "0x918abd58fb16306b30ef3886514cfaebffecb4de43e82b6f8ec4795c43cfd800";
|
|
68
69
|
export interface FaucetChainInfo {
|
|
69
70
|
network: string;
|
|
70
71
|
genesisHash: string;
|
|
71
72
|
caip2?: string;
|
|
72
73
|
}
|
|
73
74
|
export declare function resolveFaucetEndpoint(config?: FaucetConfig): string;
|
|
75
|
+
/** The public Testnet faucet hostname may be used for discovery while funding is held. */
|
|
76
|
+
export declare function isPublicTestnetFaucet(endpoint: string): boolean;
|
|
74
77
|
/** Read the chain identity served by a faucet before requesting funds. */
|
|
75
78
|
export declare function getFaucetChainInfo(config?: FaucetConfig): Promise<FaucetChainInfo>;
|
|
76
79
|
/**
|
|
77
|
-
* Request funding from
|
|
80
|
+
* Request funding from a faucet. Public Testnet funding remains disabled.
|
|
78
81
|
*
|
|
79
82
|
* @param address - The address to fund
|
|
80
83
|
* @param config - Faucet configuration
|
|
@@ -82,7 +85,7 @@ export declare function getFaucetChainInfo(config?: FaucetConfig): Promise<Fauce
|
|
|
82
85
|
*
|
|
83
86
|
* @example
|
|
84
87
|
* ```ts
|
|
85
|
-
* const result = await requestFaucetFunding('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
|
|
88
|
+
* const result = await requestFaucetFunding('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', { network: 'devnet' });
|
|
86
89
|
* if (result.outcome === 'unknown') {
|
|
87
90
|
* console.log('Preserve the result and reconcile before requesting again');
|
|
88
91
|
* }
|
|
@@ -145,7 +148,7 @@ export declare function fundFromFaucet(api: ApiPromise, address: string, config?
|
|
|
145
148
|
* Faucet client class for more advanced usage.
|
|
146
149
|
*/
|
|
147
150
|
export declare class FaucetClient {
|
|
148
|
-
private network
|
|
151
|
+
private network?;
|
|
149
152
|
private endpoint;
|
|
150
153
|
private timeout;
|
|
151
154
|
private maxRetries;
|
package/dist/faucet.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"faucet.d.ts","sourceRoot":"","sources":["../src/faucet.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO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@ -13,7 +13,7 @@ import { FLOP_NETWORK_PROFILES, getFlopNetworkProfile, isFlopNetwork, } from './
|
|
|
13
13
|
* Faucet configuration.
|
|
14
14
|
*/
|
|
15
15
|
/** Default faucet client label; faucet-confirmation.test.ts pins it to this package's version. */
|
|
16
|
-
export const FAUCET_CLIENT = 'flop-sdk-ts/0.
|
|
16
|
+
export const FAUCET_CLIENT = 'flop-sdk-ts/0.11.0';
|
|
17
17
|
/**
|
|
18
18
|
* Default faucet endpoints for different networks.
|
|
19
19
|
*/
|
|
@@ -26,6 +26,8 @@ export const FAUCET_ENDPOINTS = {
|
|
|
26
26
|
* Default funding amount for testnet (in FLOP).
|
|
27
27
|
*/
|
|
28
28
|
export const DEFAULT_FAUCET_AMOUNT = 25;
|
|
29
|
+
// Signed six-founder release block zero, used only at the faucet funding boundary.
|
|
30
|
+
export const D10_FAUCET_GENESIS_HASH = '0x918abd58fb16306b30ef3886514cfaebffecb4de43e82b6f8ec4795c43cfd800';
|
|
29
31
|
export function resolveFaucetEndpoint(config = {}) {
|
|
30
32
|
if (config.endpoint)
|
|
31
33
|
return config.endpoint.replace(/\/+$/, '');
|
|
@@ -35,6 +37,10 @@ export function resolveFaucetEndpoint(config = {}) {
|
|
|
35
37
|
throw new Error(`No faucet available for network: ${network}`);
|
|
36
38
|
return endpoint.replace(/\/+$/, '');
|
|
37
39
|
}
|
|
40
|
+
/** The public Testnet faucet hostname may be used for discovery while funding is held. */
|
|
41
|
+
export function isPublicTestnetFaucet(endpoint) {
|
|
42
|
+
return new URL(endpoint).hostname.replace(/\.$/, '') === new URL(FLOP_NETWORK_PROFILES.testnet.faucet).hostname;
|
|
43
|
+
}
|
|
38
44
|
/** Read the chain identity served by a faucet before requesting funds. */
|
|
39
45
|
export async function getFaucetChainInfo(config = {}) {
|
|
40
46
|
const endpoint = resolveFaucetEndpoint(config);
|
|
@@ -50,7 +56,7 @@ export async function getFaucetChainInfo(config = {}) {
|
|
|
50
56
|
return { network: data.network, genesisHash: data.genesisHash, caip2: data.caip2 };
|
|
51
57
|
}
|
|
52
58
|
/**
|
|
53
|
-
* Request funding from
|
|
59
|
+
* Request funding from a faucet. Public Testnet funding remains disabled.
|
|
54
60
|
*
|
|
55
61
|
* @param address - The address to fund
|
|
56
62
|
* @param config - Faucet configuration
|
|
@@ -58,7 +64,7 @@ export async function getFaucetChainInfo(config = {}) {
|
|
|
58
64
|
*
|
|
59
65
|
* @example
|
|
60
66
|
* ```ts
|
|
61
|
-
* const result = await requestFaucetFunding('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');
|
|
67
|
+
* const result = await requestFaucetFunding('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY', { network: 'devnet' });
|
|
62
68
|
* if (result.outcome === 'unknown') {
|
|
63
69
|
* console.log('Preserve the result and reconcile before requesting again');
|
|
64
70
|
* }
|
|
@@ -73,12 +79,27 @@ async function requestFunding(address, config) {
|
|
|
73
79
|
let endpoint;
|
|
74
80
|
try {
|
|
75
81
|
endpoint = resolveFaucetEndpoint(config);
|
|
82
|
+
if (isPublicTestnetFaucet(endpoint)) {
|
|
83
|
+
throw new Error('Public Testnet funding is unavailable');
|
|
84
|
+
}
|
|
76
85
|
}
|
|
77
86
|
catch (error) {
|
|
78
87
|
return { success: false, outcome: 'refused', error: String(error), network, timestamp };
|
|
79
88
|
}
|
|
89
|
+
if (config.network === 'testnet') {
|
|
90
|
+
try {
|
|
91
|
+
const chain = await getFaucetChainInfo(config);
|
|
92
|
+
if (chain.network !== network || chain.genesisHash.toLowerCase() !== D10_FAUCET_GENESIS_HASH) {
|
|
93
|
+
throw new Error('expected a Testnet faucet bound to the D10 genesis');
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
return { success: false, outcome: 'refused', error: String(error), network, endpoint, timestamp };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
80
100
|
try {
|
|
81
101
|
const response = await fetch(`${endpoint}/fund`, {
|
|
102
|
+
redirect: 'error',
|
|
82
103
|
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
83
104
|
body: JSON.stringify({ address, client: config.client ?? FAUCET_CLIENT,
|
|
84
105
|
...(config.amount === undefined ? {} : { amount: config.amount }),
|
|
@@ -191,7 +212,7 @@ export async function fundFromFaucetDetailed(api, address, config = {}) {
|
|
|
191
212
|
// The stock local faucet uses the devnet policy label; genesis still binds both services.
|
|
192
213
|
const networkMatches = chain.network === network || (network === 'local' && chain.network === 'devnet');
|
|
193
214
|
if (!/^0x[0-9a-f]{64}$/.test(expected) || chain.genesisHash.toLowerCase() !== expected
|
|
194
|
-
|| !networkMatches)
|
|
215
|
+
|| !networkMatches || (network === 'testnet' && expected !== D10_FAUCET_GENESIS_HASH))
|
|
195
216
|
throw new Error('faucet/RPC chain identity differs');
|
|
196
217
|
}
|
|
197
218
|
catch (error) {
|
|
@@ -221,7 +242,7 @@ export class FaucetClient {
|
|
|
221
242
|
retryDelay;
|
|
222
243
|
referral;
|
|
223
244
|
constructor(config = {}) {
|
|
224
|
-
this.network = config.network
|
|
245
|
+
this.network = config.network;
|
|
225
246
|
this.endpoint = resolveFaucetEndpoint(config);
|
|
226
247
|
this.timeout = config.timeout ?? 30000;
|
|
227
248
|
this.maxRetries = config.maxRetries ?? 3;
|