Skip to content

Commit 1bfd01f

Browse files
committed
verbose logging
1 parent df93385 commit 1bfd01f

12 files changed

Lines changed: 458 additions & 37 deletions

src/codestorage/infura.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import fs from "fs";
44
import { of } from "ipfs-only-hash";
55
import { CodeStorageProvider, FileUploadSpec } from "./provider";
66
import { ToContent } from "ipfs-core-types/src/utils";
7+
import { getLogger } from "../logger";
8+
9+
const logger = getLogger("infura");
10+
711
export class Infura implements CodeStorageProvider {
812
#client: IPFSHTTPClient;
913

@@ -23,25 +27,63 @@ export class Infura implements CodeStorageProvider {
2327
}
2428

2529
async writeFromContent(files: ToContent[], pin: boolean): Promise<string[]> {
26-
return Promise.all(
27-
files.map((f) =>
30+
logger.debug("Writing content to IPFS", { filesCount: files.length, pin });
31+
const startTime = Date.now();
32+
33+
const results = await Promise.all(
34+
files.map((f, index) =>
2835
this.#client.add({ content: f }, { pin }).then((r) => {
29-
return `ipfs://${r.cid.toString()}`;
36+
const cid = `ipfs://${r.cid.toString()}`;
37+
logger.debug("File uploaded to IPFS", { index, cid });
38+
return cid;
3039
}),
3140
),
3241
);
42+
43+
logger.info("Content written to IPFS successfully", {
44+
filesCount: results.length,
45+
duration: Date.now() - startTime,
46+
});
47+
48+
return results;
3349
}
3450

3551
async write(files: FileUploadSpec[], pin: boolean): Promise<string[]> {
52+
logger.debug("Writing files to IPFS", {
53+
filesCount: files.length,
54+
pin,
55+
files: files.map((f) => f.name),
56+
});
3657
return this.writeFromContent(
3758
files.map((f) => fs.createReadStream(f.path)),
3859
pin,
3960
);
4061
}
4162

4263
async read(pointer: string): Promise<string> {
43-
return (
44-
await fetch(`https://${process.env.IPFS_PROVIDER}/ipfs/${pointer.replace("ipfs://", "")}`)
45-
).text();
64+
logger.debug("Reading from IPFS", { pointer });
65+
const startTime = Date.now();
66+
67+
const url = `https://${process.env.IPFS_PROVIDER}/ipfs/${pointer.replace("ipfs://", "")}`;
68+
const response = await fetch(url);
69+
70+
if (!response.ok) {
71+
logger.error("Failed to read from IPFS", {
72+
pointer,
73+
url,
74+
status: response.status,
75+
statusText: response.statusText,
76+
});
77+
throw new Error(`IPFS read failed: ${response.status} ${response.statusText}`);
78+
}
79+
80+
const text = await response.text();
81+
logger.debug("Successfully read from IPFS", {
82+
pointer,
83+
duration: Date.now() - startTime,
84+
size: text.length,
85+
});
86+
87+
return text;
4688
}
4789
}

src/codestorage/pinata.ts

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { of } from "ipfs-only-hash";
55
import { Readable } from "stream";
66
import { PinataSDK } from "pinata";
77
import { CodeStorageProvider, FileUploadSpec } from "./provider";
8+
import { getLogger } from "../logger";
9+
10+
const logger = getLogger("pinata");
811

912
export class Pinata implements CodeStorageProvider {
1013
#pinata: PinataSDK;
@@ -20,8 +23,11 @@ export class Pinata implements CodeStorageProvider {
2023
}
2124

2225
async writeFromContent(files: ToContent[], pin: boolean): Promise<string[]> {
23-
return Promise.all(
24-
files.map(async (f) => {
26+
logger.debug("Writing content to IPFS", { filesCount: files.length, pin });
27+
const startTime = Date.now();
28+
29+
const results = await Promise.all(
30+
files.map(async (f, index) => {
2531
// Convert ToContent to a File-like object that Pinata SDK can handle
2632
let fileData: Buffer;
2733
if (Buffer.isBuffer(f)) {
@@ -41,25 +47,73 @@ export class Pinata implements CodeStorageProvider {
4147
// Create a File from the buffer
4248
const file = new File([fileData], "file");
4349
const result = await this.#pinata.upload.public.file(file);
44-
return `ipfs://${result.cid}`;
50+
const cid = `ipfs://${result.cid}`;
51+
logger.debug("File uploaded to IPFS", { index, cid });
52+
return cid;
4553
}),
4654
);
55+
56+
logger.info("Content written to IPFS successfully", {
57+
filesCount: results.length,
58+
duration: Date.now() - startTime,
59+
});
60+
61+
return results;
4762
}
4863

4964
async write(files: FileUploadSpec[], pin: boolean): Promise<string[]> {
50-
return Promise.all(
51-
files.map(async (fileSpec) => {
65+
logger.debug("Writing files to IPFS", {
66+
filesCount: files.length,
67+
pin,
68+
files: files.map((f) => f.name),
69+
});
70+
const startTime = Date.now();
71+
72+
const results = await Promise.all(
73+
files.map(async (fileSpec, index) => {
5274
// Read the file from the filesystem
5375
const fileData = await fs.promises.readFile(fileSpec.path);
5476
const file = new File([fileData], fileSpec.name);
5577
const result = await this.#pinata.upload.public.file(file);
56-
return `ipfs://${result.cid}`;
78+
const cid = `ipfs://${result.cid}`;
79+
logger.debug("File uploaded to IPFS", { index, fileName: fileSpec.name, cid });
80+
return cid;
5781
}),
5882
);
83+
84+
logger.info("Files written to IPFS successfully", {
85+
filesCount: results.length,
86+
duration: Date.now() - startTime,
87+
});
88+
89+
return results;
5990
}
6091

6192
async read(pointer: string): Promise<string> {
93+
logger.debug("Reading from IPFS", { pointer });
94+
const startTime = Date.now();
95+
6296
const hash = pointer.replace("ipfs://", "");
63-
return (await fetch(`https://${this.#gateway}/ipfs/${hash}`)).text();
97+
const url = `https://${this.#gateway}/ipfs/${hash}`;
98+
const response = await fetch(url);
99+
100+
if (!response.ok) {
101+
logger.error("Failed to read from IPFS", {
102+
pointer,
103+
url,
104+
status: response.status,
105+
statusText: response.statusText,
106+
});
107+
throw new Error(`IPFS read failed: ${response.status} ${response.statusText}`);
108+
}
109+
110+
const text = await response.text();
111+
logger.debug("Successfully read from IPFS", {
112+
pointer,
113+
duration: Date.now() - startTime,
114+
size: text.length,
115+
});
116+
117+
return text;
64118
}
65119
}

src/controller.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,54 @@ export class Controller {
6161
}
6262

6363
async addSource(verificationPayload: SourceVerifyPayload): Promise<VerifyResult> {
64+
logger.info("Starting source verification", {
65+
compiler: verificationPayload.compiler,
66+
knownContractHash: verificationPayload.knownContractHash,
67+
sourcesCount: verificationPayload.sources.length,
68+
});
69+
6470
// Compile
71+
logger.debug("Compiling sources", { compiler: verificationPayload.compiler });
6572
const compiler = this.compilers[verificationPayload.compiler];
6673
const compileResult = await compiler.verify(verificationPayload);
74+
6775
if (compileResult.error || compileResult.result !== "similar" || !compileResult.hash) {
76+
logger.warn("Compilation failed or hash mismatch", {
77+
result: compileResult.result,
78+
error: compileResult.error,
79+
hash: compileResult.hash,
80+
});
6881
return {
6982
compileResult,
7083
};
7184
}
7285

86+
logger.info("Compilation successful", {
87+
hash: compileResult.hash,
88+
result: compileResult.result,
89+
});
90+
7391
if (!this.config.allowReverification) {
92+
logger.debug("Checking if proof already deployed");
7493
const isDeployed = await this.tonReaderClient.isProofDeployed(
7594
verificationPayload.knownContractHash,
7695
this.config.verifierId,
7796
);
7897
if (isDeployed) {
98+
logger.warn("Contract already verified", {
99+
knownContractHash: verificationPayload.knownContractHash,
100+
});
79101
return {
80102
compileResult: {
81103
result: "unknown_error",
82-
error: "Contract is already deployed",
104+
error: "Proof has already been deployed",
83105
hash: null,
84106
compilerSettings: compileResult.compilerSettings,
85107
sources: compileResult.sources,
86108
},
87109
};
88110
}
111+
logger.debug("Proof not deployed, proceeding");
89112
}
90113

91114
// Upload sources to IPFS
@@ -101,7 +124,14 @@ export class Controller {
101124
name: s.filename,
102125
}),
103126
);
127+
128+
logger.info("Uploading sources to IPFS", {
129+
sourcesCount: sourcesToUpload.length,
130+
});
104131
const fileLocators = await this.ipfsProvider.write(sourcesToUpload, true);
132+
logger.info("Sources uploaded to IPFS", {
133+
filesUploaded: fileLocators.length,
134+
});
105135

106136
const sourceSpec: SourceItem = {
107137
compilerSettings: compileResult.compilerSettings,
@@ -118,14 +148,16 @@ export class Controller {
118148
};
119149

120150
// Upload source spec JSON to IPFS
151+
logger.debug("Uploading source spec to IPFS");
121152
const [ipfsLink] = await this.ipfsProvider.writeFromContent(
122153
[Buffer.from(JSON.stringify(sourceSpec))],
123154
true,
124155
);
125156

126-
logger.info(ipfsLink);
157+
logger.info("Source spec uploaded", { ipfsLink });
127158

128159
const queryId = random64BitNumber();
160+
logger.debug("Signing message", { queryId });
129161

130162
// This is the message that will be forwarded to verifier registry
131163
const msgToSign = cellToSign(
@@ -139,6 +171,11 @@ export class Controller {
139171

140172
const { sig, sigCell } = signatureCell(msgToSign, this.keypair);
141173

174+
logger.info("Source verification completed successfully", {
175+
ipfsLink,
176+
hash: compileResult.hash,
177+
});
178+
142179
return {
143180
compileResult,
144181
sig: sig.toString("base64"),
@@ -148,13 +185,17 @@ export class Controller {
148185
}
149186

150187
public async sign({ messageCell, tmpDir }: { messageCell: Buffer; tmpDir: string }) {
188+
logger.info("Starting message signing");
189+
151190
const cell = Cell.fromBoc(Buffer.from(messageCell))[0];
152191

192+
logger.debug("Getting verifier config");
153193
const verifierConfig = await this.tonReaderClient.getVerifierConfig(
154194
this.config.verifierId,
155195
this.config.sourcesRegistryAddress,
156196
);
157197

198+
logger.debug("Validating message cell");
158199
const { ipfsPointer, codeCellHash, senderAddress, queryId } = validateMessageCell(
159200
cell,
160201
this.VERIFIER_SHA256,
@@ -163,16 +204,31 @@ export class Controller {
163204
verifierConfig,
164205
);
165206

207+
logger.debug("Reading source spec from IPFS", { ipfsPointer });
166208
const sourceTemp = await this.ipfsProvider.read(ipfsPointer);
167209

168210
const json: SourceItem = JSON.parse(sourceTemp);
169211

212+
logger.debug("Verifying code hash", {
213+
expectedHash: codeCellHash,
214+
actualHash: json.hash,
215+
});
216+
170217
if (json.hash !== codeCellHash) {
218+
logger.error("Code hash mismatch", {
219+
expectedHash: codeCellHash,
220+
actualHash: json.hash,
221+
});
171222
throw new Error("Code hash mismatch");
172223
}
173224

174225
const compiler = this.compilers[json.compiler];
175226

227+
logger.debug("Reading sources from IPFS", {
228+
sourcesCount: json.sources.length,
229+
compiler: json.compiler,
230+
});
231+
176232
const sources = await Promise.all(
177233
json.sources.map(async (s) => {
178234
const content = await this.ipfsProvider.read(s.url);
@@ -188,6 +244,10 @@ export class Controller {
188244
}),
189245
);
190246

247+
logger.info("Sources downloaded from IPFS", {
248+
sourcesCount: sources.length,
249+
});
250+
191251
const sourceToVerify: SourceVerifyPayload = {
192252
sources: sources,
193253
compiler: json.compiler,
@@ -203,17 +263,27 @@ export class Controller {
203263
senderAddress: senderAddress.toString(),
204264
};
205265

266+
logger.debug("Verifying compilation");
206267
const compileResult = await compiler.verify(sourceToVerify);
207268

208269
if (compileResult.result !== "similar") {
270+
logger.error("Compilation verification failed", {
271+
result: compileResult.result,
272+
error: compileResult.error,
273+
});
209274
throw new Error("Invalid compilation result: " + compileResult.result);
210275
}
211276

277+
logger.info("Compilation verified successfully");
278+
279+
logger.debug("Creating signature cell");
212280
const slice = cell.beginParse();
213281
const msgToSign = slice.loadRef();
214282
const { sigCell } = signatureCell(msgToSign, this.keypair);
215283
let updateSigCell = addSignatureCell(slice.loadRef(), sigCell);
216284

285+
logger.info("Message signing completed successfully");
286+
217287
return {
218288
msgCell: slice.asBuilder().storeRef(msgToSign).storeRef(updateSigCell).asCell().toBoc(),
219289
};

0 commit comments

Comments
 (0)