From 155c4dfea4f7d26902977293ab876583b4dfd6d2 Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Wed, 17 Jan 2024 10:23:57 +0100 Subject: [PATCH] Upgraded `@vocdoni/proto` to `1.15.5` to accept timestamps in election creation transaction --- package.json | 2 +- src/api/api.ts | 6 ++-- src/client.ts | 17 +---------- src/core/election.ts | 10 +++---- test/api/vote.test.ts | 48 +++++++++++++++---------------- test/integration/csp.test.ts | 2 +- test/integration/election.test.ts | 4 +-- test/integration/zk.test.ts | 2 +- 8 files changed, 38 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 3e257448..e2dd54bf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/api/api.ts b/src/api/api.ts index 1a4940c1..23547773 100644 --- a/src/api/api.ts +++ b/src/api/api.ts @@ -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); } } diff --git a/src/client.ts b/src/client.ts index 31461130..617d04c5 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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, }; diff --git a/src/core/election.ts b/src/core/election.ts index 55cd177f..7ea669ab 100644 --- a/src/core/election.ts +++ b/src/core/election.ts @@ -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, @@ -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 } { @@ -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, diff --git a/test/api/vote.test.ts b/test/api/vote.test.ts index fc5ffdc5..b611fcdc 100644 --- a/test/api/vote.test.ts +++ b/test/api/vote.test.ts @@ -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); }); diff --git a/test/integration/csp.test.ts b/test/integration/csp.test.ts index 79d1bdd6..14511364 100644 --- a/test/integration/csp.test.ts +++ b/test/integration/csp.test.ts @@ -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, }); diff --git a/test/integration/election.test.ts b/test/integration/election.test.ts index a8d8dd89..df6d8157 100644 --- a/test/integration/election.test.ts +++ b/test/integration/election.test.ts @@ -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, @@ -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, diff --git a/test/integration/zk.test.ts b/test/integration/zk.test.ts index e18aff10..b2883b25 100644 --- a/test/integration/zk.test.ts +++ b/test/integration/zk.test.ts @@ -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,