Skip to content

Commit

Permalink
Added balance check and consistancy check
Browse files Browse the repository at this point in the history
  • Loading branch information
aii23 committed May 24, 2024
1 parent a896beb commit 210b1b6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
50 changes: 46 additions & 4 deletions src/Lottery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { Lottery } from './Lottery';
import { Ticket } from './Ticket';
import { getEmpty2dMerkleMap } from './util';
import { TICKET_PRICE } from './constants';

/*
* This file specifies how to test the `Add` example smart contract. It is safe to delete this file and replace
Expand Down Expand Up @@ -62,6 +63,28 @@ class StateManager {
return [roundWitness, ticketRoundWitness];
}

addTicket(
ticket: Ticket,
round: number
): [MerkleMapWitness, MerkleMapWitness, MerkleMapWitness, Field] {
const [roundWitness, ticketRoundWitness] = this.getNextTicketWitenss(round);
const [bankWitness, bankValue] = this.getBankWitness(round);

this.roundTicketMap[round].set(
Field.from(this.lastTicketInRound[round]),
ticket.hash()
);
this.lastTicketInRound[round]++;
this.ticketMap.set(Field.from(round), this.roundTicketMap[round].getRoot());

this.bankMap.set(
Field.from(round),
bankValue.add(TICKET_PRICE.mul(ticket.amount).value)
);

return [roundWitness, ticketRoundWitness, bankWitness, bankValue];
}

// Returns witness and value
getBankWitness(round: number): [MerkleMapWitness, Field] {
const bankWitness = this.bankMap.getWitness(Field.from(round));
Expand All @@ -81,7 +104,8 @@ describe('Add', () => {
zkAppAddress: PublicKey,
zkAppPrivateKey: PrivateKey,
lottery: Lottery,
state: StateManager;
state: StateManager,
checkConsistancy: () => void;

beforeAll(async () => {
if (proofsEnabled) await Lottery.compile();
Expand All @@ -98,6 +122,17 @@ describe('Add', () => {
zkAppAddress = zkAppPrivateKey.toPublicKey();
lottery = new Lottery(zkAppAddress);
state = new StateManager();

checkConsistancy = () => {
expect(lottery.ticketRoot.get()).toEqual(state.ticketMap.getRoot());
expect(lottery.ticketNullifier.get()).toEqual(
state.ticketNullifierMap.getRoot()
);
expect(lottery.bankRoot.get()).toEqual(state.bankMap.getRoot());
expect(lottery.roundResultRoot.get()).toEqual(
state.roundResultMap.getRoot()
);
};
});

async function localDeploy() {
Expand All @@ -115,11 +150,12 @@ describe('Add', () => {

let curRound = 0;

const balanceBefore = Mina.getBalance(senderAccount);

// Buy ticket
const ticket = Ticket.random(senderAccount);
let [roundWitness, roundTicketWitness] =
state.getNextTicketWitenss(curRound);
let [bankWitness, bankValue] = state.getBankWitness(curRound);
let [roundWitness, roundTicketWitness, bankWitness, bankValue] =
state.addTicket(ticket, curRound);
let tx = await Mina.transaction(senderAccount, async () => {
await lottery.buyTicket(
ticket,
Expand All @@ -133,6 +169,12 @@ describe('Add', () => {
await tx.prove();
await tx.sign([senderKey]).send();

const balanceAfter = Mina.getBalance(senderAccount);

expect(balanceBefore.sub(balanceAfter)).toEqual(TICKET_PRICE);

checkConsistancy();

// Wait next round

// Produce result
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UInt64 } from 'o1js';

export const NUMBERS_IN_TICKET = 6;

export const TICKET_PRICE = UInt64.from(10); // #TODO change to field in smartcontract
export const TICKET_PRICE = UInt64.from(10 * 10 ** 9); // #TODO change to field in smartcontract
export const BLOCK_PER_ROUND = 480; // Aproximate blocks per day

export const SCORE_COEFFICIENTS = [1, 10, 100, 1000, 10000, 100000]; // Should be updated with apropriate probaility

0 comments on commit 210b1b6

Please sign in to comment.