From e770376c40fb9dd1c457b80a762c2f170ad398f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Verg=C3=A9s?= Date: Wed, 10 Jul 2024 19:03:17 +0200 Subject: [PATCH] allow dynamic censuses --- .../admin/create_vocdoni_election_job.rb | 2 + .../src/decidim/vocdoni/voter/new-vote.js | 8 +-- .../decidim/vocdoni/census_updater_service.rb | 1 + node-wrapper/election.js | 13 ++-- node-wrapper/index.js | 3 +- node-wrapper/test_census.mjs | 70 +++++++++++-------- package-lock.json | 46 ++++++------ package.json | 2 +- 8 files changed, 84 insertions(+), 61 deletions(-) diff --git a/app/jobs/decidim/vocdoni/admin/create_vocdoni_election_job.rb b/app/jobs/decidim/vocdoni/admin/create_vocdoni_election_job.rb index b1edaeed..c993153e 100644 --- a/app/jobs/decidim/vocdoni/admin/create_vocdoni_election_job.rb +++ b/app/jobs/decidim/vocdoni/admin/create_vocdoni_election_job.rb @@ -14,6 +14,7 @@ def perform(election_id) election.build_answer_values! result = sdk.createElection(election.to_vocdoni, election.questions_to_vocdoni, election.census_status.all_wallets) @vocdoni_id = result["electionId"] + @census_id = result["censusId"] @census_identifier = result["censusIdentifier"] @census_address = result["censusAddress"] @census_private_key = result["censusPrivateKey"] @@ -33,6 +34,7 @@ def perform(election_id) def update_election election.vocdoni_election_id = vocdoni_id election.census_attributes = { + id: @census_id, identifier: @census_identifier, address: @census_address, private_key: @census_private_key, diff --git a/app/packs/src/decidim/vocdoni/voter/new-vote.js b/app/packs/src/decidim/vocdoni/voter/new-vote.js index 449bed78..536a3028 100644 --- a/app/packs/src/decidim/vocdoni/voter/new-vote.js +++ b/app/packs/src/decidim/vocdoni/voter/new-vote.js @@ -1,10 +1,10 @@ /* eslint-disable no-console */ import { ElectionStatus, VocdoniSDKClient } from "@vocdoni/sdk"; -import VoteQuestionsComponent from "./vote_questions.component"; -import VoteComponent from "./setup-vote"; -import PreviewVoteComponent from "./setup-preview"; -import { walletFromLoginForm } from "./census-utils"; +import VoteQuestionsComponent from "src/decidim/vocdoni/voter/vote_questions.component"; +import VoteComponent from "src/decidim/vocdoni/voter/setup-vote"; +import PreviewVoteComponent from "src/decidim/vocdoni/voter/setup-preview"; +import { walletFromLoginForm } from "src/decidim/vocdoni/voter/census-utils"; /* * Mount the VoteComponent object and bind the events to the UI diff --git a/app/services/decidim/vocdoni/census_updater_service.rb b/app/services/decidim/vocdoni/census_updater_service.rb index 515ed2ef..b22cc285 100644 --- a/app/services/decidim/vocdoni/census_updater_service.rb +++ b/app/services/decidim/vocdoni/census_updater_service.rb @@ -25,6 +25,7 @@ def update_census def build_census_attributes { + id: @election.census_attributes["id"], identifier: @election.census_attributes["identifier"], address: @election.census_attributes["address"], privateKey: @election.census_attributes["private_key"], diff --git a/node-wrapper/election.js b/node-wrapper/election.js index acbcb0e5..4cfbe813 100644 --- a/node-wrapper/election.js +++ b/node-wrapper/election.js @@ -30,7 +30,8 @@ const updateElectionCensus = async (client, censusAttributes, censusData) => { auth: { identifier: censusIdentifier, wallet: censusWallet - } + }, + async: { async: true, wait: 30000 } }); const newCensus = await service.create(CensusType.WEIGHTED); await service.add( @@ -38,10 +39,14 @@ const updateElectionCensus = async (client, censusAttributes, censusData) => { censusData.map((wallet) => ({ key: wallet, weight: BigInt(1) })) ); const info = await service.publish(newCensus.id); - await client.changeElectionCensus(censusAttributes.electionId, info.censusID, info.uri); - return JSON.stringify({ success: true, count: censusData.length, timestamp: new Date(), newCensusId: info.censusID}); + const oldCensusInfo = await service.get(censusAttributes.id); + let censusLength = censusData.length > oldCensusInfo.size + ? censusData.length + : oldCensusInfo.size; + await client.changeElectionCensus(censusAttributes.electionId, info.censusID, info.uri, censusLength); + return { success: true, count: censusData.length, timestamp: new Date(), newCensusId: info.censusID, newCensusSize: censusLength }; } catch (error) { - return JSON.stringify({ success: false, error: error.message, count: 0, timestamp: new Date() }); + return { success: false, error: error.message, count: 0, timestamp: new Date() }; } }; diff --git a/node-wrapper/index.js b/node-wrapper/index.js index 2031a7ce..38beeb1c 100644 --- a/node-wrapper/index.js +++ b/node-wrapper/index.js @@ -135,7 +135,8 @@ const createElection = async (electionData, questionsData, censusData) => { const updateCensus = async (censusAttributes, censusData) => { const client = vocdoniClient(); - return await updateElectionCensus(client, censusAttributes, censusData); + const _info = await updateElectionCensus(client, censusAttributes, censusData); + return JSON.stringify(_info); }; /** diff --git a/node-wrapper/test_census.mjs b/node-wrapper/test_census.mjs index e5c05416..d8413b86 100644 --- a/node-wrapper/test_census.mjs +++ b/node-wrapper/test_census.mjs @@ -1,4 +1,5 @@ /* eslint-disable no-unused-vars */ +/* eslint-disable no-relative-import-paths/no-relative-import-paths */ /** * Manual testing of the Vocdoni SDK Census features @@ -57,8 +58,16 @@ let service = null; let electionData = null; let censusId = process.env.CENSUS_ID; let censusIdentifier = process.env.CENSUS_IDENTIFIER; -const censusWallet = await VocdoniSDKClient.generateWalletFromData("a-test").address; -const newCensusWallet = await VocdoniSDKClient.generateWalletFromData("a-test-2").address; +let censusDetails = null; +let censusInfo = null; +let info = null; +const censusWallets = [ + await VocdoniSDKClient.generateWalletFromData("a-test-1").address, + await VocdoniSDKClient.generateWalletFromData("a-test-2").address, + await VocdoniSDKClient.generateWalletFromData("a-test-3").address +]; +const newCensusWallet = await VocdoniSDKClient.generateWalletFromData("b-test-1").address; +const addCensusWallet = await VocdoniSDKClient.generateWalletFromData("b-test-2").address; if (process.env.ELECTION_ID && process.env.CENSUS_WALLET && process.env.CENSUS_ID && process.env.CENSUS_IDENTIFIER) { console.log("Using census data from ENV vars CENSUS_WALLET, CENSUS_IDENTIFIER and CENSUS_ID"); service = new CensusService({ @@ -68,7 +77,8 @@ if (process.env.ELECTION_ID && process.env.CENSUS_WALLET && process.env.CENSUS_I auth: { identifier: process.env.CENSUS_IDENTIFIER, wallet: new Wallet(process.env.CENSUS_WALLET) - } + }, + async: { async: true, wait: 30000 } }); console.log("CensusService:", service) @@ -78,47 +88,45 @@ if (process.env.ELECTION_ID && process.env.CENSUS_WALLET && process.env.CENSUS_I console.log("Using election data from ENV var ELECTION_ID"); console.log("ElectionID:", electionData.id, "Census:", electionData.census); } else { - console.log("Using deterministic wallets in the census:", censusWallet); - electionData = await newElection(client, [censusWallet]); + console.log("Using deterministic wallets in the census:", censusWallets); + electionData = await newElection(client, censusWallets); console.log("New election created with"); console.log(electionData); censusId = electionData.censusId; censusIdentifier = electionData.censusIdentifier; - service = client.censusService; console.log("\n\n"); console.log("You can run this script with the following ENV var in order to skip the creation of a new election:"); console.log(`ENV=${env} WALLET=${process.env.WALLET} ELECTION_ID=${electionData.id} CENSUS_ID=${censusId} CENSUS_IDENTIFIER=${censusIdentifier} CENSUS_WALLET=${electionData.censusPrivateKey} node node-wrapper/test_census.mjs`); + service = client.censusService; + censusInfo = await service.get(censusId); + console.log("censusInfo", censusInfo); + console.log("Census size:", censusInfo.size); } -console.log("Is the old address in census?") -console.log(await checkAddress(service, censusId, censusWallet)) -console.log("Is the new addres in census?") +console.log("\n==========\n"); + +console.log("Are the old addresses in the old census?") +console.log(await checkAddress(service, censusId, censusWallets[0])) +console.log(await checkAddress(service, censusId, censusWallets[1])) +console.log(await checkAddress(service, censusId, censusWallets[2])) +console.log("Are the new addresses in the old census?") console.log(await checkAddress(service, censusId, newCensusWallet)) +console.log(await checkAddress(service, censusId, addCensusWallet)) -let censusDetails = null; -try { - console.log("Adding the new census..."); - newCensus = await service.create(CensusType.WEIGHTED); - console.log(newCensus) - const add = await service.add(newCensus.id, [{ key: newCensusWallet, weight: BigInt(1) }]); - console.log("ADD", add); - censusDetails = await service.publish(newCensus.id); - console.log("censusDetails", censusDetails); -} catch (error) { - console.error("Error adding new census", error.message); - throw error; +console.log("Adding the new census..."); +info = await updateElectionCensus(client, { privateKey: process.env.WALLET, id: censusId, identifier: censusIdentifier, electionId: electionData.id }, [newCensusWallet, addCensusWallet]); +console.log("updateElectionCensus INFO", info); +if (!info.success) { + throw new Error(`Error updating election census: ${info.error}`); } -console.log("Is the old address in census?") -console.log(await checkAddress(service, newCensus.id, censusWallet)) -console.log("Is the new addres in census?") -console.log(await checkAddress(service, newCensus.id, newCensusWallet)) - -try { - await client.changeElectionCensus(electionData.id, censusDetails.censusID, censusDetails.uri); -} catch (error) { - console.error("Error updating election census", error.message); -} +console.log("Are the old addresses in the new census?") +console.log(await checkAddress(service, info.newCensusId, censusWallets[0])) +console.log(await checkAddress(service, info.newCensusId, censusWallets[1])) +console.log(await checkAddress(service, info.newCensusId, censusWallets[2])) +console.log("Are the new addresses in the old census?") +console.log(await checkAddress(service, info.newCensusId, newCensusWallet)) +console.log(await checkAddress(service, info.newCensusId, addCensusWallet)) try { console.log("Is the election using the new census?"); diff --git a/package-lock.json b/package-lock.json index 98741d1f..299c3e4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "2.0.0", "license": "AGPL-3.0", "dependencies": { - "@vocdoni/sdk": "0.8.0" + "@vocdoni/sdk": "0.8.2" }, "devDependencies": { "@babel/eslint-parser": "^7.19.1", @@ -3178,9 +3178,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.12.11", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.11.tgz", - "integrity": "sha512-vDg9PZ/zi+Nqp6boSOT7plNuthRugEKixDv5sFTIpkE89MmNtEArAShI4mxuX2+UrLEe9pxC1vm2cjm9YlWbJw==", + "version": "20.14.10", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.10.tgz", + "integrity": "sha512-MdiXf+nDuMvY0gJKxyfZ7/6UFsETO7mGKF54MVD/ekJS6HdFtpZFBgrh6Pseu64XTb2MLyFPlbW6hj8HYRQNOQ==", "dependencies": { "undici-types": "~5.26.4" } @@ -3205,18 +3205,18 @@ "peer": true }, "node_modules/@vocdoni/proto": { - "version": "1.15.5", - "resolved": "https://registry.npmjs.org/@vocdoni/proto/-/proto-1.15.5.tgz", - "integrity": "sha512-6Tm8aluCbz10gsVLnyFnsOM7dfFMFD3w8fmG26RQh9fFZJ6eLOf3FKKXC6nlMm3x6a6pKXHEFvT89ePks0WMNA==", + "version": "1.15.8", + "resolved": "https://registry.npmjs.org/@vocdoni/proto/-/proto-1.15.8.tgz", + "integrity": "sha512-SQxE0+tKaGiAxty6RvRMFrKArKGKsOw4S/Ph6NODJDhXn2tlbwkVK8hm9/3fRU528I2psABLT4lwe7skETcrNw==", "dependencies": { "long": "^5.2.1", "protobufjs": "^7.1.2" } }, "node_modules/@vocdoni/sdk": { - "version": "0.8.0", - "resolved": "https://registry.npmjs.org/@vocdoni/sdk/-/sdk-0.8.0.tgz", - "integrity": "sha512-Kj2RG7b8Ifdspf1r9gcwPdtExojFzkhZg9l35mPRp2VD3P82QtMl1DDbDlRwSZ4ajSHkXH2dWgUZ0KKJH75ySg==", + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/@vocdoni/sdk/-/sdk-0.8.2.tgz", + "integrity": "sha512-UCquK3srSA2/fxywpqcr53Sts81sz8oLNwG7XKlUcdvPpNbXAzOXsO9reLgprqiAEud27DYV9/6Rf68TttBZsQ==", "dependencies": { "@ethersproject/abstract-signer": "^5.7.0", "@ethersproject/address": "^5.7.0", @@ -3229,8 +3229,8 @@ "@ethersproject/units": "^5.7.0", "@ethersproject/wallet": "^5.7.0", "@size-limit/file": "^8.2.4", - "@vocdoni/proto": "1.15.5", - "axios": "0.27.2", + "@vocdoni/proto": "1.15.8", + "axios": "0.28.1", "blake2b": "^2.1.4", "iso-language-codes": "^1.1.0", "js-sha256": "^0.10.1", @@ -3606,12 +3606,13 @@ } }, "node_modules/axios": { - "version": "0.27.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.27.2.tgz", - "integrity": "sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ==", + "version": "0.28.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.28.1.tgz", + "integrity": "sha512-iUcGA5a7p0mVb4Gm/sy+FSECNkPFT4y7wt6OM/CDpO/OnNCvSs3PoMG8ibrC9jRoGYU0gUK5pXVC4NPXq6lHRQ==", "dependencies": { - "follow-redirects": "^1.14.9", - "form-data": "^4.0.0" + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" } }, "node_modules/axobject-query": { @@ -8538,9 +8539,9 @@ "integrity": "sha512-SVtmxhRE/CGkn3eZY1T6pC8Nln6Fr/lu1mKSgRud0eC73whjGfoAogbn78LkD8aFL0zz3bAFerKSnOl7NlErBA==" }, "node_modules/protobufjs": { - "version": "7.3.0", - "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.3.0.tgz", - "integrity": "sha512-YWD03n3shzV9ImZRX3ccbjqLxj7NokGN0V/ESiBV5xWqrommYHYiihuIyavq03pWSGqlyvYUFmfoMKd+1rPA/g==", + "version": "7.3.2", + "resolved": "https://registry.npmjs.org/protobufjs/-/protobufjs-7.3.2.tgz", + "integrity": "sha512-RXyHaACeqXeqAKGLDl68rQKbmObRsTIn4TYVUUug1KfS47YWCo5MacGITEryugIgZqORCvJWEk4l449POg5Txg==", "hasInstallScript": true, "dependencies": { "@protobufjs/aspromise": "^1.1.2", @@ -8560,6 +8561,11 @@ "node": ">=12.0.0" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/queue-microtask": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", diff --git a/package.json b/package.json index 71dff654..498f8247 100644 --- a/package.json +++ b/package.json @@ -44,6 +44,6 @@ "web-worker": "1.2.0" }, "dependencies": { - "@vocdoni/sdk": "0.8.0" + "@vocdoni/sdk": "0.8.2" } }