Skip to content

Commit 96b5ee9

Browse files
authored
Merge pull request #2279 from finnfisher-de/add-logger
feat(core): preserve logs fetched during genMaciState execution
2 parents 801f21e + d638fb6 commit 96b5ee9

File tree

5 files changed

+37
-0
lines changed

5 files changed

+37
-0
lines changed

packages/cli/ts/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ program
10401040
"Backup files for ipfs messages (name format: ipfsHash1.json, ipfsHash2.json, ..., ipfsHashN.json)",
10411041
(value: string | undefined) => value?.split(/\s*,\s*/),
10421042
)
1043+
.option("-l, --logs-output <logsOutputPath>", "the path where to save the logs for debugging and auditing purposes")
10431044
.action(async (cmdObj) => {
10441045
try {
10451046
const signer = await getSigner();
@@ -1066,6 +1067,7 @@ program
10661067
ipfsMessageBackupFiles: cmdObj.ipfsMessageBackupFiles,
10671068
sleep: cmdObj.sleep,
10681069
signer,
1070+
logsOutputPath: cmdObj.logsOutput,
10691071
});
10701072
} catch (error) {
10711073
program.error((error as Error).message, { exitCode: 1 });

packages/contracts/ts/genMaciState.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { sleep, sortActions } from "./utils";
2323
* @param blocksPerRequest - the number of blocks to fetch in each request
2424
* @param endBlock - the block number at which to stop fetching events
2525
* @param sleepAmount - the amount of time to sleep between each request
26+
* @param ipfsMessageBackupFiles - backup files for ipfs messages
27+
* @param logsOutputPath - the file path where to save the logs for debugging and auditing purposes
2628
* @returns an instance of MaciState
2729
*/
2830
export const genMaciStateFromContract = async ({
@@ -35,6 +37,7 @@ export const genMaciStateFromContract = async ({
3537
endBlock,
3638
sleepAmount,
3739
ipfsMessageBackupFiles = [],
40+
logsOutputPath,
3841
}: IGenMaciStateFromContractArgs): Promise<MaciState> => {
3942
// ensure the pollId is valid
4043
assert(pollId >= 0);
@@ -303,6 +306,26 @@ export const genMaciStateFromContract = async ({
303306

304307
maciState.polls.set(pollId, poll);
305308

309+
// Save logs if output path is provided
310+
if (logsOutputPath) {
311+
const logsOutputDirectory = path.resolve(logsOutputPath);
312+
313+
if (!fs.existsSync(logsOutputDirectory)) {
314+
await fs.promises.mkdir(logsOutputDirectory, { recursive: true });
315+
}
316+
317+
const logs = {
318+
maciAddress: address,
319+
pollId: pollId.toString(),
320+
fromBlock,
321+
toBlock: lastBlock,
322+
timestamp: new Date().toISOString(),
323+
actions,
324+
};
325+
326+
await fs.promises.writeFile(logsOutputPath, JSON.stringify(logs, null, 2));
327+
}
328+
306329
return maciState;
307330
};
308331

packages/contracts/ts/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,11 @@ export interface IGenMaciStateFromContractArgs {
305305
* Backup files for ipfs messages (name format: ipfsHash.json)
306306
*/
307307
ipfsMessageBackupFiles?: string[];
308+
309+
/**
310+
* The file path where to save the logs for debugging and auditing purposes
311+
*/
312+
logsOutputPath?: string;
308313
}
309314

310315
/**

packages/sdk/ts/maci/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const generateMaciState = async ({
2626
sleep,
2727
signer,
2828
ipfsMessageBackupFiles,
29+
logsOutputPath,
2930
}: IGenerateMaciStateArgs): Promise<MaciState> => {
3031
if (!maciAddress) {
3132
throw new Error("MACI contract address is empty");
@@ -96,6 +97,7 @@ export const generateMaciState = async ({
9697
endBlock: endBlockNumber,
9798
sleepAmount: sleep,
9899
ipfsMessageBackupFiles,
100+
logsOutputPath,
99101
});
100102

101103
// write the state to a file

packages/sdk/ts/maci/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,9 @@ export interface IGenerateMaciStateArgs {
198198
* Backup files for ipfs messages (name format: ipfsHash.json)
199199
*/
200200
ipfsMessageBackupFiles?: string[];
201+
202+
/**
203+
* The file path where to save the logs for debugging and auditing purposes
204+
*/
205+
logsOutputPath?: string;
201206
}

0 commit comments

Comments
 (0)