Skip to content

Commit

Permalink
workshop vote new expressive script
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Chen committed Oct 10, 2023
1 parent 806efe0 commit ec315c4
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 44 deletions.
4 changes: 3 additions & 1 deletion vote/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@

NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpBDEpwdFWqxe1NdB9fxZxiX9LahJ3CdqchSs7FGZVggNw
PRIVATE_KEY=APrivateKey1zkp6NHwbT7PkpnEFeBidz5ZkZ14W8WXZmJ6kjKbEHYdMmf2

35 changes: 16 additions & 19 deletions vote/build/main.aleo
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ record Ticket:


mapping proposals:
key left as field.public;
value right as ProposalInfo.public;
key as field.public;
value as ProposalInfo.public;


mapping tickets:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;


mapping agree_votes:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;


mapping disagree_votes:
key left as field.public;
value right as u64.public;
key as field.public;
value as u64.public;

function propose:
input r0 as ProposalInfo.public;
assert.eq self.caller r0.proposer;
hash.bhp256 r0.title into r1 as field; cast self.caller r1 r0 into r2 as Proposal.record;
output r2 as Proposal.record;

finalize r1;
hash.bhp256 r0.title into r1 as field;
cast self.caller r1 r0 into r2 as Proposal.record;
async propose r1 into r3; output r2 as Proposal.record;
output r3 as vote.aleo/propose.future;

finalize propose:
input r0 as field.public;
Expand All @@ -51,9 +51,8 @@ function new_ticket:
input r0 as field.public;
input r1 as address.public;
cast r1 r0 into r2 as Ticket.record;
output r2 as Ticket.record;

finalize r0;
async new_ticket r0 into r3; output r2 as Ticket.record;
output r3 as vote.aleo/new_ticket.future;

finalize new_ticket:
input r0 as field.public;
Expand All @@ -64,8 +63,7 @@ finalize new_ticket:

function agree:
input r0 as Ticket.record;

finalize r0.pid;
async agree r0.pid into r1; output r1 as vote.aleo/agree.future;

finalize agree:
input r0 as field.public;
Expand All @@ -76,8 +74,7 @@ finalize agree:

function disagree:
input r0 as Ticket.record;

finalize r0.pid;
async disagree r0.pid into r1; output r1 as vote.aleo/disagree.future;

finalize disagree:
input r0 as field.public;
Expand Down
2 changes: 1 addition & 1 deletion vote/inputs/vote.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
info: ProposalInfo = ProposalInfo {
title: 2077160157502449938194577302446444field,
content: 1452374294790018907888397545906607852827800436field,
proposer: aleo1kkk52quhnxgn2nfrcd9jqk7c9x27c23f2wvw7fyzcze56yahvcgszgttu2,
proposer: aleo1rfez44epy0m7nv4pskvjy6vex64tnt0xy90fyhrg49cwe0t9ws8sh6nhhr,
};

[new_ticket]
Expand Down
185 changes: 162 additions & 23 deletions vote/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,156 @@ then
exit
fi

echo "
We will be playing the role of these four parties:
# The private key and address of the proposer.
# "private_key": "APrivateKey1zkp8wKHF9zFX1j4YJrK3JhxtyKDmPbRu9LrnEW8Ki56UQ3G",
# "address": "aleo1rfez44epy0m7nv4pskvjy6vex64tnt0xy90fyhrg49cwe0t9ws8sh6nhhr"
# The private key and address of voter 1.
# "private_key": "APrivateKey1zkpHmSu9zuhyuCJqVfQE8p82HXpCTLVa8Z2HUNaiy9mrug2"
# "address": "aleo1c45etea8czkyscyqawxs7auqjz08daaagp2zq4qjydkhxt997q9s77rsp2"
# The private key and address of voter 2.
# "private_key": "APrivateKey1zkp6NHwbT7PkpnEFeBidz5ZkZ14W8WXZmJ6kjKbEHYdMmf2"
# "address": "aleo1uc6jphye8y9gfqtezrz240ak963sdgugd7s96qpuw6k7jz9axs8q2qnhxc"
# The private key and address of voter 3.
# "private_key": "APrivateKey1zkpAh6JusXVdrqT4RuNeMYNc1BCCpacEQr33j8YSNoA3V4D"
# "address": "aleo12ufz737y7wgpgr5q2pyjydenee9u02rwdz32ajurmhg79m3hcc8s3fgu9m"
"

echo "
Let's propose a new ballot. Take on the role of the proposer and run the propose transition function. You'll see in the proposal inputs in vote.in that serve as the input for the call.
echo '
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp8wKHF9zFX1j4YJrK3JhxtyKDmPbRu9LrnEW8Ki56UQ3G
' > .env
leo run propose
"

# swaps in the private key of the proposer to .env
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp8wKHF9zFX1j4YJrK3JhxtyKDmPbRu9LrnEW8Ki56UQ3G
" > .env

# runs the propose transition function with proposal info as the input
leo run propose

echo "
You'll see that the output generates a new record with the proposal information and ...
"

echo "
###############################################################################
######## ########
######## STEP 0: Compile the vote program ########
######## Propose a new ballot ########
######## ########
######## --------------------------- ########
######## | | Yes | No | ########
######## --------------------------- ########
######## | Votes | | | ########
######## --------------------------- ########
######## ########
###############################################################################
"
# Build the Leo vote program.
(
leo build || exit
)

echo "
Let's create a new private ticket to make a vote. Take on the role of voter 1 and run the new_ticket transition. The inputs take a unique ticket ID and the voter's public address.
echo '
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpHmSu9zuhyuCJqVfQE8p82HXpCTLVa8Z2HUNaiy9mrug2
' > .env
leo run new_ticket 2264670486490520844857553240576860973319410481267184439818180411609250173817field aleo1c45etea8czkyscyqawxs7auqjz08daaagp2zq4qjydkhxt997q9s77rsp2
"

# swaps in the private key of voter 1 to .env
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkpHmSu9zuhyuCJqVfQE8p82HXpCTLVa8Z2HUNaiy9mrug2
" > .env

# runs the new_ticket transition function with unique id and voter address as inputs
leo run new_ticket 2264670486490520844857553240576860973319410481267184439818180411609250173817field aleo1c45etea8czkyscyqawxs7auqjz08daaagp2zq4qjydkhxt997q9s77rsp2

echo "
You'll see a new private ticket created belonging to the owner, and a public mapping in the vote program to track the ID of that ticket.
"

echo "
###############################################################################
######## ########
######## STEP 1: Propose a new ballot ########
######## Voter 1 issues a new ballot ticket ########
######## ########
######## --------------------------- ########
######## | | Yes | No | ########
######## --------------------------- ########
######## | Votes | | | ########
######## | Votes | 0 | 0 | ########
######## --------------------------- ########
######## ########
###############################################################################
"

echo "
Voter 1 can now vote privately on their ticket. Call the agree or disagree transition function, which takes the voter's ticket output as the input.
leo run agree '{
owner: aleo1c45etea8czkyscyqawxs7auqjz08daaagp2zq4qjydkhxt997q9s77rsp2.private,
pid: 2264670486490520844857553240576860973319410481267184439818180411609250173817field.private,
_nonce: 1738483341280375163846743812193292672860569105378494043894154684192972730518group.public
}'
"

leo run agree "{
owner: aleo1c45etea8czkyscyqawxs7auqjz08daaagp2zq4qjydkhxt997q9s77rsp2.private,
pid: 2264670486490520844857553240576860973319410481267184439818180411609250173817field.private,
_nonce: 1738483341280375163846743812193292672860569105378494043894154684192972730518group.public
}"

echo "
###############################################################################
######## ########
######## Voter 1 votes 'yes' on the ballot ticket ########
######## ########
######## --------------------------- ########
######## | | Yes | No | ########
######## --------------------------- ########
######## | Votes | 1 | 0 | ########
######## --------------------------- ########
######## ########
###############################################################################
"
# Run the `propose` function
(
leo run propose || exit
)

echo "
Let's create a new private ticket for voter 2. Take on the role of voter 1 and run the new_ticket transition. The inputs take a unique ticket ID and the voter's public address.
echo '
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp6NHwbT7PkpnEFeBidz5ZkZ14W8WXZmJ6kjKbEHYdMmf2
' > .env
leo run new_ticket 2158670485494560943857353240576760973319410481267184429818180411607250143681field aleo1uc6jphye8y9gfqtezrz240ak963sdgugd7s96qpuw6k7jz9axs8q2qnhxc
"

# swaps in the private key of voter 2 to .env
echo "
NETWORK=testnet3
PRIVATE_KEY=APrivateKey1zkp6NHwbT7PkpnEFeBidz5ZkZ14W8WXZmJ6kjKbEHYdMmf2
" > .env

# runs the new_ticket transition function with unique id and voter address as inputs
leo run new_ticket 2158670485494560943857353240576760973319410481267184429818180411607250143681field aleo1uc6jphye8y9gfqtezrz240ak963sdgugd7s96qpuw6k7jz9axs8q2qnhxc

echo "
###############################################################################
######## ########
######## STEP 2: Issue a new ballot ticket ########
######## Voter 2 issues a new ballot ticket ########
######## ########
######## --------------------------- ########
######## | | Yes | No | ########
Expand All @@ -49,26 +165,49 @@ echo "
######## ########
###############################################################################
"
# Run the `new_ticket` function
(
leo run new_ticket || exit
)

echo "
Voter 2 can now vote privately on their ticket. Call the agree or disagree transition function, which takes the voter's ticket output as the input.
leo run disagree '{
owner: aleo1uc6jphye8y9gfqtezrz240ak963sdgugd7s96qpuw6k7jz9axs8q2qnhxc.private,
pid: 2158670485494560943857353240576760973319410481267184429818180411607250143681field.private,
_nonce: 6511154004161574129036815174288926693337549214513234790975047364416273541105group.public
}'
"

leo run disagree "{
owner: aleo1uc6jphye8y9gfqtezrz240ak963sdgugd7s96qpuw6k7jz9axs8q2qnhxc.private,
pid: 2158670485494560943857353240576760973319410481267184429818180411607250143681field.private,
_nonce: 6511154004161574129036815174288926693337549214513234790975047364416273541105group.public
}"

echo "
###############################################################################
######## ########
######## STEP 3: Vote 'Yes' on the ballot ticket ########
######## Voter 2 votes 'no' on the ballot ticket ########
######## ########
######## --------------------------- ########
######## | | Yes | No | ########
######## --------------------------- ########
######## | Votes | 1 | 0 | ########
######## | Votes | 0 | 1 | ########
######## --------------------------- ########
######## ########
###############################################################################
"

echo "
Votes on the ticket are private. But the sum total of the agreements and disagreements are shown on-chain in the public mapping. You can query this data on-chain.
###############################################################################
######## ########
######## The Proposal ########
######## ########
######## --------------------------- ########
######## | | Yes | No | ########
######## --------------------------- ########
######## | Votes | 1 | 1 | ########
######## --------------------------- ########
######## ########
###############################################################################
"
# Run the `agree` or `disagree` function
(
leo run agree || exit
# leo run disagree || exit
)

1 comment on commit ec315c4

@parksingle
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

Please sign in to comment.