@le-space/core 0.4.4 → 0.5.2
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/{_esm-3QBVL5IV.js → _esm-SEGUHZCN.js} +29 -29
- package/{ccip-62HLVIJN.js → ccip-FZJNHKYI.js} +100 -100
- package/{chunk-3CGLGAGR.js → chunk-ZMOPJA7G.js} +4 -4
- package/index.d.ts +41 -2
- package/index.js +1116 -901
- package/package.json +11 -3
- package/{secp256k1-WKIOWUKN.js → secp256k1-UP7VCQDO.js} +10 -10
package/index.js
CHANGED
|
@@ -5,7 +5,7 @@ var DEFAULT_ALEPH_CHANNEL = "TEST";
|
|
|
5
5
|
|
|
6
6
|
// src/manifests.ts
|
|
7
7
|
var ITEM_HASH_RE = /^[a-fA-F0-9]{64}$/;
|
|
8
|
-
var DEFAULT_ALEPH_API_HOST = "https://
|
|
8
|
+
var DEFAULT_ALEPH_API_HOST = "https://api.aleph.im";
|
|
9
9
|
var DEFAULT_IPFS_GATEWAY_BASE_URL = "https://ipfs.aleph.cloud/ipfs/";
|
|
10
10
|
function isValidDateString(value) {
|
|
11
11
|
return value.trim().length > 0 && !Number.isNaN(new Date(value).getTime());
|
|
@@ -598,6 +598,13 @@ function normalizeExecution(item, crnUrl) {
|
|
|
598
598
|
}
|
|
599
599
|
|
|
600
600
|
// src/deployment-inspection.ts
|
|
601
|
+
var TRANSIENT_MESSAGE_LOOKUP_STATUSES = /* @__PURE__ */ new Set([408, 425, 429, 500, 502, 503, 504]);
|
|
602
|
+
function isTransientMessageLookupError(error) {
|
|
603
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
604
|
+
const match = message.match(/Message lookup failed: (\d{3})/);
|
|
605
|
+
if (!match) return false;
|
|
606
|
+
return TRANSIENT_MESSAGE_LOOKUP_STATUSES.has(Number(match[1]));
|
|
607
|
+
}
|
|
601
608
|
async function fetchMessageEnvelope(itemHash, options) {
|
|
602
609
|
const response = await options.fetch(`${options.apiHost ?? DEFAULT_ALEPH_API_HOST}/api/v0/messages/${itemHash}`, {
|
|
603
610
|
cache: "no-cache"
|
|
@@ -757,480 +764,498 @@ async function broadcastAlephMessage(message, options) {
|
|
|
757
764
|
throw new Error("Broadcast failed: retry budget exhausted");
|
|
758
765
|
}
|
|
759
766
|
|
|
760
|
-
// src/
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
}
|
|
764
|
-
async function createUnsignedForgetMessage(args) {
|
|
765
|
-
const content = {
|
|
766
|
-
address: args.sender,
|
|
767
|
-
time: args.now ?? Date.now() / 1e3,
|
|
768
|
-
hashes: [...new Set((args.hashes ?? []).filter(Boolean))],
|
|
769
|
-
aggregates: [...new Set((args.aggregates ?? []).filter(Boolean))],
|
|
770
|
-
reason: asOptionalReason(args.reason)
|
|
771
|
-
};
|
|
772
|
-
if (content.hashes.length === 0 && content.aggregates.length === 0) {
|
|
773
|
-
throw new Error("FORGET message requires at least one hash or aggregate key.");
|
|
774
|
-
}
|
|
775
|
-
const itemContent = JSON.stringify(content);
|
|
776
|
-
const itemHash = await args.hasher(itemContent);
|
|
777
|
-
return {
|
|
778
|
-
sender: args.sender,
|
|
779
|
-
chain: "ETH",
|
|
780
|
-
type: "FORGET",
|
|
781
|
-
item_hash: itemHash,
|
|
782
|
-
item_type: "inline",
|
|
783
|
-
item_content: itemContent,
|
|
784
|
-
time: args.now ?? Date.now() / 1e3,
|
|
785
|
-
channel: args.channel ?? DEFAULT_ALEPH_CHANNEL
|
|
786
|
-
};
|
|
787
|
-
}
|
|
788
|
-
async function forgetAlephMessages(args) {
|
|
789
|
-
args.onProgress?.({
|
|
790
|
-
stage: "building-delete-message",
|
|
791
|
-
label: "Building delete message",
|
|
792
|
-
progress: 18,
|
|
793
|
-
status: "info",
|
|
794
|
-
itemHash: null,
|
|
795
|
-
detail: [...new Set((args.hashes ?? []).filter(Boolean))].join(", ") || null,
|
|
796
|
-
error: null,
|
|
797
|
-
timestamp: Date.now()
|
|
798
|
-
});
|
|
799
|
-
const unsignedMessage = await createUnsignedForgetMessage({
|
|
800
|
-
sender: args.sender,
|
|
801
|
-
hashes: args.hashes,
|
|
802
|
-
aggregates: args.aggregates,
|
|
803
|
-
reason: args.reason,
|
|
804
|
-
hasher: args.hasher,
|
|
805
|
-
channel: args.channel,
|
|
806
|
-
now: args.now
|
|
807
|
-
});
|
|
808
|
-
args.onProgress?.({
|
|
809
|
-
stage: "signing-delete-message",
|
|
810
|
-
label: "Waiting for delete signature",
|
|
811
|
-
progress: 42,
|
|
812
|
-
status: "info",
|
|
813
|
-
itemHash: unsignedMessage.item_hash,
|
|
814
|
-
detail: unsignedMessage.item_hash,
|
|
815
|
-
error: null,
|
|
816
|
-
timestamp: Date.now()
|
|
817
|
-
});
|
|
818
|
-
const message = await signAlephMessage(unsignedMessage, args.signer);
|
|
819
|
-
args.onProgress?.({
|
|
820
|
-
stage: "broadcasting-delete",
|
|
821
|
-
label: "Broadcasting delete to Aleph",
|
|
822
|
-
progress: 68,
|
|
823
|
-
status: "warning",
|
|
824
|
-
itemHash: message.item_hash,
|
|
825
|
-
detail: message.item_hash,
|
|
826
|
-
error: null,
|
|
827
|
-
timestamp: Date.now()
|
|
828
|
-
});
|
|
829
|
-
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
830
|
-
apiHost: args.apiHost,
|
|
831
|
-
sync: args.sync,
|
|
832
|
-
fetch: args.fetch
|
|
833
|
-
});
|
|
834
|
-
const status = normalizeBroadcastStatus(httpStatus, response?.message_status);
|
|
835
|
-
args.onProgress?.({
|
|
836
|
-
stage: status === "rejected" ? "error" : "delete-completed",
|
|
837
|
-
label: status === "rejected" ? "Delete rejected by Aleph" : "Delete submitted to Aleph",
|
|
838
|
-
progress: 100,
|
|
839
|
-
status: status === "rejected" ? "error" : "success",
|
|
840
|
-
itemHash: message.item_hash,
|
|
841
|
-
detail: status === "rejected" ? String(response?.details ?? "Aleph rejected this delete request.") : null,
|
|
842
|
-
error: status === "rejected" ? String(response?.details ?? "Aleph rejected this delete request.") : null,
|
|
843
|
-
timestamp: Date.now()
|
|
844
|
-
});
|
|
845
|
-
return {
|
|
846
|
-
sender: args.sender,
|
|
847
|
-
itemHash: message.item_hash,
|
|
848
|
-
response,
|
|
849
|
-
httpStatus,
|
|
850
|
-
status
|
|
851
|
-
};
|
|
852
|
-
}
|
|
853
|
-
async function cleanupFailedDeployment(args) {
|
|
854
|
-
try {
|
|
855
|
-
return await forgetAlephMessages({
|
|
856
|
-
sender: args.sender,
|
|
857
|
-
hashes: [args.instanceItemHash],
|
|
858
|
-
reason: args.reason ?? "Discard failed deployment attempt",
|
|
859
|
-
signer: args.signer,
|
|
860
|
-
hasher: args.hasher,
|
|
861
|
-
fetch: args.fetch,
|
|
862
|
-
channel: args.channel,
|
|
863
|
-
apiHost: args.apiHost
|
|
864
|
-
});
|
|
865
|
-
} catch (error) {
|
|
866
|
-
return {
|
|
867
|
-
error: error instanceof Error ? error.message : String(error)
|
|
868
|
-
};
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
|
|
872
|
-
// src/retention.ts
|
|
873
|
-
var SUCCESSFUL_DEPLOYMENTS_AGGREGATE_KEY = "uc-go-peer-successful-deployments";
|
|
767
|
+
// src/runtime.ts
|
|
768
|
+
var DEFAULT_SCHEDULER_ALLOCATION_URL = "https://scheduler.api.aleph.cloud/api/v0/allocation";
|
|
769
|
+
var DEFAULT_TWO_N_SIX_HASH_URL = "https://api.2n6.me/api/hash";
|
|
874
770
|
function asString3(value) {
|
|
875
771
|
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
876
772
|
}
|
|
877
|
-
function
|
|
878
|
-
|
|
879
|
-
const candidate = record;
|
|
880
|
-
const instanceItemHash = asString3(candidate.instance_item_hash);
|
|
881
|
-
if (!instanceItemHash) return null;
|
|
882
|
-
return {
|
|
883
|
-
instance_item_hash: instanceItemHash,
|
|
884
|
-
rootfs_item_hash: asString3(candidate.rootfs_item_hash) ?? "",
|
|
885
|
-
site_item_hash: asString3(candidate.site_item_hash) ?? "",
|
|
886
|
-
rootfs_cid: asString3(candidate.rootfs_cid) ?? "",
|
|
887
|
-
site_url: asString3(candidate.site_url) ?? "",
|
|
888
|
-
relay_peer_id: asString3(candidate.relay_peer_id) ?? "",
|
|
889
|
-
rootfs_version: asString3(candidate.rootfs_version) ?? "",
|
|
890
|
-
deployed_at: asString3(candidate.deployed_at) ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
891
|
-
vm_name: asString3(candidate.vm_name) ?? ""
|
|
892
|
-
};
|
|
893
|
-
}
|
|
894
|
-
function normalizeRetentionLedger(value) {
|
|
895
|
-
if (Array.isArray(value)) {
|
|
896
|
-
return value.map(normalizeRetentionRecord).filter((entry) => entry != null);
|
|
897
|
-
}
|
|
898
|
-
if (value && typeof value === "object" && Array.isArray(value.deployments)) {
|
|
899
|
-
return value.deployments.map(normalizeRetentionRecord).filter((entry) => entry != null);
|
|
900
|
-
}
|
|
901
|
-
return [];
|
|
902
|
-
}
|
|
903
|
-
function retentionRecordId(record) {
|
|
904
|
-
return [record.instance_item_hash, record.rootfs_item_hash, record.site_item_hash].join(":");
|
|
773
|
+
function asNumber2(value) {
|
|
774
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
905
775
|
}
|
|
906
|
-
function
|
|
907
|
-
|
|
776
|
+
function normalizeIpv6(value) {
|
|
777
|
+
if (typeof value !== "string") return null;
|
|
778
|
+
const normalized = value.trim().toLowerCase();
|
|
779
|
+
if (!normalized) return null;
|
|
780
|
+
const zoneIndex = normalized.indexOf("%");
|
|
781
|
+
return zoneIndex >= 0 ? normalized.slice(0, zoneIndex) : normalized;
|
|
908
782
|
}
|
|
909
|
-
function
|
|
910
|
-
|
|
783
|
+
function isLikelyGlobalIpv6(value) {
|
|
784
|
+
const normalized = normalizeIpv6(value);
|
|
785
|
+
if (!normalized || normalized === "::" || normalized === "::1") return false;
|
|
786
|
+
const firstHextetText = normalized.split(":", 1)[0];
|
|
787
|
+
if (!firstHextetText) return false;
|
|
788
|
+
const firstHextet = Number.parseInt(firstHextetText, 16);
|
|
789
|
+
if (!Number.isFinite(firstHextet)) return false;
|
|
790
|
+
if ((firstHextet & 65024) === 64512) return false;
|
|
791
|
+
if ((firstHextet & 65472) === 65152) return false;
|
|
792
|
+
if ((firstHextet & 65280) === 65280) return false;
|
|
793
|
+
if ((firstHextet & 65472) === 65216) return false;
|
|
794
|
+
return true;
|
|
911
795
|
}
|
|
912
|
-
function
|
|
913
|
-
return
|
|
796
|
+
function findCrnByHash(crns, crnHash) {
|
|
797
|
+
return crns.find((crn) => crn.hash === crnHash) ?? null;
|
|
914
798
|
}
|
|
915
|
-
function
|
|
916
|
-
const
|
|
917
|
-
|
|
799
|
+
async function fetchSchedulerAllocation(itemHash, options) {
|
|
800
|
+
const response = await options.fetch(
|
|
801
|
+
`${options.schedulerAllocationUrl ?? DEFAULT_SCHEDULER_ALLOCATION_URL}/${itemHash}`,
|
|
802
|
+
{ cache: "no-cache" }
|
|
918
803
|
);
|
|
919
|
-
|
|
920
|
-
...args.prunedRecords.flatMap(dependentHashesFromRetentionRecord),
|
|
921
|
-
...args.extraForgetHashes
|
|
922
|
-
]).filter((hash) => !args.retainedHashes.has(hash) && !instanceForgetHashes.includes(hash));
|
|
923
|
-
return {
|
|
924
|
-
instanceForgetHashes,
|
|
925
|
-
dependentForgetHashes,
|
|
926
|
-
orderedForgetHashes: [...instanceForgetHashes, ...dependentForgetHashes]
|
|
927
|
-
};
|
|
928
|
-
}
|
|
929
|
-
async function fetchAggregateKey(args) {
|
|
930
|
-
const requestUrl = new URL(`/api/v0/aggregates/${args.address}.json`, args.apiHost ?? "https://api2.aleph.im");
|
|
931
|
-
requestUrl.searchParams.set("keys", args.key);
|
|
932
|
-
const response = await args.fetch(requestUrl.toString(), { cache: "no-cache" });
|
|
933
|
-
if (response.status === 404) return {};
|
|
804
|
+
if (response.status === 404) return null;
|
|
934
805
|
if (!response.ok) {
|
|
935
|
-
throw new Error(`
|
|
806
|
+
throw new Error(`Scheduler allocation request failed: ${response.status}`);
|
|
936
807
|
}
|
|
937
808
|
const payload = await response.json();
|
|
938
|
-
|
|
939
|
-
}
|
|
940
|
-
async function createUnsignedAggregateMessage(args) {
|
|
941
|
-
const itemContent = JSON.stringify(args.content);
|
|
942
|
-
const itemHash = await args.hasher(itemContent);
|
|
809
|
+
const node = payload?.node;
|
|
943
810
|
return {
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
811
|
+
source: "scheduler",
|
|
812
|
+
crnHash: asString3(node?.node_id),
|
|
813
|
+
crnUrl: asString3(node?.url),
|
|
814
|
+
node: node ? {
|
|
815
|
+
node_id: asString3(node.node_id) ?? void 0,
|
|
816
|
+
url: asString3(node.url) ?? void 0,
|
|
817
|
+
ipv6: asString3(node.ipv6),
|
|
818
|
+
supports_ipv6: typeof node.supports_ipv6 === "boolean" ? node.supports_ipv6 : void 0
|
|
819
|
+
} : null,
|
|
820
|
+
vmIpv6: asString3(payload?.vm_ipv6),
|
|
821
|
+
period: payload?.period ? {
|
|
822
|
+
start_timestamp: asString3(payload.period.start_timestamp) ?? void 0,
|
|
823
|
+
duration_seconds: asNumber2(payload.period.duration_seconds) ?? void 0
|
|
824
|
+
} : null
|
|
952
825
|
};
|
|
953
826
|
}
|
|
954
|
-
async function
|
|
955
|
-
const
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
const unsignedMessage = await createUnsignedAggregateMessage({
|
|
962
|
-
sender: args.sender,
|
|
963
|
-
content: aggregateContent,
|
|
964
|
-
hasher: args.hasher,
|
|
965
|
-
channel: args.channel,
|
|
966
|
-
now: args.now
|
|
967
|
-
});
|
|
968
|
-
const message = await signAlephMessage(unsignedMessage, args.signer);
|
|
969
|
-
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
970
|
-
apiHost: args.apiHost,
|
|
971
|
-
sync: true,
|
|
972
|
-
fetch: args.fetch
|
|
973
|
-
});
|
|
827
|
+
async function fetch2n6WebAccessUrl(itemHash, options) {
|
|
828
|
+
const response = await options.fetch(
|
|
829
|
+
`${options.twoN6HashUrl ?? DEFAULT_TWO_N_SIX_HASH_URL}/${itemHash}`,
|
|
830
|
+
{ cache: "no-cache" }
|
|
831
|
+
);
|
|
832
|
+
if (!response.ok) return null;
|
|
833
|
+
const payload = await response.json();
|
|
974
834
|
return {
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
httpStatus
|
|
835
|
+
url: normalizeProxyUrl(payload?.url ?? payload?.subdomain),
|
|
836
|
+
active: typeof payload?.active === "boolean" ? payload.active : null,
|
|
837
|
+
subdomain: asString3(payload?.subdomain)
|
|
979
838
|
};
|
|
980
839
|
}
|
|
981
|
-
async function
|
|
982
|
-
const
|
|
983
|
-
const
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
const statuses = {};
|
|
991
|
-
for (const hash of args.hashes) {
|
|
992
|
-
statuses[hash] = await fetchMessageStatus({
|
|
993
|
-
hash,
|
|
994
|
-
fetch: args.fetch,
|
|
995
|
-
apiHost: args.apiHost
|
|
840
|
+
async function fetchCrnExecutionMap(crnUrl, options) {
|
|
841
|
+
const normalizedCrnUrl = String(crnUrl).replace(/\/+$/, "");
|
|
842
|
+
for (const [version, suffix] of [
|
|
843
|
+
["v2", "/v2/about/executions/list"],
|
|
844
|
+
["v1", "/about/executions/list"]
|
|
845
|
+
]) {
|
|
846
|
+
const requestUrl = `${normalizedCrnUrl}${suffix}`;
|
|
847
|
+
const response = await options.fetch(requestUrl, {
|
|
848
|
+
cache: "no-cache"
|
|
996
849
|
});
|
|
850
|
+
if (response.ok) {
|
|
851
|
+
const payload = await response.json();
|
|
852
|
+
return {
|
|
853
|
+
version,
|
|
854
|
+
payload: payload && typeof payload === "object" ? payload : null,
|
|
855
|
+
requestUrl
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
if (response.status !== 404) {
|
|
859
|
+
return {
|
|
860
|
+
version,
|
|
861
|
+
payload: null,
|
|
862
|
+
requestUrl
|
|
863
|
+
};
|
|
864
|
+
}
|
|
997
865
|
}
|
|
998
|
-
const forgottenHashes = args.hashes.filter((hash) => statuses[hash] === "forgotten");
|
|
999
|
-
const outstandingForgetHashes = args.hashes.filter((hash) => statuses[hash] !== "forgotten");
|
|
1000
866
|
return {
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
867
|
+
version: "v2",
|
|
868
|
+
payload: null,
|
|
869
|
+
requestUrl: `${normalizedCrnUrl}/v2/about/executions/list`
|
|
1004
870
|
};
|
|
1005
871
|
}
|
|
1006
|
-
|
|
1007
|
-
const
|
|
1008
|
-
|
|
1009
|
-
const
|
|
1010
|
-
|
|
872
|
+
function describeRuntimeAvailability(runtime) {
|
|
873
|
+
const execution = runtime.execution ?? null;
|
|
874
|
+
const mappedPorts = runtime.mappedPorts ?? {};
|
|
875
|
+
const hostIpv4 = runtime.hostIpv4 ?? null;
|
|
876
|
+
const ipv6 = runtime.ipv6 ?? null;
|
|
877
|
+
const executionGuestIpv6 = execution?.networking?.ipv6_ip ?? null;
|
|
878
|
+
const proxyUrl = runtime.proxyUrl ?? null;
|
|
879
|
+
const schedulerSource = runtime.allocation?.source ?? null;
|
|
880
|
+
const webAccessActive = runtime.webAccess?.active ?? null;
|
|
881
|
+
const mappedPortCount = Object.keys(mappedPorts).length;
|
|
882
|
+
if (hostIpv4 && mappedPortCount > 0 && runtime.requirePublicGuestIpv6ForProxy === true && proxyUrl && executionGuestIpv6 && !isLikelyGlobalIpv6(executionGuestIpv6)) {
|
|
1011
883
|
return {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
884
|
+
state: "execution-invalid-public-ipv6",
|
|
885
|
+
reason: `The CRN exposed runtime networking, but the guest IPv6 (${executionGuestIpv6}) is not globally routable. Proxy-backed HTTPS activation cannot succeed until the CRN reports a public guest IPv6.`,
|
|
886
|
+
schedulerSource,
|
|
887
|
+
executionSeen: Boolean(execution),
|
|
888
|
+
webAccessActive,
|
|
889
|
+
mappedPortCount,
|
|
890
|
+
proxyUrl
|
|
1018
891
|
};
|
|
1019
892
|
}
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
893
|
+
if (hostIpv4 && mappedPortCount > 0) {
|
|
894
|
+
return {
|
|
895
|
+
state: "ready",
|
|
896
|
+
reason: null,
|
|
897
|
+
schedulerSource,
|
|
898
|
+
executionSeen: Boolean(execution),
|
|
899
|
+
webAccessActive,
|
|
900
|
+
mappedPortCount,
|
|
901
|
+
proxyUrl
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
if (!execution && proxyUrl && webAccessActive === false) {
|
|
905
|
+
return {
|
|
906
|
+
state: "proxy-reserved-inactive",
|
|
907
|
+
reason: "Aleph reserved a proxy URL for the VM, but it is still inactive and the selected CRN has not exposed execution networking yet.",
|
|
908
|
+
schedulerSource,
|
|
909
|
+
executionSeen: false,
|
|
910
|
+
webAccessActive,
|
|
911
|
+
mappedPortCount,
|
|
912
|
+
proxyUrl
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
if (!execution && schedulerSource === "manual") {
|
|
916
|
+
return {
|
|
917
|
+
state: "crn-execution-missing",
|
|
918
|
+
reason: "The deployment is pinned to a specific CRN, but that CRN is not exposing this VM in its execution list yet.",
|
|
919
|
+
schedulerSource,
|
|
920
|
+
executionSeen: false,
|
|
921
|
+
webAccessActive,
|
|
922
|
+
mappedPortCount,
|
|
923
|
+
proxyUrl
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
if (execution && !hostIpv4) {
|
|
927
|
+
return {
|
|
928
|
+
state: "execution-missing-host-ipv4",
|
|
929
|
+
reason: "The CRN exposed an execution record, but it does not include a public host IPv4 yet.",
|
|
930
|
+
schedulerSource,
|
|
931
|
+
executionSeen: true,
|
|
932
|
+
webAccessActive,
|
|
933
|
+
mappedPortCount,
|
|
934
|
+
proxyUrl
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
if (execution && mappedPortCount === 0) {
|
|
938
|
+
return {
|
|
939
|
+
state: "execution-missing-port-mappings",
|
|
940
|
+
reason: "The CRN exposed an execution record, but mapped ports are still empty.",
|
|
941
|
+
schedulerSource,
|
|
942
|
+
executionSeen: true,
|
|
943
|
+
webAccessActive,
|
|
944
|
+
mappedPortCount,
|
|
945
|
+
proxyUrl
|
|
946
|
+
};
|
|
947
|
+
}
|
|
948
|
+
return {
|
|
949
|
+
state: "runtime-pending",
|
|
950
|
+
reason: "Aleph has not exposed enough runtime networking details yet.",
|
|
951
|
+
schedulerSource,
|
|
952
|
+
executionSeen: Boolean(execution),
|
|
953
|
+
webAccessActive,
|
|
954
|
+
mappedPortCount,
|
|
955
|
+
proxyUrl
|
|
956
|
+
};
|
|
957
|
+
}
|
|
958
|
+
async function fetchVmRuntime(args) {
|
|
959
|
+
const crns = args.crns ?? await fetchCrns({
|
|
960
|
+
url: args.crnListUrl ?? DEFAULT_CRN_LIST_URL,
|
|
961
|
+
fetch: args.fetch
|
|
1024
962
|
});
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
963
|
+
const schedulerAllocation = await fetchSchedulerAllocation(args.itemHash, {
|
|
964
|
+
fetch: args.fetch,
|
|
965
|
+
schedulerAllocationUrl: args.schedulerAllocationUrl
|
|
966
|
+
}).catch(() => null);
|
|
967
|
+
const selectedCrn = args.crnHash ? findCrnByHash(crns, args.crnHash) : null;
|
|
968
|
+
const allocation = schedulerAllocation ?? (selectedCrn ? {
|
|
969
|
+
source: "manual",
|
|
970
|
+
crnHash: selectedCrn.hash,
|
|
971
|
+
crnUrl: selectedCrn.address,
|
|
972
|
+
node: { url: selectedCrn.address },
|
|
973
|
+
vmIpv6: null,
|
|
974
|
+
period: null
|
|
975
|
+
} : null);
|
|
976
|
+
const webAccess = await fetch2n6WebAccessUrl(args.itemHash, {
|
|
977
|
+
fetch: args.fetch,
|
|
978
|
+
twoN6HashUrl: args.twoN6HashUrl
|
|
979
|
+
}).catch(() => null);
|
|
980
|
+
const webAccessUrl = webAccess?.url ?? null;
|
|
981
|
+
let execution = null;
|
|
982
|
+
let executionLookupBlocked = false;
|
|
983
|
+
if (allocation?.crnUrl) {
|
|
984
|
+
const executionLookup = await fetchCrnExecutionMap(allocation.crnUrl, {
|
|
985
|
+
fetch: args.fetch
|
|
1041
986
|
});
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
reason: args.reason,
|
|
1048
|
-
signer: args.signer,
|
|
1049
|
-
hasher: args.hasher,
|
|
1050
|
-
fetch: args.fetch,
|
|
1051
|
-
channel: args.channel,
|
|
1052
|
-
apiHost: args.apiHost,
|
|
1053
|
-
now: args.now
|
|
1054
|
-
});
|
|
1055
|
-
followUpForgetResults.push({ hash, result });
|
|
987
|
+
const executionPayload = executionLookup.payload?.[args.itemHash];
|
|
988
|
+
if (executionPayload && typeof executionPayload === "object") {
|
|
989
|
+
execution = normalizeExecution(executionPayload, allocation.crnUrl);
|
|
990
|
+
if (!execution.networking.proxy_url && webAccessUrl) {
|
|
991
|
+
execution.networking.proxy_url = webAccessUrl;
|
|
1056
992
|
}
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
fetch: args.fetch,
|
|
1060
|
-
apiHost: args.apiHost
|
|
1061
|
-
});
|
|
993
|
+
} else if (executionLookup.payload == null) {
|
|
994
|
+
executionLookupBlocked = true;
|
|
1062
995
|
}
|
|
1063
996
|
}
|
|
997
|
+
const hostIpv4 = execution?.networking?.host_ipv4 ?? execution?.networking?.ipv4 ?? null;
|
|
998
|
+
const ipv6 = execution?.networking?.ipv6_ip ?? execution?.networking?.ipv6 ?? allocation?.vmIpv6 ?? null;
|
|
999
|
+
const mappedPorts = execution?.networking?.mapped_ports ?? {};
|
|
1000
|
+
const sshPort = mappedPorts?.["22"]?.host ?? null;
|
|
1001
|
+
const proxyUrl = execution?.networking?.proxy_url ?? webAccessUrl ?? null;
|
|
1002
|
+
const diagnostics = describeRuntimeAvailability({
|
|
1003
|
+
allocation,
|
|
1004
|
+
execution,
|
|
1005
|
+
webAccess,
|
|
1006
|
+
hostIpv4,
|
|
1007
|
+
ipv6,
|
|
1008
|
+
proxyUrl,
|
|
1009
|
+
mappedPorts,
|
|
1010
|
+
requirePublicGuestIpv6ForProxy: args.requirePublicGuestIpv6ForProxy
|
|
1011
|
+
});
|
|
1064
1012
|
return {
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1013
|
+
allocation,
|
|
1014
|
+
execution,
|
|
1015
|
+
webAccess,
|
|
1016
|
+
webAccessUrl,
|
|
1017
|
+
hostIpv4,
|
|
1018
|
+
ipv6,
|
|
1019
|
+
proxyUrl,
|
|
1020
|
+
mappedPorts,
|
|
1021
|
+
diagnostics,
|
|
1022
|
+
sshCommand: hostIpv4 && sshPort ? `ssh root@${hostIpv4} -p ${sshPort}` : ipv6 ? `ssh root@${ipv6}` : null,
|
|
1023
|
+
selectedCrn,
|
|
1024
|
+
executionLookupBlocked
|
|
1071
1025
|
};
|
|
1072
1026
|
}
|
|
1073
|
-
async function
|
|
1074
|
-
const
|
|
1075
|
-
const
|
|
1076
|
-
const
|
|
1077
|
-
|
|
1078
|
-
|
|
1027
|
+
async function waitForVmRuntime(args) {
|
|
1028
|
+
const attempts = Math.max(1, Number(args.attempts ?? 20));
|
|
1029
|
+
const delayMs = Math.max(0, Number(args.delayMs ?? 4e3));
|
|
1030
|
+
const sleep2 = args.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
1031
|
+
let lastRuntime = await fetchVmRuntime(args);
|
|
1032
|
+
args.onAttempt?.(lastRuntime, 1, attempts);
|
|
1033
|
+
for (let attempt = 0; attempt < attempts - 1; attempt += 1) {
|
|
1034
|
+
if (lastRuntime.diagnostics?.state === "ready" || lastRuntime.diagnostics?.state === "execution-invalid-public-ipv6") {
|
|
1035
|
+
return lastRuntime;
|
|
1036
|
+
}
|
|
1037
|
+
await sleep2(delayMs);
|
|
1038
|
+
lastRuntime = await fetchVmRuntime(args);
|
|
1039
|
+
args.onAttempt?.(lastRuntime, attempt + 2, attempts);
|
|
1079
1040
|
}
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
key: aggregateKey,
|
|
1083
|
-
fetch: args.fetch,
|
|
1084
|
-
apiHost: args.apiHost
|
|
1085
|
-
});
|
|
1086
|
-
const existingRecords = normalizeRetentionLedger(existingValue);
|
|
1087
|
-
const mergedRecords = [currentRecord, ...existingRecords];
|
|
1088
|
-
const uniqueRecords = [];
|
|
1089
|
-
const seenIds = /* @__PURE__ */ new Set();
|
|
1090
|
-
for (const record of mergedRecords) {
|
|
1091
|
-
const id = retentionRecordId(record);
|
|
1092
|
-
if (seenIds.has(id)) continue;
|
|
1093
|
-
seenIds.add(id);
|
|
1094
|
-
uniqueRecords.push(record);
|
|
1041
|
+
if (lastRuntime.diagnostics) {
|
|
1042
|
+
lastRuntime.diagnostics.timedOut = true;
|
|
1095
1043
|
}
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1044
|
+
return lastRuntime;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
// src/crn-control.ts
|
|
1048
|
+
function bytesToHex(bytes) {
|
|
1049
|
+
return Array.from(bytes).map((byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1050
|
+
}
|
|
1051
|
+
function utf8Hex(value) {
|
|
1052
|
+
return bytesToHex(new TextEncoder().encode(value));
|
|
1053
|
+
}
|
|
1054
|
+
function normalizeHexSignature(signature) {
|
|
1055
|
+
return signature.startsWith("0x") ? signature : `0x${signature}`;
|
|
1056
|
+
}
|
|
1057
|
+
function asString4(value) {
|
|
1058
|
+
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
1059
|
+
}
|
|
1060
|
+
function trimTrailingSlashes(value) {
|
|
1061
|
+
return value.replace(/\/+$/u, "");
|
|
1062
|
+
}
|
|
1063
|
+
function isoTimestampWithMicros(date = /* @__PURE__ */ new Date()) {
|
|
1064
|
+
return date.toISOString().replace(/\.(\d{3})Z$/u, ".$1000Z");
|
|
1065
|
+
}
|
|
1066
|
+
async function generateEphemeralKeyPair() {
|
|
1067
|
+
return globalThis.crypto.subtle.generateKey(
|
|
1068
|
+
{
|
|
1069
|
+
name: "ECDSA",
|
|
1070
|
+
namedCurve: "P-256"
|
|
1071
|
+
},
|
|
1072
|
+
true,
|
|
1073
|
+
["sign", "verify"]
|
|
1074
|
+
);
|
|
1075
|
+
}
|
|
1076
|
+
async function exportP256PublicJwk(publicKey) {
|
|
1077
|
+
const jwk = await globalThis.crypto.subtle.exportKey("jwk", publicKey);
|
|
1078
|
+
return {
|
|
1079
|
+
kty: String(jwk.kty ?? "EC"),
|
|
1080
|
+
crv: String(jwk.crv ?? "P-256"),
|
|
1081
|
+
x: String(jwk.x ?? ""),
|
|
1082
|
+
y: String(jwk.y ?? "")
|
|
1110
1083
|
};
|
|
1111
|
-
|
|
1084
|
+
}
|
|
1085
|
+
async function buildSignedPubKeyHeader(args) {
|
|
1086
|
+
const payload = {
|
|
1087
|
+
pubkey: await exportP256PublicJwk(args.publicKey),
|
|
1088
|
+
alg: "ECDSA",
|
|
1089
|
+
domain: args.domain,
|
|
1090
|
+
address: args.sender,
|
|
1091
|
+
expires: isoTimestampWithMicros(new Date(Date.now() + 24 * 60 * 60 * 1e3)),
|
|
1092
|
+
chain: "ETH"
|
|
1093
|
+
};
|
|
1094
|
+
const payloadJson = JSON.stringify(payload);
|
|
1095
|
+
const signature = normalizeHexSignature(await args.signer(args.sender, payloadJson));
|
|
1096
|
+
return JSON.stringify({
|
|
1112
1097
|
sender: args.sender,
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1098
|
+
payload: utf8Hex(payloadJson),
|
|
1099
|
+
signature,
|
|
1100
|
+
content: {
|
|
1101
|
+
domain: args.domain
|
|
1102
|
+
}
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
async function buildSignedOperationHeader(args) {
|
|
1106
|
+
const payload = {
|
|
1107
|
+
time: isoTimestampWithMicros(),
|
|
1108
|
+
method: args.method,
|
|
1109
|
+
path: args.path,
|
|
1110
|
+
domain: args.domain
|
|
1111
|
+
};
|
|
1112
|
+
const payloadJson = JSON.stringify(payload);
|
|
1113
|
+
const signature = new Uint8Array(
|
|
1114
|
+
await globalThis.crypto.subtle.sign(
|
|
1115
|
+
{
|
|
1116
|
+
name: "ECDSA",
|
|
1117
|
+
hash: "SHA-256"
|
|
1118
|
+
},
|
|
1119
|
+
args.privateKey,
|
|
1120
|
+
new TextEncoder().encode(payloadJson)
|
|
1121
|
+
)
|
|
1122
|
+
);
|
|
1123
|
+
return JSON.stringify({
|
|
1124
|
+
payload: utf8Hex(payloadJson),
|
|
1125
|
+
signature: bytesToHex(signature)
|
|
1126
|
+
});
|
|
1127
|
+
}
|
|
1128
|
+
async function fetchInstanceNodeHash(args) {
|
|
1129
|
+
const envelope = await fetchMessageEnvelope(args.instanceHash, {
|
|
1117
1130
|
fetch: args.fetch,
|
|
1118
|
-
|
|
1119
|
-
apiHost: args.apiHost,
|
|
1120
|
-
now: args.now
|
|
1131
|
+
apiHost: args.apiHost
|
|
1121
1132
|
});
|
|
1122
|
-
const
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
reason: forgetReason,
|
|
1134
|
-
signer: args.signer,
|
|
1135
|
-
hasher: args.hasher,
|
|
1136
|
-
fetch: args.fetch,
|
|
1137
|
-
channel: args.channel,
|
|
1138
|
-
apiHost: args.apiHost,
|
|
1139
|
-
now: args.now
|
|
1140
|
-
});
|
|
1141
|
-
forgetStageResults.push({ stage: "instances", ...instanceStage });
|
|
1142
|
-
forgottenHashes.push(...instanceStage.forgottenHashes);
|
|
1143
|
-
outstandingForgetHashes.push(...instanceStage.outstandingForgetHashes);
|
|
1144
|
-
forgetStatuses = { ...forgetStatuses, ...instanceStage.statuses };
|
|
1145
|
-
if (!forgetResult && instanceStage.forgetResult) {
|
|
1146
|
-
forgetResult = instanceStage.forgetResult;
|
|
1147
|
-
}
|
|
1148
|
-
followUpForgetResults.push(...instanceStage.followUpForgetResults);
|
|
1133
|
+
const payload = envelope;
|
|
1134
|
+
return asString4(payload?.content?.requirements?.node?.node_hash) ?? asString4(payload?.message?.content?.requirements?.node?.node_hash) ?? null;
|
|
1135
|
+
}
|
|
1136
|
+
async function resolveInstanceCrn(args) {
|
|
1137
|
+
const providedCrnUrl = asString4(args.crnUrl);
|
|
1138
|
+
if (providedCrnUrl) {
|
|
1139
|
+
return {
|
|
1140
|
+
crnUrl: trimTrailingSlashes(providedCrnUrl),
|
|
1141
|
+
crnHash: asString4(args.crnHash),
|
|
1142
|
+
source: "provided"
|
|
1143
|
+
};
|
|
1149
1144
|
}
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
stage: "dependents",
|
|
1179
|
-
hashes: dependentForgetHashes,
|
|
1180
|
-
skipped: true,
|
|
1181
|
-
skippedReason: "Waiting for instance forget stage to complete before pruning dependent store items.",
|
|
1182
|
-
forgottenHashes: dependentStageStatus.forgottenHashes,
|
|
1183
|
-
outstandingForgetHashes: dependentStageStatus.outstandingForgetHashes,
|
|
1184
|
-
statuses: dependentStageStatus.statuses,
|
|
1185
|
-
forgetResult: null,
|
|
1186
|
-
followUpForgetResults: []
|
|
1187
|
-
});
|
|
1188
|
-
forgottenHashes.push(...dependentStageStatus.forgottenHashes);
|
|
1189
|
-
outstandingForgetHashes.push(...dependentStageStatus.outstandingForgetHashes);
|
|
1190
|
-
forgetStatuses = { ...forgetStatuses, ...dependentStageStatus.statuses };
|
|
1145
|
+
const allocation = await fetchSchedulerAllocation(args.instanceHash, {
|
|
1146
|
+
fetch: args.fetch,
|
|
1147
|
+
schedulerAllocationUrl: args.schedulerAllocationUrl ?? DEFAULT_SCHEDULER_ALLOCATION_URL
|
|
1148
|
+
}).catch(() => null);
|
|
1149
|
+
if (allocation?.crnUrl) {
|
|
1150
|
+
return {
|
|
1151
|
+
crnUrl: trimTrailingSlashes(allocation.crnUrl),
|
|
1152
|
+
crnHash: asString4(allocation.crnHash),
|
|
1153
|
+
source: "scheduler"
|
|
1154
|
+
};
|
|
1155
|
+
}
|
|
1156
|
+
const manualCrnHash = asString4(args.crnHash) ?? await fetchInstanceNodeHash({
|
|
1157
|
+
instanceHash: args.instanceHash,
|
|
1158
|
+
fetch: args.fetch,
|
|
1159
|
+
apiHost: args.apiHost ?? DEFAULT_ALEPH_API_HOST
|
|
1160
|
+
}).catch(() => null);
|
|
1161
|
+
if (manualCrnHash) {
|
|
1162
|
+
const crns = await fetchCrns({
|
|
1163
|
+
url: args.crnListUrl ?? DEFAULT_CRN_LIST_URL,
|
|
1164
|
+
fetch: args.fetch
|
|
1165
|
+
}).catch(() => []);
|
|
1166
|
+
const matchingCrn = crns.find((candidate) => candidate.hash === manualCrnHash);
|
|
1167
|
+
if (matchingCrn?.address) {
|
|
1168
|
+
return {
|
|
1169
|
+
crnUrl: trimTrailingSlashes(matchingCrn.address),
|
|
1170
|
+
crnHash: manualCrnHash,
|
|
1171
|
+
source: "manual"
|
|
1172
|
+
};
|
|
1191
1173
|
}
|
|
1192
1174
|
}
|
|
1193
|
-
forgottenHashes = uniqueHashes(forgottenHashes);
|
|
1194
|
-
outstandingForgetHashes = uniqueHashes(outstandingForgetHashes);
|
|
1195
1175
|
return {
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
aggregatePublication,
|
|
1200
|
-
retainedRecords,
|
|
1201
|
-
prunedRecords,
|
|
1202
|
-
forgetHashes,
|
|
1203
|
-
forgottenHashes,
|
|
1204
|
-
outstandingForgetHashes,
|
|
1205
|
-
forgetStatuses,
|
|
1206
|
-
forgetResult,
|
|
1207
|
-
followUpForgetResults,
|
|
1208
|
-
forgetStageResults
|
|
1176
|
+
crnUrl: null,
|
|
1177
|
+
crnHash: manualCrnHash,
|
|
1178
|
+
source: "missing"
|
|
1209
1179
|
};
|
|
1210
1180
|
}
|
|
1181
|
+
async function eraseInstanceOnCrn(args) {
|
|
1182
|
+
const resolution = await resolveInstanceCrn({
|
|
1183
|
+
instanceHash: args.instanceHash,
|
|
1184
|
+
fetch: args.fetch,
|
|
1185
|
+
apiHost: args.apiHost,
|
|
1186
|
+
crnUrl: args.crnUrl,
|
|
1187
|
+
crnHash: args.crnHash,
|
|
1188
|
+
crnListUrl: args.crnListUrl,
|
|
1189
|
+
schedulerAllocationUrl: args.schedulerAllocationUrl
|
|
1190
|
+
});
|
|
1191
|
+
if (!resolution.crnUrl) {
|
|
1192
|
+
return {
|
|
1193
|
+
...resolution,
|
|
1194
|
+
status: "skipped"
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
const crnUrl = new URL(resolution.crnUrl);
|
|
1198
|
+
const domain = crnUrl.host;
|
|
1199
|
+
const path = `/control/machine/${args.instanceHash}/erase`;
|
|
1200
|
+
const { privateKey, publicKey } = await generateEphemeralKeyPair();
|
|
1201
|
+
const signedPubKeyHeader = await buildSignedPubKeyHeader({
|
|
1202
|
+
sender: args.sender,
|
|
1203
|
+
signer: args.signer,
|
|
1204
|
+
domain,
|
|
1205
|
+
publicKey
|
|
1206
|
+
});
|
|
1207
|
+
const signedOperationHeader = await buildSignedOperationHeader({
|
|
1208
|
+
privateKey,
|
|
1209
|
+
domain,
|
|
1210
|
+
method: "POST",
|
|
1211
|
+
path
|
|
1212
|
+
});
|
|
1213
|
+
const response = await args.fetch(`${trimTrailingSlashes(resolution.crnUrl)}${path}`, {
|
|
1214
|
+
method: "POST",
|
|
1215
|
+
headers: {
|
|
1216
|
+
"X-SignedPubKey": signedPubKeyHeader,
|
|
1217
|
+
"X-SignedOperation": signedOperationHeader
|
|
1218
|
+
}
|
|
1219
|
+
});
|
|
1220
|
+
if (response.status === 404) {
|
|
1221
|
+
return {
|
|
1222
|
+
...resolution,
|
|
1223
|
+
status: "missing"
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
if (response.ok) {
|
|
1227
|
+
return {
|
|
1228
|
+
...resolution,
|
|
1229
|
+
status: "erased"
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
const errorBody = await response.json().then((payload) => JSON.stringify(payload)).catch(() => "");
|
|
1233
|
+
throw new Error(
|
|
1234
|
+
`CRN erase failed for ${args.instanceHash} on ${resolution.crnUrl}: ${response.status}${errorBody ? ` ${errorBody}` : ""}`
|
|
1235
|
+
);
|
|
1236
|
+
}
|
|
1211
1237
|
|
|
1212
|
-
// src/
|
|
1213
|
-
function
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1238
|
+
// src/forget.ts
|
|
1239
|
+
function asOptionalReason(value) {
|
|
1240
|
+
return typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
1241
|
+
}
|
|
1242
|
+
async function createUnsignedForgetMessage(args) {
|
|
1243
|
+
const content = {
|
|
1217
1244
|
address: args.sender,
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
}
|
|
1223
|
-
},
|
|
1224
|
-
time: args.now ?? Date.now() / 1e3
|
|
1245
|
+
time: args.now ?? Date.now() / 1e3,
|
|
1246
|
+
hashes: [...new Set((args.hashes ?? []).filter(Boolean))],
|
|
1247
|
+
aggregates: [...new Set((args.aggregates ?? []).filter(Boolean))],
|
|
1248
|
+
reason: asOptionalReason(args.reason)
|
|
1225
1249
|
};
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1250
|
+
if (content.hashes.length === 0 && content.aggregates.length === 0) {
|
|
1251
|
+
throw new Error("FORGET message requires at least one hash or aggregate key.");
|
|
1252
|
+
}
|
|
1253
|
+
const itemContent = JSON.stringify(content);
|
|
1229
1254
|
const itemHash = await args.hasher(itemContent);
|
|
1230
1255
|
return {
|
|
1231
1256
|
sender: args.sender,
|
|
1232
1257
|
chain: "ETH",
|
|
1233
|
-
type: "
|
|
1258
|
+
type: "FORGET",
|
|
1234
1259
|
item_hash: itemHash,
|
|
1235
1260
|
item_type: "inline",
|
|
1236
1261
|
item_content: itemContent,
|
|
@@ -1238,169 +1263,165 @@ async function createUnsignedAggregateMessage2(args) {
|
|
|
1238
1263
|
channel: args.channel ?? DEFAULT_ALEPH_CHANNEL
|
|
1239
1264
|
};
|
|
1240
1265
|
}
|
|
1241
|
-
async function
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
existingAggregate: aggregate
|
|
1266
|
+
async function forgetAlephMessages(args) {
|
|
1267
|
+
args.onProgress?.({
|
|
1268
|
+
stage: "building-delete-message",
|
|
1269
|
+
label: "Building delete message",
|
|
1270
|
+
progress: 18,
|
|
1271
|
+
status: "info",
|
|
1272
|
+
itemHash: null,
|
|
1273
|
+
detail: [...new Set((args.hashes ?? []).filter(Boolean))].join(", ") || null,
|
|
1274
|
+
error: null,
|
|
1275
|
+
timestamp: Date.now()
|
|
1252
1276
|
});
|
|
1253
|
-
const unsignedMessage = await
|
|
1277
|
+
const unsignedMessage = await createUnsignedForgetMessage({
|
|
1254
1278
|
sender: args.sender,
|
|
1255
|
-
|
|
1279
|
+
hashes: args.hashes,
|
|
1280
|
+
aggregates: args.aggregates,
|
|
1281
|
+
reason: args.reason,
|
|
1256
1282
|
hasher: args.hasher,
|
|
1257
|
-
channel: args.channel
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1283
|
+
channel: args.channel,
|
|
1284
|
+
now: args.now
|
|
1285
|
+
});
|
|
1286
|
+
args.onProgress?.({
|
|
1287
|
+
stage: "signing-delete-message",
|
|
1288
|
+
label: "Waiting for delete signature",
|
|
1289
|
+
progress: 42,
|
|
1290
|
+
status: "info",
|
|
1291
|
+
itemHash: unsignedMessage.item_hash,
|
|
1292
|
+
detail: unsignedMessage.item_hash,
|
|
1293
|
+
error: null,
|
|
1294
|
+
timestamp: Date.now()
|
|
1295
|
+
});
|
|
1296
|
+
const message = await signAlephMessage(unsignedMessage, args.signer);
|
|
1297
|
+
args.onProgress?.({
|
|
1298
|
+
stage: "broadcasting-delete",
|
|
1299
|
+
label: "Broadcasting delete to Aleph",
|
|
1300
|
+
progress: 68,
|
|
1301
|
+
status: "warning",
|
|
1302
|
+
itemHash: message.item_hash,
|
|
1303
|
+
detail: message.item_hash,
|
|
1304
|
+
error: null,
|
|
1305
|
+
timestamp: Date.now()
|
|
1306
|
+
});
|
|
1307
|
+
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
1308
|
+
apiHost: args.apiHost,
|
|
1309
|
+
sync: args.sync,
|
|
1310
|
+
fetch: args.fetch
|
|
1311
|
+
});
|
|
1312
|
+
const status = normalizeBroadcastStatus(httpStatus, response?.message_status);
|
|
1313
|
+
args.onProgress?.({
|
|
1314
|
+
stage: status === "rejected" ? "error" : "delete-completed",
|
|
1315
|
+
label: status === "rejected" ? "Delete rejected by Aleph" : "Delete submitted to Aleph",
|
|
1316
|
+
progress: 100,
|
|
1317
|
+
status: status === "rejected" ? "error" : "success",
|
|
1318
|
+
itemHash: message.item_hash,
|
|
1319
|
+
detail: status === "rejected" ? String(response?.details ?? "Aleph rejected this delete request.") : null,
|
|
1320
|
+
error: status === "rejected" ? String(response?.details ?? "Aleph rejected this delete request.") : null,
|
|
1321
|
+
timestamp: Date.now()
|
|
1264
1322
|
});
|
|
1265
|
-
const aggregateStatus = normalizeBroadcastStatus(httpStatus, response.message_status);
|
|
1266
|
-
if (aggregateStatus === "rejected") {
|
|
1267
|
-
throw new Error(`Port-forward aggregate was rejected by Aleph: ${JSON.stringify(response.details ?? response)}`);
|
|
1268
|
-
}
|
|
1269
1323
|
return {
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1324
|
+
sender: args.sender,
|
|
1325
|
+
itemHash: message.item_hash,
|
|
1326
|
+
response,
|
|
1327
|
+
httpStatus,
|
|
1328
|
+
status
|
|
1273
1329
|
};
|
|
1274
1330
|
}
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
if (typeof randomUuid === "string" && randomUuid.trim()) {
|
|
1292
|
-
return randomUuid;
|
|
1331
|
+
async function cleanupFailedDeployment(args) {
|
|
1332
|
+
try {
|
|
1333
|
+
return await forgetAlephMessages({
|
|
1334
|
+
sender: args.sender,
|
|
1335
|
+
hashes: [args.instanceItemHash],
|
|
1336
|
+
reason: args.reason ?? "Discard failed deployment attempt",
|
|
1337
|
+
signer: args.signer,
|
|
1338
|
+
hasher: args.hasher,
|
|
1339
|
+
fetch: args.fetch,
|
|
1340
|
+
channel: args.channel,
|
|
1341
|
+
apiHost: args.apiHost
|
|
1342
|
+
});
|
|
1343
|
+
} catch (error) {
|
|
1344
|
+
return {
|
|
1345
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1346
|
+
};
|
|
1293
1347
|
}
|
|
1294
|
-
return `deploy-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
|
|
1295
1348
|
}
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
const token = deploymentToken.trim();
|
|
1302
|
-
if (!algorithm || !key || !owner || !token) {
|
|
1303
|
-
return normalized;
|
|
1304
|
-
}
|
|
1305
|
-
return `${algorithm} ${key} aleph-bootstrap-config:${owner}:${token}`;
|
|
1349
|
+
|
|
1350
|
+
// src/retention.ts
|
|
1351
|
+
var SUCCESSFUL_DEPLOYMENTS_AGGREGATE_KEY = "uc-go-peer-successful-deployments";
|
|
1352
|
+
function asString5(value) {
|
|
1353
|
+
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
1306
1354
|
}
|
|
1307
|
-
function
|
|
1355
|
+
function normalizeRetentionRecord(record) {
|
|
1356
|
+
if (!record || typeof record !== "object" || Array.isArray(record)) return null;
|
|
1357
|
+
const candidate = record;
|
|
1358
|
+
const instanceItemHash = asString5(candidate.instance_item_hash);
|
|
1359
|
+
if (!instanceItemHash) return null;
|
|
1308
1360
|
return {
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1361
|
+
instance_item_hash: instanceItemHash,
|
|
1362
|
+
rootfs_item_hash: asString5(candidate.rootfs_item_hash) ?? "",
|
|
1363
|
+
site_item_hash: asString5(candidate.site_item_hash) ?? "",
|
|
1364
|
+
rootfs_cid: asString5(candidate.rootfs_cid) ?? "",
|
|
1365
|
+
site_url: asString5(candidate.site_url) ?? "",
|
|
1366
|
+
relay_peer_id: asString5(candidate.relay_peer_id) ?? "",
|
|
1367
|
+
rootfs_version: asString5(candidate.rootfs_version) ?? "",
|
|
1368
|
+
deployed_at: asString5(candidate.deployed_at) ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
1369
|
+
vm_name: asString5(candidate.vm_name) ?? ""
|
|
1312
1370
|
};
|
|
1313
1371
|
}
|
|
1314
|
-
function
|
|
1315
|
-
|
|
1372
|
+
function normalizeRetentionLedger(value) {
|
|
1373
|
+
if (Array.isArray(value)) {
|
|
1374
|
+
return value.map(normalizeRetentionRecord).filter((entry) => entry != null);
|
|
1375
|
+
}
|
|
1376
|
+
if (value && typeof value === "object" && Array.isArray(value.deployments)) {
|
|
1377
|
+
return value.deployments.map(normalizeRetentionRecord).filter((entry) => entry != null);
|
|
1378
|
+
}
|
|
1379
|
+
return [];
|
|
1316
1380
|
}
|
|
1317
|
-
function
|
|
1318
|
-
return
|
|
1319
|
-
vcpus: pricing.compute_unit.vcpus * tier.compute_units,
|
|
1320
|
-
memoryMiB: pricing.compute_unit.memory_mib * tier.compute_units,
|
|
1321
|
-
diskMiB: pricing.compute_unit.disk_mib * tier.compute_units
|
|
1322
|
-
};
|
|
1381
|
+
function retentionRecordId(record) {
|
|
1382
|
+
return [record.instance_item_hash, record.rootfs_item_hash, record.site_item_hash].join(":");
|
|
1323
1383
|
}
|
|
1324
|
-
function
|
|
1325
|
-
|
|
1326
|
-
if (!computeUnitPrice) return null;
|
|
1327
|
-
const unitPrice = toFiniteNumber(computeUnitPrice.credit);
|
|
1328
|
-
if (!Number.isFinite(unitPrice)) return null;
|
|
1329
|
-
return {
|
|
1330
|
-
required: unitPrice * tier.compute_units,
|
|
1331
|
-
available: Number(balance.credit_balance ?? 0),
|
|
1332
|
-
computeUnits: tier.compute_units,
|
|
1333
|
-
unitPrice,
|
|
1334
|
-
label: "credits"
|
|
1335
|
-
};
|
|
1384
|
+
function hashesFromRetentionRecord(record) {
|
|
1385
|
+
return [record.instance_item_hash, record.rootfs_item_hash, record.site_item_hash].filter(Boolean);
|
|
1336
1386
|
}
|
|
1337
|
-
function
|
|
1338
|
-
|
|
1339
|
-
return BigInt(Math.ceil(quote.required * 1e18));
|
|
1387
|
+
function uniqueHashes(hashes) {
|
|
1388
|
+
return [...new Set(hashes.filter(Boolean))];
|
|
1340
1389
|
}
|
|
1341
|
-
function
|
|
1342
|
-
|
|
1343
|
-
if (balanceMessage) {
|
|
1344
|
-
return `Aleph rejected this deployment due to ${balanceMessage}.`;
|
|
1345
|
-
}
|
|
1346
|
-
const errorCode = typeof response.error_code === "number" ? response.error_code : null;
|
|
1347
|
-
return `Aleph rejected this deployment${errorCode ? ` (error ${errorCode})` : ""}.`;
|
|
1390
|
+
function dependentHashesFromRetentionRecord(record) {
|
|
1391
|
+
return [record.rootfs_item_hash, record.site_item_hash].filter(Boolean);
|
|
1348
1392
|
}
|
|
1349
|
-
function
|
|
1350
|
-
const
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
if (!args.rootfsItemHash || !/^[a-fA-F0-9]{64}$/.test(args.rootfsItemHash)) {
|
|
1358
|
-
throw new Error("rootfsItemHash must be a 64-character Aleph item hash.");
|
|
1359
|
-
}
|
|
1393
|
+
function splitForgetStages(args) {
|
|
1394
|
+
const instanceForgetHashes = uniqueHashes(args.prunedRecords.map((record) => record.instance_item_hash)).filter(
|
|
1395
|
+
(hash) => !args.retainedHashes.has(hash)
|
|
1396
|
+
);
|
|
1397
|
+
const dependentForgetHashes = uniqueHashes([
|
|
1398
|
+
...args.prunedRecords.flatMap(dependentHashesFromRetentionRecord),
|
|
1399
|
+
...args.extraForgetHashes
|
|
1400
|
+
]).filter((hash) => !args.retainedHashes.has(hash) && !instanceForgetHashes.includes(hash));
|
|
1360
1401
|
return {
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
metadata: createReleaseMetadata(args.name.trim(), args.rootfsVersion ?? "custom-rootfs", args.deployer),
|
|
1365
|
-
authorized_keys: [sshKey],
|
|
1366
|
-
environment: {
|
|
1367
|
-
internet: true,
|
|
1368
|
-
aleph_api: true,
|
|
1369
|
-
hypervisor: "qemu",
|
|
1370
|
-
reproducible: false,
|
|
1371
|
-
shared_cache: false
|
|
1372
|
-
},
|
|
1373
|
-
resources: {
|
|
1374
|
-
vcpus: Number(args.vcpus),
|
|
1375
|
-
memory: Number(args.memoryMiB),
|
|
1376
|
-
seconds: Number(args.seconds ?? 30)
|
|
1377
|
-
},
|
|
1378
|
-
payment: {
|
|
1379
|
-
type: "credit"
|
|
1380
|
-
},
|
|
1381
|
-
requirements: args.crnHash ? {
|
|
1382
|
-
node: {
|
|
1383
|
-
node_hash: args.crnHash
|
|
1384
|
-
}
|
|
1385
|
-
} : void 0,
|
|
1386
|
-
volumes: [],
|
|
1387
|
-
rootfs: {
|
|
1388
|
-
parent: {
|
|
1389
|
-
ref: args.rootfsItemHash,
|
|
1390
|
-
use_latest: true
|
|
1391
|
-
},
|
|
1392
|
-
persistence: "host",
|
|
1393
|
-
size_mib: Number(args.rootfsSizeMiB)
|
|
1394
|
-
}
|
|
1402
|
+
instanceForgetHashes,
|
|
1403
|
+
dependentForgetHashes,
|
|
1404
|
+
orderedForgetHashes: [...instanceForgetHashes, ...dependentForgetHashes]
|
|
1395
1405
|
};
|
|
1396
1406
|
}
|
|
1397
|
-
async function
|
|
1407
|
+
async function fetchAggregateKey(args) {
|
|
1408
|
+
const requestUrl = new URL(`/api/v0/aggregates/${args.address}.json`, args.apiHost ?? "https://api.aleph.im");
|
|
1409
|
+
requestUrl.searchParams.set("keys", args.key);
|
|
1410
|
+
const response = await args.fetch(requestUrl.toString(), { cache: "no-cache" });
|
|
1411
|
+
if (response.status === 404) return {};
|
|
1412
|
+
if (!response.ok) {
|
|
1413
|
+
throw new Error(`Aggregate request failed for key ${args.key}: ${response.status}`);
|
|
1414
|
+
}
|
|
1415
|
+
const payload = await response.json();
|
|
1416
|
+
return payload?.data?.[args.key] ?? {};
|
|
1417
|
+
}
|
|
1418
|
+
async function createUnsignedAggregateMessage(args) {
|
|
1398
1419
|
const itemContent = JSON.stringify(args.content);
|
|
1399
1420
|
const itemHash = await args.hasher(itemContent);
|
|
1400
1421
|
return {
|
|
1401
1422
|
sender: args.sender,
|
|
1402
1423
|
chain: "ETH",
|
|
1403
|
-
type: "
|
|
1424
|
+
type: "AGGREGATE",
|
|
1404
1425
|
item_hash: itemHash,
|
|
1405
1426
|
item_type: "inline",
|
|
1406
1427
|
item_content: itemContent,
|
|
@@ -1408,375 +1429,566 @@ async function createUnsignedInstanceMessage(args) {
|
|
|
1408
1429
|
channel: args.channel ?? DEFAULT_ALEPH_CHANNEL
|
|
1409
1430
|
};
|
|
1410
1431
|
}
|
|
1411
|
-
async function
|
|
1412
|
-
const
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
rootfsRef: args.content.rootfs.parent.ref,
|
|
1418
|
-
rootfsSizeMiB: args.content.rootfs.size_mib,
|
|
1419
|
-
computeUnits: args.computeUnits,
|
|
1420
|
-
vcpus: args.content.resources.vcpus,
|
|
1421
|
-
memoryMiB: args.content.resources.memory,
|
|
1422
|
-
crnHash: args.content.requirements?.node?.node_hash ?? null,
|
|
1423
|
-
channel: args.unsignedMessage.channel,
|
|
1424
|
-
expiresAt: args.expiresAt,
|
|
1425
|
-
maxCost: args.maxCost
|
|
1432
|
+
async function publishAggregateKey(args) {
|
|
1433
|
+
const aggregateContent = {
|
|
1434
|
+
address: args.sender,
|
|
1435
|
+
key: args.key,
|
|
1436
|
+
content: args.content,
|
|
1437
|
+
time: args.now ?? Date.now() / 1e3
|
|
1426
1438
|
};
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
error: null,
|
|
1441
|
-
timestamp: Date.now()
|
|
1442
|
-
});
|
|
1443
|
-
const unsignedMessage = await createUnsignedInstanceMessage({
|
|
1444
|
-
sender: args.sender,
|
|
1445
|
-
content: args.content,
|
|
1446
|
-
hasher: args.hasher,
|
|
1447
|
-
channel: args.channel,
|
|
1448
|
-
now: args.now
|
|
1449
|
-
});
|
|
1450
|
-
args.onProgress?.({
|
|
1451
|
-
stage: "signing-message",
|
|
1452
|
-
label: "Waiting for MetaMask signature",
|
|
1453
|
-
progress: 48,
|
|
1454
|
-
status: "info",
|
|
1455
|
-
detail: unsignedMessage.item_hash,
|
|
1456
|
-
itemHash: unsignedMessage.item_hash,
|
|
1457
|
-
error: null,
|
|
1458
|
-
timestamp: Date.now()
|
|
1459
|
-
});
|
|
1460
|
-
const signature = await args.signer(unsignedMessage.sender, [unsignedMessage.chain, unsignedMessage.sender, unsignedMessage.type, unsignedMessage.item_hash].join("\n"));
|
|
1461
|
-
const message = {
|
|
1462
|
-
...unsignedMessage,
|
|
1463
|
-
signature: signature.startsWith("0x") ? signature : `0x${signature}`
|
|
1464
|
-
};
|
|
1465
|
-
args.onProgress?.({
|
|
1466
|
-
stage: "broadcasting",
|
|
1467
|
-
label: "Broadcasting to Aleph",
|
|
1468
|
-
progress: 64,
|
|
1469
|
-
status: "info",
|
|
1470
|
-
detail: message.item_hash,
|
|
1471
|
-
itemHash: message.item_hash,
|
|
1472
|
-
error: null,
|
|
1473
|
-
timestamp: Date.now()
|
|
1474
|
-
});
|
|
1475
|
-
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
1476
|
-
apiHost: args.apiHost,
|
|
1477
|
-
sync: args.sync,
|
|
1478
|
-
fetch: args.fetch
|
|
1479
|
-
});
|
|
1480
|
-
const status = httpStatus === 202 ? "pending" : typeof response.message_status === "string" ? response.message_status : "unknown";
|
|
1481
|
-
args.onProgress?.({
|
|
1482
|
-
stage: status === "processed" ? "deployment-confirmed" : status === "rejected" ? "deployment-rejected" : "waiting-for-aleph",
|
|
1483
|
-
label: status === "processed" ? "Deployment accepted by Aleph" : status === "rejected" ? "Deployment rejected by Aleph" : "Deployment submitted to Aleph",
|
|
1484
|
-
progress: status === "processed" ? 88 : status === "rejected" ? 100 : 78,
|
|
1485
|
-
status: status === "processed" ? "success" : status === "rejected" ? "error" : "warning",
|
|
1486
|
-
detail: status === "rejected" ? describeRejectedInstanceBroadcast(response) : null,
|
|
1487
|
-
itemHash: message.item_hash,
|
|
1488
|
-
error: status === "rejected" ? describeRejectedInstanceBroadcast(response) : null,
|
|
1489
|
-
timestamp: Date.now()
|
|
1490
|
-
});
|
|
1439
|
+
const unsignedMessage = await createUnsignedAggregateMessage({
|
|
1440
|
+
sender: args.sender,
|
|
1441
|
+
content: aggregateContent,
|
|
1442
|
+
hasher: args.hasher,
|
|
1443
|
+
channel: args.channel,
|
|
1444
|
+
now: args.now
|
|
1445
|
+
});
|
|
1446
|
+
const message = await signAlephMessage(unsignedMessage, args.signer);
|
|
1447
|
+
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
1448
|
+
apiHost: args.apiHost,
|
|
1449
|
+
sync: true,
|
|
1450
|
+
fetch: args.fetch
|
|
1451
|
+
});
|
|
1491
1452
|
return {
|
|
1492
1453
|
itemHash: message.item_hash,
|
|
1493
|
-
httpStatus,
|
|
1494
|
-
status,
|
|
1495
|
-
message,
|
|
1454
|
+
status: normalizeBroadcastStatus(httpStatus, response?.message_status),
|
|
1496
1455
|
response,
|
|
1497
|
-
|
|
1456
|
+
httpStatus
|
|
1498
1457
|
};
|
|
1499
1458
|
}
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
return typeof value === "string" && value.trim() ? value.trim() : null;
|
|
1506
|
-
}
|
|
1507
|
-
function asNumber2(value) {
|
|
1508
|
-
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
1509
|
-
}
|
|
1510
|
-
function normalizeIpv6(value) {
|
|
1511
|
-
if (typeof value !== "string") return null;
|
|
1512
|
-
const normalized = value.trim().toLowerCase();
|
|
1513
|
-
if (!normalized) return null;
|
|
1514
|
-
const zoneIndex = normalized.indexOf("%");
|
|
1515
|
-
return zoneIndex >= 0 ? normalized.slice(0, zoneIndex) : normalized;
|
|
1516
|
-
}
|
|
1517
|
-
function isLikelyGlobalIpv6(value) {
|
|
1518
|
-
const normalized = normalizeIpv6(value);
|
|
1519
|
-
if (!normalized || normalized === "::" || normalized === "::1") return false;
|
|
1520
|
-
const firstHextetText = normalized.split(":", 1)[0];
|
|
1521
|
-
if (!firstHextetText) return false;
|
|
1522
|
-
const firstHextet = Number.parseInt(firstHextetText, 16);
|
|
1523
|
-
if (!Number.isFinite(firstHextet)) return false;
|
|
1524
|
-
if ((firstHextet & 65024) === 64512) return false;
|
|
1525
|
-
if ((firstHextet & 65472) === 65152) return false;
|
|
1526
|
-
if ((firstHextet & 65280) === 65280) return false;
|
|
1527
|
-
if ((firstHextet & 65472) === 65216) return false;
|
|
1528
|
-
return true;
|
|
1529
|
-
}
|
|
1530
|
-
function findCrnByHash(crns, crnHash) {
|
|
1531
|
-
return crns.find((crn) => crn.hash === crnHash) ?? null;
|
|
1532
|
-
}
|
|
1533
|
-
async function fetchSchedulerAllocation(itemHash, options) {
|
|
1534
|
-
const response = await options.fetch(
|
|
1535
|
-
`${options.schedulerAllocationUrl ?? DEFAULT_SCHEDULER_ALLOCATION_URL}/${itemHash}`,
|
|
1536
|
-
{ cache: "no-cache" }
|
|
1537
|
-
);
|
|
1538
|
-
if (response.status === 404) return null;
|
|
1539
|
-
if (!response.ok) {
|
|
1540
|
-
throw new Error(`Scheduler allocation request failed: ${response.status}`);
|
|
1541
|
-
}
|
|
1459
|
+
async function fetchMessageStatus(args) {
|
|
1460
|
+
const requestUrl = new URL(`/api/v0/messages/${args.hash}`, args.apiHost ?? "https://api.aleph.im");
|
|
1461
|
+
const response = await args.fetch(requestUrl.toString(), { cache: "no-cache" });
|
|
1462
|
+
if (response.status === 404) return "missing";
|
|
1463
|
+
if (!response.ok) return "unknown";
|
|
1542
1464
|
const payload = await response.json();
|
|
1543
|
-
|
|
1544
|
-
return {
|
|
1545
|
-
source: "scheduler",
|
|
1546
|
-
crnHash: asString4(node?.node_id),
|
|
1547
|
-
crnUrl: asString4(node?.url),
|
|
1548
|
-
node: node ? {
|
|
1549
|
-
node_id: asString4(node.node_id) ?? void 0,
|
|
1550
|
-
url: asString4(node.url) ?? void 0,
|
|
1551
|
-
ipv6: asString4(node.ipv6),
|
|
1552
|
-
supports_ipv6: typeof node.supports_ipv6 === "boolean" ? node.supports_ipv6 : void 0
|
|
1553
|
-
} : null,
|
|
1554
|
-
vmIpv6: asString4(payload?.vm_ipv6),
|
|
1555
|
-
period: payload?.period ? {
|
|
1556
|
-
start_timestamp: asString4(payload.period.start_timestamp) ?? void 0,
|
|
1557
|
-
duration_seconds: asNumber2(payload.period.duration_seconds) ?? void 0
|
|
1558
|
-
} : null
|
|
1559
|
-
};
|
|
1465
|
+
return asString5(payload?.status) ?? "unknown";
|
|
1560
1466
|
}
|
|
1561
|
-
async function
|
|
1562
|
-
const
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1467
|
+
async function classifyForgetHashes(args) {
|
|
1468
|
+
const statuses = {};
|
|
1469
|
+
for (const hash of args.hashes) {
|
|
1470
|
+
statuses[hash] = await fetchMessageStatus({
|
|
1471
|
+
hash,
|
|
1472
|
+
fetch: args.fetch,
|
|
1473
|
+
apiHost: args.apiHost
|
|
1474
|
+
});
|
|
1475
|
+
}
|
|
1476
|
+
const forgottenHashes = args.hashes.filter((hash) => statuses[hash] === "forgotten");
|
|
1477
|
+
const outstandingForgetHashes = args.hashes.filter((hash) => statuses[hash] !== "forgotten");
|
|
1568
1478
|
return {
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1479
|
+
forgottenHashes,
|
|
1480
|
+
outstandingForgetHashes,
|
|
1481
|
+
statuses
|
|
1572
1482
|
};
|
|
1573
1483
|
}
|
|
1574
|
-
async function
|
|
1575
|
-
const
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1484
|
+
async function executeForgetStage(args) {
|
|
1485
|
+
const stageHashes = uniqueHashes(args.hashes);
|
|
1486
|
+
let forgetResult = null;
|
|
1487
|
+
const followUpForgetResults = [];
|
|
1488
|
+
if (stageHashes.length === 0) {
|
|
1489
|
+
return {
|
|
1490
|
+
hashes: stageHashes,
|
|
1491
|
+
forgottenHashes: [],
|
|
1492
|
+
outstandingForgetHashes: [],
|
|
1493
|
+
statuses: {},
|
|
1494
|
+
forgetResult,
|
|
1495
|
+
followUpForgetResults
|
|
1496
|
+
};
|
|
1497
|
+
}
|
|
1498
|
+
let classified = await classifyForgetHashes({
|
|
1499
|
+
hashes: stageHashes,
|
|
1500
|
+
fetch: args.fetch,
|
|
1501
|
+
apiHost: args.apiHost
|
|
1502
|
+
});
|
|
1503
|
+
if (classified.outstandingForgetHashes.length > 0) {
|
|
1504
|
+
forgetResult = await forgetAlephMessages({
|
|
1505
|
+
sender: args.sender,
|
|
1506
|
+
hashes: classified.outstandingForgetHashes,
|
|
1507
|
+
reason: args.reason,
|
|
1508
|
+
signer: args.signer,
|
|
1509
|
+
hasher: args.hasher,
|
|
1510
|
+
fetch: args.fetch,
|
|
1511
|
+
channel: args.channel,
|
|
1512
|
+
apiHost: args.apiHost,
|
|
1513
|
+
now: args.now
|
|
1583
1514
|
});
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1515
|
+
classified = await classifyForgetHashes({
|
|
1516
|
+
hashes: stageHashes,
|
|
1517
|
+
fetch: args.fetch,
|
|
1518
|
+
apiHost: args.apiHost
|
|
1519
|
+
});
|
|
1520
|
+
if (classified.outstandingForgetHashes.length > 0) {
|
|
1521
|
+
for (const hash of classified.outstandingForgetHashes) {
|
|
1522
|
+
const result = await forgetAlephMessages({
|
|
1523
|
+
sender: args.sender,
|
|
1524
|
+
hashes: [hash],
|
|
1525
|
+
reason: args.reason,
|
|
1526
|
+
signer: args.signer,
|
|
1527
|
+
hasher: args.hasher,
|
|
1528
|
+
fetch: args.fetch,
|
|
1529
|
+
channel: args.channel,
|
|
1530
|
+
apiHost: args.apiHost,
|
|
1531
|
+
now: args.now
|
|
1532
|
+
});
|
|
1533
|
+
followUpForgetResults.push({ hash, result });
|
|
1534
|
+
}
|
|
1535
|
+
classified = await classifyForgetHashes({
|
|
1536
|
+
hashes: stageHashes,
|
|
1537
|
+
fetch: args.fetch,
|
|
1538
|
+
apiHost: args.apiHost
|
|
1539
|
+
});
|
|
1598
1540
|
}
|
|
1599
1541
|
}
|
|
1600
1542
|
return {
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1543
|
+
hashes: stageHashes,
|
|
1544
|
+
forgottenHashes: classified.forgottenHashes,
|
|
1545
|
+
outstandingForgetHashes: classified.outstandingForgetHashes,
|
|
1546
|
+
statuses: classified.statuses,
|
|
1547
|
+
forgetResult,
|
|
1548
|
+
followUpForgetResults
|
|
1604
1549
|
};
|
|
1605
1550
|
}
|
|
1606
|
-
function
|
|
1607
|
-
const
|
|
1608
|
-
const
|
|
1609
|
-
const
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
const proxyUrl = runtime.proxyUrl ?? null;
|
|
1613
|
-
const schedulerSource = runtime.allocation?.source ?? null;
|
|
1614
|
-
const webAccessActive = runtime.webAccess?.active ?? null;
|
|
1615
|
-
const mappedPortCount = Object.keys(mappedPorts).length;
|
|
1616
|
-
if (hostIpv4 && mappedPortCount > 0 && runtime.requirePublicGuestIpv6ForProxy === true && proxyUrl && executionGuestIpv6 && !isLikelyGlobalIpv6(executionGuestIpv6)) {
|
|
1617
|
-
return {
|
|
1618
|
-
state: "execution-invalid-public-ipv6",
|
|
1619
|
-
reason: `The CRN exposed runtime networking, but the guest IPv6 (${executionGuestIpv6}) is not globally routable. Proxy-backed HTTPS activation cannot succeed until the CRN reports a public guest IPv6.`,
|
|
1620
|
-
schedulerSource,
|
|
1621
|
-
executionSeen: Boolean(execution),
|
|
1622
|
-
webAccessActive,
|
|
1623
|
-
mappedPortCount,
|
|
1624
|
-
proxyUrl
|
|
1625
|
-
};
|
|
1626
|
-
}
|
|
1627
|
-
if (hostIpv4 && mappedPortCount > 0) {
|
|
1628
|
-
return {
|
|
1629
|
-
state: "ready",
|
|
1630
|
-
reason: null,
|
|
1631
|
-
schedulerSource,
|
|
1632
|
-
executionSeen: Boolean(execution),
|
|
1633
|
-
webAccessActive,
|
|
1634
|
-
mappedPortCount,
|
|
1635
|
-
proxyUrl
|
|
1636
|
-
};
|
|
1551
|
+
async function retainSuccessfulDeployments(args) {
|
|
1552
|
+
const keepCount = Math.max(0, Number.parseInt(String(args.keepCount ?? 0), 10) || 0);
|
|
1553
|
+
const aggregateKey = asString5(args.aggregateKey) ?? SUCCESSFUL_DEPLOYMENTS_AGGREGATE_KEY;
|
|
1554
|
+
const currentRecord = normalizeRetentionRecord(args.currentRecord);
|
|
1555
|
+
if (!currentRecord) {
|
|
1556
|
+
throw new Error("retainSuccessfulDeployments requires a current deployment record with instance_item_hash.");
|
|
1637
1557
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1558
|
+
const existingValue = await fetchAggregateKey({
|
|
1559
|
+
address: args.sender,
|
|
1560
|
+
key: aggregateKey,
|
|
1561
|
+
fetch: args.fetch,
|
|
1562
|
+
apiHost: args.apiHost
|
|
1563
|
+
});
|
|
1564
|
+
const existingRecords = normalizeRetentionLedger(existingValue);
|
|
1565
|
+
const mergedRecords = [currentRecord, ...existingRecords];
|
|
1566
|
+
const uniqueRecords = [];
|
|
1567
|
+
const seenIds = /* @__PURE__ */ new Set();
|
|
1568
|
+
for (const record of mergedRecords) {
|
|
1569
|
+
const id = retentionRecordId(record);
|
|
1570
|
+
if (seenIds.has(id)) continue;
|
|
1571
|
+
seenIds.add(id);
|
|
1572
|
+
uniqueRecords.push(record);
|
|
1648
1573
|
}
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1574
|
+
const retainedRecords = keepCount > 0 ? uniqueRecords.slice(0, keepCount) : [];
|
|
1575
|
+
const prunedRecords = keepCount > 0 ? uniqueRecords.slice(keepCount) : uniqueRecords;
|
|
1576
|
+
const retainedHashes = new Set(retainedRecords.flatMap(hashesFromRetentionRecord));
|
|
1577
|
+
const extraForgetHashes = uniqueHashes(args.extraForgetHashes ?? []);
|
|
1578
|
+
const { instanceForgetHashes, dependentForgetHashes, orderedForgetHashes } = splitForgetStages({
|
|
1579
|
+
prunedRecords,
|
|
1580
|
+
extraForgetHashes,
|
|
1581
|
+
retainedHashes
|
|
1582
|
+
});
|
|
1583
|
+
const forgetHashes = orderedForgetHashes;
|
|
1584
|
+
const aggregateContent = {
|
|
1585
|
+
keep: keepCount,
|
|
1586
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1587
|
+
deployments: retainedRecords
|
|
1588
|
+
};
|
|
1589
|
+
const aggregatePublication = await publishAggregateKey({
|
|
1590
|
+
sender: args.sender,
|
|
1591
|
+
key: aggregateKey,
|
|
1592
|
+
content: aggregateContent,
|
|
1593
|
+
signer: args.signer,
|
|
1594
|
+
hasher: args.hasher,
|
|
1595
|
+
fetch: args.fetch,
|
|
1596
|
+
channel: args.channel,
|
|
1597
|
+
apiHost: args.apiHost,
|
|
1598
|
+
now: args.now
|
|
1599
|
+
});
|
|
1600
|
+
const forgetReason = args.reason ?? `Prune successful deployments beyond retention limit ${keepCount}`;
|
|
1601
|
+
const forgetStageResults = [];
|
|
1602
|
+
let forgottenHashes = [];
|
|
1603
|
+
let outstandingForgetHashes = [];
|
|
1604
|
+
let forgetStatuses = {};
|
|
1605
|
+
let forgetResult = null;
|
|
1606
|
+
const followUpForgetResults = [];
|
|
1607
|
+
if (instanceForgetHashes.length > 0) {
|
|
1608
|
+
const eraseResults = await Promise.all(
|
|
1609
|
+
instanceForgetHashes.map(async (hash) => ({
|
|
1610
|
+
hash,
|
|
1611
|
+
...await eraseInstanceOnCrn({
|
|
1612
|
+
sender: args.sender,
|
|
1613
|
+
signer: args.signer,
|
|
1614
|
+
instanceHash: hash,
|
|
1615
|
+
fetch: args.fetch,
|
|
1616
|
+
apiHost: args.apiHost,
|
|
1617
|
+
crnListUrl: args.crnListUrl,
|
|
1618
|
+
schedulerAllocationUrl: args.schedulerAllocationUrl
|
|
1619
|
+
})
|
|
1620
|
+
}))
|
|
1621
|
+
);
|
|
1622
|
+
const instanceStage = await executeForgetStage({
|
|
1623
|
+
sender: args.sender,
|
|
1624
|
+
hashes: instanceForgetHashes,
|
|
1625
|
+
reason: forgetReason,
|
|
1626
|
+
signer: args.signer,
|
|
1627
|
+
hasher: args.hasher,
|
|
1628
|
+
fetch: args.fetch,
|
|
1629
|
+
channel: args.channel,
|
|
1630
|
+
apiHost: args.apiHost,
|
|
1631
|
+
now: args.now
|
|
1632
|
+
});
|
|
1633
|
+
forgetStageResults.push({ stage: "instances", eraseResults, ...instanceStage });
|
|
1634
|
+
forgottenHashes.push(...instanceStage.forgottenHashes);
|
|
1635
|
+
outstandingForgetHashes.push(...instanceStage.outstandingForgetHashes);
|
|
1636
|
+
forgetStatuses = { ...forgetStatuses, ...instanceStage.statuses };
|
|
1637
|
+
if (!forgetResult && instanceStage.forgetResult) {
|
|
1638
|
+
forgetResult = instanceStage.forgetResult;
|
|
1639
|
+
}
|
|
1640
|
+
followUpForgetResults.push(...instanceStage.followUpForgetResults);
|
|
1641
|
+
}
|
|
1642
|
+
if (dependentForgetHashes.length > 0) {
|
|
1643
|
+
if (outstandingForgetHashes.length === 0) {
|
|
1644
|
+
const dependentStage = await executeForgetStage({
|
|
1645
|
+
sender: args.sender,
|
|
1646
|
+
hashes: dependentForgetHashes,
|
|
1647
|
+
reason: forgetReason,
|
|
1648
|
+
signer: args.signer,
|
|
1649
|
+
hasher: args.hasher,
|
|
1650
|
+
fetch: args.fetch,
|
|
1651
|
+
channel: args.channel,
|
|
1652
|
+
apiHost: args.apiHost,
|
|
1653
|
+
now: args.now
|
|
1654
|
+
});
|
|
1655
|
+
forgetStageResults.push({ stage: "dependents", ...dependentStage });
|
|
1656
|
+
forgottenHashes.push(...dependentStage.forgottenHashes);
|
|
1657
|
+
outstandingForgetHashes.push(...dependentStage.outstandingForgetHashes);
|
|
1658
|
+
forgetStatuses = { ...forgetStatuses, ...dependentStage.statuses };
|
|
1659
|
+
if (!forgetResult && dependentStage.forgetResult) {
|
|
1660
|
+
forgetResult = dependentStage.forgetResult;
|
|
1661
|
+
}
|
|
1662
|
+
followUpForgetResults.push(...dependentStage.followUpForgetResults);
|
|
1663
|
+
} else {
|
|
1664
|
+
const dependentStageStatus = await classifyForgetHashes({
|
|
1665
|
+
hashes: dependentForgetHashes,
|
|
1666
|
+
fetch: args.fetch,
|
|
1667
|
+
apiHost: args.apiHost
|
|
1668
|
+
});
|
|
1669
|
+
forgetStageResults.push({
|
|
1670
|
+
stage: "dependents",
|
|
1671
|
+
hashes: dependentForgetHashes,
|
|
1672
|
+
skipped: true,
|
|
1673
|
+
skippedReason: "Waiting for instance forget stage to complete before pruning dependent store items.",
|
|
1674
|
+
forgottenHashes: dependentStageStatus.forgottenHashes,
|
|
1675
|
+
outstandingForgetHashes: dependentStageStatus.outstandingForgetHashes,
|
|
1676
|
+
statuses: dependentStageStatus.statuses,
|
|
1677
|
+
forgetResult: null,
|
|
1678
|
+
followUpForgetResults: []
|
|
1679
|
+
});
|
|
1680
|
+
forgottenHashes.push(...dependentStageStatus.forgottenHashes);
|
|
1681
|
+
outstandingForgetHashes.push(...dependentStageStatus.outstandingForgetHashes);
|
|
1682
|
+
forgetStatuses = { ...forgetStatuses, ...dependentStageStatus.statuses };
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
forgottenHashes = uniqueHashes(forgottenHashes);
|
|
1686
|
+
outstandingForgetHashes = uniqueHashes(outstandingForgetHashes);
|
|
1687
|
+
return {
|
|
1688
|
+
sender: args.sender,
|
|
1689
|
+
aggregateKey,
|
|
1690
|
+
keepCount,
|
|
1691
|
+
aggregatePublication,
|
|
1692
|
+
retainedRecords,
|
|
1693
|
+
prunedRecords,
|
|
1694
|
+
forgetHashes,
|
|
1695
|
+
forgottenHashes,
|
|
1696
|
+
outstandingForgetHashes,
|
|
1697
|
+
forgetStatuses,
|
|
1698
|
+
forgetResult,
|
|
1699
|
+
followUpForgetResults,
|
|
1700
|
+
forgetStageResults
|
|
1701
|
+
};
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
// src/aggregate-publication.ts
|
|
1705
|
+
function createPortForwardAggregateContent(args) {
|
|
1706
|
+
const existingPorts = normalizeExistingPortForwardEntry(args.existingAggregate?.[args.instanceItemHash]);
|
|
1707
|
+
const mergedPorts = mergePortFlagMaps(existingPorts, requestedPortFlags(args.requestedPorts));
|
|
1708
|
+
return {
|
|
1709
|
+
address: args.sender,
|
|
1710
|
+
key: "port-forwarding",
|
|
1711
|
+
content: {
|
|
1712
|
+
[args.instanceItemHash]: {
|
|
1713
|
+
ports: mergedPorts
|
|
1714
|
+
}
|
|
1715
|
+
},
|
|
1716
|
+
time: args.now ?? Date.now() / 1e3
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
async function createUnsignedAggregateMessage2(args) {
|
|
1720
|
+
const itemContent = JSON.stringify(args.content);
|
|
1721
|
+
const itemHash = await args.hasher(itemContent);
|
|
1722
|
+
return {
|
|
1723
|
+
sender: args.sender,
|
|
1724
|
+
chain: "ETH",
|
|
1725
|
+
type: "AGGREGATE",
|
|
1726
|
+
item_hash: itemHash,
|
|
1727
|
+
item_type: "inline",
|
|
1728
|
+
item_content: itemContent,
|
|
1729
|
+
time: args.now ?? Date.now() / 1e3,
|
|
1730
|
+
channel: args.channel ?? DEFAULT_ALEPH_CHANNEL
|
|
1731
|
+
};
|
|
1732
|
+
}
|
|
1733
|
+
async function ensureInstancePortForwards(args) {
|
|
1734
|
+
const requestedPorts = requiredInstancePortForwards(args.manifest);
|
|
1735
|
+
const aggregate = await fetchPortForwardAggregate(args.sender, {
|
|
1736
|
+
apiHost: args.apiHost,
|
|
1737
|
+
fetch: args.fetch
|
|
1738
|
+
});
|
|
1739
|
+
const content = createPortForwardAggregateContent({
|
|
1740
|
+
sender: args.sender,
|
|
1741
|
+
instanceItemHash: args.instanceItemHash,
|
|
1742
|
+
requestedPorts,
|
|
1743
|
+
existingAggregate: aggregate
|
|
1744
|
+
});
|
|
1745
|
+
const unsignedMessage = await createUnsignedAggregateMessage2({
|
|
1746
|
+
sender: args.sender,
|
|
1747
|
+
content,
|
|
1748
|
+
hasher: args.hasher,
|
|
1749
|
+
channel: args.channel
|
|
1750
|
+
});
|
|
1751
|
+
const message = await signAlephMessage(unsignedMessage, args.signer);
|
|
1752
|
+
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
1753
|
+
apiHost: args.apiHost,
|
|
1754
|
+
sync: args.sync,
|
|
1755
|
+
fetch: args.fetch
|
|
1756
|
+
});
|
|
1757
|
+
const aggregateStatus = normalizeBroadcastStatus(httpStatus, response.message_status);
|
|
1758
|
+
if (aggregateStatus === "rejected") {
|
|
1759
|
+
throw new Error(`Port-forward aggregate was rejected by Aleph: ${JSON.stringify(response.details ?? response)}`);
|
|
1760
|
+
}
|
|
1761
|
+
return {
|
|
1762
|
+
aggregateItemHash: message.item_hash,
|
|
1763
|
+
aggregateStatus,
|
|
1764
|
+
requestedPorts
|
|
1765
|
+
};
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
// src/instance-deployment.ts
|
|
1769
|
+
var SSH_PUBLIC_KEY_PATTERN = /^(ssh-rsa|ssh-ed25519|ecdsa-sha2-nistp256|ecdsa-sha2-nistp384|ecdsa-sha2-nistp521|sk-ssh-ed25519@openssh\.com|sk-ecdsa-sha2-nistp256@openssh\.com)\s+[A-Za-z0-9+/]+={0,3}(?:\s+.+)?$/;
|
|
1770
|
+
function toFiniteNumber(value) {
|
|
1771
|
+
if (typeof value === "number") return value;
|
|
1772
|
+
if (typeof value === "string" && value.trim()) return Number(value);
|
|
1773
|
+
return Number.NaN;
|
|
1774
|
+
}
|
|
1775
|
+
function normalizeSshPublicKey(value) {
|
|
1776
|
+
return value.split(/\r?\n/g).map((line) => line.trim()).filter(Boolean).join(" ").replace(/\s+/g, " ").trim();
|
|
1777
|
+
}
|
|
1778
|
+
function isValidSshPublicKey(value) {
|
|
1779
|
+
return SSH_PUBLIC_KEY_PATTERN.test(normalizeSshPublicKey(value));
|
|
1780
|
+
}
|
|
1781
|
+
function createDeploymentToken() {
|
|
1782
|
+
const randomUuid = globalThis.crypto?.randomUUID?.();
|
|
1783
|
+
if (typeof randomUuid === "string" && randomUuid.trim()) {
|
|
1784
|
+
return randomUuid;
|
|
1785
|
+
}
|
|
1786
|
+
return `deploy-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
|
|
1787
|
+
}
|
|
1788
|
+
function appendDeploymentTokenToSshPublicKey(sshPublicKey, ownerAddress, deploymentToken) {
|
|
1789
|
+
const normalized = normalizeSshPublicKey(sshPublicKey);
|
|
1790
|
+
if (!normalized) return normalized;
|
|
1791
|
+
const [algorithm = "", key = ""] = normalized.split(/\s+/, 3);
|
|
1792
|
+
const owner = ownerAddress.trim();
|
|
1793
|
+
const token = deploymentToken.trim();
|
|
1794
|
+
if (!algorithm || !key || !owner || !token) {
|
|
1795
|
+
return normalized;
|
|
1796
|
+
}
|
|
1797
|
+
return `${algorithm} ${key} aleph-bootstrap-config:${owner}:${token}`;
|
|
1798
|
+
}
|
|
1799
|
+
function createReleaseMetadata(name, rootfsVersion, deployer = "relay-button") {
|
|
1800
|
+
return {
|
|
1801
|
+
name,
|
|
1802
|
+
rootfs_version: rootfsVersion,
|
|
1803
|
+
deployer
|
|
1804
|
+
};
|
|
1805
|
+
}
|
|
1806
|
+
function selectedTier(pricing, tierId) {
|
|
1807
|
+
return pricing?.tiers.find((tier) => tier.id === tierId) ?? null;
|
|
1808
|
+
}
|
|
1809
|
+
function tierSpec(pricing, tier) {
|
|
1810
|
+
return {
|
|
1811
|
+
vcpus: pricing.compute_unit.vcpus * tier.compute_units,
|
|
1812
|
+
memoryMiB: pricing.compute_unit.memory_mib * tier.compute_units,
|
|
1813
|
+
diskMiB: pricing.compute_unit.disk_mib * tier.compute_units
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
function buildPaymentQuote(tier, pricing, balance) {
|
|
1817
|
+
const computeUnitPrice = pricing.price.compute_unit;
|
|
1818
|
+
if (!computeUnitPrice) return null;
|
|
1819
|
+
const unitPrice = toFiniteNumber(computeUnitPrice.credit);
|
|
1820
|
+
if (!Number.isFinite(unitPrice)) return null;
|
|
1821
|
+
return {
|
|
1822
|
+
required: unitPrice * tier.compute_units,
|
|
1823
|
+
available: Number(balance.credit_balance ?? 0),
|
|
1824
|
+
computeUnits: tier.compute_units,
|
|
1825
|
+
unitPrice,
|
|
1826
|
+
label: "credits"
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
function quoteRequiredBudgetUnits(quote) {
|
|
1830
|
+
if (!quote) return 0n;
|
|
1831
|
+
return BigInt(Math.ceil(quote.required * 1e18));
|
|
1832
|
+
}
|
|
1833
|
+
function describeRejectedInstanceBroadcast(response) {
|
|
1834
|
+
const balanceMessage = extractInsufficientBalanceMessage(response.details);
|
|
1835
|
+
if (balanceMessage) {
|
|
1836
|
+
return `Aleph rejected this deployment due to ${balanceMessage}.`;
|
|
1659
1837
|
}
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
mappedPortCount,
|
|
1668
|
-
proxyUrl
|
|
1669
|
-
};
|
|
1838
|
+
const errorCode = typeof response.error_code === "number" ? response.error_code : null;
|
|
1839
|
+
return `Aleph rejected this deployment${errorCode ? ` (error ${errorCode})` : ""}.`;
|
|
1840
|
+
}
|
|
1841
|
+
function createInstanceContent(args) {
|
|
1842
|
+
const sshKey = normalizeSshPublicKey(args.sshPublicKey);
|
|
1843
|
+
if (!sshKey) {
|
|
1844
|
+
throw new Error("An SSH public key is required.");
|
|
1670
1845
|
}
|
|
1671
|
-
if (
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
executionSeen: true,
|
|
1677
|
-
webAccessActive,
|
|
1678
|
-
mappedPortCount,
|
|
1679
|
-
proxyUrl
|
|
1680
|
-
};
|
|
1846
|
+
if (!isValidSshPublicKey(sshKey)) {
|
|
1847
|
+
throw new Error("SSH public key must be a single valid .pub line.");
|
|
1848
|
+
}
|
|
1849
|
+
if (!args.rootfsItemHash || !/^[a-fA-F0-9]{64}$/.test(args.rootfsItemHash)) {
|
|
1850
|
+
throw new Error("rootfsItemHash must be a 64-character Aleph item hash.");
|
|
1681
1851
|
}
|
|
1682
1852
|
return {
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1853
|
+
address: args.address,
|
|
1854
|
+
time: args.now ?? Date.now() / 1e3,
|
|
1855
|
+
allow_amend: false,
|
|
1856
|
+
metadata: createReleaseMetadata(args.name.trim(), args.rootfsVersion ?? "custom-rootfs", args.deployer),
|
|
1857
|
+
authorized_keys: [sshKey],
|
|
1858
|
+
environment: {
|
|
1859
|
+
internet: true,
|
|
1860
|
+
aleph_api: true,
|
|
1861
|
+
hypervisor: "qemu",
|
|
1862
|
+
reproducible: false,
|
|
1863
|
+
shared_cache: false
|
|
1864
|
+
},
|
|
1865
|
+
resources: {
|
|
1866
|
+
vcpus: Number(args.vcpus),
|
|
1867
|
+
memory: Number(args.memoryMiB),
|
|
1868
|
+
seconds: Number(args.seconds ?? 30)
|
|
1869
|
+
},
|
|
1870
|
+
payment: {
|
|
1871
|
+
type: "credit"
|
|
1872
|
+
},
|
|
1873
|
+
requirements: args.crnHash ? {
|
|
1874
|
+
node: {
|
|
1875
|
+
node_hash: args.crnHash
|
|
1876
|
+
}
|
|
1877
|
+
} : void 0,
|
|
1878
|
+
volumes: [],
|
|
1879
|
+
rootfs: {
|
|
1880
|
+
parent: {
|
|
1881
|
+
ref: args.rootfsItemHash,
|
|
1882
|
+
use_latest: true
|
|
1883
|
+
},
|
|
1884
|
+
persistence: "host",
|
|
1885
|
+
size_mib: Number(args.rootfsSizeMiB)
|
|
1886
|
+
}
|
|
1690
1887
|
};
|
|
1691
1888
|
}
|
|
1692
|
-
async function
|
|
1693
|
-
const
|
|
1694
|
-
|
|
1889
|
+
async function createUnsignedInstanceMessage(args) {
|
|
1890
|
+
const itemContent = JSON.stringify(args.content);
|
|
1891
|
+
const itemHash = await args.hasher(itemContent);
|
|
1892
|
+
return {
|
|
1893
|
+
sender: args.sender,
|
|
1894
|
+
chain: "ETH",
|
|
1895
|
+
type: "INSTANCE",
|
|
1896
|
+
item_hash: itemHash,
|
|
1897
|
+
item_type: "inline",
|
|
1898
|
+
item_content: itemContent,
|
|
1899
|
+
time: args.now ?? Date.now() / 1e3,
|
|
1900
|
+
channel: args.channel ?? DEFAULT_ALEPH_CHANNEL
|
|
1901
|
+
};
|
|
1902
|
+
}
|
|
1903
|
+
async function createDeploymentIntent(args) {
|
|
1904
|
+
const intent = {
|
|
1905
|
+
ownerAddress: args.sender,
|
|
1906
|
+
messageTime: args.unsignedMessage.time,
|
|
1907
|
+
itemHash: args.unsignedMessage.item_hash,
|
|
1908
|
+
paymentType: args.content.payment.type,
|
|
1909
|
+
rootfsRef: args.content.rootfs.parent.ref,
|
|
1910
|
+
rootfsSizeMiB: args.content.rootfs.size_mib,
|
|
1911
|
+
computeUnits: args.computeUnits,
|
|
1912
|
+
vcpus: args.content.resources.vcpus,
|
|
1913
|
+
memoryMiB: args.content.resources.memory,
|
|
1914
|
+
crnHash: args.content.requirements?.node?.node_hash ?? null,
|
|
1915
|
+
channel: args.unsignedMessage.channel,
|
|
1916
|
+
expiresAt: args.expiresAt,
|
|
1917
|
+
maxCost: args.maxCost
|
|
1918
|
+
};
|
|
1919
|
+
return {
|
|
1920
|
+
intent,
|
|
1921
|
+
intentHash: await args.hasher(JSON.stringify(intent))
|
|
1922
|
+
};
|
|
1923
|
+
}
|
|
1924
|
+
async function deployInstance(args) {
|
|
1925
|
+
args.onProgress?.({
|
|
1926
|
+
stage: "building-message",
|
|
1927
|
+
label: "Building deployment message",
|
|
1928
|
+
progress: 32,
|
|
1929
|
+
status: "info",
|
|
1930
|
+
detail: args.content.metadata?.name ?? null,
|
|
1931
|
+
itemHash: null,
|
|
1932
|
+
error: null,
|
|
1933
|
+
timestamp: Date.now()
|
|
1934
|
+
});
|
|
1935
|
+
const unsignedMessage = await createUnsignedInstanceMessage({
|
|
1936
|
+
sender: args.sender,
|
|
1937
|
+
content: args.content,
|
|
1938
|
+
hasher: args.hasher,
|
|
1939
|
+
channel: args.channel,
|
|
1940
|
+
now: args.now
|
|
1941
|
+
});
|
|
1942
|
+
args.onProgress?.({
|
|
1943
|
+
stage: "signing-message",
|
|
1944
|
+
label: "Waiting for MetaMask signature",
|
|
1945
|
+
progress: 48,
|
|
1946
|
+
status: "info",
|
|
1947
|
+
detail: unsignedMessage.item_hash,
|
|
1948
|
+
itemHash: unsignedMessage.item_hash,
|
|
1949
|
+
error: null,
|
|
1950
|
+
timestamp: Date.now()
|
|
1951
|
+
});
|
|
1952
|
+
const signature = await args.signer(unsignedMessage.sender, [unsignedMessage.chain, unsignedMessage.sender, unsignedMessage.type, unsignedMessage.item_hash].join("\n"));
|
|
1953
|
+
const message = {
|
|
1954
|
+
...unsignedMessage,
|
|
1955
|
+
signature: signature.startsWith("0x") ? signature : `0x${signature}`
|
|
1956
|
+
};
|
|
1957
|
+
args.onProgress?.({
|
|
1958
|
+
stage: "broadcasting",
|
|
1959
|
+
label: "Broadcasting to Aleph",
|
|
1960
|
+
progress: 64,
|
|
1961
|
+
status: "info",
|
|
1962
|
+
detail: message.item_hash,
|
|
1963
|
+
itemHash: message.item_hash,
|
|
1964
|
+
error: null,
|
|
1965
|
+
timestamp: Date.now()
|
|
1966
|
+
});
|
|
1967
|
+
const { response, httpStatus } = await broadcastAlephMessage(message, {
|
|
1968
|
+
apiHost: args.apiHost,
|
|
1969
|
+
sync: args.sync,
|
|
1695
1970
|
fetch: args.fetch
|
|
1696
1971
|
});
|
|
1697
|
-
const
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
vmIpv6: null,
|
|
1708
|
-
period: null
|
|
1709
|
-
} : null);
|
|
1710
|
-
const webAccess = await fetch2n6WebAccessUrl(args.itemHash, {
|
|
1711
|
-
fetch: args.fetch,
|
|
1712
|
-
twoN6HashUrl: args.twoN6HashUrl
|
|
1713
|
-
}).catch(() => null);
|
|
1714
|
-
const webAccessUrl = webAccess?.url ?? null;
|
|
1715
|
-
let execution = null;
|
|
1716
|
-
let executionLookupBlocked = false;
|
|
1717
|
-
if (allocation?.crnUrl) {
|
|
1718
|
-
const executionLookup = await fetchCrnExecutionMap(allocation.crnUrl, {
|
|
1719
|
-
fetch: args.fetch
|
|
1720
|
-
});
|
|
1721
|
-
const executionPayload = executionLookup.payload?.[args.itemHash];
|
|
1722
|
-
if (executionPayload && typeof executionPayload === "object") {
|
|
1723
|
-
execution = normalizeExecution(executionPayload, allocation.crnUrl);
|
|
1724
|
-
if (!execution.networking.proxy_url && webAccessUrl) {
|
|
1725
|
-
execution.networking.proxy_url = webAccessUrl;
|
|
1726
|
-
}
|
|
1727
|
-
} else if (executionLookup.payload == null) {
|
|
1728
|
-
executionLookupBlocked = true;
|
|
1729
|
-
}
|
|
1730
|
-
}
|
|
1731
|
-
const hostIpv4 = execution?.networking?.host_ipv4 ?? execution?.networking?.ipv4 ?? null;
|
|
1732
|
-
const ipv6 = execution?.networking?.ipv6_ip ?? execution?.networking?.ipv6 ?? allocation?.vmIpv6 ?? null;
|
|
1733
|
-
const mappedPorts = execution?.networking?.mapped_ports ?? {};
|
|
1734
|
-
const sshPort = mappedPorts?.["22"]?.host ?? null;
|
|
1735
|
-
const proxyUrl = execution?.networking?.proxy_url ?? webAccessUrl ?? null;
|
|
1736
|
-
const diagnostics = describeRuntimeAvailability({
|
|
1737
|
-
allocation,
|
|
1738
|
-
execution,
|
|
1739
|
-
webAccess,
|
|
1740
|
-
hostIpv4,
|
|
1741
|
-
ipv6,
|
|
1742
|
-
proxyUrl,
|
|
1743
|
-
mappedPorts,
|
|
1744
|
-
requirePublicGuestIpv6ForProxy: args.requirePublicGuestIpv6ForProxy
|
|
1972
|
+
const status = httpStatus === 202 ? "pending" : typeof response.message_status === "string" ? response.message_status : "unknown";
|
|
1973
|
+
args.onProgress?.({
|
|
1974
|
+
stage: status === "processed" ? "deployment-confirmed" : status === "rejected" ? "deployment-rejected" : "waiting-for-aleph",
|
|
1975
|
+
label: status === "processed" ? "Deployment accepted by Aleph" : status === "rejected" ? "Deployment rejected by Aleph" : "Deployment submitted to Aleph",
|
|
1976
|
+
progress: status === "processed" ? 88 : status === "rejected" ? 100 : 78,
|
|
1977
|
+
status: status === "processed" ? "success" : status === "rejected" ? "error" : "warning",
|
|
1978
|
+
detail: status === "rejected" ? describeRejectedInstanceBroadcast(response) : null,
|
|
1979
|
+
itemHash: message.item_hash,
|
|
1980
|
+
error: status === "rejected" ? describeRejectedInstanceBroadcast(response) : null,
|
|
1981
|
+
timestamp: Date.now()
|
|
1745
1982
|
});
|
|
1746
1983
|
return {
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
proxyUrl,
|
|
1754
|
-
mappedPorts,
|
|
1755
|
-
diagnostics,
|
|
1756
|
-
sshCommand: hostIpv4 && sshPort ? `ssh root@${hostIpv4} -p ${sshPort}` : ipv6 ? `ssh root@${ipv6}` : null,
|
|
1757
|
-
selectedCrn,
|
|
1758
|
-
executionLookupBlocked
|
|
1984
|
+
itemHash: message.item_hash,
|
|
1985
|
+
httpStatus,
|
|
1986
|
+
status,
|
|
1987
|
+
message,
|
|
1988
|
+
response,
|
|
1989
|
+
rejectionReason: status === "rejected" ? describeRejectedInstanceBroadcast(response) : null
|
|
1759
1990
|
};
|
|
1760
1991
|
}
|
|
1761
|
-
async function waitForVmRuntime(args) {
|
|
1762
|
-
const attempts = Math.max(1, Number(args.attempts ?? 20));
|
|
1763
|
-
const delayMs = Math.max(0, Number(args.delayMs ?? 4e3));
|
|
1764
|
-
const sleep2 = args.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
1765
|
-
let lastRuntime = await fetchVmRuntime(args);
|
|
1766
|
-
args.onAttempt?.(lastRuntime, 1, attempts);
|
|
1767
|
-
for (let attempt = 0; attempt < attempts - 1; attempt += 1) {
|
|
1768
|
-
if (lastRuntime.diagnostics?.state === "ready" || lastRuntime.diagnostics?.state === "execution-invalid-public-ipv6") {
|
|
1769
|
-
return lastRuntime;
|
|
1770
|
-
}
|
|
1771
|
-
await sleep2(delayMs);
|
|
1772
|
-
lastRuntime = await fetchVmRuntime(args);
|
|
1773
|
-
args.onAttempt?.(lastRuntime, attempt + 2, attempts);
|
|
1774
|
-
}
|
|
1775
|
-
if (lastRuntime.diagnostics) {
|
|
1776
|
-
lastRuntime.diagnostics.timedOut = true;
|
|
1777
|
-
}
|
|
1778
|
-
return lastRuntime;
|
|
1779
|
-
}
|
|
1780
1992
|
|
|
1781
1993
|
// src/guest.ts
|
|
1782
1994
|
function proxyHostnameFromUrl(value) {
|
|
@@ -2127,7 +2339,7 @@ async function verifyUcGoPeerReachability(args) {
|
|
|
2127
2339
|
}
|
|
2128
2340
|
|
|
2129
2341
|
// ../aleph-bootstrap/src/index.ts
|
|
2130
|
-
var DEFAULT_ALEPH_API_HOST2 = "https://
|
|
2342
|
+
var DEFAULT_ALEPH_API_HOST2 = "https://api.aleph.im";
|
|
2131
2343
|
var DEFAULT_ALEPH_BOOTSTRAP_CHANNEL = "simple-todo";
|
|
2132
2344
|
var DEFAULT_ALEPH_BOOTSTRAP_REF = "simple-todo-bootstrap";
|
|
2133
2345
|
var DEFAULT_ALEPH_BOOTSTRAP_POST_TYPE = "relay-bootstrap";
|
|
@@ -2816,7 +3028,7 @@ async function listStaleVmBootstrapConfigAggregateMessageHashes(args) {
|
|
|
2816
3028
|
const requestUrl = new URL("/api/v0/messages.json", args.apiHost ?? DEFAULT_ALEPH_API_HOST);
|
|
2817
3029
|
requestUrl.searchParams.set("msgType", "AGGREGATE");
|
|
2818
3030
|
requestUrl.searchParams.set("addresses", address);
|
|
2819
|
-
requestUrl.searchParams.set("message_statuses", "processed,pending,rejected
|
|
3031
|
+
requestUrl.searchParams.set("message_statuses", "processed,pending,rejected");
|
|
2820
3032
|
requestUrl.searchParams.set("pagination", String(pagination));
|
|
2821
3033
|
requestUrl.searchParams.set("page", String(page));
|
|
2822
3034
|
requestUrl.searchParams.set("sortOrder", "-1");
|
|
@@ -3011,7 +3223,7 @@ async function fetchOwnerInstances(address, options) {
|
|
|
3011
3223
|
const url = new URL("/api/v0/messages.json", options.apiHost ?? DEFAULT_ALEPH_API_HOST);
|
|
3012
3224
|
url.searchParams.set("msgTypes", "INSTANCE");
|
|
3013
3225
|
url.searchParams.set("addresses", address);
|
|
3014
|
-
url.searchParams.set("message_statuses", "processed,pending,rejected
|
|
3226
|
+
url.searchParams.set("message_statuses", "processed,pending,rejected");
|
|
3015
3227
|
url.searchParams.set("pagination", "100");
|
|
3016
3228
|
url.searchParams.set("page", "1");
|
|
3017
3229
|
url.searchParams.set("sortOrder", "-1");
|
|
@@ -3207,6 +3419,7 @@ export {
|
|
|
3207
3419
|
describeRuntimeAvailability,
|
|
3208
3420
|
enrichCrnsWithGeo,
|
|
3209
3421
|
ensureInstancePortForwards,
|
|
3422
|
+
eraseInstanceOnCrn,
|
|
3210
3423
|
extractInsufficientBalanceMessage,
|
|
3211
3424
|
extractProxyUrl,
|
|
3212
3425
|
extractReferenceHashes,
|
|
@@ -3229,6 +3442,7 @@ export {
|
|
|
3229
3442
|
inspectMessageResult,
|
|
3230
3443
|
isInvalidMessageFormatResponse,
|
|
3231
3444
|
isRetryableBroadcastFailure,
|
|
3445
|
+
isTransientMessageLookupError,
|
|
3232
3446
|
isValidSshPublicKey,
|
|
3233
3447
|
listGeocodedCrns,
|
|
3234
3448
|
listStaleVmBootstrapConfigAggregateMessageHashes,
|
|
@@ -3254,6 +3468,7 @@ export {
|
|
|
3254
3468
|
reconcileOwnerRelayBootstrapRegistrations,
|
|
3255
3469
|
requestedPortFlags,
|
|
3256
3470
|
requiredInstancePortForwards,
|
|
3471
|
+
resolveInstanceCrn,
|
|
3257
3472
|
resolveRootfsReference,
|
|
3258
3473
|
retainSuccessfulDeployments,
|
|
3259
3474
|
selectPreferredCrn,
|