Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow dynamic censuses
Browse files Browse the repository at this point in the history
microstudi committed Jul 10, 2024
1 parent b108e68 commit e770376
Showing 8 changed files with 84 additions and 61 deletions.
2 changes: 2 additions & 0 deletions app/jobs/decidim/vocdoni/admin/create_vocdoni_election_job.rb
Original file line number Diff line number Diff line change
@@ -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,
8 changes: 4 additions & 4 deletions app/packs/src/decidim/vocdoni/voter/new-vote.js
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions app/services/decidim/vocdoni/census_updater_service.rb
Original file line number Diff line number Diff line change
@@ -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"],
13 changes: 9 additions & 4 deletions node-wrapper/election.js
Original file line number Diff line number Diff line change
@@ -30,18 +30,23 @@ 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(
newCensus.id,
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() };
}
};

3 changes: 2 additions & 1 deletion node-wrapper/index.js
Original file line number Diff line number Diff line change
@@ -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);
};

/**
70 changes: 39 additions & 31 deletions node-wrapper/test_census.mjs
Original file line number Diff line number Diff line change
@@ -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?");
46 changes: 26 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -44,6 +44,6 @@
"web-worker": "1.2.0"
},
"dependencies": {
"@vocdoni/sdk": "0.8.0"
"@vocdoni/sdk": "0.8.2"
}
}

0 comments on commit e770376

Please sign in to comment.