11import { EasyPrivateVotingContractArtifact , EasyPrivateVotingContract } from "../artifacts/EasyPrivateVoting.js"
22import { AccountWallet , CompleteAddress , ContractDeployer , createLogger , Fr , PXE , waitForPXE , TxStatus , createPXEClient , getContractInstanceFromDeployParams , Logger } from "@aztec/aztec.js" ;
33import { getInitialTestAccountsWallets } from "@aztec/accounts/testing"
4+ import { spawn } from 'child_process' ;
45
56const setupSandbox = async ( ) => {
67 const { PXE_URL = 'http://localhost:8080' } = process . env ;
78 const pxe = createPXEClient ( PXE_URL ) ;
89 await waitForPXE ( pxe ) ;
910 return pxe ;
1011} ;
12+ const sleep = ( ms : number ) => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
1113
1214describe ( "Voting" , ( ) => {
1315 let pxe : PXE ;
1416 let wallets : AccountWallet [ ] = [ ] ;
1517 let accounts : CompleteAddress [ ] = [ ] ;
1618 let logger : Logger ;
19+ let sandboxInstance ;
1720
1821 beforeAll ( async ( ) => {
22+ sandboxInstance = spawn ( "aztec" , [ "start" , "--sandbox" ] , {
23+ detached : true ,
24+ stdio : 'ignore'
25+ } )
26+ sleep ( 15000 )
1927 logger = createLogger ( 'aztec:aztec-starter' ) ;
2028 logger . info ( "Aztec-Starter tests running." )
2129
@@ -25,6 +33,10 @@ describe("Voting", () => {
2533 accounts = wallets . map ( w => w . getCompleteAddress ( ) )
2634 } )
2735
36+ afterAll ( async ( ) => {
37+ sandboxInstance ! . kill ( ) ;
38+ } )
39+
2840 it ( "Deploys the contract" , async ( ) => {
2941 const salt = Fr . random ( ) ;
3042 const VotingContractArtifact = EasyPrivateVotingContractArtifact
@@ -72,16 +84,18 @@ describe("Voting", () => {
7284 it ( "It should fail when trying to vote twice" , async ( ) => {
7385 const candidate = new Fr ( 1 )
7486
75- const contract = await EasyPrivateVotingContract . deploy ( wallets [ 0 ] , accounts [ 0 ] . address ) . send ( ) . deployed ( ) ;
76- await contract . methods . cast_vote ( candidate ) . send ( ) . wait ( ) ;
87+ const votingContract = await EasyPrivateVotingContract . deploy ( wallets [ 0 ] , accounts [ 0 ] . address ) . send ( ) . deployed ( ) ;
88+ await votingContract . methods . cast_vote ( candidate ) . send ( ) . wait ( ) ;
89+ expect ( await votingContract . methods . get_vote ( candidate ) . simulate ( ) ) . toBe ( 1n ) ;
7790
7891 // We try voting again, but our TX is dropped due to trying to emit duplicate nullifiers
7992 // first confirm that it fails simulation
80- await expect ( contract . methods . cast_vote ( candidate ) . send ( ) . wait ( ) ) . rejects . toThrow ( / N u l l i f i e r c o l l i s i o n / ) ;
81- // if we skip simulation, tx is dropped
93+ await expect ( votingContract . methods . cast_vote ( candidate ) . send ( ) . wait ( ) ) . rejects . toThrow ( / N u l l i f i e r c o l l i s i o n / ) ;
94+ // if we skip simulation before submitting the tx,
95+ // tx will be included in a block but with app logic reverted
8296 await expect (
83- contract . methods . cast_vote ( candidate ) . send ( { skipPublicSimulation : true } ) . wait ( ) ,
84- ) . rejects . toThrow ( 'Reason: Tx dropped by P2P node.' ) ;
97+ votingContract . methods . cast_vote ( candidate ) . send ( { skipPublicSimulation : true } ) . wait ( ) ,
98+ ) . rejects . toThrow ( TxStatus . APP_LOGIC_REVERTED ) ;
8599
86100 } , 300_000 )
87101
0 commit comments