@thru/programs 0.3.4 → 0.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +124 -39
- package/dist/abi-manager/index.cjs +9568 -0
- package/dist/abi-manager/index.cjs.map +1 -0
- package/dist/abi-manager/index.d.cts +2169 -0
- package/dist/abi-manager/index.d.ts +2169 -0
- package/dist/abi-manager/index.js +4 -0
- package/dist/abi-manager/index.js.map +1 -0
- package/dist/amm/index.cjs +132 -33
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.d.cts +1 -1
- package/dist/amm/index.d.ts +1 -1
- package/dist/amm/index.js +5 -2
- package/dist/amm/index.js.map +1 -1
- package/dist/bootstrap-addresses/index.cjs +118 -0
- package/dist/bootstrap-addresses/index.cjs.map +1 -0
- package/dist/bootstrap-addresses/index.d.cts +44 -0
- package/dist/bootstrap-addresses/index.d.ts +44 -0
- package/dist/bootstrap-addresses/index.js +5 -0
- package/dist/bootstrap-addresses/index.js.map +1 -0
- package/dist/chunk-6IC6CO5S.js +9443 -0
- package/dist/chunk-6IC6CO5S.js.map +1 -0
- package/dist/chunk-7DHFYBRW.js +51 -0
- package/dist/chunk-7DHFYBRW.js.map +1 -0
- package/dist/chunk-7FBCBZIY.js +41 -0
- package/dist/chunk-7FBCBZIY.js.map +1 -0
- package/dist/chunk-BNYDH4ZP.js +4351 -0
- package/dist/chunk-BNYDH4ZP.js.map +1 -0
- package/dist/{chunk-J7B7XYOI.js → chunk-CYG4PB65.js} +14 -41
- package/dist/chunk-CYG4PB65.js.map +1 -0
- package/dist/chunk-VNTPD4AR.js +37 -0
- package/dist/chunk-VNTPD4AR.js.map +1 -0
- package/dist/chunk-YX7BLFQZ.js +81 -0
- package/dist/chunk-YX7BLFQZ.js.map +1 -0
- package/dist/chunk-ZHF2PH5H.js +6536 -0
- package/dist/chunk-ZHF2PH5H.js.map +1 -0
- package/dist/clob/index.cjs +8461 -4922
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +1115 -53
- package/dist/clob/index.d.ts +1115 -53
- package/dist/clob/index.js +6817 -3395
- package/dist/clob/index.js.map +1 -1
- package/dist/deploy/index.cjs +24751 -0
- package/dist/deploy/index.cjs.map +1 -0
- package/dist/deploy/index.d.cts +180 -0
- package/dist/deploy/index.d.ts +180 -0
- package/dist/deploy/index.js +1849 -0
- package/dist/deploy/index.js.map +1 -0
- package/dist/helpers-DD7q61nr.d.cts +7 -0
- package/dist/helpers-DD7q61nr.d.ts +7 -0
- package/dist/manager/index.cjs +6680 -0
- package/dist/manager/index.cjs.map +1 -0
- package/dist/manager/index.d.cts +1279 -0
- package/dist/manager/index.d.ts +1279 -0
- package/dist/manager/index.js +5 -0
- package/dist/manager/index.js.map +1 -0
- package/dist/multicall/index.cjs +117 -40
- package/dist/multicall/index.cjs.map +1 -1
- package/dist/multicall/index.d.cts +5 -2
- package/dist/multicall/index.d.ts +5 -2
- package/dist/multicall/index.js +4 -1
- package/dist/oracle/index.cjs +122 -14
- package/dist/oracle/index.cjs.map +1 -1
- package/dist/oracle/index.d.cts +3 -1
- package/dist/oracle/index.d.ts +3 -1
- package/dist/oracle/index.js +7 -1
- package/dist/oracle/index.js.map +1 -1
- package/dist/passkey-manager/index.cjs +107 -6
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.d.cts +1 -1
- package/dist/passkey-manager/index.d.ts +1 -1
- package/dist/passkey-manager/index.js +6 -3
- package/dist/passkey-manager/index.js.map +1 -1
- package/dist/uploader/index.cjs +4442 -0
- package/dist/uploader/index.cjs.map +1 -0
- package/dist/uploader/index.d.cts +761 -0
- package/dist/uploader/index.d.ts +761 -0
- package/dist/uploader/index.js +4 -0
- package/dist/uploader/index.js.map +1 -0
- package/package.json +27 -2
- package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
- package/src/abi-manager/accounts.ts +91 -0
- package/src/abi-manager/constants.ts +19 -0
- package/src/abi-manager/derivation.ts +86 -0
- package/src/abi-manager/errors.ts +48 -0
- package/src/abi-manager/index.test.ts +136 -0
- package/src/abi-manager/index.ts +56 -0
- package/src/abi-manager/instructions.ts +573 -0
- package/src/abi-manager/types.ts +150 -0
- package/src/amm/index.test.ts +2 -6
- package/src/amm/index.ts +2 -2
- package/src/bootstrap-addresses/index.test.ts +59 -0
- package/src/bootstrap-addresses/index.ts +58 -0
- package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
- package/src/clob/exchange-client.test.ts +115 -2
- package/src/clob/exchange-client.ts +52 -13
- package/src/clob/exchange.test.ts +18 -16
- package/src/clob/exchange.ts +4 -0
- package/src/clob/index.test.ts +140 -7
- package/src/clob/index.ts +304 -12
- package/src/clob/price.test.ts +32 -0
- package/src/clob/price.ts +77 -0
- package/src/deploy/accounts.ts +208 -0
- package/src/deploy/chain.ts +163 -0
- package/src/deploy/constants.ts +30 -0
- package/src/deploy/deploy.test.ts +349 -0
- package/src/deploy/errors.ts +72 -0
- package/src/deploy/index.ts +44 -0
- package/src/deploy/inspection.ts +146 -0
- package/src/deploy/seeds.ts +29 -0
- package/src/deploy/types.ts +178 -0
- package/src/deploy/uploader.test.ts +326 -0
- package/src/deploy/uploader.ts +558 -0
- package/src/deploy/validation-imports.test.ts +55 -0
- package/src/deploy/validation.ts +183 -0
- package/src/deploy/workflows.test.ts +422 -0
- package/src/deploy/workflows.ts +1088 -0
- package/src/helpers/bytes.test.ts +58 -0
- package/src/helpers/bytes.ts +28 -3
- package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/manager/abi/thru/program/manager/types.ts +3814 -0
- package/src/manager/accounts.ts +44 -0
- package/src/manager/constants.ts +16 -0
- package/src/manager/derivation.ts +32 -0
- package/src/manager/errors.ts +47 -0
- package/src/manager/index.test.ts +108 -0
- package/src/manager/index.ts +68 -0
- package/src/manager/instructions.ts +281 -0
- package/src/manager/types.ts +95 -0
- package/src/manager/validation.ts +35 -0
- package/src/multicall/index.test.ts +11 -2
- package/src/multicall/index.ts +12 -5
- package/src/oracle/index.ts +4 -0
- package/src/passkey-manager/constants.ts +2 -1
- package/src/passkey-manager/validate.test.ts +2 -2
- package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
- package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
- package/src/uploader/accounts.ts +38 -0
- package/src/uploader/constants.ts +14 -0
- package/src/uploader/derivation.ts +34 -0
- package/src/uploader/errors.ts +25 -0
- package/src/uploader/index.test.ts +107 -0
- package/src/uploader/index.ts +34 -0
- package/src/uploader/instructions.ts +163 -0
- package/src/uploader/types.ts +67 -0
- package/src/utils/helpers.ts +121 -0
- package/tsup.config.ts +5 -0
- package/dist/chunk-J7B7XYOI.js.map +0 -1
- package/dist/chunk-P5OABVJI.js +0 -28
- package/dist/chunk-P5OABVJI.js.map +0 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { Thru } from "@thru/sdk/client";
|
|
2
|
+
|
|
3
|
+
export type DeploymentBytes = Uint8Array | ArrayBuffer | ArrayBufferView;
|
|
4
|
+
|
|
5
|
+
export type DeploymentPrivateKey = Uint8Array | string;
|
|
6
|
+
|
|
7
|
+
export interface DeploymentSigner {
|
|
8
|
+
address: string;
|
|
9
|
+
privateKey: DeploymentPrivateKey;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type DeploymentOperation =
|
|
13
|
+
"deployProgram" | "deployProgramABI" | "upgradeProgram" | "upgradeProgramABI";
|
|
14
|
+
|
|
15
|
+
export type DeployProgressPhase =
|
|
16
|
+
"validation" | "preflight" | "upload" | "commit" | "verification" | "cleanup";
|
|
17
|
+
|
|
18
|
+
export type DeployProgressStatus =
|
|
19
|
+
"started" | "progress" | "succeeded" | "failed";
|
|
20
|
+
|
|
21
|
+
export interface DeployProgressEvent {
|
|
22
|
+
operation: DeploymentOperation;
|
|
23
|
+
phase: DeployProgressPhase;
|
|
24
|
+
status: DeployProgressStatus;
|
|
25
|
+
artifact?: "program" | "abi";
|
|
26
|
+
uploadStep?: "create" | "write" | "finalize";
|
|
27
|
+
completedChunks?: number;
|
|
28
|
+
totalChunks?: number;
|
|
29
|
+
signature?: string;
|
|
30
|
+
message?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface DeploymentRequestBase {
|
|
34
|
+
seed: string;
|
|
35
|
+
signer: DeploymentSigner;
|
|
36
|
+
client?: Thru;
|
|
37
|
+
ephemeral?: boolean;
|
|
38
|
+
chunkSize?: number;
|
|
39
|
+
onProgress?: (event: DeployProgressEvent) => void;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface ProgramMutationRequest extends DeploymentRequestBase {
|
|
43
|
+
program: DeploymentBytes;
|
|
44
|
+
abi?: DeploymentBytes;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface ProgramABIMutationRequest extends DeploymentRequestBase {
|
|
48
|
+
abi: DeploymentBytes;
|
|
49
|
+
programAddress?: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type DeployProgramRequest = ProgramMutationRequest;
|
|
53
|
+
|
|
54
|
+
export type DeployProgramABIRequest = ProgramABIMutationRequest;
|
|
55
|
+
|
|
56
|
+
export type UpgradeProgramRequest = ProgramMutationRequest & {
|
|
57
|
+
programAddress?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type UpgradeProgramABIRequest = ProgramABIMutationRequest;
|
|
61
|
+
|
|
62
|
+
export type UploadCleanupResult =
|
|
63
|
+
| { status: "succeeded"; transactionSignature: string }
|
|
64
|
+
| { status: "not-needed" }
|
|
65
|
+
| {
|
|
66
|
+
status: "failed";
|
|
67
|
+
error: string;
|
|
68
|
+
transactionSignature?: string;
|
|
69
|
+
remainingAccountAddresses?: string[];
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export interface UploadArtifactResult {
|
|
73
|
+
artifact: "program" | "abi";
|
|
74
|
+
size: number;
|
|
75
|
+
sha256: string;
|
|
76
|
+
metaAccountAddress: string;
|
|
77
|
+
bufferAccountAddress: string;
|
|
78
|
+
reused: boolean;
|
|
79
|
+
transactionSignatures: string[];
|
|
80
|
+
cleanup: UploadCleanupResult;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface ManagedProgramResultBase {
|
|
84
|
+
seed: string;
|
|
85
|
+
ephemeral: boolean;
|
|
86
|
+
authorityAddress: string;
|
|
87
|
+
programMetaAccountAddress: string;
|
|
88
|
+
programAccountAddress: string;
|
|
89
|
+
programVersion: bigint;
|
|
90
|
+
transactionSignature: string;
|
|
91
|
+
warnings: string[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
interface ABIResultFields {
|
|
95
|
+
abiMetaAccountAddress: string;
|
|
96
|
+
abiAccountAddress: string;
|
|
97
|
+
abiRevision: bigint;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
interface ProgramMutationResult extends ManagedProgramResultBase {
|
|
101
|
+
programSize: number;
|
|
102
|
+
abiSize?: number;
|
|
103
|
+
abiMetaAccountAddress?: string;
|
|
104
|
+
abiAccountAddress?: string;
|
|
105
|
+
abiRevision?: bigint;
|
|
106
|
+
programUpload: UploadArtifactResult;
|
|
107
|
+
abiUpload?: UploadArtifactResult;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
interface ProgramABIMutationResult
|
|
111
|
+
extends ManagedProgramResultBase, ABIResultFields {
|
|
112
|
+
abiSize: number;
|
|
113
|
+
abiUpload: UploadArtifactResult;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export type DeployProgramResult = ProgramMutationResult;
|
|
117
|
+
|
|
118
|
+
export type DeployProgramABIResult = ProgramABIMutationResult;
|
|
119
|
+
|
|
120
|
+
export type UpgradeProgramResult = ProgramMutationResult;
|
|
121
|
+
|
|
122
|
+
export type UpgradeProgramABIResult = ProgramABIMutationResult;
|
|
123
|
+
|
|
124
|
+
export interface InspectProgramDeploymentRequest {
|
|
125
|
+
client: Thru;
|
|
126
|
+
seed: string;
|
|
127
|
+
ephemeral?: boolean;
|
|
128
|
+
authorityAddress?: string;
|
|
129
|
+
expectedProgramBytes?: Uint8Array;
|
|
130
|
+
inspectABI?: boolean;
|
|
131
|
+
expectedABIBytes?: Uint8Array;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
interface MissingAccountPair {
|
|
135
|
+
status: "missing";
|
|
136
|
+
metadataPresent: false;
|
|
137
|
+
accountPresent: false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface PartialAccountPair {
|
|
141
|
+
status: "partial";
|
|
142
|
+
metadataPresent: boolean;
|
|
143
|
+
accountPresent: boolean;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type ProgramAccountInspection =
|
|
147
|
+
| MissingAccountPair
|
|
148
|
+
| PartialAccountPair
|
|
149
|
+
| {
|
|
150
|
+
status: "present";
|
|
151
|
+
metadataPresent: true;
|
|
152
|
+
accountPresent: true;
|
|
153
|
+
authorityAddress: string;
|
|
154
|
+
version: bigint;
|
|
155
|
+
state: number;
|
|
156
|
+
bytesMatch?: boolean;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export type ABIAccountInspection =
|
|
160
|
+
| MissingAccountPair
|
|
161
|
+
| PartialAccountPair
|
|
162
|
+
| {
|
|
163
|
+
status: "present";
|
|
164
|
+
metadataPresent: true;
|
|
165
|
+
accountPresent: true;
|
|
166
|
+
revision: bigint;
|
|
167
|
+
state: number;
|
|
168
|
+
bytesMatch?: boolean;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export interface ProgramDeploymentInspection {
|
|
172
|
+
programMetaAccountAddress: string;
|
|
173
|
+
programAccountAddress: string;
|
|
174
|
+
abiMetaAccountAddress: string;
|
|
175
|
+
abiAccountAddress: string;
|
|
176
|
+
program: ProgramAccountInspection;
|
|
177
|
+
abi?: ABIAccountInspection;
|
|
178
|
+
}
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import { Pubkey, type Account, type InstructionContext } from "@thru/sdk";
|
|
2
|
+
import type { Thru } from "@thru/sdk/client";
|
|
3
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { readU32le } from "../helpers/bytes";
|
|
5
|
+
import {
|
|
6
|
+
UPLOADER_PROGRAM_ADDRESS,
|
|
7
|
+
UPLOADER_STATE_FINALIZED,
|
|
8
|
+
UPLOADER_STATE_OPEN,
|
|
9
|
+
} from "./constants";
|
|
10
|
+
import { DeployError } from "./errors";
|
|
11
|
+
import {
|
|
12
|
+
cleanupUpload,
|
|
13
|
+
prepareUpload,
|
|
14
|
+
uploadArtifact,
|
|
15
|
+
type PreparedUpload,
|
|
16
|
+
} from "./uploader";
|
|
17
|
+
import type { ResolvedSigner } from "./validation";
|
|
18
|
+
|
|
19
|
+
const mocks = vi.hoisted(() => ({ submitTransaction: vi.fn() }));
|
|
20
|
+
|
|
21
|
+
vi.mock("./chain", async (importOriginal) => {
|
|
22
|
+
const actual = await importOriginal<typeof import("./chain")>();
|
|
23
|
+
return { ...actual, submitTransaction: mocks.submitTransaction };
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
function missing(): never {
|
|
27
|
+
throw Object.assign(new Error("not found"), { code: 5 });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function storedAccount(address: string, data: Uint8Array): Account {
|
|
31
|
+
return {
|
|
32
|
+
address: Pubkey.from(address),
|
|
33
|
+
meta: {
|
|
34
|
+
owner: Pubkey.from(UPLOADER_PROGRAM_ADDRESS),
|
|
35
|
+
flags: { isProgram: false },
|
|
36
|
+
},
|
|
37
|
+
data: { data },
|
|
38
|
+
} as Account;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function uploaderMeta(
|
|
42
|
+
upload: PreparedUpload,
|
|
43
|
+
signer: ResolvedSigner,
|
|
44
|
+
state: number,
|
|
45
|
+
): Uint8Array {
|
|
46
|
+
const data = new Uint8Array(65);
|
|
47
|
+
data.set(signer.publicKey);
|
|
48
|
+
data.set(upload.hash, 32);
|
|
49
|
+
data[64] = state;
|
|
50
|
+
return data;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function contextFor(
|
|
54
|
+
transaction: {
|
|
55
|
+
program: string;
|
|
56
|
+
readWrite?: string[];
|
|
57
|
+
readOnly?: string[];
|
|
58
|
+
},
|
|
59
|
+
feePayer: string,
|
|
60
|
+
): InstructionContext {
|
|
61
|
+
const addresses = [
|
|
62
|
+
feePayer,
|
|
63
|
+
transaction.program,
|
|
64
|
+
...(transaction.readWrite ?? []),
|
|
65
|
+
...(transaction.readOnly ?? []),
|
|
66
|
+
];
|
|
67
|
+
return {
|
|
68
|
+
accounts: [],
|
|
69
|
+
getAccountIndex: (value) => {
|
|
70
|
+
const normalized =
|
|
71
|
+
typeof value === "string" ? value : Pubkey.from(value).toThruFmt();
|
|
72
|
+
const index = addresses.indexOf(normalized);
|
|
73
|
+
if (index < 0) throw new Error(`missing account ${normalized}`);
|
|
74
|
+
return index;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
describe("resumable uploader", () => {
|
|
80
|
+
const privateKey = new Uint8Array(32).fill(1);
|
|
81
|
+
const signer: ResolvedSigner = {
|
|
82
|
+
address: Pubkey.from(new Uint8Array(32).fill(2)).toThruFmt(),
|
|
83
|
+
publicKey: new Uint8Array(32).fill(2),
|
|
84
|
+
privateKey,
|
|
85
|
+
};
|
|
86
|
+
let accounts: Map<string, Account>;
|
|
87
|
+
let client: Thru;
|
|
88
|
+
let signatureNumber: number;
|
|
89
|
+
let failCreateAfterApply: boolean;
|
|
90
|
+
let failFirstWriteAfterApply: boolean;
|
|
91
|
+
let failFinalizeAfterApply: boolean;
|
|
92
|
+
|
|
93
|
+
beforeEach(() => {
|
|
94
|
+
accounts = new Map();
|
|
95
|
+
signatureNumber = 0;
|
|
96
|
+
failCreateAfterApply = false;
|
|
97
|
+
failFirstWriteAfterApply = false;
|
|
98
|
+
failFinalizeAfterApply = false;
|
|
99
|
+
client = {
|
|
100
|
+
accounts: {
|
|
101
|
+
get: vi.fn(
|
|
102
|
+
async (address: string) => accounts.get(address) ?? missing(),
|
|
103
|
+
),
|
|
104
|
+
},
|
|
105
|
+
} as unknown as Thru;
|
|
106
|
+
mocks.submitTransaction.mockReset();
|
|
107
|
+
mocks.submitTransaction.mockImplementation(
|
|
108
|
+
async (
|
|
109
|
+
_client: Thru,
|
|
110
|
+
_signer: ResolvedSigner,
|
|
111
|
+
_operation: string,
|
|
112
|
+
_phase: string,
|
|
113
|
+
transaction: {
|
|
114
|
+
program: string;
|
|
115
|
+
readWrite?: string[];
|
|
116
|
+
readOnly?: string[];
|
|
117
|
+
instructionData:
|
|
118
|
+
| Uint8Array
|
|
119
|
+
| ((
|
|
120
|
+
context: InstructionContext,
|
|
121
|
+
) => Uint8Array | Promise<Uint8Array>);
|
|
122
|
+
},
|
|
123
|
+
) => {
|
|
124
|
+
const data =
|
|
125
|
+
typeof transaction.instructionData === "function"
|
|
126
|
+
? await transaction.instructionData(
|
|
127
|
+
contextFor(transaction, signer.address),
|
|
128
|
+
)
|
|
129
|
+
: transaction.instructionData;
|
|
130
|
+
const opcode = readU32le(data, 0);
|
|
131
|
+
const [metaAddress, bufferAddress] = transaction.readWrite ?? [];
|
|
132
|
+
const upload = currentUpload;
|
|
133
|
+
const signature = `sig-${++signatureNumber}`;
|
|
134
|
+
if (opcode === 0) {
|
|
135
|
+
accounts.set(
|
|
136
|
+
metaAddress,
|
|
137
|
+
storedAccount(
|
|
138
|
+
metaAddress,
|
|
139
|
+
uploaderMeta(upload, signer, UPLOADER_STATE_OPEN),
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
accounts.set(
|
|
143
|
+
bufferAddress,
|
|
144
|
+
storedAccount(bufferAddress, new Uint8Array(upload.bytes.length)),
|
|
145
|
+
);
|
|
146
|
+
if (failCreateAfterApply) {
|
|
147
|
+
failCreateAfterApply = false;
|
|
148
|
+
throw new DeployError("OUTCOME_UNKNOWN", "lost response", {
|
|
149
|
+
transactionSignature: signature,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
} else if (opcode === 1) {
|
|
153
|
+
const size = readU32le(data, 8);
|
|
154
|
+
const offset = readU32le(data, 12);
|
|
155
|
+
accounts
|
|
156
|
+
.get(bufferAddress)!
|
|
157
|
+
.data!.data!.set(data.slice(16, 16 + size), offset);
|
|
158
|
+
if (failFirstWriteAfterApply) {
|
|
159
|
+
failFirstWriteAfterApply = false;
|
|
160
|
+
throw new DeployError("OUTCOME_UNKNOWN", "lost response", {
|
|
161
|
+
transactionSignature: signature,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
} else if (opcode === 3) {
|
|
165
|
+
accounts.get(metaAddress)!.data!.data![64] = UPLOADER_STATE_FINALIZED;
|
|
166
|
+
if (failFinalizeAfterApply) {
|
|
167
|
+
failFinalizeAfterApply = false;
|
|
168
|
+
throw new DeployError("OUTCOME_UNKNOWN", "lost response", {
|
|
169
|
+
transactionSignature: signature,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
} else if (opcode === 2) {
|
|
173
|
+
accounts.delete(metaAddress);
|
|
174
|
+
accounts.delete(bufferAddress);
|
|
175
|
+
}
|
|
176
|
+
return signature;
|
|
177
|
+
},
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
let currentUpload: PreparedUpload;
|
|
182
|
+
|
|
183
|
+
async function makeUpload(
|
|
184
|
+
bytes = new Uint8Array(2_048).fill(0x5a),
|
|
185
|
+
): Promise<PreparedUpload> {
|
|
186
|
+
currentUpload = await prepareUpload(
|
|
187
|
+
"deployProgram",
|
|
188
|
+
"program",
|
|
189
|
+
"upload-test",
|
|
190
|
+
bytes,
|
|
191
|
+
);
|
|
192
|
+
return currentUpload;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
it("creates, uploads, finalizes, reports progress, and cleans a fresh upload", async () => {
|
|
196
|
+
const upload = await makeUpload();
|
|
197
|
+
const events: string[] = [];
|
|
198
|
+
const result = await uploadArtifact(
|
|
199
|
+
client,
|
|
200
|
+
signer,
|
|
201
|
+
upload,
|
|
202
|
+
1_024,
|
|
203
|
+
(event) => {
|
|
204
|
+
events.push(`${event.uploadStep ?? event.phase}:${event.status}`);
|
|
205
|
+
},
|
|
206
|
+
);
|
|
207
|
+
expect(result.reused).toBe(false);
|
|
208
|
+
expect(result.transactionSignatures).toEqual([
|
|
209
|
+
"sig-1",
|
|
210
|
+
"sig-2",
|
|
211
|
+
"sig-3",
|
|
212
|
+
"sig-4",
|
|
213
|
+
]);
|
|
214
|
+
expect(accounts.get(result.bufferAccountAddress)?.data?.data).toEqual(
|
|
215
|
+
upload.bytes,
|
|
216
|
+
);
|
|
217
|
+
expect(accounts.get(result.metaAccountAddress)?.data?.data?.[64]).toBe(
|
|
218
|
+
UPLOADER_STATE_FINALIZED,
|
|
219
|
+
);
|
|
220
|
+
expect(events).toContain("write:progress");
|
|
221
|
+
expect(events.at(-1)).toBe("finalize:succeeded");
|
|
222
|
+
|
|
223
|
+
await expect(cleanupUpload(client, signer, upload)).resolves.toEqual({
|
|
224
|
+
status: "succeeded",
|
|
225
|
+
transactionSignature: "sig-5",
|
|
226
|
+
});
|
|
227
|
+
expect(accounts.size).toBe(0);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
it("resumes matching chunks and writes only missing data", async () => {
|
|
231
|
+
const bytes = new Uint8Array(2_048).fill(0x33);
|
|
232
|
+
const upload = await makeUpload(bytes);
|
|
233
|
+
const buffer = new Uint8Array(bytes.length);
|
|
234
|
+
buffer.set(bytes.slice(0, 1_024));
|
|
235
|
+
accounts.set(
|
|
236
|
+
upload.result.metaAccountAddress,
|
|
237
|
+
storedAccount(
|
|
238
|
+
upload.result.metaAccountAddress,
|
|
239
|
+
uploaderMeta(upload, signer, UPLOADER_STATE_OPEN),
|
|
240
|
+
),
|
|
241
|
+
);
|
|
242
|
+
accounts.set(
|
|
243
|
+
upload.result.bufferAccountAddress,
|
|
244
|
+
storedAccount(upload.result.bufferAccountAddress, buffer),
|
|
245
|
+
);
|
|
246
|
+
|
|
247
|
+
const result = await uploadArtifact(client, signer, upload, 1_024);
|
|
248
|
+
expect(result.reused).toBe(true);
|
|
249
|
+
expect(result.transactionSignatures).toEqual(["sig-1", "sig-2"]);
|
|
250
|
+
expect(accounts.get(result.bufferAccountAddress)?.data?.data).toEqual(
|
|
251
|
+
bytes,
|
|
252
|
+
);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("reconciles an unknown write outcome and retains its signature", async () => {
|
|
256
|
+
const upload = await makeUpload(new Uint8Array(1_024).fill(0x44));
|
|
257
|
+
failFirstWriteAfterApply = true;
|
|
258
|
+
const result = await uploadArtifact(client, signer, upload, 1_024);
|
|
259
|
+
expect(result.transactionSignatures).toEqual(["sig-1", "sig-2", "sig-3"]);
|
|
260
|
+
expect(accounts.get(result.bufferAccountAddress)?.data?.data).toEqual(
|
|
261
|
+
upload.bytes,
|
|
262
|
+
);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("reconciles an unknown create outcome and retains its signature", async () => {
|
|
266
|
+
const upload = await makeUpload(new Uint8Array(1_024).fill(0x45));
|
|
267
|
+
failCreateAfterApply = true;
|
|
268
|
+
const result = await uploadArtifact(client, signer, upload, 1_024);
|
|
269
|
+
expect(result.transactionSignatures).toEqual(["sig-1", "sig-2", "sig-3"]);
|
|
270
|
+
expect(accounts.get(result.metaAccountAddress)?.data?.data?.[64]).toBe(
|
|
271
|
+
UPLOADER_STATE_FINALIZED,
|
|
272
|
+
);
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("reconciles an unknown finalize outcome and retains its signature", async () => {
|
|
276
|
+
const upload = await makeUpload(new Uint8Array(1_024).fill(0x46));
|
|
277
|
+
failFinalizeAfterApply = true;
|
|
278
|
+
const result = await uploadArtifact(client, signer, upload, 1_024);
|
|
279
|
+
expect(result.transactionSignatures).toEqual(["sig-1", "sig-2", "sig-3"]);
|
|
280
|
+
expect(accounts.get(result.metaAccountAddress)?.data?.data?.[64]).toBe(
|
|
281
|
+
UPLOADER_STATE_FINALIZED,
|
|
282
|
+
);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("reuses a matching finalized upload without submitting transactions", async () => {
|
|
286
|
+
const upload = await makeUpload();
|
|
287
|
+
accounts.set(
|
|
288
|
+
upload.result.metaAccountAddress,
|
|
289
|
+
storedAccount(
|
|
290
|
+
upload.result.metaAccountAddress,
|
|
291
|
+
uploaderMeta(upload, signer, UPLOADER_STATE_FINALIZED),
|
|
292
|
+
),
|
|
293
|
+
);
|
|
294
|
+
accounts.set(
|
|
295
|
+
upload.result.bufferAccountAddress,
|
|
296
|
+
storedAccount(upload.result.bufferAccountAddress, upload.bytes.slice()),
|
|
297
|
+
);
|
|
298
|
+
const result = await uploadArtifact(client, signer, upload, 1_024);
|
|
299
|
+
expect(result.reused).toBe(true);
|
|
300
|
+
expect(result.transactionSignatures).toEqual([]);
|
|
301
|
+
expect(mocks.submitTransaction).not.toHaveBeenCalled();
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("rejects an existing upload with a conflicting hash", async () => {
|
|
305
|
+
const upload = await makeUpload();
|
|
306
|
+
const meta = uploaderMeta(upload, signer, UPLOADER_STATE_OPEN);
|
|
307
|
+
meta[32] ^= 0xff;
|
|
308
|
+
accounts.set(
|
|
309
|
+
upload.result.metaAccountAddress,
|
|
310
|
+
storedAccount(upload.result.metaAccountAddress, meta),
|
|
311
|
+
);
|
|
312
|
+
accounts.set(
|
|
313
|
+
upload.result.bufferAccountAddress,
|
|
314
|
+
storedAccount(
|
|
315
|
+
upload.result.bufferAccountAddress,
|
|
316
|
+
new Uint8Array(upload.bytes.length),
|
|
317
|
+
),
|
|
318
|
+
);
|
|
319
|
+
await expect(
|
|
320
|
+
uploadArtifact(client, signer, upload, 1_024),
|
|
321
|
+
).rejects.toMatchObject({
|
|
322
|
+
code: "UPLOAD_CONFLICT",
|
|
323
|
+
});
|
|
324
|
+
expect(mocks.submitTransaction).not.toHaveBeenCalled();
|
|
325
|
+
});
|
|
326
|
+
});
|