Skip to content

Commit

Permalink
rename pythong worker
Browse files Browse the repository at this point in the history
  • Loading branch information
microstudi committed Oct 2, 2023
1 parent a4a9209 commit 593f2a3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class KeyCeremonyComponent extends TrusteeComponent {
setupElection({
bulletinBoardClientParams,
electionUniqueId,
authorizationExpirationTimestamp,
authorizationExpirationTimestamp
}) {
return this.setupElectionWithTypesFilter({
electionUniqueId,
Expand All @@ -28,7 +28,7 @@ export class KeyCeremonyComponent extends TrusteeComponent {
"create_election",
"start_key_ceremony",
"key_ceremony",
"end_key_ceremony",
"end_key_ceremony"
],
});
}
Expand Down Expand Up @@ -64,7 +64,7 @@ export class KeyCeremonyComponent extends TrusteeComponent {
onTrusteeNeedsToBeRestored,
onBackupNeeded,
onBindBackupButton,
onBackupStarted,
onBackupStarted
}) {
const onSetupDone = await this.trustee.setup();

Expand Down
4 changes: 2 additions & 2 deletions bulletin_board/js-client/src/tally/tally.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class TallyComponent extends TrusteeComponent {
"end_key_ceremony",
"start_tally",
"tally",
"end_tally",
"end_tally"
],
});
}
Expand Down Expand Up @@ -60,7 +60,7 @@ export class TallyComponent extends TrusteeComponent {
onRestore,
onComplete,
onStart,
onTrusteeNeedsToBeRestored,
onTrusteeNeedsToBeRestored
}) {
const onSetupDone = this.trustee.setup();

Expand Down
6 changes: 3 additions & 3 deletions bulletin_board/js-client/src/trustee/trustee.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class TrusteeComponent {
uniqueId: trusteeUniqueId,
authorityPublicKeyJSON,
identificationKeys: trusteeIdentificationKeys,
wrapperAdapter: trusteeWrapperAdapter,
wrapperAdapter: trusteeWrapperAdapter
});
}

Expand Down Expand Up @@ -69,14 +69,14 @@ export class TrusteeComponent {
...bulletinBoardClientParams,
headers: {
Authorization: authorizationHeader,
TrusteeUniqueId: trusteeUniqueIdHeader,
TrusteeUniqueId: trusteeUniqueIdHeader
},
});

const election = new Election({
uniqueId: electionUniqueId,
bulletinBoardClient,
typesFilter,
typesFilter
});

this.trustee.election = election;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<undefined>}
*/
async setup() {
return await this.processPythonCode(
return await this.processPythonCodeOnWorker(
`
from js import trustee_id
from bulletin_board.electionguard.trustee import Trustee
trustee = Trustee(trustee_id)
`,
{
trustee_id: this.trusteeId,
trustee_id: this.trusteeId
}
);
}
Expand All @@ -47,7 +47,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Object|undefined>}
*/
async processMessage(messageType, decodedData) {
const result = await this.processPythonCode(
const result = await this.processPythonCodeOnWorker(
`
from js import message_type, decoded_data
import json
Expand All @@ -58,16 +58,17 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
`,
{
message_type: messageType,
decoded_data: JSON.stringify(decodedData),
decoded_data: JSON.stringify(decodedData)
}
);

if (result && result.length > 0) {
// eslint-disable-next-line camelcase
const [{ message_type, content }] = result;
// Pyodide 0.17 return a Map instead of a object when python is a dict
const { message_type, content } = result[0] instanceof Map ? Object.fromEntries(result[0]) : result[0];

Check failure on line 68 in voting_schemes/electionguard/js-adapter/src/trustee_wrapper_adapter.js

View workflow job for this annotation

GitHub Actions / Lint code

Identifier 'message_type' is not in camel case
return {
messageType: message_type,
content,
content
};
}
}
Expand All @@ -78,7 +79,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Boolean>}
*/
isFresh() {
return this.processPythonCode(
return this.processPythonCodeOnWorker(
`
trustee.is_fresh()
`
Expand All @@ -91,7 +92,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<String>}
*/
backup() {
return this.processPythonCode(
return this.processPythonCodeOnWorker(
`
trustee.backup().hex()
`
Expand All @@ -105,14 +106,14 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Boolean>}
*/
restore(state) {
return this.processPythonCode(
return this.processPythonCodeOnWorker(
`
from js import state
trustee = Trustee.restore(bytes.fromhex(state))
True
`,
{
state,
state
}
);
}
Expand All @@ -123,7 +124,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Boolean>}
*/
isKeyCeremonyDone() {
return this.processPythonCode(
return this.processPythonCodeOnWorker(
`
trustee.is_key_ceremony_done()
`
Expand All @@ -136,7 +137,7 @@ export class TrusteeWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Boolean>}
*/
isTallyDone() {
return this.processPythonCode(
return this.processPythonCodeOnWorker(
`
trustee.is_tally_done()
`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class VoterWrapperAdapter extends WrapperAdapter {
* @returns {Promise<undefined>}
*/
async setup() {
return await this.processPythonCode(
return await this.processPythonCodeOnWorker(
`
from js import voter_id
from bulletin_board.electionguard.voter import Voter
voter = Voter(voter_id)
`,
{
voter_id: this.voterId,
voter_id: this.voterId
}
);
}
Expand All @@ -47,24 +47,25 @@ export class VoterWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Object|undefined>}
*/
async processMessage(messageType, decodedData) {
const result = await this.processPythonCode(
const result = await this.processPythonCodeOnWorker(
`
from js import message_type, decoded_data
import json
voter.process_message(message_type, json.loads(decoded_data))
`,
{
message_type: messageType,
decoded_data: JSON.stringify(decodedData),
decoded_data: JSON.stringify(decodedData)
}
);

if (result && result[0]) {
if (result && result.length > 0) {
// eslint-disable-next-line camelcase
const { message_type, content } = result[0];
// Pyodide 0.17 return a Map instead of a object when python is a dict
const { message_type, content } = result[0] instanceof Map ? Object.fromEntries(result[0]) : result[0];

Check failure on line 65 in voting_schemes/electionguard/js-adapter/src/voter_wrapper_adapter.js

View workflow job for this annotation

GitHub Actions / Lint code

Identifier 'message_type' is not in camel case
return {
messageType: message_type,
content: content.toJs(),
content: content
};
}
}
Expand All @@ -80,17 +81,19 @@ export class VoterWrapperAdapter extends WrapperAdapter {
* @returns {Promise<Object|undefined>}
*/
async encrypt(plainVote, ballotStyle) {
const [auditableData, encryptedData] = await this.processPythonCode(
console.log("Encrypting vote", plainVote, ballotStyle);
const [auditableData, encryptedData] = await this.processPythonCodeOnWorker(
`
from js import plain_vote, ballot_style
voter.encrypt(plain_vote.to_py(), ballot_style)
`,
{
plain_vote: plainVote,
ballot_style: ballotStyle,
ballot_style: ballotStyle
}
);

console.log("Encrypted vote: auditableData:", auditableData, "encryptedData:", encryptedData);
// TODO, FIXME check if we need Object.fromEntries
return { auditableData, encryptedData };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@
*/
export class WrapperAdapter {
/**
* Runs arbitrary python code
* Runs arbitrary python code in the web worker
*
* @param {String} pythonCode - A string representing valid python code.
* @param {Object} pythonData - An Object which values can be referenced from
* the python code using the js module.
* @private
* @returns {Promise<Object>}
*/
async processPythonCode(pythonCode, pythonData) {
async processPythonCodeOnWorker(pythonCode, pythonData) {
return new Promise((resolve, reject) => {
this.worker.onmessage = (event) => {
resolve(event.data.results);
};

this.worker.onerror = (error) => {
console.error("Error in worker, pythonCode:", pythonCode, "pythonData:", pythonData);
console.error(error);
reject(error);
};

this.worker.postMessage({
python: pythonCode,
...pythonData,
...pythonData
});
});
}
Expand Down

0 comments on commit 593f2a3

Please sign in to comment.