|
| 1 | +import { StorageHubClient, ReplicationLevel } from "@storagehub-sdk/core"; |
| 2 | +import { MspClient, type Session } from "@storagehub-sdk/msp-client"; |
| 3 | +import { createPublicClient, http } from "viem"; |
| 4 | +import { privateKeyToAccount } from "viem/accounts"; |
| 5 | +import { resolve } from "node:path"; |
| 6 | +import { readEnv } from "../config.js"; |
| 7 | +import { DEFAULT_EVM_RPC_TIMEOUT_MS } from "../config/constants.js"; |
| 8 | +import { |
| 9 | + ensureVars, |
| 10 | + persistVars, |
| 11 | + requirePersistedVar, |
| 12 | + requirePersistedVarNumber, |
| 13 | + requirePersistedVarString, |
| 14 | + requireVarString, |
| 15 | + type ArtilleryContext, |
| 16 | + type ArtilleryEvents, |
| 17 | + type Done, |
| 18 | +} from "../helpers/artillery.js"; |
| 19 | +import { toError } from "../helpers/errors.js"; |
| 20 | +import { createEmitter } from "../helpers/metrics.js"; |
| 21 | +import { ensure0xPrefix } from "../helpers/validation.js"; |
| 22 | +import { NETWORKS } from "../networks.js"; |
| 23 | +import { pickSequentialResource } from "../resources/index.js"; |
| 24 | +import { buildMspHttpClientConfig } from "../sdk/mspHttpConfig.js"; |
| 25 | +import { createViemWallet, toViemChain } from "../sdk/viemWallet.js"; |
| 26 | +import { |
| 27 | + computeFileKeyFromMetadata, |
| 28 | + filePathToWebStream, |
| 29 | + waitForMspFileStatus, |
| 30 | + waitForStorageRequestReadyForUpload, |
| 31 | +} from "../files.js"; |
| 32 | +import { |
| 33 | + getUserApiSingleton, |
| 34 | + waitForStorageRequestFulfilledFinalized, |
| 35 | +} from "../userApi.js"; |
| 36 | + |
| 37 | +// Default bucket location used by our tests. |
| 38 | +const REMOTE_LOCATION = "/"; |
| 39 | + |
| 40 | +/** |
| 41 | + * Condensed upload flow: |
| 42 | + * - pick resource (deterministic) |
| 43 | + * - compute fileKey |
| 44 | + * - issue storage request + waits |
| 45 | + * - upload + waits |
| 46 | + * |
| 47 | + * Leaves all required vars persisted into `context.vars` for downstream steps (e.g. delete). |
| 48 | + */ |
| 49 | +export async function uploadFileFlow( |
| 50 | + context: ArtilleryContext, |
| 51 | + events: ArtilleryEvents, |
| 52 | + done?: Done |
| 53 | +): Promise<void> { |
| 54 | + const m = createEmitter(context, events); |
| 55 | + |
| 56 | + try { |
| 57 | + const vars = ensureVars(context); |
| 58 | + const bucketId = requirePersistedVarString(context, "__uploadBucketId"); |
| 59 | + const pk = ensure0xPrefix(requireVarString(vars, "privateKey"), 32); |
| 60 | + const account = privateKeyToAccount(pk); |
| 61 | + const sequence = requirePersistedVarNumber(context, "__accountIndexRaw"); |
| 62 | + const session = requirePersistedVar(context, "__siweSession") as Session; |
| 63 | + |
| 64 | + const picked = pickSequentialResource({ sequence }); |
| 65 | + const localFilePath = resolve(process.cwd(), picked.path); |
| 66 | + const sizeBytes = picked.sizeBytes; |
| 67 | + const fingerprint = picked.fingerprint; |
| 68 | + |
| 69 | + const fileKey = await computeFileKeyFromMetadata({ |
| 70 | + owner: account.address, |
| 71 | + bucketId, |
| 72 | + location: REMOTE_LOCATION, |
| 73 | + size: BigInt(sizeBytes), |
| 74 | + fingerprint, |
| 75 | + }); |
| 76 | + |
| 77 | + const env = readEnv(); |
| 78 | + const network = NETWORKS[env.network]; |
| 79 | + const { chain, transportUrl } = toViemChain(network); |
| 80 | + |
| 81 | + const publicClient = createPublicClient({ |
| 82 | + chain, |
| 83 | + transport: http(transportUrl, { timeout: DEFAULT_EVM_RPC_TIMEOUT_MS }), |
| 84 | + }); |
| 85 | + |
| 86 | + const config = buildMspHttpClientConfig(network); |
| 87 | + const mspClient = await MspClient.connect(config, async () => session); |
| 88 | + const info = await mspClient.info.getInfo(); |
| 89 | + const mspId = ensure0xPrefix(info.mspId, 32); |
| 90 | + const peerIds: string[] = info.multiaddresses |
| 91 | + .map((addr) => { |
| 92 | + const parts = addr.split("/"); |
| 93 | + return parts[parts.length - 1] || ""; |
| 94 | + }) |
| 95 | + .filter((id) => id.length > 0); |
| 96 | + |
| 97 | + const walletClient = createViemWallet(network, account); |
| 98 | + const storageHubClient = new StorageHubClient({ |
| 99 | + rpcUrl: transportUrl, |
| 100 | + chain, |
| 101 | + walletClient, |
| 102 | + filesystemContractAddress: network.chain.filesystemPrecompileAddress, |
| 103 | + }); |
| 104 | + |
| 105 | + const srStart = Date.now(); |
| 106 | + const txHash = await storageHubClient.issueStorageRequest( |
| 107 | + bucketId, |
| 108 | + REMOTE_LOCATION, |
| 109 | + fingerprint, |
| 110 | + BigInt(sizeBytes), |
| 111 | + mspId, |
| 112 | + peerIds, |
| 113 | + ReplicationLevel.Basic, |
| 114 | + 1 |
| 115 | + ); |
| 116 | + if (!txHash) throw new Error("issueStorageRequest returned undefined txHash"); |
| 117 | + m.histogram("uploadFlow.issueStorageRequest.ms", Date.now() - srStart); |
| 118 | + |
| 119 | + // Wait until StorageRequest is processed and the backend is expecting the filekey |
| 120 | + const userApi = await getUserApiSingleton(network); |
| 121 | + const readyStart = Date.now(); |
| 122 | + await waitForStorageRequestReadyForUpload({ |
| 123 | + publicClient, |
| 124 | + txHash, |
| 125 | + fileKey, |
| 126 | + bucketId, |
| 127 | + location: REMOTE_LOCATION, |
| 128 | + filesystemContractAddress: network.chain.filesystemPrecompileAddress, |
| 129 | + expectedWho: account.address, |
| 130 | + userApi, |
| 131 | + mspClient, |
| 132 | + }); |
| 133 | + m.histogram("uploadFlow.wait.readyForUpload.ms", Date.now() - readyStart); |
| 134 | + |
| 135 | + // Upload file bytes to MSP |
| 136 | + const upStart = Date.now(); |
| 137 | + const uploadReceipt = await mspClient.files.uploadFile( |
| 138 | + bucketId, |
| 139 | + fileKey, |
| 140 | + filePathToWebStream(localFilePath), |
| 141 | + account.address, |
| 142 | + REMOTE_LOCATION, |
| 143 | + { |
| 144 | + mspDistribution: true, |
| 145 | + contentLength: sizeBytes, |
| 146 | + } |
| 147 | + ); |
| 148 | + m.histogram("uploadFlow.uploadFile.ms", Date.now() - upStart); |
| 149 | + |
| 150 | + // Wait after upload (chain fulfillment + MSP ready) |
| 151 | + const fulfillStart = Date.now(); |
| 152 | + await waitForStorageRequestFulfilledFinalized(userApi, fileKey); |
| 153 | + m.histogram("uploadFlow.wait.fulfilledChain.ms", Date.now() - fulfillStart); |
| 154 | + |
| 155 | + const mspReadyStart = Date.now(); |
| 156 | + await waitForMspFileStatus({ |
| 157 | + mspClient, |
| 158 | + bucketId, |
| 159 | + fileKey, |
| 160 | + desiredStatus: "ready", |
| 161 | + timeoutMs: 660_000, |
| 162 | + intervalMs: 3_000, |
| 163 | + }); |
| 164 | + m.histogram("uploadFlow.wait.mspReady.ms", Date.now() - mspReadyStart); |
| 165 | + |
| 166 | + // Persist for follow-up steps (delete / waits). |
| 167 | + persistVars(context, { |
| 168 | + __uploadFileKey: fileKey, |
| 169 | + __uploadStorageRequestTxHash: txHash, |
| 170 | + __uploadLocation: REMOTE_LOCATION, |
| 171 | + __uploadLocalFilePath: localFilePath, |
| 172 | + __uploadFileSizeBytes: sizeBytes, |
| 173 | + __uploadFingerprint: fingerprint, |
| 174 | + __uploadOwner: account.address, |
| 175 | + }); |
| 176 | + |
| 177 | + void uploadReceipt; |
| 178 | + done?.(); |
| 179 | + } catch (err) { |
| 180 | + done?.(toError(err)); |
| 181 | + } |
| 182 | +} |
| 183 | + |
0 commit comments