Skip to content

Commit

Permalink
Fixed issue with ticket round and ticketId in reduceProof
Browse files Browse the repository at this point in the history
  • Loading branch information
aii23 committed Jul 16, 2024
1 parent 6175c60 commit 0ab1a55
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 17 deletions.
29 changes: 27 additions & 2 deletions src/PLottery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,23 @@ describe('Add', () => {
await tx2.sign([senderKey]).send();
checkConsistancy();

// Buy dummy ticket in next round, so reudcer works as expected
state.syncWithCurBlock(
+Mina.activeInstance.getNetworkState().globalSlotSinceGenesis
);
let dummy_ticket = Ticket.random(senderAccount);
dummy_ticket.amount = UInt64.zero;
let tx_1 = await Mina.transaction(senderAccount, async () => {
await lottery.buyTicket(dummy_ticket, Field.from(curRound + 1));
});
await tx_1.prove();
await tx_1.sign([senderKey]).send();

// Reduce tickets
let reduceProof = await state.reduceTickets();

let tx2_1 = await Mina.transaction(senderAccount, async () => {
await lottery.reduceTickets(reduceProof, Field(1));
await lottery.reduceTickets(reduceProof);
});

await tx2_1.prove();
Expand Down Expand Up @@ -257,10 +269,23 @@ describe('Add', () => {
checkConsistancy();

// Reduce tickets

// Buy dummy ticket in next round, so reudcer works as expected
state.syncWithCurBlock(
+Mina.activeInstance.getNetworkState().globalSlotSinceGenesis
);
let dummy_ticket = Ticket.random(senderAccount);
dummy_ticket.amount = UInt64.zero;
let tx_1 = await Mina.transaction(senderAccount, async () => {
await lottery.buyTicket(dummy_ticket, Field.from(round + 1));
});
await tx_1.prove();
await tx_1.sign([senderKey]).send();

let reduceProof = await state.reduceTickets();

let tx2_1 = await Mina.transaction(senderAccount, async () => {
await lottery.reduceTickets(reduceProof, Field(round + 1));
await lottery.reduceTickets(reduceProof);
});

await tx2_1.prove();
Expand Down
19 changes: 15 additions & 4 deletions src/PLottery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ export class PLottery extends SmartContract {

@state(Field) lastReduceInRound = State<Field>();

@state(Field) lastProcessedTicketId = State<Field>();

init() {
super.init();

Expand Down Expand Up @@ -183,12 +185,12 @@ export class PLottery extends SmartContract {
);
}

@method async reduceTickets(reduceProof: TicketReduceProof, round: Field) {
@method async reduceTickets(reduceProof: TicketReduceProof) {
reduceProof.verify();

this.checkCurrentRound(UInt32.fromFields([round]));

let lastProcessedState = this.lastProcessedState.getAndRequireEquals();
let lastProcessedTicketId =
this.lastProcessedTicketId.getAndRequireEquals();
let actionState = this.account.actionState.getAndRequireEquals();

reduceProof.publicOutput.processedActionList.assertEquals(
Expand All @@ -208,10 +210,19 @@ export class PLottery extends SmartContract {
'Final state is not match contract actionState'
);

// Check inital ticket id
lastProcessedTicketId.assertEquals(
reduceProof.publicOutput.initialTicketId,
'Initial ticket id don not match contract last processed ticket id'
);

this.lastProcessedState.set(reduceProof.publicOutput.finalState);
this.ticketRoot.set(reduceProof.publicOutput.newTicketRoot);
this.bankRoot.set(reduceProof.publicOutput.newBankRoot);
this.lastReduceInRound.set(round);
this.lastReduceInRound.set(reduceProof.publicOutput.lastProcessedRound);
this.lastProcessedTicketId.set(
reduceProof.publicOutput.lastProcessedTicketId
);

this.emitEvent(
'reduce',
Expand Down
26 changes: 21 additions & 5 deletions src/StateManager/PStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class PStateManager extends BaseStateManager {

this.contract = plottery;
this.processedTicketData = {
ticketId: 1,
ticketId: 0,
round: 0,
};
}
Expand Down Expand Up @@ -114,23 +114,41 @@ export class PStateManager extends BaseStateManager {

let curProof = this.isMock
? await mockProof(
await TRinit(input, initialState, initialTicketRoot, initialBankRoot),
await TRinit(
input,
initialState,
initialTicketRoot,
initialBankRoot,
Field.from(this.processedTicketData.round),
Field.from(this.processedTicketData.ticketId)
),
TicketReduceProof,
input
)
: await TicketReduceProgram.init(
input,
initialState,
initialTicketRoot,
initialBankRoot
initialBankRoot,
Field.from(this.processedTicketData.round),
Field.from(this.processedTicketData.ticketId)
);

for (let actionList of actionLists) {
for (let action of actionList) {
if (+action.round != this.processedTicketData.round) {
this.processedTicketData.round = +action.round;
this.processedTicketData.ticketId = 1;
} else {
this.processedTicketData.ticketId++;
}

console.log(
`Process ticket: <${+action.round}> <${
this.processedTicketData.ticketId
}>`
);

input = new TicketReduceProofPublicInput({
action: action,
roundWitness: this.ticketMap.getWitness(action.round),
Expand All @@ -141,8 +159,6 @@ export class PStateManager extends BaseStateManager {
bankValue: this.bankMap.get(action.round),
});

this.processedTicketData.ticketId++;

curProof = this.isMock
? await mockProof(
await TRaddTicket(input, curProof),
Expand Down
27 changes: 21 additions & 6 deletions src/TicketReduceProof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class TicketReduceProofPublicOutput extends Struct({
finalState: Field,
initialTicketRoot: Field,
initialBankRoot: Field,
initialTicketId: Field,
newTicketRoot: Field,
newBankRoot: Field,
processedActionList: Field,
Expand All @@ -97,18 +98,21 @@ export const init = async (
input: TicketReduceProofPublicInput,
initialState: Field,
initialTicketRoot: Field,
initialBankRoot: Field
initialBankRoot: Field,
initialRound: Field,
initialTicketId: Field
): Promise<TicketReduceProofPublicOutput> => {
return new TicketReduceProofPublicOutput({
initialState,
finalState: initialState,
initialTicketRoot,
initialBankRoot,
initialTicketId,
newTicketRoot: initialTicketRoot,
newBankRoot: initialBankRoot,
processedActionList: ActionList.emptyHash,
lastProcessedRound: Field(0),
lastProcessedTicketId: Field(0),
lastProcessedRound: initialRound,
lastProcessedTicketId: initialTicketId,
});
};

Expand Down Expand Up @@ -174,6 +178,7 @@ export const addTicket = async (
finalState: prevProof.publicOutput.finalState,
initialTicketRoot: prevProof.publicOutput.initialTicketRoot,
initialBankRoot: prevProof.publicOutput.initialBankRoot,
initialTicketId: prevProof.publicOutput.initialTicketId,
newTicketRoot,
newBankRoot,
processedActionList,
Expand Down Expand Up @@ -202,6 +207,7 @@ export const cutActions = async (
finalState,
initialTicketRoot: prevProof.publicOutput.initialTicketRoot,
initialBankRoot: prevProof.publicOutput.initialBankRoot,
initialTicketId: prevProof.publicOutput.initialTicketId,
newTicketRoot: prevProof.publicOutput.newTicketRoot,
newBankRoot: prevProof.publicOutput.newBankRoot,
processedActionList,
Expand All @@ -221,14 +227,23 @@ export const TicketReduceProgram = ZkProgram({
publicOutput: TicketReduceProofPublicOutput,
methods: {
init: {
privateInputs: [Field, Field, Field],
privateInputs: [Field, Field, Field, Field, Field],
async method(
input: TicketReduceProofPublicInput,
initialState: Field,
initialTicketRoot: Field,
initialBankRoot: Field
initialBankRoot: Field,
initialRound: Field,
initialTicketId: Field
): Promise<TicketReduceProofPublicOutput> {
return init(input, initialState, initialTicketRoot, initialBankRoot);
return init(
input,
initialState,
initialTicketRoot,
initialBankRoot,
initialRound,
initialTicketId
);
},
},
addTicket: {
Expand Down

0 comments on commit 0ab1a55

Please sign in to comment.