Skip to content

Commit

Permalink
Upgraded @vocdoni/proto to 1.15.5 to accept timestamps in electio…
Browse files Browse the repository at this point in the history
…n creation transaction
  • Loading branch information
marcvelmer committed Feb 29, 2024
1 parent 331c2f8 commit 155c4df
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 53 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"@ethersproject/units": "^5.7.0",
"@ethersproject/wallet": "^5.7.0",
"@size-limit/file": "^8.2.4",
"@vocdoni/proto": "1.15.4",
"@vocdoni/proto": "1.15.5",
"axios": "0.27.2",
"blake2b": "^2.1.4",
"iso-language-codes": "^1.1.0",
Expand Down
6 changes: 3 additions & 3 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ export abstract class API {

private static isVochainError(error: string): never {
switch (true) {
case error.includes('starts at height') && error.includes('current height is'):
case error.includes('starts at') && error.includes('current'):
throw new ErrElectionNotStarted(error);
case error.includes('finished at height') && error.includes('current height is'):
case error.includes('finished at') && error.includes('current'):
throw new ErrElectionFinished(error);
case error.includes('current state: ENDED'):
throw new ErrElectionFinished(error);
default:
throw error;
throw new ErrAPI(error);
}
}

Expand Down
17 changes: 1 addition & 16 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,26 +564,11 @@ export class VocdoniSDKClient {
key: ElectionCreationSteps.GET_DATA_PIN,
};

const blocks = {
actual: chainData.height,
start: 0,
end: 0,
};
if (election.startDate) {
blocks.start = await this.chainService.dateToBlock(election.startDate);
}
blocks.end = await this.chainService.dateToBlock(election.endDate);
yield {
key: ElectionCreationSteps.ESTIMATE_BLOCK_TIMES,
};

const electionTxData = ElectionCore.generateNewElectionTransaction(
election,
cid,
blocks,
account.address,
account.nonce
);
const electionTxData = ElectionCore.generateNewElectionTransaction(election, cid, account.address, account.nonce);
yield {
key: ElectionCreationSteps.GENERATE_TX,
};
Expand Down
10 changes: 5 additions & 5 deletions src/core/election.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ export abstract class ElectionCore extends TransactionCore {
public static generateNewElectionTransaction(
election: UnpublishedElection,
cid: string,
blocks: { actual: number; start: number; end: number },
address: string,
nonce: number
): { tx: Uint8Array; metadata: string; message: string } {
const txData = this.prepareElectionData(election, cid, blocks, address, nonce);
const txData = this.prepareElectionData(election, cid, address, nonce);

const newProcess = NewProcessTx.fromPartial({
txtype: TxType.NEW_PROCESS,
Expand All @@ -93,7 +92,6 @@ export abstract class ElectionCore extends TransactionCore {
private static prepareElectionData(
election: UnpublishedElection,
cid: string,
blocks: { actual: number; start: number; end: number },
address: string,
nonce: number
): { metadata: string; electionData: object } {
Expand All @@ -103,8 +101,10 @@ export abstract class ElectionCore extends TransactionCore {
nonce: nonce,
process: {
entityId: Uint8Array.from(Buffer.from(address, 'hex')),
startBlock: election.startDate ? blocks.start : 0,
blockCount: blocks.end - (election.startDate ? blocks.start : blocks.actual),
startTime: election.startDate ? Math.floor(election.startDate.getTime() / 1000) : 0,
duration: election.startDate
? Math.floor((election.endDate.getTime() - election.startDate.getTime()) / 1000)
: Math.floor((election.endDate.getTime() - Date.now()) / 1000),
censusRoot: Uint8Array.from(Buffer.from(election.census.censusId, 'hex')),
censusURI: election.census.censusURI,
status: ProcessStatus.READY,
Expand Down
48 changes: 24 additions & 24 deletions test/api/vote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,29 +53,29 @@
//
describe('Vote API tests', () => {
it('should throw trying to vote when election has not started and when is already finished', async () => {
// const voter = Wallet.createRandom();
// const census = new PlainCensus();
// census.add(await voter.getAddress());
//
// const election = createElection(census);
// await client.createAccount();
// const electionId = await client.createElection(election);
//
// client.wallet = voter;
// client.setElectionId(electionId);
// const vote = new Vote([1]);
//
// await expect(async () => {
// await client.submitVote(vote);
// }).rejects.toThrow(ErrElectionNotStarted);
//
// let publishedElection;
// do {
// publishedElection = await client.fetchElection(electionId);
// } while (publishedElection.status !== ElectionStatus.ENDED);
//
// await expect(async () => {
// await client.submitVote(vote);
// }).rejects.toThrow(ErrElectionFinished);
const voter = Wallet.createRandom();
const census = new PlainCensus();
census.add(await voter.getAddress());

const election = createElection(census);
await client.createAccount();
const electionId = await client.createElection(election);

client.wallet = voter;
client.setElectionId(electionId);
const vote = new Vote([1]);

await expect(async () => {
await client.submitVote(vote);
}).rejects.toThrow(ErrElectionNotStarted);

let publishedElection;
do {
publishedElection = await client.fetchElection(electionId);
} while (publishedElection.status !== ElectionStatus.ENDED);

await expect(async () => {
await client.submitVote(vote);
}).rejects.toThrow(ErrElectionFinished);
}, 120000);
});
2 changes: 1 addition & 1 deletion test/integration/csp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('CSP tests', () => {
description: 'Election description',
header: 'https://source.unsplash.com/random',
streamUri: 'https://source.unsplash.com/random',
endDate: new Date().getTime() + 10000000,
endDate: new Date().getTime() + 60 * 60 * 1000,
census,
maxCensusSize: numVotes,
});
Expand Down
4 changes: 2 additions & 2 deletions test/integration/election.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const createElection = (census, electionType?, voteType?, maxCensusSize?) => {
const election = Election.from({
title: 'SDK Testing - Title',
description: 'SDK Testing - Description',
endDate: new Date().getTime() + 10000000,
endDate: new Date().getTime() + 60 * 60 * 1000,
census,
maxCensusSize,
electionType: electionType ?? null,
Expand Down Expand Up @@ -601,7 +601,7 @@ describe('Election integration tests', () => {
const election = MultiChoiceElection.from({
title: 'SDK Testing - Title',
description: 'SDK Testing - Description',
endDate: new Date().getTime() + 10000000,
endDate: new Date().getTime() + 60 * 60 * 1000,
census,
maxNumberOfChoices: 3,
canAbstain: true,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/zk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const createElection = (census, electionType?, voteType?, maxCensusSize?) => {
const election = Election.from({
title: 'SDK Testing - Title',
description: 'SDK Testing - Description',
endDate: new Date().getTime() + 10000000,
endDate: new Date().getTime() + 60 * 60 * 1000,
census,
maxCensusSize,
electionType: electionType ?? null,
Expand Down

0 comments on commit 155c4df

Please sign in to comment.