@maci-protocol/testing 0.0.0-ci.2653bc0
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/CHANGELOG.md +262 -0
- package/LICENSE +22 -0
- package/README.md +16 -0
- package/build/hardhat.config.d.ts +5 -0
- package/build/hardhat.config.d.ts.map +1 -0
- package/build/hardhat.config.js +37 -0
- package/build/hardhat.config.js.map +1 -0
- package/build/ts/__tests__/e2e.nonQv.test.d.ts +2 -0
- package/build/ts/__tests__/e2e.nonQv.test.d.ts.map +1 -0
- package/build/ts/__tests__/e2e.nonQv.test.js +208 -0
- package/build/ts/__tests__/e2e.nonQv.test.js.map +1 -0
- package/build/ts/__tests__/e2e.test.d.ts +2 -0
- package/build/ts/__tests__/e2e.test.d.ts.map +1 -0
- package/build/ts/__tests__/e2e.test.js +1588 -0
- package/build/ts/__tests__/e2e.test.js.map +1 -0
- package/build/ts/__tests__/integration.test.d.ts +2 -0
- package/build/ts/__tests__/integration.test.d.ts.map +1 -0
- package/build/ts/__tests__/integration.test.js +288 -0
- package/build/ts/__tests__/integration.test.js.map +1 -0
- package/build/ts/__tests__/keyChange.test.d.ts +2 -0
- package/build/ts/__tests__/keyChange.test.d.ts.map +1 -0
- package/build/ts/__tests__/keyChange.test.js +444 -0
- package/build/ts/__tests__/keyChange.test.js.map +1 -0
- package/build/ts/__tests__/maci-keys.test.d.ts +2 -0
- package/build/ts/__tests__/maci-keys.test.d.ts.map +1 -0
- package/build/ts/__tests__/maci-keys.test.js +78 -0
- package/build/ts/__tests__/maci-keys.test.js.map +1 -0
- package/build/ts/__tests__/unit/fundWallet.test.d.ts +2 -0
- package/build/ts/__tests__/unit/fundWallet.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/fundWallet.test.js +15 -0
- package/build/ts/__tests__/unit/fundWallet.test.js.map +1 -0
- package/build/ts/__tests__/unit/joinPoll.test.d.ts +2 -0
- package/build/ts/__tests__/unit/joinPoll.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/joinPoll.test.js +125 -0
- package/build/ts/__tests__/unit/joinPoll.test.js.map +1 -0
- package/build/ts/__tests__/unit/poll.test.d.ts +2 -0
- package/build/ts/__tests__/unit/poll.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/poll.test.js +89 -0
- package/build/ts/__tests__/unit/poll.test.js.map +1 -0
- package/build/ts/__tests__/unit/publish.test.d.ts +2 -0
- package/build/ts/__tests__/unit/publish.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/publish.test.js +129 -0
- package/build/ts/__tests__/unit/publish.test.js.map +1 -0
- package/build/ts/__tests__/unit/signup.test.d.ts +2 -0
- package/build/ts/__tests__/unit/signup.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/signup.test.js +63 -0
- package/build/ts/__tests__/unit/signup.test.js.map +1 -0
- package/build/ts/__tests__/unit/timeTravel.test.d.ts +2 -0
- package/build/ts/__tests__/unit/timeTravel.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/timeTravel.test.js +14 -0
- package/build/ts/__tests__/unit/timeTravel.test.js.map +1 -0
- package/build/ts/__tests__/unit/utils.test.d.ts +2 -0
- package/build/ts/__tests__/unit/utils.test.d.ts.map +1 -0
- package/build/ts/__tests__/unit/utils.test.js +36 -0
- package/build/ts/__tests__/unit/utils.test.js.map +1 -0
- package/build/ts/constants.d.ts +83 -0
- package/build/ts/constants.d.ts.map +1 -0
- package/build/ts/constants.js +156 -0
- package/build/ts/constants.js.map +1 -0
- package/build/ts/index.d.ts +4 -0
- package/build/ts/index.d.ts.map +1 -0
- package/build/ts/index.js +23 -0
- package/build/ts/index.js.map +1 -0
- package/build/ts/testingClass.d.ts +43 -0
- package/build/ts/testingClass.d.ts.map +1 -0
- package/build/ts/testingClass.js +139 -0
- package/build/ts/testingClass.js.map +1 -0
- package/build/ts/types.d.ts +100 -0
- package/build/ts/types.d.ts.map +1 -0
- package/build/ts/types.js +3 -0
- package/build/ts/types.js.map +1 -0
- package/build/ts/user.d.ts +31 -0
- package/build/ts/user.d.ts.map +1 -0
- package/build/ts/user.js +37 -0
- package/build/ts/user.js.map +1 -0
- package/build/ts/utils.d.ts +70 -0
- package/build/ts/utils.d.ts.map +1 -0
- package/build/ts/utils.js +192 -0
- package/build/ts/utils.js.map +1 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +53 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Keypair, type PrivKey } from "@maci-protocol/domainobjs";
|
|
2
|
+
import type { IVote } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* A class representing a user and its votes
|
|
5
|
+
*/
|
|
6
|
+
export declare class User {
|
|
7
|
+
keypair: Keypair;
|
|
8
|
+
votes: IVote[];
|
|
9
|
+
voiceCreditBalance: bigint;
|
|
10
|
+
nonce: bigint;
|
|
11
|
+
stateLeafIndex?: bigint;
|
|
12
|
+
/**
|
|
13
|
+
* The timestamp of when the user joined the poll
|
|
14
|
+
*/
|
|
15
|
+
timestamp?: bigint;
|
|
16
|
+
/**
|
|
17
|
+
* Create a new instance of a UserCommand object
|
|
18
|
+
* @param _keypair the keypair of the user
|
|
19
|
+
* @param votes the votes casted by the user
|
|
20
|
+
* @param voiceCreditBalance the voice credit balance of the user
|
|
21
|
+
* @param nonce the nonce of the user
|
|
22
|
+
* @param timestamp the timestamp of when the user joined the poll
|
|
23
|
+
*/
|
|
24
|
+
constructor(_keypair: Keypair, votes: IVote[], voiceCreditBalance: bigint, nonce: bigint, timestamp?: bigint, stateLeafIndex?: bigint);
|
|
25
|
+
/**
|
|
26
|
+
* Helper function that can be used to change the keypair of the user
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
changeKeypair(): PrivKey;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=user.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.d.ts","sourceRoot":"","sources":["../../ts/user.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAElE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC;;GAEG;AACH,qBAAa,IAAI;IACf,OAAO,EAAE,OAAO,CAAC;IAEjB,KAAK,EAAE,KAAK,EAAE,CAAC;IAEf,kBAAkB,EAAE,MAAM,CAAC;IAE3B,KAAK,EAAE,MAAM,CAAC;IAEd,cAAc,CAAC,EAAE,MAAM,CAAC;IAExB;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;;;;OAOG;gBAED,QAAQ,EAAE,OAAO,EACjB,KAAK,EAAE,KAAK,EAAE,EACd,kBAAkB,EAAE,MAAM,EAC1B,KAAK,EAAE,MAAM,EACb,SAAS,CAAC,EAAE,MAAM,EAClB,cAAc,CAAC,EAAE,MAAM;IAUzB;;;OAGG;IACH,aAAa,IAAI,OAAO;CAMzB"}
|
package/build/ts/user.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.User = void 0;
|
|
4
|
+
const domainobjs_1 = require("@maci-protocol/domainobjs");
|
|
5
|
+
/**
|
|
6
|
+
* A class representing a user and its votes
|
|
7
|
+
*/
|
|
8
|
+
class User {
|
|
9
|
+
/**
|
|
10
|
+
* Create a new instance of a UserCommand object
|
|
11
|
+
* @param _keypair the keypair of the user
|
|
12
|
+
* @param votes the votes casted by the user
|
|
13
|
+
* @param voiceCreditBalance the voice credit balance of the user
|
|
14
|
+
* @param nonce the nonce of the user
|
|
15
|
+
* @param timestamp the timestamp of when the user joined the poll
|
|
16
|
+
*/
|
|
17
|
+
constructor(_keypair, votes, voiceCreditBalance, nonce, timestamp, stateLeafIndex) {
|
|
18
|
+
this.keypair = _keypair;
|
|
19
|
+
this.votes = votes;
|
|
20
|
+
this.voiceCreditBalance = voiceCreditBalance;
|
|
21
|
+
this.nonce = nonce;
|
|
22
|
+
this.timestamp = timestamp;
|
|
23
|
+
this.stateLeafIndex = stateLeafIndex;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Helper function that can be used to change the keypair of the user
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
changeKeypair() {
|
|
30
|
+
const newUserKeypair = new domainobjs_1.Keypair();
|
|
31
|
+
const oldPrivateKey = this.keypair.privKey;
|
|
32
|
+
this.keypair = !newUserKeypair.equals(this.keypair) ? newUserKeypair : this.keypair;
|
|
33
|
+
return oldPrivateKey;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.User = User;
|
|
37
|
+
//# sourceMappingURL=user.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user.js","sourceRoot":"","sources":["../../ts/user.ts"],"names":[],"mappings":";;;AAAA,0DAAkE;AAIlE;;GAEG;AACH,MAAa,IAAI;IAgBf;;;;;;;OAOG;IACH,YACE,QAAiB,EACjB,KAAc,EACd,kBAA0B,EAC1B,KAAa,EACb,SAAkB,EAClB,cAAuB;QAEvB,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC;QACxB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,kBAAkB,GAAG,kBAAkB,CAAC;QAC7C,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;IAED;;;OAGG;IACH,aAAa;QACX,MAAM,cAAc,GAAG,IAAI,oBAAO,EAAE,CAAC;QACrC,MAAM,aAAa,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;QAC3C,IAAI,CAAC,OAAO,GAAG,CAAC,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC;QACpF,OAAO,aAAa,CAAC;IACvB,CAAC;CACF;AAlDD,oBAkDC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { type ITallyData } from "@maci-protocol/sdk";
|
|
2
|
+
import type { IVote, IBriber, IRelayTestMessagesArgs } from "./types";
|
|
3
|
+
import { User } from "./user";
|
|
4
|
+
export declare const backupFolder = "./backup";
|
|
5
|
+
export declare const defaultVote: {
|
|
6
|
+
voteWeight: bigint;
|
|
7
|
+
nonce: bigint;
|
|
8
|
+
maxVoteWeight: bigint;
|
|
9
|
+
voteCreditBalance: bigint;
|
|
10
|
+
voteOptionIndex: bigint;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Read a JSON file from disk
|
|
14
|
+
* @param filePath - the path of the file
|
|
15
|
+
* @returns the JSON object
|
|
16
|
+
*/
|
|
17
|
+
export declare const readJSONFile: <T = Record<string, Record<string, string> | undefined>>(filePath: string) => Promise<T>;
|
|
18
|
+
/**
|
|
19
|
+
* Generate a list of user commands for integration tests
|
|
20
|
+
* @param numUsers - the number of users
|
|
21
|
+
* @param numVotesPerUser - the number of votes per user
|
|
22
|
+
* @param bribers - the number of bribers
|
|
23
|
+
* @param presetVotes - the preset votes if any
|
|
24
|
+
* @returns an array of UserCommand objects
|
|
25
|
+
*/
|
|
26
|
+
export declare const genTestUserCommands: (numUsers: number, numVotesPerUser: number, bribers?: IBriber[], presetVotes?: IVote[][]) => User[];
|
|
27
|
+
/**
|
|
28
|
+
* Assertion function to validate that the tally results are as expected
|
|
29
|
+
* @param maxMessages - the max number of messages
|
|
30
|
+
* @param expectedTally - the expected tally values
|
|
31
|
+
* @param expectedPerVOSpentVoiceCredits - the expected per VO spent voice credits
|
|
32
|
+
* @param expectedTotalSpentVoiceCredits - the expected total spent voice credits
|
|
33
|
+
* @param tallyFile the tally file itself as an object
|
|
34
|
+
*/
|
|
35
|
+
export declare const expectTally: (maxMessages: number, expectedTally: number[], expectedPerVOSpentVoiceCredits: number[], expectedTotalSpentVoiceCredits: number, tallyFile: ITallyData) => void;
|
|
36
|
+
/**
|
|
37
|
+
* Stop the current thread for x seconds
|
|
38
|
+
* @param ms - the number of ms to sleep for
|
|
39
|
+
*/
|
|
40
|
+
export declare const sleep: (ms: number) => Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Check whether we are running on an arm chip
|
|
43
|
+
* @returns whether we are running on an arm chip
|
|
44
|
+
*/
|
|
45
|
+
export declare const isArm: () => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Write backup data to file.
|
|
48
|
+
*
|
|
49
|
+
* @param name file name without extension
|
|
50
|
+
* @param data backup data
|
|
51
|
+
*/
|
|
52
|
+
export declare const writeBackupFile: (name: string, data: unknown) => Promise<void>;
|
|
53
|
+
/**
|
|
54
|
+
* Test utility to clean up the proofs directory
|
|
55
|
+
* and the tally.json file
|
|
56
|
+
*/
|
|
57
|
+
export declare const clean: (withBackup?: boolean) => Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Get backup filenames from backup folder.
|
|
60
|
+
*
|
|
61
|
+
* @returns filenames
|
|
62
|
+
*/
|
|
63
|
+
export declare const getBackupFilenames: () => Promise<string[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Relay test messages to mock contracts and save messages to backup folder.
|
|
66
|
+
*
|
|
67
|
+
* @param args relay test messages arguments
|
|
68
|
+
*/
|
|
69
|
+
export declare const relayTestMessages: ({ messages, maciAddress, pollId, signer, }: IRelayTestMessagesArgs) => Promise<void>;
|
|
70
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../ts/utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAQrD,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEtE,OAAO,EAAE,IAAI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ault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.relayTestMessages = exports.getBackupFilenames = exports.clean = exports.writeBackupFile = exports.isArm = exports.sleep = exports.expectTally = exports.genTestUserCommands = exports.readJSONFile = exports.defaultVote = exports.backupFolder = void 0;
|
|
7
|
+
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
8
|
+
const domainobjs_1 = require("@maci-protocol/domainobjs");
|
|
9
|
+
const sdk_1 = require("@maci-protocol/sdk");
|
|
10
|
+
const chai_1 = require("chai");
|
|
11
|
+
const fs_1 = __importDefault(require("fs"));
|
|
12
|
+
const os_1 = require("os");
|
|
13
|
+
const path_1 = __importDefault(require("path"));
|
|
14
|
+
const user_1 = require("./user");
|
|
15
|
+
exports.backupFolder = "./backup";
|
|
16
|
+
exports.defaultVote = {
|
|
17
|
+
voteWeight: 1n,
|
|
18
|
+
nonce: 1n,
|
|
19
|
+
maxVoteWeight: 25n,
|
|
20
|
+
voteCreditBalance: 1n,
|
|
21
|
+
voteOptionIndex: 0n,
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Read a JSON file from disk
|
|
25
|
+
* @param filePath - the path of the file
|
|
26
|
+
* @returns the JSON object
|
|
27
|
+
*/
|
|
28
|
+
const readJSONFile = async (filePath) => {
|
|
29
|
+
const isExists = fs_1.default.existsSync(filePath);
|
|
30
|
+
if (!isExists) {
|
|
31
|
+
throw new Error(`File ${filePath} does not exist`);
|
|
32
|
+
}
|
|
33
|
+
return fs_1.default.promises.readFile(filePath).then((res) => JSON.parse(res.toString()));
|
|
34
|
+
};
|
|
35
|
+
exports.readJSONFile = readJSONFile;
|
|
36
|
+
/**
|
|
37
|
+
* Test utility to generate vote objects for integrationt ests
|
|
38
|
+
* @param userIndex - the index of the user
|
|
39
|
+
* @param voteIndex - the index of the vote
|
|
40
|
+
* @param numVotesPerUser - the amount of votes per user
|
|
41
|
+
* @param votes - the votes object
|
|
42
|
+
* @param bribers - the bribers votes
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
const getTestVoteValues = (userIndex, voteIndex, numVotesPerUser, votes, bribers) => {
|
|
46
|
+
// check if we have specific votes
|
|
47
|
+
const useVotes = votes && userIndex in votes;
|
|
48
|
+
let { voteOptionIndex } = exports.defaultVote;
|
|
49
|
+
let { voteWeight } = exports.defaultVote;
|
|
50
|
+
// if we have bribers
|
|
51
|
+
if (bribers && userIndex in bribers) {
|
|
52
|
+
if (!(bribers[userIndex].voteOptionIndices.length === numVotesPerUser)) {
|
|
53
|
+
throw new Error("failed generating user commands: more bribes than votes set per user");
|
|
54
|
+
}
|
|
55
|
+
// if we were provided specific votes
|
|
56
|
+
if (useVotes) {
|
|
57
|
+
if (bribers[userIndex].voteOptionIndices[voteIndex] !== votes[userIndex][voteIndex].voteOptionIndex) {
|
|
58
|
+
throw new Error("failed generating user commands: conflict between bribers voteOptionIndex and the one set by voters");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
voteOptionIndex = bribers[userIndex].voteOptionIndices[voteIndex];
|
|
62
|
+
}
|
|
63
|
+
else if (useVotes) {
|
|
64
|
+
voteOptionIndex = votes[userIndex][voteIndex].voteOptionIndex;
|
|
65
|
+
}
|
|
66
|
+
if (useVotes) {
|
|
67
|
+
voteWeight = votes[userIndex][voteIndex].voteWeight;
|
|
68
|
+
}
|
|
69
|
+
return { voteOptionIndex, voteWeight };
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Generate a list of user commands for integration tests
|
|
73
|
+
* @param numUsers - the number of users
|
|
74
|
+
* @param numVotesPerUser - the number of votes per user
|
|
75
|
+
* @param bribers - the number of bribers
|
|
76
|
+
* @param presetVotes - the preset votes if any
|
|
77
|
+
* @returns an array of UserCommand objects
|
|
78
|
+
*/
|
|
79
|
+
const genTestUserCommands = (numUsers, numVotesPerUser, bribers, presetVotes) => {
|
|
80
|
+
const usersCommands = [];
|
|
81
|
+
for (let i = 0; i < numUsers; i += 1) {
|
|
82
|
+
const userKeypair = new domainobjs_1.Keypair();
|
|
83
|
+
const votes = [];
|
|
84
|
+
for (let j = 0; j < numVotesPerUser; j += 1) {
|
|
85
|
+
const { voteOptionIndex, voteWeight } = getTestVoteValues(i, j, numVotesPerUser, presetVotes, bribers);
|
|
86
|
+
const vote = {
|
|
87
|
+
voteOptionIndex,
|
|
88
|
+
voteWeight,
|
|
89
|
+
nonce: BigInt(j + 1),
|
|
90
|
+
};
|
|
91
|
+
votes.push(vote);
|
|
92
|
+
}
|
|
93
|
+
const userCommand = new user_1.User(userKeypair, votes, exports.defaultVote.maxVoteWeight, exports.defaultVote.nonce);
|
|
94
|
+
usersCommands.push(userCommand);
|
|
95
|
+
}
|
|
96
|
+
return usersCommands;
|
|
97
|
+
};
|
|
98
|
+
exports.genTestUserCommands = genTestUserCommands;
|
|
99
|
+
/**
|
|
100
|
+
* Assertion function to validate that the tally results are as expected
|
|
101
|
+
* @param maxMessages - the max number of messages
|
|
102
|
+
* @param expectedTally - the expected tally values
|
|
103
|
+
* @param expectedPerVOSpentVoiceCredits - the expected per VO spent voice credits
|
|
104
|
+
* @param expectedTotalSpentVoiceCredits - the expected total spent voice credits
|
|
105
|
+
* @param tallyFile the tally file itself as an object
|
|
106
|
+
*/
|
|
107
|
+
const expectTally = (maxMessages, expectedTally, expectedPerVOSpentVoiceCredits, expectedTotalSpentVoiceCredits, tallyFile) => {
|
|
108
|
+
const genTally = Array(maxMessages).fill("0");
|
|
109
|
+
const genPerVOSpentVoiceCredits = Array(maxMessages).fill("0");
|
|
110
|
+
expectedTally.forEach((voteWeight, voteOption) => {
|
|
111
|
+
if (voteWeight !== 0) {
|
|
112
|
+
genTally[voteOption] = voteWeight.toString();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
expectedPerVOSpentVoiceCredits.forEach((spentCredit, index) => {
|
|
116
|
+
if (spentCredit !== 0) {
|
|
117
|
+
genPerVOSpentVoiceCredits[index] = spentCredit.toString();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
(0, chai_1.expect)(tallyFile.results.tally).to.deep.equal(genTally);
|
|
121
|
+
(0, chai_1.expect)(tallyFile.perVOSpentVoiceCredits?.tally).to.deep.equal(genPerVOSpentVoiceCredits);
|
|
122
|
+
(0, chai_1.expect)(tallyFile.totalSpentVoiceCredits.spent).to.eq(expectedTotalSpentVoiceCredits.toString());
|
|
123
|
+
};
|
|
124
|
+
exports.expectTally = expectTally;
|
|
125
|
+
/**
|
|
126
|
+
* Stop the current thread for x seconds
|
|
127
|
+
* @param ms - the number of ms to sleep for
|
|
128
|
+
*/
|
|
129
|
+
const sleep = async (ms) => new Promise((resolve) => {
|
|
130
|
+
setTimeout(resolve, ms);
|
|
131
|
+
});
|
|
132
|
+
exports.sleep = sleep;
|
|
133
|
+
/**
|
|
134
|
+
* Check whether we are running on an arm chip
|
|
135
|
+
* @returns whether we are running on an arm chip
|
|
136
|
+
*/
|
|
137
|
+
const isArm = () => (0, os_1.arch)().includes("arm");
|
|
138
|
+
exports.isArm = isArm;
|
|
139
|
+
/**
|
|
140
|
+
* Write backup data to file.
|
|
141
|
+
*
|
|
142
|
+
* @param name file name without extension
|
|
143
|
+
* @param data backup data
|
|
144
|
+
*/
|
|
145
|
+
const writeBackupFile = async (name, data) => {
|
|
146
|
+
if (!fs_1.default.existsSync(exports.backupFolder)) {
|
|
147
|
+
await fs_1.default.promises.mkdir(exports.backupFolder);
|
|
148
|
+
}
|
|
149
|
+
await fs_1.default.promises.writeFile(path_1.default.resolve(exports.backupFolder, `${name}.json`), JSON.stringify(data));
|
|
150
|
+
};
|
|
151
|
+
exports.writeBackupFile = writeBackupFile;
|
|
152
|
+
/**
|
|
153
|
+
* Test utility to clean up the proofs directory
|
|
154
|
+
* and the tally.json file
|
|
155
|
+
*/
|
|
156
|
+
const clean = async (withBackup = true) => {
|
|
157
|
+
const files = await fs_1.default.promises.readdir("./proofs");
|
|
158
|
+
await Promise.all(files.map((file) => fs_1.default.promises.rm(path_1.default.resolve("./proofs", file))));
|
|
159
|
+
if (fs_1.default.existsSync("./tally.json")) {
|
|
160
|
+
await fs_1.default.promises.rm("./tally.json");
|
|
161
|
+
}
|
|
162
|
+
if (withBackup && fs_1.default.existsSync(exports.backupFolder)) {
|
|
163
|
+
await fs_1.default.promises.rm(exports.backupFolder, { recursive: true, force: true });
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
exports.clean = clean;
|
|
167
|
+
/**
|
|
168
|
+
* Get backup filenames from backup folder.
|
|
169
|
+
*
|
|
170
|
+
* @returns filenames
|
|
171
|
+
*/
|
|
172
|
+
const getBackupFilenames = async () => fs_1.default.promises.readdir(exports.backupFolder).then((paths) => paths.map((filename) => path_1.default.resolve(exports.backupFolder, filename)));
|
|
173
|
+
exports.getBackupFilenames = getBackupFilenames;
|
|
174
|
+
/**
|
|
175
|
+
* Relay test messages to mock contracts and save messages to backup folder.
|
|
176
|
+
*
|
|
177
|
+
* @param args relay test messages arguments
|
|
178
|
+
*/
|
|
179
|
+
const relayTestMessages = async ({ messages, maciAddress, pollId, signer, }) => {
|
|
180
|
+
const cid = await (0, sdk_1.createCidFromObject)(messages);
|
|
181
|
+
const ipfsHash = await (0, sdk_1.cidToBytes32)(cid);
|
|
182
|
+
await (0, exports.writeBackupFile)(ipfsHash, messages);
|
|
183
|
+
await (0, sdk_1.relayMessages)({
|
|
184
|
+
maciAddress,
|
|
185
|
+
pollId,
|
|
186
|
+
ipfsHash,
|
|
187
|
+
messages,
|
|
188
|
+
signer,
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
exports.relayTestMessages = relayTestMessages;
|
|
192
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../ts/utils.ts"],"names":[],"mappings":";;;;;;AAAA,6DAA6D;AAC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